This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgdr.py
executable file
·70 lines (53 loc) · 1.8 KB
/
gdr.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
#!/usr/bin/env python
from __future__ import absolute_import, division, print_function, unicode_literals
import json
import sys
from subprocess import Popen as Process, PIPE
class ProcessError(RuntimeError):
pass
class ResolutionError(RuntimeError):
pass
def run(cmd):
proc = Process(cmd.split(), stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
if proc.poll() > 0:
raise ProcessError(err)
return out
def requirements_txt(raw):
open('requirements.txt', 'w+').write(raw)
run("virtualenv env")
run("env/bin/pip install -r requirements.txt")
packages = run("env/bin/pip freeze")
for package in packages.splitlines():
name, version = package.split('==')
path = 'env/lib/python2.7/site-packages/{}-{}.dist-info/METADATA'.format(name, version)
license = 'n/a'
try:
fp = open(path)
except IOError:
pass
else:
for line in fp:
line = line.decode('utf8')
if line.startswith('License:'):
license = line.split()[1]
yield {'name': name, 'version': version, 'license': license}
resolvers = {}
resolvers['requirements.txt'] = requirements_txt
def resolve(filename, content):
if filename not in resolvers: raise Exception # sanity check
proc = Process(['docker', 'run', '-i', 'gdr', filename], stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate(content)
if proc.poll() > 0:
raise ResolutionError(err)
return json.loads(out)
def main(argv):
filename = argv[1]
if filename not in resolvers:
return 1
content = sys.stdin.read()
deps = tuple(resolvers[filename](content))
json.dump(deps, sys.stdout)
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))