-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAsset_Download.py
More file actions
79 lines (71 loc) · 2.92 KB
/
Asset_Download.py
File metadata and controls
79 lines (71 loc) · 2.92 KB
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
import os
import re
import errno
import aiohttp
import asyncio
import argparse
def merge_path_dir(path):
new_dir = os.path.dirname(path).replace('/', '_')
return new_dir + '/' + os.path.basename(path)
def check_target_path(target):
if not os.path.exists(os.path.dirname(target)):
try:
os.makedirs(os.path.dirname(target))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
async def download(session, source, target):
if os.path.exists(target):
return
print('Download', source, target)
async with session.get(source) as resp:
assert resp.status == 200
check_target_path(target)
with open(target, 'wb') as f:
f.write(await resp.read())
def read_manifest_by_filter_str(manifest, filter_str):
manifest_set = set()
with open(manifest, 'r') as m:
for l in m:
sp = l.split('|')
if not filter_str or filter_str in sp[1]:
# yield sp[0].strip(), 'output/'+merge_path_dir(sp[1].strip())
# yield sp[0].strip(), './'+sp[1].strip()
mtuple = sp[0].strip(), 'download/'+merge_path_dir(sp[1].strip())
manifest_set.add(mtuple)
return manifest_set
def read_manifest_by_file_list(manifest, file_list):
manifest_set = set()
with open(manifest, 'r') as m:
for l in m:
sp = l.split('|')
label = sp[1].strip()
if label in file_list:
mtuple = sp[0].strip(), 'download/'+merge_path_dir(label)
manifest_set.add(mtuple)
file_list.remove(label)
if len(file_list) == 0:
break
return manifest_set
async def main(manifest, filter_str, old_manifest=None, file_list=[]):
if file_list:
manifest_set = read_manifest_by_file_list(manifest, file_list)
else:
manifest_set = read_manifest_by_filter_str(manifest, filter_str)
if old_manifest is not None:
manifest_old = read_manifest_by_filter_str(old_manifest, filter_str)
manifest_set = manifest_set - manifest_old
async with aiohttp.ClientSession() as session:
await asyncio.gather(*[
download(session, source, target)
for source, target in manifest_set
])
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Download asset files from manifest.')
parser.add_argument('-m', type=str, help='manifest file', required=True)
parser.add_argument('-l', type=str, help='list of files, exact names', default=[], action='extend', nargs='+')
parser.add_argument('-f', type=str, help='filter string', default=None)
parser.add_argument('-o', type=str, help='older manifest', default=None)
args = parser.parse_args()
loop = asyncio.get_event_loop()
loop.run_until_complete(main(args.m, args.f, args.o, [l.strip() for l in args.l]))