-
Notifications
You must be signed in to change notification settings - Fork 3
/
bkrdoc_regenerator.py
115 lines (91 loc) · 3.92 KB
/
bkrdoc_regenerator.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
import argparse
import sys
import yaml
import subprocess
class BkrdocException(Exception):
pass
def get_ran_command_data(command):
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
return out, err, p.returncode
def get_purpose_from_markup(test_file_name):
command = "bkrdoc markup {0}".format(test_file_name)
out, err, returncode = get_ran_command_data(command)
if err.find("Given file has no bkrdoc markup!") == 0:
out, err, returncode = get_purpose_from_analysis(test_file_name)
return out, err, returncode
def get_purpose_from_analysis(test_file_name):
command = "bkrdoc analysis {0}".format(test_file_name)
return get_ran_command_data(command)
def do_purpose_regeneration(yaml_doc, test_file_name, purpose_name):
parsed_yaml = yaml.load(yaml_doc)
bkrdoc_front_matter = "---\n" \
"# This doc was generated by bkrdoc\n" \
"bkrdoc : {0}\n" \
"---\n"
if parsed_yaml["bkrdoc"]:
out, err, returncode = get_purpose_from_markup(test_file_name)
bkrdoc_front_matter = bkrdoc_front_matter.format("true")
else:
out, err, returncode = get_purpose_from_analysis(test_file_name)
bkrdoc_front_matter = bkrdoc_front_matter.format("false")
if returncode != 0:
raise BkrdocException("Error occurred by execution of bkrdoc tool")
purpose = open(purpose_name, "w")
purpose.write(bkrdoc_front_matter + out)
purpose.close()
def is_bkrdoc_front_matter(yaml_doc):
bkrdoc_comment = "# This doc was generated by bkrdoc"
if yaml_doc is None:
return False
if yaml_doc.find(bkrdoc_comment) == -1:
return False
parsed_yaml = yaml.load(yaml_doc)
if isinstance(parsed_yaml, dict) and "bkrdoc" in parsed_yaml.keys():
return True
return False
def get_documentation_from_file(file_name):
with open(file_name, "r") as input_file:
return input_file.read()
def get_yaml_front_matter(purpose_doc):
begin = False
yaml_doc = ""
for line in purpose_doc.strip().split('\n'):
if (line.find("---") >= 0) and (not begin):
begin = True
elif (line.find("---") >= 0) and begin:
return yaml_doc
elif begin:
yaml_doc += line + "\n"
elif not begin and not len(yaml_doc):
return ""
def parse_cmd_arguments():
pom_parser = argparse.ArgumentParser(description='File for documentation regeneration due bkrdoc front matter')
pom_parser.add_argument('file', metavar='file', type=str, help='script file')
pom_parser.add_argument('purpose', metavar='purpose', type=str, help='purpose file')
parser_arg = pom_parser.parse_args()
try:
purpose_doc = get_documentation_from_file(parser_arg.purpose)
yaml_doc = get_yaml_front_matter(purpose_doc)
if is_bkrdoc_front_matter(yaml_doc):
do_purpose_regeneration(yaml_doc, parser_arg.file, parser_arg.purpose)
else:
sys.exit(1)
except IOError:
sys.stderr.write("ERROR: Fail to open file: " + parser_arg.purpose + "\n")
sys.exit(1)
except BkrdocException as exc:
sys.stderr.write("Something went wrong during purpose file regeneration. Probably this is a bkrdoc bug.\n"
"Please contact Jiri Kulda(Kulda12@seznam.cz, jkulda@redhat.com) or "
"Petr Muller(pmuller@redhat.com)\n")
sys.stderr.write("More details: {0}\n".format(exc))
sys.exit(1)
except Exception as exc:
sys.stderr.write("Unknown exception. \n"
"Please contact Jiri Kulda(Kulda12@seznam.cz, jkulda@redhat.com) or "
"Petr Muller(pmuller@redhat.com)\n")
sys.stderr.write("{0}\n".format(exc))
sys.exit(1)
if __name__ == "__main__":
parse_cmd_arguments()