OILS / soil / collect_json.py View on Github | oilshell.org

35 lines, 24 significant
1#!/usr/bin/env python3
2"""
3collect_json.py: Dump files and selected environment variables as JSON.
4"""
5from __future__ import print_function
6
7import glob
8import json
9import os
10import sys
11
12
13def 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
30if __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)