forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_dependencies.py
executable file
·76 lines (64 loc) · 2.28 KB
/
check_dependencies.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
72
73
74
75
76
#!/usr/bin/python
"""Script for checking if a directory tree matches a dependencies list."""
from __future__ import print_function
from argparse import ArgumentParser
import os
import shutil
import sys
def get_dependencies(filename):
"""Get path of each dependency from tsv file."""
deps = set()
with open(filename) as f:
for line in f:
deps.add(line.split("\t", 1)[0])
return deps
def compare_dependencies(deps, srcdir):
"""Give the difference between expected deps and go src directory."""
present = []
unknown = []
for r, ds, fs in os.walk(srcdir):
path = os.path.relpath(r, srcdir)
d = os.path.basename(r)
if path in deps or ("." in d and path.rsplit(".", 1)[0] in deps):
present.append(path)
del ds[:]
elif fs:
unknown.append(path)
del ds[:]
else:
ds.sort()
return present, unknown
def main(argv):
"""Execute the commands from the command line."""
exitcode = 0
args = get_arg_parser().parse_args(argv[1:])
deps = get_dependencies(args.depfile)
known = deps.union(args.ignore)
present, unknown = compare_dependencies(known, args.srcdir)
missing = deps.difference(present)
if missing:
print("Given dependencies missing:\n {}".format("\n ".join(missing)))
exitcode = 1
if unknown:
print("Extant directories unknown:\n {}".format("\n ".join(unknown)))
if args.delete_unknown:
print("...deleting")
for d in unknown:
shutil.rmtree(os.path.join(args.srcdir, d))
else:
exitcode = 1
return exitcode
def get_arg_parser():
"""Return the argument parser for this program."""
parser = ArgumentParser("Compare dependencies with src tree")
parser.add_argument(
"--delete-unknown", action="store_true", default=False,
help="Delete unknown directories rather than fail")
parser.add_argument(
"-i", "--ignore", action="append", default=[],
help="The dependencies.tsv file to check")
parser.add_argument("depfile", help="The dependencies.tsv file to check")
parser.add_argument("srcdir", help="The go src dir to compare")
return parser
if __name__ == '__main__':
sys.exit(main(sys.argv))