Skip to content

Commit ee4a74f

Browse files
committed
[rkscript] Fix bug: ~ or ~user in a paths
README.rst: update "rk 0.3" in "History". rk/__init__.py: prepare ver. 0.3a4. rk/rk.py: prepare ver. 0.3a4. scripts/rkscript: fix bug "initial component of ``~`` or ``~user`` is not replaced in a paths". setup.py: prepare ver. 0.3a4.
1 parent 2b64ca3 commit ee4a74f

File tree

5 files changed

+97
-32
lines changed

5 files changed

+97
-32
lines changed

README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ Legend
320320
rk 0.3
321321
------
322322

323+
* bug in the rk and in the rkscript: an initial component of ``~`` or ``~user`` is not replaced in a paths.
323324
* bug in the rk: a superuser (root) privileges required for the user kernels location ``~/.ipython/kernels``.
324325
* **setup SSH for auto login without a password with a "ssh" subcommand.**
325326
* error in the rkscript: list index out of range.

rk/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# -*- coding: utf-8 -*-
22

3-
__version__ = '0.3a3'
3+
__version__ = '0.3a4'

rk/rk.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ def parse_command_line_args():
260260
# Create top parser
261261
parser = ArgumentParser(prog="rk", description=argparse["_parser"],
262262
add_help=True)
263-
parser.add_argument("-v", "--version", action="version", version="rk 0.3a3")
263+
parser.add_argument("-v", "--version", action="version",
264+
version="rk 0.3a4")
264265
# Create subparsers for the top parser
265266
subparsers = parser.add_subparsers(title=argparse["_subparsers"])
266267
# Create the parser for the "list" subcommand

scripts/rkscript

+92-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33

44
"""Remote jupyter kernel via SSH
@@ -7,11 +7,11 @@ Make sure that you can login to a remote machine without entering password.
77
"""
88

99
from datetime import datetime
10-
from errno import ENOTDIR
10+
from errno import EACCES, ENOTDIR
1111
from getpass import getuser
1212
from json import load
1313
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
1515
from site import getsitepackages
1616
from sys import argv
1717

@@ -40,16 +40,40 @@ def create_directory(directory_name, mode=0o777):
4040

4141
try:
4242
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)
5174
else:
52-
raise exception
75+
print(messages["_error_Oops"] % strerror(error_code))
76+
exit(1)
5377

5478
def get_date_time():
5579
"""Get yyyy-mm-dd_hh.mm.ss"""
@@ -112,7 +136,7 @@ else:
112136
remote_username = local_username
113137
remote_host = remote_username_at_remote_host
114138
# Load a connection file
115-
with open(local_connection_file) as f:
139+
with open(local_connection_file, 'r') as f:
116140
cfg = load(f)
117141
# GET a current working directory of a process
118142
cwd = getcwd()
@@ -199,9 +223,20 @@ date, time = date_time.replace('.', ':').split('_')
199223
date = date + ' ' + week[datetime.weekday(datetime.now())]
200224
rk_log_file_name = "%s@%s_%s.txt" % (local_username, remote_host, date_time)
201225
rk_log_location = config["rk_log_location"]
226+
if '~' in rk_log_location:
227+
rk_log_location = expanduser(rk_log_location)
202228
rk_log_abs_path = join(rk_log_location, rk_log_file_name)
203229
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)
205240
if not exists(rk_log_location):
206241
create_directory(rk_log_location, 0o777)
207242
path = rk_log_location
@@ -211,25 +246,53 @@ if not exists(rk_log_location):
211246
except OSError:
212247
break
213248
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)
220270
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)
228273
# Waits for closing, i.e. remote_exec() finish
229274
ch.waitclose()
230275
# Delete paramiko log file
231276
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)
233287
# Delete rk log file
234288
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)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@
4141
platforms = ["Linux"],
4242
scripts=['scripts/rkscript'],
4343
url = "https://github.com/korniichuk/rk",
44-
version = "0.3a3",
44+
version = "0.3a4",
4545
zip_safe = True
4646
)

0 commit comments

Comments
 (0)