Debian Codegraph @

hacking/report.py

raw
1 #!/usr/bin/env python
2 # PYTHON_ARGCOMPLETE_OK
3 """A tool to aggregate data about Ansible source and testing into a sqlite DB for reporting."""
4
5 from __future__ import annotations
6
7 import argparse
8 import json
9 import os
10 import sqlite3
11 import sys
12
13 DATABASE_PATH = os.path.expanduser('~/.ansible/report.db')
14 BASE_PATH = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) + '/'
15 ANSIBLE_PATH = os.path.join(BASE_PATH, 'lib')
16 ANSIBLE_TEST_PATH = os.path.join(BASE_PATH, 'test/lib')
17
18 if ANSIBLE_PATH not in sys.path:
19 sys.path.insert(0, ANSIBLE_PATH)
20
21 if ANSIBLE_TEST_PATH not in sys.path:
22 sys.path.insert(0, ANSIBLE_TEST_PATH)
23
24 from ansible.module_utils.urls import open_url
25 from ansible.parsing.plugin_docs import read_docstring
26
27 from ansible_test._internal.target import walk_integration_targets
28
29
30 def main():
31 os.chdir(BASE_PATH)
32
33 args = parse_args()
34 args.func()
35
36
37 def parse_args():
38 try:
39 import argcomplete
40 except ImportError:
41 argcomplete = None
42
43 parser = argparse.ArgumentParser()
44
45 subparsers = parser.add_subparsers(metavar='COMMAND')
46 subparsers.required = True # work-around for python 3 bug which makes subparsers optional
47
48 populate = subparsers.add_parser('populate',
49 help='populate report database')
50
51 populate.set_defaults(func=populate_database)
52
53 query = subparsers.add_parser('query',
54 help='query report database')
55
56 query.set_defaults(func=query_database)
57
58 if argcomplete:
59 argcomplete.autocomplete(parser)
60
61 args = parser.parse_args()
62
63 return args
64
65
66 def query_database():
67 if not os.path.exists(DATABASE_PATH):
68 sys.exit('error: Database not found. Did you run `report.py populate` first?')
69
70 os.execvp('sqlite3', ('sqlite3', DATABASE_PATH))
71
72
73 def populate_database():
74 populate_modules()
75 populate_coverage()
76 populate_integration_targets()
77
78
79 def populate_modules():
80 module_dir = os.path.join(BASE_PATH, 'lib/ansible/modules/')
81
82 modules_rows = []
83
84 for root, dir_names, file_names in os.walk(module_dir):
85 for file_name in file_names:
86 module, extension = os.path.splitext(file_name)
87
88 if module == '__init__' or extension != '.py':
89 continue
90
91 if module.startswith('_'):
92 module = module[1:]
93
94 namespace = os.path.join(root.replace(module_dir, '')).replace('/', '.')
95
96 path = os.path.join(root, file_name)
97
98 result = read_docstring(path)
99
100 doc = result['doc']
101
102 modules_rows.append(dict(
103 module=module,
104 namespace=namespace,
105 path=path.replace(BASE_PATH, ''),
106 version_added=str(doc.get('version_added', '')) if doc else '',
107 ))
108
109 populate_data(dict(
110 modules=dict(
111 rows=modules_rows,
112 schema=(
113 ('module', 'TEXT'),
114 ('namespace', 'TEXT'),
115 ('path', 'TEXT'),
116 ('version_added', 'TEXT'),
117 )),
118 ))
119
120
121 def populate_coverage():
122 response = open_url('https://codecov.io/api/gh/ansible/ansible/tree/devel/?src=extension')
123 data = json.load(response)
124 files = data['commit']['report']['files']
125 coverage_rows = []
126
127 for path, data in files.items():
128 report = data['t']
129 coverage_rows.append(dict(
130 path=path,
131 coverage=float(report['c']),
132 lines=report['n'],
133 hit=report['h'],
134 partial=report['p'],
135 missed=report['m'],
136 ))
137
138 populate_data(dict(
139 coverage=dict(
140 rows=coverage_rows,
141 schema=(
142 ('path', 'TEXT'),
143 ('coverage', 'REAL'),
144 ('lines', 'INTEGER'),
145 ('hit', 'INTEGER'),
146 ('partial', 'INTEGER'),
147 ('missed', 'INTEGER'),
148 )),
149 ))
150
151
152 def populate_integration_targets():
153 targets = list(walk_integration_targets())
154
155 integration_targets_rows = [dict(
156 target=target.name,
157 type=target.type,
158 path=target.path,
159 script_path=target.script_path,
160 ) for target in targets]
161
162 integration_target_aliases_rows = [dict(
163 target=target.name,
164 alias=alias,
165 ) for target in targets for alias in target.aliases]
166
167 integration_target_modules_rows = [dict(
168 target=target.name,
169 module=module,
170 ) for target in targets for module in target.modules]
171
172 populate_data(dict(
173 integration_targets=dict(
174 rows=integration_targets_rows,
175 schema=(
176 ('target', 'TEXT'),
177 ('type', 'TEXT'),
178 ('path', 'TEXT'),
179 ('script_path', 'TEXT'),
180 )),
181 integration_target_aliases=dict(
182 rows=integration_target_aliases_rows,
183 schema=(
184 ('target', 'TEXT'),
185 ('alias', 'TEXT'),
186 )),
187 integration_target_modules=dict(
188 rows=integration_target_modules_rows,
189 schema=(
190 ('target', 'TEXT'),
191 ('module', 'TEXT'),
192 )),
193 ))
194
195
196 def create_table(cursor, name, columns):
197 schema = ', '.join('%s %s' % column for column in columns)
198
199 cursor.execute('DROP TABLE IF EXISTS %s' % name)
200 cursor.execute('CREATE TABLE %s (%s)' % (name, schema))
201
202
203 def populate_table(cursor, rows, name, columns):
204 create_table(cursor, name, columns)
205
206 values = ', '.join([':%s' % column[0] for column in columns])
207
208 for row in rows:
209 cursor.execute('INSERT INTO %s VALUES (%s)' % (name, values), row)
210
211
212 def populate_data(data):
213 connection = sqlite3.connect(DATABASE_PATH)
214 cursor = connection.cursor()
215
216 for table in data:
217 populate_table(cursor, data[table]['rows'], table, data[table]['schema'])
218
219 connection.commit()
220 connection.close()
221
222
223 if __name__ == '__main__':
224 main()