-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalidate_requirements.py
30 lines (21 loc) · 1011 Bytes
/
validate_requirements.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
"""Assert requirements.txt file and actual installed dependencies match.
* Use the ``pip freeze`` command to determine what are the installed libraries,
and find differences from the written requirements.
* At the end, write the actual dependencies to the file.
"""
from subprocess import check_call, check_output
GET_DEPENDENCIES_COMMAND = ["pip", "freeze", "--exclude-editable"]
def main():
with open("requirements.txt", "rt") as requirements_file:
written_requirements = set(requirements_file.read().split())
actual_requirements = set(check_output(
GET_DEPENDENCIES_COMMAND).decode("utf-8").split())
try:
diff = "Difference: {}".format(
", ".join(actual_requirements ^ written_requirements))
assert actual_requirements == written_requirements, diff
finally:
with open("requirements.txt", "wt") as requirements_file:
check_call(GET_DEPENDENCIES_COMMAND, stdout=requirements_file)
if __name__ == "__main__":
main()