-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_writer.py
More file actions
executable file
·75 lines (65 loc) · 2.75 KB
/
test_writer.py
File metadata and controls
executable file
·75 lines (65 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
import sys
import os, os.path
import yaml
import json
import argparse
import httpd_pyparser.apache
import httpd_pyparser.nginx
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="A HTTPD config writer example")
parser.add_argument("-t", "--httpdtype", dest="httpdtype", metavar='Type of HTTPD', type=str,
help='Type of HTTPD, could be apache or nginx', required=True)
parser.add_argument("-c", "--config", dest="config", metavar='/path/to/config.conf', type=str,
help="Path to config file", required=True)
parser.add_argument("-o", "--outputdir", dest="outputdir", metavar='/path/to/outputdir', type=str,
help="Output directory, if it's empty, stdout will be used", required=False)
parser.add_argument("-f", "--format", dest="format", type=str,
help="Input format, could be json or yaml - default is autodetect", required=False)
args = parser.parse_args()
httpdtype = args.httpdtype
srcobj = args.config
if args.outputdir is not None:
outputdir = args.outputdir
else:
outputdir = os.path.dirname(os.path.realpath(__file__))
format = args.format
print("Parsing CRS structure: %s" % srcobj)
if srcobj.endswith(".yaml") or srcobj.endswith(".yml") or (format is not None and format == "yaml"):
try:
with open(srcobj) as file:
if yaml.__version__ >= "5.1":
data = yaml.load(file, Loader=yaml.FullLoader)
else:
data = yaml.load(file)
except:
print("Exception catched - ", sys.exc_info())
sys.exit(-1)
if srcobj.endswith(".json") or (format is not None and format == "json"):
try:
with open(srcobj) as file:
data = json.load(file)
except:
print("Exception catched - ", sys.exc_info())
sys.exit(-1)
dname = ".".join(os.path.basename(srcobj).split(".")[:-1]) + ".conf"
try:
if httpdtype == 'apache':
mwriter = httpd_pyparser.apache.Writer(data, " ")
elif httpdtype == 'nginx':
mwriter = httpd_pyparser.nginx.Writer(data, " ")
else:
print("Unknown httpd type - choose 'apache' or 'nginx'")
sys.exit(1)
except:
print(sys.exc_info()[1])
sys.exit(1)
try:
with open(os.path.join(outputdir, dname), "w") as file:
mwriter.generate()
# add extra new line at the end of file
mwriter.output.append("")
file.write("\n".join(mwriter.output))
except:
print("Exception catched - ", sys.exc_info())
sys.exit(1)