-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromcom.py
151 lines (101 loc) · 5.15 KB
/
romcom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import sys, os, random, string, uuid
import py7zr
class RomCompressor:
def __init__(self, input_dir, input_file_extension, output_dir, output_file_extension):
self.input_dir = input_dir
self.input_file_extension = input_file_extension
self.output_dir = output_dir
self.output_file_extension = output_file_extension
def __gen_rand_uuid(self):
return uuid.uuid4().hex[-10:]
def __is_each_path_valid(self, path_list):
''''''
#is_path_invalid_lambda = lambda path : !os.path.isdir(path)
is_each_path_valid = False
print("path_list: ", path_list)
'''
for path in path_list:
if os.path.isdir(path):
print("%s is a valid path!" % path)
else:
print("%s is an invalid path!" % path)
'''
if path_list:
valid_paths = list(filter(lambda path : os.path.isdir(path), path_list))
is_each_path_valid = len(path_list) == len(valid_paths)
return is_each_path_valid
def __compress_files(self, input_dir, output_dir):
rand_prefix = self.__gen_rand_uuid()
output_prefix = ''.join(("compressed-", rand_prefix[-10:]))
output_root_path = os.path.join(output_dir, output_prefix)
print("TODO - mkdir output root path: %s" % output_root_path)
print("making directory %s" % output_root_path)
os.makedirs(output_root_path, exist_ok=True)
if os.path.isdir(output_root_path):
for root, dirs, files in os.walk(input_dir):
relative_path = os.path.relpath(root, input_dir)
output_relative_path = os.path.join(output_root_path, relative_path)
is_root_dir = root == input_dir
if not is_root_dir:
print("making directory %s" % output_relative_path)
os.makedirs(output_relative_path, exist_ok=True)
if os.path.isdir(output_relative_path):
for file in files:
file_tuple = os.path.splitext(file)
file_name = file_tuple[0]
file_ext = file_tuple[1]
if file_ext and self.input_file_extension and file_ext.lower() == self.input_file_extension.lower():
#print(file)
#print(os.path.join(root, file))
input_file_fullpath = os.path.join(root, file)
output_file_name = f'{file_name}{self.output_file_extension.lower()}'
if is_root_dir:
#output_relative_path = ""
output_file_fullpath = os.path.join(output_root_path, output_file_name)
else:
#output_relative_path = os.path.join(relative_path, output_file)
output_file_fullpath = os.path.join(output_relative_path, output_file_name)
print("output file: %s" % output_file_fullpath)
if output_file_fullpath:
self.__create_seven_zip(input_file_fullpath, output_file_fullpath, file)
def __create_seven_zip(self, input_fullpath, archive_fullpath, input_file_name):
print("compressing %s to %s" % (input_fullpath, archive_fullpath))
with py7zr.SevenZipFile(archive_fullpath, 'w') as archive:
archive.writeall(input_fullpath, input_file_name)
def start_compressor(self, custom_input_dir=None, custom_output_dir=None):
final_input_dir = ""
final_output_dir = ""
# support overriding the default input/output locations
if None != custom_input_dir:
final_input_dir = custom_input_dir
else:
final_input_dir = self.input_dir
if None != custom_output_dir:
final_output_dir = custom_output_dir
else:
final_output_dir = self.output_dir
if self.__is_each_path_valid([final_input_dir, final_output_dir]):
self.__compress_files(final_input_dir, final_output_dir)
print("All paths are valid!")
else:
sys.exit("Error: one or more paths are invalid.")
def print_usage():
print('\n'.join(('usage: romcom.py input_dir input_file_extension output_dir',
'example usage: romcom.py /home/user/roms ".nes" /mnt/backup/nes/roms',
'all arguments are required')))
def main():
print("Welcome to RomCom!")
print_usage()
if len(sys.argv) != 4:
#print_usage()
sys.exit("Error: invalid arguments")
input_dir = sys.argv[1]
input_file_extension = sys.argv[2]
output_dir = sys.argv[3]
output_file_extension = ".7z"
compressor = RomCompressor(input_dir, input_file_extension, output_dir, output_file_extension)
compressor.start_compressor()
#output_directory = os.path.join(output_root,
#print(traverseDirectory(sourceDirectory, output_directory))
if __name__ == '__main__':
main()