Skip to content

Commit fe27c2e

Browse files
committed
Improved exception handling for symlink operations
1 parent ca2139e commit fe27c2e

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

transcode.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,32 @@ def link():
100100
proceed = get_user_response()
101101
if proceed:
102102
if not oct(os.stat(script_realpath).st_mode)[-3:] == 755:
103-
os.chmod(script_realpath, 0o755)
103+
try:
104+
os.chmod(script_realpath, 0o755)
105+
except PermissionError:
106+
sys.exit("\nError: failed to make {script_name} executable, operation not permitted.".format(script_name=script_name))
104107
print("Use default location? /usr/local/bin")
105108
default_location = get_user_response()
106109
if default_location:
107-
os.symlink(script_realpath, os.path.join("usr", "local", "bin", script_name))
108-
sys.exit("Created symlink to {script_name} in /usr/local/bin\n")
110+
try:
111+
os.symlink(script_realpath, os.path.join(os.sep, "usr", "local", "bin", script_name))
112+
except PermissionError:
113+
sys.exit("\nError: failed to create symlink, operation not permitted.")
114+
else:
115+
sys.exit("Created symlink to {script_name} in /usr/local/bin\n")
109116
else:
110117
print("Use alternate $PATH location?")
111118
alternate_location = get_user_response()
112119
if alternate_location:
113120
alternate_path = str(input("Alternate $PATH location: (case-sensitive) "))
114121
if alternate_path[0] == "~": alternate_path = os.path.expanduser(alternate_path)
115122
if alternate_path in os.get_exec_path():
116-
os.symlink(script_realpath, os.path.join(alternate_path, script_name))
117-
sys.exit("Created symlink to {script_name} in {alternate_path}\n".format(script_name=script_name, alternate_path=alternate_path))
123+
try:
124+
os.symlink(script_realpath, os.path.join(alternate_path, script_name))
125+
except PermissionError:
126+
sys.exit("\nError: failed to create symlink, operation not permitted.")
127+
else:
128+
sys.exit("Created symlink to {script_name} in {alternate_path}\n".format(script_name=script_name, alternate_path=alternate_path))
118129
else:
119130
sys.exit("\nError: {alternate_path} not found on $PATH, aborting install.\n".format(alternate_path=alternate_path))
120131
else:
@@ -128,8 +139,12 @@ def unlink():
128139
print("Remove symlink to {script_name} in {path_dir}?".format(script_name=script_name, path_dir=path_dir))
129140
proceed = get_user_response()
130141
if proceed:
131-
os.unlink(script_path_location)
132-
print("Unlinked {script_path_location}\n".format(script_path_location=script_path_location))
142+
try:
143+
os.unlink(script_path_location)
144+
except PermissionError:
145+
sys.exit("\nError: operation not permitted.")
146+
else:
147+
print("Unlinked {script_path_location}\n".format(script_path_location=script_path_location))
133148
else:
134149
sys.exit("Aborting uninstall.\n")
135150
else:

0 commit comments

Comments
 (0)