-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverify.py
executable file
·80 lines (65 loc) · 2.7 KB
/
serverify.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
#!/usr/bin/env python
"""
Run `serverify-pip -h` for help.
"""
import argparse
import logging
import os
import sys
import pip
pip_version = tuple(int(x) if x.isdigit() else x
for x in pip.__version__.split('.'))
from pip._vendor.packaging.requirements import Requirement
if pip_version < (10,):
from pip.index import Link
from pip.req import parse_requirements, InstallRequirement
else:
from pip._internal.index import Link
from pip._internal.req import parse_requirements, InstallRequirement
__version__ = '1.0.6'
parser = argparse.ArgumentParser(
description='Serverify Requirements - export VCS dependencies locally')
parser.add_argument('-o', '--output-file', required=True, metavar='OUTPUT_FILE',
help='Combined requirements file')
parser.add_argument('-d', '--download-to', required=True, metavar='SRC_DIR',
help='Directory for exporting source files')
parser.add_argument('--debug', action='store_true',
help='Sets logging level to DEBUG')
parser.add_argument('reqs', nargs='+', metavar='requirements.txt',
help='Path to requirements file')
def serverify(output_path, download_to, req_files):
abs_download_to = os.path.abspath(download_to)
if not os.path.exists(abs_download_to):
os.makedirs(abs_download_to)
with open(output_path, 'w+') as fp:
for req_file in req_files:
for r in parse_requirements(req_file, session=True):
line = requirement_line(r, download_to, abs_download_to)
print(line)
sys.stdout.flush()
fp.write(line + '\n')
def requirement_line(r, download_to, abs_download_to):
if isinstance(r.link, Link):
req = r.req if pip_version > (10,) else r
ir = InstallRequirement(
req=req, link=r.link, comes_from=None,
markers=True, editable=True)
assert r.name, "Package name not provided for %s" % r.link
s = '{}/{}/'.format(download_to, ir.name.lower())
ir.source_dir = '{}/{}'.format(abs_download_to, ir.name.lower())
ir.update_editable(obtain=False)
else:
assert isinstance(r.req, Requirement), r.req.__class__
s = '%-30s' % str(r.req)
for hash_type, hashes in r.options.get('hashes', {}).items():
for h in hashes:
s += ' --hash={}:{}'.format(hash_type, h)
return s.strip()
def main(args=None):
if not args:
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG if args.debug else logging.ERROR)
serverify(output_path=args.output_file, download_to=args.download_to,
req_files=args.reqs)
if __name__ == '__main__':
main()