-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_hash_db.py
executable file
·282 lines (217 loc) · 8.64 KB
/
build_hash_db.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/python3
# Copyright (c) 2021 José Manuel Barroso Galindo <theypsilon@gmail.com>
import json
import subprocess
import tempfile
import re
import os
import hashlib
from typing import Any, Dict, List, TypedDict, Tuple
import zlib
import signal
import time
import sys
from pathlib import Path
_print = print
def print(text=""):
_print(text, flush=True)
sys.stdout.flush()
skip_list = []
class HashData(TypedDict):
md5: str
size: int
class InterruptHandler:
def __init__(self, timeout: int):
self._timeout = timeout
self._kill_now = False
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, *args) -> None:
self._kill_now = True
def should_end(self) -> bool:
if self._timeout > 0 and int(time.time()) > self._timeout:
print('Time is out!')
return True
if self._kill_now:
print('Signal received for termination!')
return True
return False
def main() -> None:
print('START!')
source = os.environ['SOURCE'].strip()
db_file = os.environ['DB_FILE'].strip()
oneshot = "ONESHOT" in os.environ
uniq_path = "UNIQ_PATH" in os.environ
timeout = int(os.environ.get('TIMEOUT_MINUTES', '-1').strip()) * 60
if timeout >= 0:
timeout += int(time.time())
print('source: %s' % source)
print('db_file: %s' % db_file)
print('timeout: %d' % timeout)
process(source, InterruptHandler(timeout), db_file, oneshot, uniq_path)
print('Done.')
def process(source: str, interrupt_handler: InterruptHandler, db_file: str, oneshot: bool, uniq_path: bool) -> None:
if re.fullmatch('https://archive[.]org/download/([-_a-z0-9.%]+)/([-_a-z0-9.%]+)[.]zip/', source.lower()):
print('process_with_downloads')
return process_with_downloads(source, interrupt_handler, db_file, oneshot)
if re.fullmatch('([-_a-z0-9.%/\[\]]+)', source.lower()):
print('process_with_metadata_query')
return process_with_metadata_query(source, interrupt_handler, db_file, oneshot, uniq_path)
raise Exception('Could not process source %s' % source)
def process_with_metadata_query(source: str, interrupt_handler: InterruptHandler, db_file: str, oneshot: bool, uniq_path: bool) -> None:
source_route, source_dir = split_on_first_slash(source)
proc = subprocess.run(curl(["https://archive.org/metadata/%s" % source_route]), stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
if proc.returncode == 0:
print('Ok')
else:
print("FAILED! %d" % proc.returncode)
exit(-1)
files = load_files(db_file)
for description in json.loads(proc.stdout.decode())["files"]:
if 'name' not in description:
continue
rom = description["name"]
if description["format"].strip().lower() != "zip":
print('Skip: ' + rom)
continue
if source_dir is not None and not rom.startswith(source_dir):
print('Skip: ' + rom)
continue
print(rom if uniq_path else os.path.basename(rom))
print(uniq_path)
print("CALL SAVE")
save_rom_in_files(db_file, not oneshot, files, rom if uniq_path else os.path.basename(rom), {
"md5": description["md5"].strip(),
"size": int(description["size"].strip()),
"fullpath": rom
})
if interrupt_handler.should_end():
if oneshot: save_db_file(db_file, files)
return
if oneshot: save_db_file(db_file, files)
def process_with_downloads(source: str, interrupt_handler: InterruptHandler, db_file: str, oneshot: bool) -> None:
roms = query_roms(source)
files = load_files(db_file)
verbose = os.environ.get('VERBOSE', 'false') == 'true'
with tempfile.NamedTemporaryFile() as temp:
for rom in roms:
if rom in files or in_skip_list(files, rom):
continue
rom_size = roms[rom]
if rom_size > 1_000_000_000:
add_rom_to_skip_list(files, rom)
save_db_file(db_file, files)
continue
rom_description = try_work_on_rom_a_few_times(rom, source, temp, rom_size, interrupt_handler, verbose)
save_rom_in_files(db_file, not oneshot, files, rom, rom_description)
if interrupt_handler.should_end():
return
def in_skip_list(files, rom):
return '0000_skip_list' in files and rom in files['0000_skip_list']
def add_rom_to_skip_list(files, rom):
print('Skipping %s' % rom)
if '0000_skip_list' not in files:
files['0000_skip_list'] = []
files['0000_skip_list'].append(rom)
def save_rom_in_files(db_file: str, do_save: bool, files: Dict[str, HashData], rom: str, rom_description: HashData) -> None:
uniq_path = "UNIQ_PATH" in os.environ
print(rom if uniq_path else Path(rom).name)
if rom_description is not None:
files[rom if uniq_path else Path(rom).name] = rom_description
if do_save: save_db_file(db_file, files)
def save_db_file(db_file, files):
with open(db_file, 'wt') as f:
json.dump(files, f, indent=4, sort_keys=True)
def try_work_on_rom_a_few_times(rom: str, source: str, temp: Any, expected_size: int, interrupt_handler: InterruptHandler, verbose: bool) -> HashData:
for try_index in range(3):
rom_description = work_on_rom(rom, source, temp, expected_size, verbose)
if rom_description is not None:
return rom_description
print('Try %d failed!' % try_index)
if interrupt_handler.should_end():
return None
print('Waiting 5 minutes until next try...')
time.sleep(300.0)
if interrupt_handler.should_end():
return None
print('Aborting execution with errors.')
exit(-1)
def work_on_rom(rom: str, source: str, temp: Any, expected_size: int, verbose: bool) -> HashData:
print(rom)
url = source + rom
proc = subprocess.run(curl(['-o', temp.name, url], size=expected_size, verbose=verbose), stderr=subprocess.STDOUT)
if proc.returncode != 0:
print('Failed! %d' % proc.returncode)
return None
filesize = size(temp.name)
if filesize != expected_size:
print('File size missmatch')
print('%s != %s' % (filesize, expected_size))
return None
proc = subprocess.run(['unzip', '-t', temp.name], stdout=subprocess.DEVNULL)
if proc.returncode != 0:
print('Wrong zip! %d' % proc.returncode)
return None
md5 = md5_calc(temp.name)
print(md5)
print()
return {
"md5": md5,
"size": filesize
}
def query_roms(source: str) -> Dict[str, int]:
proc = subprocess.run(curl([source]), stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
if proc.returncode != 0:
print('Failed! %d' % proc.returncode)
exit(1)
printing = False
regex = re.compile('.*>([-_a-zA-Z0-9.]+[.]zip)<.*"size">([0-9]+)<.*')
roms = {}
for line in proc.stdout.decode().splitlines():
if not printing and '<main id="maincontent">' in line:
printing = True
elif '</main>' in line:
printing = False
if not printing:
continue
match = regex.match(line.lower())
if not match:
continue
roms[match.group(1)] = int(match.group(2))
return roms
def load_files(db_file: str) -> Dict[str, HashData]:
files = {}
if os.path.isfile(db_file):
with open(db_file, 'r') as f:
files = json.load(f)
return files
def curl(params: List[str], size=0, verbose=False) -> List[str]:
curl_parameters = ['curl', '-L' if verbose else '-sL']
curl_parameters.extend(os.environ.get('CURL_SECURE', '').split())
if size > 1_000_000_000:
curl_parameters.extend(['--header', 'X-Accel-Buffering: no'])
curl_parameters.extend(params)
return curl_parameters
def md5_calc(file: str) -> str:
with open(file, "rb") as f:
file_hash = hashlib.md5()
chunk = f.read(8192)
while chunk:
file_hash.update(chunk)
chunk = f.read(8192)
return file_hash.hexdigest()
def split_on_first_slash(input_string) -> Tuple[str, None]:
split_index = input_string.find('/')
if split_index == -1:
return input_string, None
else:
return input_string[:split_index], input_string[split_index+1:]
def crc32_calc(file: str) -> str:
prev = 0
for line in open(file,"rb"):
prev = zlib.crc32(line, prev)
return "%X"%(prev & 0xFFFFFFFF)
def size(file: str):
return os.path.getsize(file)
if __name__ == "__main__":
main()