|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
|
|
3
|
|
|
# -*- coding: utf-8 -*- |
|
4
|
|
|
|
|
5
|
|
|
import argparse |
|
6
|
|
|
from os.path import abspath, basename, dirname, exists, expandvars, isdir, isfile, join |
|
7
|
|
|
import json |
|
8
|
|
|
import os |
|
9
|
|
|
import fnmatch |
|
10
|
|
|
import sys |
|
11
|
|
|
import yaml |
|
12
|
|
|
|
|
13
|
|
|
def json_parse(string): |
|
14
|
|
|
""" Parse a JSON string into a dictionary """ |
|
15
|
|
|
try: |
|
16
|
|
|
return json.loads(string) |
|
17
|
|
|
except Exception as e: |
|
18
|
|
|
msg = 'Error parsing JSON config: {}'.format(str(e)) |
|
19
|
|
|
print(msg, file=sys.stderr) |
|
20
|
|
|
raise TypeError(msg) |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def kv_parse(string): |
|
24
|
|
|
""" Parse a string of the form foo=bar into a dictionary """ |
|
25
|
|
|
try: |
|
26
|
|
|
key, val = string.split('=', 1) |
|
27
|
|
|
if key is None or key == '': |
|
28
|
|
|
raise TypeError('Empty key') |
|
29
|
|
|
except Exception as e: |
|
30
|
|
|
err = "Invalid key found parsing KV string '{}': {}".format(string, str(e)) |
|
31
|
|
|
print(err, file=sys.stderr) |
|
32
|
|
|
raise |
|
33
|
|
|
return { key: val } |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def read_context(yaml_file): |
|
37
|
|
|
yaml_file = yaml_file.__str__() |
|
38
|
|
|
with open(yaml_file, 'r') as f: |
|
39
|
|
|
try: |
|
40
|
|
|
in_vars = yaml.load(f, Loader=yaml.SafeLoader) |
|
41
|
|
|
if in_vars is None: |
|
42
|
|
|
raise TypeError("'{}' contains no data".format(yaml_file)) |
|
43
|
|
|
except TypeError as exc: |
|
44
|
|
|
raise exc |
|
45
|
|
|
return in_vars |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def recursive_iglob(rootdir='.', pattern='*'): |
|
49
|
|
|
for root, dirnames, filenames in os.walk(rootdir): |
|
50
|
|
|
for filename in fnmatch.filter(filenames, pattern): |
|
51
|
|
|
yield os.path.join(root, filename) |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def path(fspath, type='file'): |
|
55
|
|
|
""" |
|
56
|
|
|
Checks if a filesystem path exists with the correct type |
|
57
|
|
|
""" |
|
58
|
|
|
|
|
59
|
|
|
fspath = abspath(expandvars(str(fspath))) |
|
60
|
|
|
msg = None |
|
61
|
|
|
prefix = "path '{0}'".format(fspath) |
|
62
|
|
|
|
|
63
|
|
|
if not exists(fspath): |
|
64
|
|
|
msg = "{0} does not exist".format(prefix) |
|
65
|
|
|
|
|
66
|
|
|
if type == 'file' and isdir(fspath): |
|
67
|
|
|
msg = "{0} is not a file".format(prefix) |
|
68
|
|
|
|
|
69
|
|
|
if msg is not None: |
|
70
|
|
|
print(msg, file=sys.stderr) |
|
71
|
|
|
raise argparse.ArgumentTypeError(msg) |
|
72
|
|
|
|
|
73
|
|
|
return fspath |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
def file_or_stdin(file): |
|
77
|
|
|
# /dev/stdin is a special case allowing bash (and other shells?) to name stdin |
|
78
|
|
|
# as a file. While python should have no problem reading from it, we actually |
|
79
|
|
|
# read template relative to the template's basedir and /dev has no templates. |
|
80
|
|
|
if file == '-' or file == '/dev/stdin': |
|
81
|
|
|
return '-' |
|
82
|
|
|
return path(file) |
|
83
|
|
|
|