1 | #!/usr/bin/env python3
|
2 | """
|
3 | collect_json.py: Dump files and selected environment variables as JSON.
|
4 | """
|
5 | from __future__ import print_function
|
6 |
|
7 | import glob
|
8 | import json
|
9 | import os
|
10 | import sys
|
11 |
|
12 |
|
13 | def main(argv):
|
14 | d = {}
|
15 | metadata_dir = argv[1]
|
16 |
|
17 | for path in glob.glob('%s/*.txt' % metadata_dir):
|
18 | filename = os.path.basename(path)
|
19 | key, _ = os.path.splitext(filename)
|
20 | with open(path) as f:
|
21 | value = f.read()
|
22 | d[key] = value.strip()
|
23 |
|
24 | for name in argv[2:]:
|
25 | d[name] = os.getenv(name) # could be None
|
26 | json.dump(d, sys.stdout, indent=2)
|
27 | print()
|
28 |
|
29 |
|
30 | if __name__ == '__main__':
|
31 | try:
|
32 | main(sys.argv)
|
33 | except RuntimeError as e:
|
34 | print('FATAL: %s' % e, file=sys.stderr)
|
35 | sys.exit(1)
|