This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-requirements
executable file
·57 lines (44 loc) · 1.91 KB
/
update-requirements
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
#!/usr/bin/env python3
from os.path import join
from subprocess import check_call, check_output
from sys import argv
from tempfile import TemporaryDirectory
import re
def print_packages(packages, heading):
print(f'{heading}:')
print(' ', end='')
print('\n '.join(packages))
# would be nice if there was a cleaner way to get this, but I've not found a
# more reliable one.
with open('setup.py') as fh:
match = re.search(r"name='(?P<pkg>[\w-]+)',", fh.read())
if not match:
raise Exception('failed to determine our package name')
our_package_name = match.group('pkg')
print(f'our_package_name: {our_package_name}')
with TemporaryDirectory() as tmpdir:
check_call(['python3', '-m', 'venv', tmpdir])
# base needs
check_call([join(tmpdir, 'bin', 'pip'), 'install', '.'])
frozen = check_output([join(tmpdir, 'bin', 'pip'), 'freeze'])
frozen = set(frozen.decode('utf-8').strip().split('\n'))
# dev additions
check_call([join(tmpdir, 'bin', 'pip'), 'install', '.[dev]'])
dev_frozen = check_output([join(tmpdir, 'bin', 'pip'), 'freeze'])
dev_frozen = set(dev_frozen.decode('utf-8').strip().split('\n')) - frozen
# pip installs the module itself along with deps so we need to get that out of
# our list by finding the thing that was file installed during dev
frozen = sorted([p for p in frozen if not p.startswith(our_package_name)])
dev_frozen = sorted([p for p in dev_frozen
if not p.startswith(our_package_name)])
print_packages(frozen, 'frozen')
print_packages(dev_frozen, 'dev_frozen')
script = argv[0]
with open('requirements.txt', 'w') as fh:
fh.write(f'# DO NOT EDIT THIS FILE DIRECTLY - use {script} to update\n')
fh.write('\n'.join(frozen))
fh.write('\n')
with open('requirements-dev.txt', 'w') as fh:
fh.write(f'# DO NOT EDIT THIS FILE DIRECTLY - use {script} to update\n')
fh.write('\n'.join(dev_frozen))
fh.write('\n')