-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackage_archaeology.py
executable file
·71 lines (56 loc) · 1.83 KB
/
package_archaeology.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
#!/usr/bin/env python
from subprocess import check_output
from pathlib import Path
import pandas as pd
import io
def parse_import(content):
lines = content.split('\n')
packages = []
for line in lines:
line = line.strip().replace('\"', '').replace('\\n', '').replace(',', ' ')
if '#' in line or len(line.strip()) == 0:
continue
tokens = line.split()
if 'from' in tokens:
package = tokens[tokens.index('from') + 1]
elif 'import' in tokens:
package = tokens[tokens.index('import') + 1]
else:
print(f'Not likely an import line. Here is the tokens {tokens}. Decide for yourself.')
package = package.split('.')[0]
if len(package) > 0:
packages.append(package)
return packages
if __name__ == '__main__':
pip_or_conda = 'conda'
cmd = f'{pip_or_conda} list'
columns = ['name', 'Version', 'Build', 'Channel']
output = check_output(cmd, shell=True).decode('utf-8')
data = io.StringIO(output)
df = pd.read_csv(data, sep='\s+', comment='#', index_col=0, names=columns)
print(df)
fnames = list(Path('./').glob('**/*.py')) + list(Path('./').glob('**/*.ipynb'))
# fnames = ['cycleGan.ipynb', 'models/base_model.py', 'models/alexnet.py', 'models/resnet.py', 'utils/network.py']
packages_all = []
for fname in fnames:
self_stem = Path(__file__).stem
if self_stem in str(fname):
continue
cmd = f"grep \"import\" {fname}"
try:
output = check_output(cmd, shell=True).decode('utf-8')
packages = parse_import(output)
packages_all += packages
except:
continue
requirements = []
packages_all = set(packages_all)
for i, package in enumerate(packages_all):
try:
version = df.loc[package, 'Version']
print(f'{i + 1}: {package}, {version}')
requirements.append(f'{package}>={version}')
except:
continue
with open('requirements.txt', 'w') as fh:
fh.write('\n'.join(requirements))