-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_catalogs.py
68 lines (61 loc) · 2.78 KB
/
run_catalogs.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
"""
Runner to add target catalog, if catalog info is missing. Print output.
"""
import argparse
import logging
from tendrils import api
from flows import download_catalog
def parse():
"""
# Parse command line arguments:
"""
parser = argparse.ArgumentParser(description='Run catalog.')
parser.add_argument('-d', '--debug', help='Print debug messages.', action='store_true')
parser.add_argument('-q', '--quiet', help='Only report warnings and errors.', action='store_true')
parser.add_argument('-c', '--commit', help='Commit downloaded catalog(s) to flows database.', action='store_true')
parser.add_argument('-p', '--print', help='Print single catalog which already exists in flows database.', action='store_true')
parser.add_argument('-t', '--target', type=str, help='Optionally specify just one target for downloading/committing/printing.', nargs='?', default=None)
return parser.parse_args()
def set_logging_level(args):
# Set logging level:
logging_level = logging.INFO
if args.quiet:
logging_level = logging.WARNING
elif args.debug:
logging_level = logging.DEBUG
return logging_level
def main():
# Parse command line arguments:
args = parse()
# Setup logging:
logging_level = set_logging_level(args)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console = logging.StreamHandler()
console.setFormatter(formatter)
logger = logging.getLogger('flows')
if not logger.hasHandlers():
logger.addHandler(console)
logger.setLevel(logging_level)
targets = api.get_catalog_missing()
if args.target is not None:
if args.print is None:
# We want to download and possibly commit the target catalog
if int(args.target) in targets:
logger.info("Downloading catalog for target=%s (committing to db=%s)...", args.target, args.commit)
download_catalog(args.target, update_existing=args.commit) # @TODO: refactor to Tendrils
else:
logger.warning("Cannot find target=%s in list generated by api.get_catalog_missing()", args.target)
else:
# Download target catalog from db for printing
cat = api.get_catalog(args.target)
print(f"Target:{cat['target'].pprint_all()} "
f"\nReferences: {cat['references'].pprint_all()} "
f"\nAvoid:cat['avoid'].pprint_all()")
else:
# Download and possibly commit all missing target catalogs
logger.info("%d catalogs missing", len(targets))
for target in targets:
logger.info("Downloading catalog for target=%s...", target)
download_catalog(target, update_existing=args.commit) # @TODO: refactor to Tendrils
if __name__ == '__main__':
main()