|
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 requests |
|
11
|
1 |
|
import subprocess |
|
12
|
1 |
|
import sys |
|
13
|
1 |
|
import yaml |
|
14
|
|
|
|
|
15
|
1 |
|
def json_parse(string): |
|
16
|
|
|
""" Parse a JSON string into a dictionary """ |
|
17
|
1 |
|
try: |
|
18
|
1 |
|
return json.loads(string) |
|
19
|
1 |
|
except Exception as e: |
|
20
|
1 |
|
msg = 'Error parsing JSON config: {}'.format(str(e)) |
|
21
|
1 |
|
print(msg, file=sys.stderr) |
|
22
|
1 |
|
raise TypeError(msg) |
|
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
|
1 |
|
def read_context(yaml_file): |
|
37
|
1 |
|
yaml_file = yaml_file.__str__() |
|
38
|
1 |
|
with open(yaml_file, 'r') as f: |
|
39
|
1 |
|
try: |
|
40
|
1 |
|
in_vars = yaml.load(f, Loader=yaml.SafeLoader) |
|
41
|
1 |
|
if in_vars is None: |
|
42
|
1 |
|
raise TypeError("'{}' contains no data".format(yaml_file)) |
|
43
|
1 |
|
except TypeError as exc: |
|
44
|
1 |
|
raise exc |
|
45
|
1 |
|
return in_vars |
|
46
|
|
|
|
|
47
|
1 |
|
def recursive_iglob(rootdir='.', pattern='*'): |
|
48
|
1 |
|
for root, dirnames, filenames in os.walk(rootdir): |
|
49
|
1 |
|
for filename in fnmatch.filter(filenames, pattern): |
|
50
|
1 |
|
yield os.path.join(root, filename) |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
1 |
|
def path(fspath, type='file'): |
|
54
|
|
|
""" |
|
55
|
|
|
Checks if a filesystem path exists with the correct type |
|
56
|
|
|
""" |
|
57
|
|
|
|
|
58
|
1 |
|
fspath = abspath(expandvars(str(fspath))) |
|
59
|
1 |
|
msg = None |
|
60
|
1 |
|
prefix = "path '{0}'".format(fspath) |
|
61
|
|
|
|
|
62
|
1 |
|
if not exists(fspath): |
|
63
|
1 |
|
msg = "{0} does not exist".format(prefix) |
|
64
|
|
|
|
|
65
|
1 |
|
if type == 'file' and isdir(fspath): |
|
66
|
1 |
|
msg = "{0} is not a file".format(prefix) |
|
67
|
|
|
|
|
68
|
1 |
|
if msg is not None: |
|
69
|
1 |
|
print(msg, file=sys.stderr) |
|
70
|
1 |
|
raise argparse.ArgumentTypeError(msg) |
|
71
|
|
|
|
|
72
|
1 |
|
return fspath |
|
73
|
|
|
|
|
74
|
1 |
|
def file_or_stdin(file): |
|
75
|
|
|
# /dev/stdin is a special case allowing bash (and other shells?) to name stdin |
|
76
|
|
|
# as a file. While python should have no problem reading from it, we actually |
|
77
|
|
|
# read template relative to the template's basedir and /dev has no templates. |
|
78
|
1 |
|
if file == '-' or file == '/dev/stdin': |
|
79
|
1 |
|
return '-' |
|
80
|
1 |
|
return path(file) |
|
81
|
|
|
|
|
82
|
1 |
|
def cmd(args): |
|
83
|
1 |
|
return subprocess.check_output(args.split(' ')).decode('utf-8').strip() |
|
84
|
|
|
|
|
85
|
1 |
|
def load_file(file): |
|
86
|
1 |
|
return open(file, encoding='utf-8').read().strip() |
|
87
|
|
|
|
|
88
|
1 |
|
def get(url): |
|
89
|
1 |
|
response = requests.get(url) |
|
90
|
1 |
|
if 'Content-Type' in response.headers and response.headers['Content-Type'] == 'application/json': |
|
91
|
1 |
|
ret = response.json() |
|
92
|
|
|
else: |
|
93
|
1 |
|
ret = response.content.decode('utf8') |
|
94
|
1 |
|
return ret |
|
95
|
|
|
|
|
96
|
1 |
|
def ip_api(key): |
|
97
|
1 |
|
return json.loads(get('http://ip-api.com/json'))[key] |
|
98
|
|
|
|
|
99
|
1 |
|
def whatismyip(): |
|
100
|
|
|
return get('http://checkip.amazonaws.com/').strip() |
|
101
|
|
|
|