Skip to content

Commit 42ee8f1

Browse files
committed
0.5.5-beta: moved install/uninstall logic into setup.py, README updates
1 parent c30a4ac commit 42ee8f1

File tree

4 files changed

+152
-101
lines changed

4 files changed

+152
-101
lines changed

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,34 @@ Tools to transcode video files into HEVC, experiment with different encoder pres
44
<br>
55
<br>
66

7+
## setup.py
8+
python script to manage `$PATH` symlink to transcode.py.
9+
10+
```
11+
usage: setup.py [-h] (--install | --uninstall)
12+
13+
Manages $PATH symlink for transcode.py
14+
15+
optional arguments:
16+
-h, --help show this help message and exit
17+
--install install symlink to transcode.py on $PATH
18+
--uninstall remove symlink to transcode.py
19+
```
20+
21+
<br>
22+
<br>
23+
724
## transcode.py
825
python script to transcode movies to HEVC using custom encoder options based on source file's resolution. This has only been tested with H.264 MP4 files, but should work with source files with any of the following extensions: ".mp4", ".m4v", ".mov", ".mkv", ".mpg", ".mpeg", ".avi", ".wmv", ".flv", ".webm", ".ts" but YMMV.
926

1027
```
11-
usage: transcode.py [-h] [--install | --uninstall] [--file FILE | --all] [--quality QUALITY] [--preset PRESET]
12-
[--baseline | --best] [--small] [--delete]
28+
usage: transcode.py [-h] [--file FILE | --all] [--quality QUALITY] [--preset PRESET] [--baseline | --best] [--small]
29+
[--delete]
1330
1431
Transcodes given file(s) in ./source/ to HEVC format.
1532
1633
optional arguments:
1734
-h, --help show this help message and exit
18-
--install install symlink to transcode.py on $PATH
19-
--uninstall remove symlink to transcode.py
2035
--file FILE relative path to movie in source directory
2136
--all transcode all supported movies in source directory
2237
--quality QUALITY HandBrake quality slider value (-12,51)

lib/common.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def get_user_response():
2+
""" Accepts yes/no answer as user input and returns answer as boolean
3+
"""
4+
while "need response":
5+
reply = str(input(" Proceed? (y/n) ")).lower().strip()
6+
if len(reply) > 0:
7+
if reply[0] == "y":
8+
response = True
9+
break
10+
if reply[0] == "n":
11+
response = False
12+
break
13+
14+
return response
15+
16+
if __name__ == "__main__":
17+
sys.exit("I am a module, not a script.")

setup.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/local/bin/python3
2+
3+
import argparse
4+
import os
5+
import sys
6+
7+
# Verify script is colocated with ./lib/ and import dependencies
8+
if not os.path.isdir(os.path.join(sys.path[0], "lib")):
9+
sys.exit("FATAL: ./lib/ not present in parent diectory.\n")
10+
sys.path.append(os.path.join(sys.path[0], "lib"))
11+
try:
12+
from common import get_user_response
13+
except ImportError:
14+
sys.exit("FATAL: failed to import dependencies from ./lib/\n")
15+
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.")
40+
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.")
53+
else:
54+
sys.exit("Created symlink to {script_name} in {alternate_path}\n".format(script_name=script_name, alternate_path=alternate_path))
55+
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.")
74+
else:
75+
print("Unlinked {script_path_location}\n".format(script_path_location=script_path_location))
76+
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
90+
91+
if script_on_path:
92+
path_dir = os.path.dirname(script_path_location)
93+
script_executable = os.access(script_realpath, os.X_OK)
94+
95+
if args.install:
96+
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()
105+
106+
if len(sys.argv) > 2:
107+
print("\nFATAL: --install/--uninstall may not be called with any other arguments")
108+
else:
109+
if args.install:
110+
symlink(True)
111+
else:
112+
symlink(False)

transcode.py

Lines changed: 4 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
import os
66
import sys
77

8-
# Verify script is colocated with ./lib/ and import TranscodeSession.py
8+
# Verify script is colocated with ./lib/ and import dependencies
99
if not os.path.isdir(os.path.join(sys.path[0], "lib")):
1010
sys.exit("FATAL: ./lib/ not present in parent diectory.\n")
1111
sys.path.append(os.path.join(sys.path[0], "lib"))
1212
try:
1313
from TranscodeSession import Session
14+
from common import get_user_response
1415
except ImportError:
15-
sys.exit("FATAL: failed to import TranscodeSession from lib/TranscodeSession.py\n")
16+
sys.exit("FATAL: failed to import dependencies from ./lib/\n")
1617

1718
def evaluate_args():
1819
""" Exits with error messages if command-line arguments are invalid
1920
"""
2021
parser = argparse.ArgumentParser(description="Transcodes given file(s) in .{sep}source{sep} to HEVC format.".format(sep=os.sep))
21-
install_group = parser.add_mutually_exclusive_group()
22-
install_group.add_argument("--install", action="store_true", help="install symlink to transcode.py on $PATH")
23-
install_group.add_argument("--uninstall", action="store_true", help="remove symlink to transcode.py")
2422
files_group = parser.add_mutually_exclusive_group()
2523
files_group.add_argument("--file", help="relative path to movie in source directory")
2624
files_group.add_argument("--all", action="store_true", help="transcode all supported movies in source directory")
@@ -35,15 +33,7 @@ def evaluate_args():
3533

3634
valid_arguments = False
3735

38-
if args.install or args.uninstall:
39-
if len(sys.argv) > 2:
40-
print("\nFATAL: --install/--uninstall may not be called with any other arguments")
41-
else:
42-
if args.install:
43-
symlink(True)
44-
else:
45-
symlink(False)
46-
elif os.path.dirname(os.path.realpath(__file__)) == os.getcwd():
36+
if os.path.dirname(os.path.realpath(__file__)) == os.getcwd():
4737
print("\nFATAL: invalid working directory: running from master directory. Please create working directory in another location.")
4838
elif not "source" in os.listdir():
4939
print("\nFATAL: invalid working directory: ./source/ does not exist")
@@ -85,89 +75,6 @@ def get_user_response():
8575

8676
return response
8777

88-
def symlink(install):
89-
""" Installs / uninstalls a symlink to transcode.py in /usr/local/bin or alternate $PATH location
90-
"""
91-
def link():
92-
print("\nCreate symlink for {script_name} on $PATH?".format(script_name=script_name))
93-
proceed = get_user_response()
94-
if proceed:
95-
if not oct(os.stat(script_realpath).st_mode)[-3:] == 755:
96-
try:
97-
os.chmod(script_realpath, 0o755)
98-
except PermissionError:
99-
sys.exit("\nError: failed to make {script_name} executable, operation not permitted.".format(script_name=script_name))
100-
print("Use default location? /usr/local/bin")
101-
default_location = get_user_response()
102-
if default_location:
103-
try:
104-
os.symlink(script_realpath, os.path.join(os.sep, "usr", "local", "bin", script_name))
105-
except PermissionError:
106-
sys.exit("\nError: failed to create symlink, operation not permitted.")
107-
else:
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-
try:
117-
os.symlink(script_realpath, os.path.join(alternate_path, script_name))
118-
except PermissionError:
119-
sys.exit("\nError: failed to create symlink, operation not permitted.")
120-
else:
121-
sys.exit("Created symlink to {script_name} in {alternate_path}\n".format(script_name=script_name, alternate_path=alternate_path))
122-
else:
123-
sys.exit("\nError: {alternate_path} not found on $PATH, aborting install.\n".format(alternate_path=alternate_path))
124-
else:
125-
sys.exit("Aborting install.\n")
126-
else:
127-
sys.exit("Aborting install.\n")
128-
129-
def unlink():
130-
print("\nFound {script_name} on $PATH in {path_dir}\n".format(script_name=script_name, path_dir=path_dir))
131-
if os.path.islink(script_path_location):
132-
print("Remove symlink to {script_name} in {path_dir}?".format(script_name=script_name, path_dir=path_dir))
133-
proceed = get_user_response()
134-
if proceed:
135-
try:
136-
os.unlink(script_path_location)
137-
except PermissionError:
138-
sys.exit("\nError: operation not permitted.")
139-
else:
140-
print("Unlinked {script_path_location}\n".format(script_path_location=script_path_location))
141-
else:
142-
sys.exit("Aborting uninstall.\n")
143-
else:
144-
sys.exit("Error: {script_path_location} exists on $PATH but is not a symlink, skipping uninstall.\n".format(script_path_location=script_path_location))
145-
sys.exit()
146-
147-
script_name=os.path.basename(sys.argv[0])
148-
script_realpath = os.path.realpath(__file__)
149-
script_on_path = False
150-
for location in os.get_exec_path():
151-
if script_name in os.listdir(location):
152-
script_on_path = True
153-
script_path_location = os.path.join(location, script_name)
154-
break
155-
156-
if script_on_path:
157-
path_dir = os.path.dirname(script_path_location)
158-
script_executable = os.access(script_realpath, os.X_OK)
159-
160-
if install:
161-
if script_on_path:
162-
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))
163-
else:
164-
link()
165-
else:
166-
if not script_on_path:
167-
sys.exit("\n{script_name} not on $PATH, skipping uninstall.\n".format(script_name=script_name))
168-
else:
169-
unlink()
170-
17178
def build_source_list(args):
17279
""" Constructs and returns list of source files
17380
"""

0 commit comments

Comments
 (0)