-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_jenkins_builds.py
200 lines (171 loc) · 5.72 KB
/
parse_jenkins_builds.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "beautifulsoup4",
# "lxml",
# "tqdm",
# ]
# ///
import argparse
import glob
import os
import csv
import logging
import pathlib
from tqdm import tqdm
from multiprocessing import Pool
from bs4 import BeautifulSoup
from itertools import repeat
from datetime import datetime, timezone
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
fh = logging.FileHandler("jenkins_build_parser.log")
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
fieldnames = [
"config_modified_time",
"build_modified_time",
"build_start_time",
"keep_log",
"username",
"build_number",
"result",
"job_name",
"config_description",
]
plugin_fieldnames = [
"name",
"version",
"url",
]
def from_unix(unix_timestamp):
try:
ts = int(unix_timestamp)
except ValueError:
return None
try:
return datetime.fromtimestamp(ts, timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
except ValueError:
return datetime.fromtimestamp(ts / 1000, timezone.utc).strftime(
"%Y-%m-%d %H:%M:%S"
)
def get_attribute_from_soup(attr, soup, file_):
try:
tags = soup.findAll(attr)
if len(tags) > 1:
logger.warning(
f"Multiple {attr} tags found in {file_}. Using the first one."
)
return tags[0].text
except IndexError:
return ""
def get_build_attributes(build_file):
handle = open(build_file)
soup = BeautifulSoup(handle, "xml")
return {
"username": get_attribute_from_soup("userId", soup, build_file),
"build_start_time": from_unix(
get_attribute_from_soup("startTime", soup, build_file)
),
"keep_log": bool(get_attribute_from_soup("keepLog", soup, build_file)),
"result": get_attribute_from_soup("result", soup, build_file),
}
def parse_build(job_to_config, build_path):
path = pathlib.Path(build_path).parts
try:
# Example path to build config: <job_name>/builds/<build_number>/build.xml
job_name = path[-4]
build_number = path[-2]
except IndexError:
logger.error(f"Could not parse job name or build number from {build_path}")
raise
job = job_to_config.get(job_name, {})
build_dict = {
"job_name": job_name,
"build_number": build_number,
"config_modified_time": job.get("modified_time"),
"build_modified_time": from_unix(os.path.getmtime(build_path)),
"config_description": job.get("description"),
**get_build_attributes(build_path),
}
return build_dict
def parse_config(config_path):
path = pathlib.Path(config_path).parts
modified_time = os.path.getmtime(config_path)
formatted_modified_time = from_unix(modified_time)
try:
# Example path to job config: <job_name>/config.xml
job_name = path[-2]
except IndexError:
logger.error(f"Could not parse job name from {config_path}")
raise
try:
with open(config_path, "r") as f:
soup = BeautifulSoup(f, "xml")
description = (
get_attribute_from_soup("description", soup, config_path)
.replace("\n", "\\n")
.encode("utf-8")
)
except Exception as e:
logger.error(f"Could not parse description from {config_path}: {e}")
raise e
job_attrs = {
"modified_time": formatted_modified_time,
"description": description,
}
return job_name, job_attrs
def parse_plugin(plugin_path):
plugin_attrs = {}
try:
with open(plugin_path, "r") as f:
soup = BeautifulSoup(f, "xml")
for field in plugin_fieldnames:
plugin_attrs[field] = get_attribute_from_soup(field, soup, plugin_path)
except Exception as e:
logger.error(f"Could not parse plugin information from {plugin_path}: {e}")
return plugin_attrs
def setup_argparse():
parser = argparse.ArgumentParser(
prog="parse_jenkins_builds.py",
description="Parses Jenkins build.xml and config.xml files and aggregates into a csv file.",
)
parser.add_argument(
"path",
type=str,
help="Path to directory containing Jenkins config.xml and build.xml files. Processed recursively.",
)
return parser.parse_args()
def main():
parser = setup_argparse()
path = parser.path
print("Getting all build.xml files...")
builds = glob.glob(path + "/jobs/**/build.xml", recursive=True)
print("Getting all config.xml files...")
configs = glob.glob(path + "/jobs/**/config.xml", recursive=True)
job_to_config = {}
plugins = glob.glob(path + "/plugins/**/pom.xml", recursive=True)
print("Parsing configs...")
for config_path in tqdm(configs):
job_name, job_attrs = parse_config(config_path)
job_to_config[job_name] = job_attrs
print("Parsing and writing builds...")
with open("jenkins_jobs.csv", "w") as jobs_csv:
writer = csv.DictWriter(jobs_csv, fieldnames=fieldnames)
writer.writeheader()
with Pool() as pool:
for result in pool.starmap(
parse_build, tqdm(zip(repeat(job_to_config), builds), total=len(builds))
):
writer.writerow(result)
jobs_csv.flush()
print("Parsing and writing plugins...")
with open("jenkins_plugins.csv", "w") as plugins_csv:
writer = csv.DictWriter(plugins_csv, fieldnames=plugin_fieldnames)
writer.writeheader()
with Pool() as pool:
for result in pool.map(parse_plugin, tqdm(plugins, total=len(plugins))):
writer.writerow(result)
plugins_csv.flush()
if __name__ == "__main__":
main()