Files
zulip/scripts/lib/hash_reqs.py
Aman Agrawal 2668829c93 hash_reqs: Use combined package name and version to generate hash.
We were already using packages names along with their versions
to generate hash for the requirement file, as we were passing
the `.txt` files to the hash_reqs file instead of intended `.in` files
for which the functions in this file was originially designed.

Changed the expand_reqs_helper function to adapt for the `.txt` files.
2020-07-13 13:06:15 -07:00

54 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import hashlib
import os
import sys
from typing import Iterable, List, MutableSet
def expand_reqs_helper(fpath: str) -> List[str]:
result = [] # type: List[str]
for line in open(fpath):
if line.strip().startswith(('#', '--hash')):
continue
dep = line.split(" \\", 1)[0].strip()
if dep:
result.append(dep)
return result
def expand_reqs(fpath: str) -> List[str]:
"""
Returns a sorted list of unique dependencies specified by the requirements file `fpath`.
Removes comments from the output and recursively visits files specified inside `fpath`.
`fpath` can be either an absolute path or a relative path.
"""
absfpath = os.path.abspath(fpath)
output = expand_reqs_helper(absfpath)
return sorted(set(output))
def hash_deps(deps: Iterable[str]) -> str:
deps_str = "\n".join(deps) + "\n"
return hashlib.sha1(deps_str.encode('utf-8')).hexdigest()
def main() -> int:
description = ("Finds the SHA1 hash of list of dependencies in a requirements file"
" after recursively visiting all files specified in it.")
parser = argparse.ArgumentParser(description=description)
parser.add_argument("fpath", metavar="FILE",
help="Path to requirements file")
parser.add_argument("--print", dest="print_reqs", action='store_true',
help="Print all dependencies")
args = parser.parse_args()
deps = expand_reqs(args.fpath)
hash = hash_deps(deps)
print(hash)
if args.print_reqs:
for dep in deps:
print(dep)
return 0
if __name__ == "__main__":
sys.exit(main())