Skip to content

Commit ecb9834

Browse files
committed
Tweaking formatting + refactor
1 parent 17dd1c0 commit ecb9834

File tree

1 file changed

+90
-86
lines changed

1 file changed

+90
-86
lines changed

setup.py

Lines changed: 90 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -13,100 +13,104 @@
1313
except ImportError:
1414
sys.exit("FATAL: failed to import dependencies from ./lib/\n")
1515

16-
parser = argparse.ArgumentParser(description="Manages $PATH symlink for transcode.py".format(sep=os.sep))
17-
install_group = parser.add_mutually_exclusive_group(required=True)
18-
install_group.add_argument("--install", action="store_true", help="install symlink to transcode.py on $PATH")
19-
install_group.add_argument("--uninstall", action="store_true", help="remove symlink to transcode.py")
20-
args = parser.parse_args()
21-
22-
def link():
23-
""" Creates symlink to transcode.py in /usr/local/bin or alternate $PATH
24-
"""
25-
print("\nCreate symlink for {script_name} on $PATH?".format(script_name=script_name))
26-
proceed = get_user_response()
27-
if proceed:
28-
if not oct(os.stat(script_realpath).st_mode)[-3:] == 755:
29-
try:
30-
os.chmod(script_realpath, 0o755)
31-
except PermissionError:
32-
sys.exit("\nError: failed to make {script_name} executable, operation not permitted.".format(script_name=script_name))
33-
print("Use default location? /usr/local/bin")
34-
default_location = get_user_response()
35-
if default_location:
36-
try:
37-
os.symlink(script_realpath, os.path.join(os.sep, "usr", "local", "bin", script_name))
38-
except PermissionError:
39-
sys.exit("\nError: failed to create symlink, operation not permitted.")
16+
def main():
17+
def link():
18+
""" Creates symlink to transcode.py in /usr/local/bin or alternate $PATH
19+
"""
20+
print("\nCreate symlink for {script_name} on $PATH?".format(script_name=script_name))
21+
proceed = get_user_response()
22+
if proceed:
23+
if not oct(os.stat(script_realpath).st_mode)[-3:] == 755:
24+
try:
25+
os.chmod(script_realpath, 0o755)
26+
except PermissionError:
27+
sys.exit("\nError: failed to make {script_name} executable, operation not permitted.".format(script_name=script_name))
28+
print("Use default location? /usr/local/bin")
29+
default_location = get_user_response()
30+
if default_location:
31+
try:
32+
os.symlink(script_realpath, os.path.join(os.sep, "usr", "local", "bin", script_name))
33+
except PermissionError:
34+
sys.exit("\nError: failed to create symlink, operation not permitted.")
35+
else:
36+
sys.exit("Created symlink to {script_name} in /usr/local/bin\n")
4037
else:
41-
sys.exit("Created symlink to {script_name} in /usr/local/bin\n")
42-
else:
43-
print("Use alternate $PATH location?")
44-
alternate_location = get_user_response()
45-
if alternate_location:
46-
alternate_path = str(input("Alternate $PATH location: (case-sensitive) "))
47-
if alternate_path[0] == "~": alternate_path = os.path.expanduser(alternate_path)
48-
if alternate_path in os.get_exec_path():
49-
try:
50-
os.symlink(script_realpath, os.path.join(alternate_path, script_name))
51-
except PermissionError:
52-
sys.exit("\nError: failed to create symlink, operation not permitted.")
38+
print("Use alternate $PATH location?")
39+
alternate_location = get_user_response()
40+
if alternate_location:
41+
alternate_path = str(input("Alternate $PATH location: (case-sensitive) "))
42+
if alternate_path[0] == "~": alternate_path = os.path.expanduser(alternate_path)
43+
if alternate_path in os.get_exec_path():
44+
try:
45+
os.symlink(script_realpath, os.path.join(alternate_path, script_name))
46+
except PermissionError:
47+
sys.exit("\nError: failed to create symlink, operation not permitted.")
48+
else:
49+
sys.exit("\nCreated symlink to {script_name} in {alternate_path}\n".format(script_name=script_name, alternate_path=alternate_path))
5350
else:
54-
sys.exit("\nCreated symlink to {script_name} in {alternate_path}\n".format(script_name=script_name, alternate_path=alternate_path))
51+
sys.exit("\nError: {alternate_path} not found on $PATH, aborting install.\n".format(alternate_path=alternate_path))
5552
else:
56-
sys.exit("\nError: {alternate_path} not found on $PATH, aborting install.\n".format(alternate_path=alternate_path))
57-
else:
58-
sys.exit("Aborting install.\n")
59-
else:
60-
sys.exit("Aborting install.\n")
61-
62-
def unlink():
63-
""" Removes symlink to transcode.py from $PATH
64-
"""
65-
print("\nFound {script_name} on $PATH in {path_dir}\n".format(script_name=script_name, path_dir=path_dir))
66-
if os.path.islink(script_path_location):
67-
print("Remove symlink to {script_name} in {path_dir}?".format(script_name=script_name, path_dir=path_dir))
68-
proceed = get_user_response()
69-
if proceed:
70-
try:
71-
os.unlink(script_path_location)
72-
except PermissionError:
73-
sys.exit("\nError: operation not permitted.")
53+
sys.exit("Aborting install.\n")
54+
else:
55+
sys.exit("Aborting install.\n")
56+
57+
def unlink():
58+
""" Removes symlink to transcode.py from $PATH
59+
"""
60+
print("\nFound {script_name} on $PATH in {path_dir}\n".format(script_name=script_name, path_dir=path_dir))
61+
if os.path.islink(script_path_location):
62+
print("Remove symlink to {script_name} in {path_dir}?".format(script_name=script_name, path_dir=path_dir))
63+
proceed = get_user_response()
64+
if proceed:
65+
try:
66+
os.unlink(script_path_location)
67+
except PermissionError:
68+
sys.exit("\nError: operation not permitted.")
69+
else:
70+
print("\nUnlinked {script_path_location}\n".format(script_path_location=script_path_location))
7471
else:
75-
print("\nUnlinked {script_path_location}\n".format(script_path_location=script_path_location))
72+
sys.exit("Aborting uninstall.\n")
7673
else:
77-
sys.exit("Aborting uninstall.\n")
78-
else:
79-
sys.exit("Error: {script_path_location} exists on $PATH but is not a symlink, skipping uninstall.\n".format(script_path_location=script_path_location))
80-
sys.exit()
81-
82-
script_name = "transcode.py"
83-
script_realpath = os.path.realpath(script_name)
84-
script_on_path = False
85-
for location in os.get_exec_path():
86-
if script_name in os.listdir(location):
87-
script_on_path = True
88-
script_path_location = os.path.join(location, script_name)
89-
break
74+
sys.exit("Error: {script_path_location} exists on $PATH but is not a symlink, skipping uninstall.\n".format(script_path_location=script_path_location))
75+
sys.exit()
76+
77+
parser = argparse.ArgumentParser(description="Manages $PATH symlink for transcode.py".format(sep=os.sep))
78+
install_group = parser.add_mutually_exclusive_group(required=True)
79+
install_group.add_argument("--install", action="store_true", help="install symlink to transcode.py on $PATH")
80+
install_group.add_argument("--uninstall", action="store_true", help="remove symlink to transcode.py")
81+
args = parser.parse_args()
9082

91-
if script_on_path:
92-
path_dir = os.path.dirname(script_path_location)
93-
script_executable = os.access(script_realpath, os.X_OK)
83+
script_name = "transcode.py"
84+
script_realpath = os.path.realpath(script_name)
85+
script_on_path = False
86+
for location in os.get_exec_path():
87+
if script_name in os.listdir(location):
88+
script_on_path = True
89+
script_path_location = os.path.join(location, script_name)
90+
break
9491

95-
if args.install:
9692
if script_on_path:
97-
sys.exit("\n{script_name} already on $PATH at {script_path_location}, skipping install.\n".format(script_name=script_name, script_path_location=script_path_location))
98-
else:
99-
link()
100-
else:
101-
if not script_on_path:
102-
sys.exit("\n{script_name} not on $PATH, skipping uninstall.\n".format(script_name=script_name))
103-
else:
104-
unlink()
93+
path_dir = os.path.dirname(script_path_location)
94+
script_executable = os.access(script_realpath, os.X_OK)
10595

106-
if len(sys.argv) > 2:
107-
print("\nFATAL: --install/--uninstall may not be called with any other arguments")
108-
else:
10996
if args.install:
110-
symlink(True)
97+
if script_on_path:
98+
sys.exit("\n{script_name} already on $PATH at {script_path_location}, skipping install.\n".format(script_name=script_name, script_path_location=script_path_location))
99+
else:
100+
link()
111101
else:
112-
symlink(False)
102+
if not script_on_path:
103+
sys.exit("\n{script_name} not on $PATH, skipping uninstall.\n".format(script_name=script_name))
104+
else:
105+
unlink()
106+
107+
if len(sys.argv) > 2:
108+
print("\nFATAL: --install/--uninstall may not be called with any other arguments")
109+
else:
110+
if args.install:
111+
symlink(True)
112+
else:
113+
symlink(False)
114+
115+
if __name__ == "__main__":
116+
main()

0 commit comments

Comments
 (0)