Skip to content

Commit 5aaf60f

Browse files
committed
Refactoring for v0.5-Beta
1 parent 553863c commit 5aaf60f

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

transcode.py

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
except ImportError:
1515
sys.exit("FATAL: failed to import TranscodeSession from src/TranscodeSession.py\n")
1616

17-
def validate_args():
17+
def evaluate_args():
1818
""" Exits with error messages if command-line arguments are invalid
1919
"""
2020
parser = argparse.ArgumentParser(description="Transcodes given file(s) to HEVC format.")
@@ -78,7 +78,7 @@ def validate_args():
7878
return args
7979

8080
def get_user_response():
81-
""" Accepts yes/no answer as user input and returns as boolean
81+
""" Accepts yes/no answer as user input and returns answer as boolean
8282
"""
8383
while "need response":
8484
reply = str(input(" Proceed? (y/n) ")).lower().strip()
@@ -95,6 +95,47 @@ def get_user_response():
9595
def symlink(install):
9696
""" Installs / uninstalls a symlink to transcode.py in /usr/local/bin or alternate $PATH location
9797
"""
98+
def link():
99+
print("\nCreate symlink for {script_name} on $PATH?".format(script_name=script_name))
100+
proceed = get_user_response()
101+
if proceed:
102+
if not oct(os.stat(script_realpath).st_mode)[-3:] == 755:
103+
os.chmod(script_realpath, 0o755)
104+
print("Use default location? /usr/local/bin")
105+
default_location = get_user_response()
106+
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")
109+
else:
110+
print("Use alternate $PATH location?")
111+
alternate_location = get_user_response()
112+
if alternate_location:
113+
alternate_path = str(input("Alternate $PATH location: (case-sensitive) "))
114+
if alternate_path[0] == "~": alternate_path = os.path.expanduser(alternate_path)
115+
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))
118+
else:
119+
sys.exit("\nError: {alternate_path} not found on $PATH, aborting install.\n".format(alternate_path=alternate_path))
120+
else:
121+
sys.exit("Aborting install.\n")
122+
else:
123+
sys.exit("Aborting install.\n")
124+
125+
def unlink():
126+
print("\nFound {script_name} on $PATH in {path_dir}\n".format(script_name=script_name, path_dir=path_dir))
127+
if os.path.islink(script_path_location):
128+
print("Remove symlink to {script_name} in {path_dir}?".format(script_name=script_name, path_dir=path_dir))
129+
proceed = get_user_response()
130+
if proceed:
131+
os.unlink(script_path_location)
132+
print("Unlinked {script_path_location}\n".format(script_path_location=script_path_location))
133+
else:
134+
sys.exit("Aborting uninstall.\n")
135+
else:
136+
sys.exit("Error: {script_path_location} exists on $PATH but is not a symlink, skipping uninstall.\n".format(script_path_location=script_path_location))
137+
sys.exit()
138+
98139
script_name=os.path.basename(sys.argv[0])
99140
script_realpath = os.path.realpath(__file__)
100141
script_on_path = False
@@ -112,47 +153,12 @@ def symlink(install):
112153
if script_on_path:
113154
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))
114155
else:
115-
print("\nCreate symlink for {script_name} on $PATH?".format(script_name=script_name))
116-
proceed = get_user_response()
117-
if proceed:
118-
if not oct(os.stat(script_realpath).st_mode)[-3:] == 755:
119-
os.chmod(script_realpath, 0o755)
120-
print("Use default location? /usr/local/bin")
121-
default_location = get_user_response()
122-
if default_location:
123-
os.symlink(script_realpath, os.path.join("/usr/local/bin", script_name))
124-
sys.exit("Created symlink to {script_name} in /usr/local/bin\n")
125-
else:
126-
print("Use alternate $PATH location?")
127-
alternate_location = get_user_response()
128-
if alternate_location:
129-
alternate_path = str(input("Alternate $PATH location: (case-sensitive) "))
130-
if alternate_path[0] == "~": alternate_path = os.path.expanduser(alternate_path)
131-
if alternate_path in os.get_exec_path():
132-
os.symlink(script_realpath, os.path.join(alternate_path, script_name))
133-
sys.exit("Created symlink to {script_name} in {alternate_path}\n".format(script_name=script_name, alternate_path=alternate_path))
134-
else:
135-
sys.exit("\nError: {alternate_path} not found on $PATH, aborting install.\n".format(alternate_path=alternate_path))
136-
else:
137-
sys.exit("Aborting install.\n")
138-
else:
139-
sys.exit("Aborting install.\n")
156+
link()
140157
else:
141158
if not script_on_path:
142159
sys.exit("\n{script_name} not on $PATH, skipping uninstall.\n".format(script_name=script_name))
143160
else:
144-
print("\nFound {script_name} on $PATH in {path_dir}\n".format(script_name=script_name, path_dir=path_dir))
145-
if os.path.islink(script_path_location):
146-
print("Remove symlink to {script_name} in {path_dir}?".format(script_name=script_name, path_dir=path_dir))
147-
proceed = get_user_response()
148-
if proceed:
149-
os.unlink(script_path_location)
150-
print("Unlinked {script_path_location}\n".format(script_path_location=script_path_location))
151-
else:
152-
sys.exit("Aborting uninstall.\n")
153-
else:
154-
sys.exit("Error: {script_path_location} exists on $PATH but is not a symlink, skipping uninstall.\n".format(script_path_location=script_path_location))
155-
sys.exit()
161+
unlink()
156162

157163
def build_source_list(args):
158164
""" Constructs and returns list of source files
@@ -186,7 +192,7 @@ def build_source_list(args):
186192
return source_files
187193

188194
def main():
189-
args = validate_args()
195+
args = evaluate_args()
190196
source_files = build_source_list(args)
191197
time_script_started = datetime.now()
192198
for file in source_files:
@@ -200,8 +206,5 @@ def main():
200206

201207
sys.exit("{date}: Finished after {duration}.\n".format(date=str(datetime.now()), duration=time_script_duration))
202208

203-
# Check for Python 3.8 (required for shlex usage)
204-
if not (sys.version_info[0] >= 3 and sys.version_info[1] >= 8):
205-
sys.exit("FATAL: Requires Python3.8 or newer.\n")
206-
elif __name__ == "__main__":
209+
if __name__ == "__main__":
207210
main()

0 commit comments

Comments
 (0)