- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathget_external.py
executable file
·77 lines (66 loc) · 2.3 KB
/
get_external.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
#!/usr/bin/env python3
importargparse
importos
importpathlib
importsys
importtime
importzipfile
fromurllib.requestimporturlretrieve
deffetch_zip(commit_hash, zip_dir, *, org='python', binary=False, verbose):
repo=f'cpython-{"bin"ifbinaryelse"source"}-deps'
url=f'https://github.com/{org}/{repo}/archive/{commit_hash}.zip'
reporthook=None
ifverbose:
reporthook=print
zip_dir.mkdir(parents=True, exist_ok=True)
filename, headers=urlretrieve(
url,
zip_dir/f'{commit_hash}.zip',
reporthook=reporthook,
)
returnfilename
defextract_zip(externals_dir, zip_path):
withzipfile.ZipFile(os.fspath(zip_path)) aszf:
zf.extractall(os.fspath(externals_dir))
returnexternals_dir/zf.namelist()[0].split('/')[0]
defparse_args():
p=argparse.ArgumentParser()
p.add_argument('-v', '--verbose', action='store_true')
p.add_argument('-b', '--binary', action='store_true',
help='Is the dependency in the binary repo?')
p.add_argument('-O', '--organization',
help='Organization owning the deps repos', default='python')
p.add_argument('-e', '--externals-dir', type=pathlib.Path,
help='Directory in which to store dependencies',
default=pathlib.Path(__file__).parent.parent/'externals')
p.add_argument('tag',
help='tag of the dependency')
returnp.parse_args()
defmain():
args=parse_args()
zip_path=fetch_zip(
args.tag,
args.externals_dir/'zips',
org=args.organization,
binary=args.binary,
verbose=args.verbose,
)
final_name=args.externals_dir/args.tag
extracted=extract_zip(args.externals_dir, zip_path)
forwaitin [1, 2, 3, 5, 8, 0]:
try:
extracted.replace(final_name)
break
exceptPermissionErrorasex:
retry=f" Retrying in {wait}s..."ifwaitelse""
print(f"Encountered permission error '{ex}'.{retry}", file=sys.stderr)
time.sleep(wait)
else:
print(
f"ERROR: Failed to extract {final_name}.",
"You may need to restart your build",
file=sys.stderr,
)
sys.exit(1)
if__name__=='__main__':
main()