1
- #! /usr/bin/env python
1
+ #!/usr/bin/python
2
2
# -*- coding: utf-8 -*-
3
3
4
4
"""Remote jupyter kernel via SSH
@@ -7,11 +7,11 @@ Make sure that you can login to a remote machine without entering password.
7
7
"""
8
8
9
9
from datetime import datetime
10
- from errno import ENOTDIR
10
+ from errno import EACCES , ENOTDIR
11
11
from getpass import getuser
12
12
from json import load
13
13
from os import chmod , getcwd , getpid , makedirs , remove
14
- from os .path import dirname , exists , isfile , join , split
14
+ from os .path import dirname , exists , expanduser , isfile , join , split
15
15
from site import getsitepackages
16
16
from sys import argv
17
17
@@ -40,16 +40,40 @@ def create_directory(directory_name, mode=0o777):
40
40
41
41
try :
42
42
makedirs (directory_name , mode )
43
- except OSError as exception : # Python3 NotADirectoryError
44
- if exception .errno == ENOTDIR :
45
- path = directory_name
46
- while path != '/' :
47
- if isfile (path ):
48
- remove (path )
49
- path = dirname (path )
50
- makedirs (directory_name , mode )
43
+ except Exception as exception :
44
+ error_code = exception .errno
45
+ if error_code == EACCES : # 13 (Python3 PermissionError)
46
+ print (messages ["_error_NoRoot" ])
47
+ exit (1 )
48
+ elif error_code == ENOTDIR : # 20 (Python3 NotADirectoryError)
49
+ path = directory_name
50
+ while path != '/' :
51
+ if isfile (path ):
52
+ try :
53
+ remove (path )
54
+ except Exception as exception : # Python3 PermissionError
55
+ error_code = exception .errno
56
+ if error_code == EACCES : # 13
57
+ print (messages ["_error_NoRoot" ])
58
+ exit (1 )
59
+ else :
60
+ print (messages ["_error_Oops" ] %
61
+ strerror (error_code ))
62
+ exit (1 )
63
+ path = dirname (path )
64
+ try :
65
+ makedirs (directory_name , mode )
66
+ except Exception as exception : # Python3 PermissionError
67
+ error_code = exception .errno
68
+ if error_code == EACCES : # 13
69
+ print (messages ["_error_NoRoot" ])
70
+ exit (1 )
71
+ else :
72
+ print (messages ["_error_Oops" ] % strerror (error_code ))
73
+ exit (1 )
51
74
else :
52
- raise exception
75
+ print (messages ["_error_Oops" ] % strerror (error_code ))
76
+ exit (1 )
53
77
54
78
def get_date_time ():
55
79
"""Get yyyy-mm-dd_hh.mm.ss"""
@@ -112,7 +136,7 @@ else:
112
136
remote_username = local_username
113
137
remote_host = remote_username_at_remote_host
114
138
# Load a connection file
115
- with open (local_connection_file ) as f :
139
+ with open (local_connection_file , 'r' ) as f :
116
140
cfg = load (f )
117
141
# GET a current working directory of a process
118
142
cwd = getcwd ()
@@ -199,9 +223,20 @@ date, time = date_time.replace('.', ':').split('_')
199
223
date = date + ' ' + week [datetime .weekday (datetime .now ())]
200
224
rk_log_file_name = "%s@%s_%s.txt" % (local_username , remote_host , date_time )
201
225
rk_log_location = config ["rk_log_location" ]
226
+ if '~' in rk_log_location :
227
+ rk_log_location = expanduser (rk_log_location )
202
228
rk_log_abs_path = join (rk_log_location , rk_log_file_name )
203
229
if exists (rk_log_location ) and isfile (rk_log_location ):
204
- remove (rk_log_location )
230
+ try :
231
+ remove (rk_log_location )
232
+ except Exception as exception : # Python3 PermissionError
233
+ error_code = exception .errno
234
+ if error_code == EACCES : # 13
235
+ print (messages ["_error_NoRoot" ])
236
+ exit (1 )
237
+ else :
238
+ print (messages ["_error_Oops" ] % strerror (error_code ))
239
+ exit (1 )
205
240
if not exists (rk_log_location ):
206
241
create_directory (rk_log_location , 0o777 )
207
242
path = rk_log_location
@@ -211,25 +246,53 @@ if not exists(rk_log_location):
211
246
except OSError :
212
247
break
213
248
path = dirname (path )
214
- with open (rk_log_abs_path , 'w' ) as f :
215
- f .write ("date: %s\n " % date )
216
- f .write ("time: %s\n " % time )
217
- f .write ("\n " )
218
- if local_username == remote_username :
219
- f .write ("usernames: %s\n " % local_username )
249
+ try :
250
+ with open (rk_log_abs_path , 'w' ) as f :
251
+ f .write ("date: %s\n " % date )
252
+ f .write ("time: %s\n " % time )
253
+ f .write ("\n " )
254
+ if local_username == remote_username :
255
+ f .write ("usernames: %s\n " % local_username )
256
+ else :
257
+ f .write ("usernames: %s<->%s\n " % (local_username , remote_username ))
258
+ f .write ("remote host: %s\n " % remote_host )
259
+ f .write ("\n " )
260
+ for k ,v in local_ports .items ():
261
+ f .write ("%ss: %s<->%s\n " % (k .replace ('_' , ' ' ), v ,
262
+ remote_ports [k ]))
263
+ f .write ("\n " )
264
+ f .write ("pids: %s<->%s\n " % (local_pid , remote_pid ))
265
+ except Exception as exception :
266
+ error_code = exception .errno
267
+ if error_code == EACCES : # 13 (Python3 PermissionError)
268
+ print (messages ["_error_NoRoot" ])
269
+ exit (1 )
220
270
else :
221
- f .write ("usernames: %s<->%s\n " % (local_username , remote_username ))
222
- f .write ("remote host: %s\n " % remote_host )
223
- f .write ("\n " )
224
- for k ,v in local_ports .items ():
225
- f .write ("%ss: %s<->%s\n " % (k .replace ('_' , ' ' ), v , remote_ports [k ]))
226
- f .write ("\n " )
227
- f .write ("pids: %s<->%s\n " % (local_pid , remote_pid ))
271
+ print (messages ["_error_Oops" ] % strerror (error_code ))
272
+ exit (1 )
228
273
# Waits for closing, i.e. remote_exec() finish
229
274
ch .waitclose ()
230
275
# Delete paramiko log file
231
276
if exists (paramiko_log_abs_path ) and isfile (paramiko_log_abs_path ):
232
- remove (paramiko_log_abs_path )
277
+ try :
278
+ remove (paramiko_log_abs_path )
279
+ except Exception as exception :
280
+ error_code = exception .errno
281
+ if error_code == EACCES : # 13 (Python3 PermissionError)
282
+ print (messages ["_error_NoRoot" ])
283
+ exit (1 )
284
+ else :
285
+ print (messages ["_error_Oops" ] % strerror (error_code ))
286
+ exit (1 )
233
287
# Delete rk log file
234
288
if exists (rk_log_abs_path ) and isfile (rk_log_abs_path ):
235
- remove (rk_log_abs_path )
289
+ try :
290
+ remove (rk_log_abs_path )
291
+ except Exception as exception :
292
+ error_code = exception .errno
293
+ if error_code == EACCES : # 13 (Python3 PermissionError)
294
+ print (messages ["_error_NoRoot" ])
295
+ exit (1 )
296
+ else :
297
+ print (messages ["_error_Oops" ] % strerror (error_code ))
298
+ exit (1 )
0 commit comments