py3: Switch almost all shebang lines to use `python3`.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2. In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.
One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2. See discussion on the respective previous
commits that made those explicit. There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-06-20 00:07:06 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import hashlib
|
2019-07-23 23:58:11 +02:00
|
|
|
from typing import Iterable, List, MutableSet
|
2016-06-20 00:07:06 +02:00
|
|
|
|
|
|
|
def expand_reqs_helper(fpath, visited):
|
|
|
|
# type: (str, MutableSet[str]) -> List[str]
|
|
|
|
if fpath in visited:
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
visited.add(fpath)
|
|
|
|
|
|
|
|
curr_dir = os.path.dirname(fpath)
|
2017-05-07 16:50:41 +02:00
|
|
|
result = [] # type: List[str]
|
2016-06-20 00:07:06 +02:00
|
|
|
|
|
|
|
for line in open(fpath):
|
|
|
|
if line.startswith('#'):
|
|
|
|
continue
|
2017-05-07 16:50:41 +02:00
|
|
|
dep = line.split(" #", 1)[0].strip() # remove comments and strip whitespace
|
2016-06-20 00:07:06 +02:00
|
|
|
if dep:
|
|
|
|
if dep.startswith('-r'):
|
|
|
|
child = os.path.join(curr_dir, dep[3:])
|
|
|
|
result += expand_reqs_helper(child, visited)
|
|
|
|
else:
|
|
|
|
result.append(dep)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def expand_reqs(fpath):
|
|
|
|
# type: (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, set())
|
|
|
|
return sorted(set(output))
|
|
|
|
|
|
|
|
def hash_deps(deps):
|
|
|
|
# type: (Iterable[str]) -> str
|
|
|
|
deps_str = "\n".join(deps) + "\n"
|
|
|
|
return hashlib.sha1(deps_str.encode('utf-8')).hexdigest()
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# type: () -> 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())
|