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