-
Notifications
You must be signed in to change notification settings - Fork 3
/
run
executable file
·379 lines (327 loc) · 10.5 KB
/
run
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/python3
import ast
import contextlib
import os
import pathlib
import requests
import sqlite3
import subprocess
import yaml
BASE_DIRECTORY = pathlib.Path(__file__).resolve().parent
CLONE_DIRECTORY = BASE_DIRECTORY / "analyse"
OUTPUT_DIRECTORY = BASE_DIRECTORY / "output"
CONFIG_FILE = BASE_DIRECTORY / "config.yml"
def run(cwd, *args, **kwargs):
"""Shortcut to run a command in a given directory."""
kwargs.setdefault("check", True)
kwargs.setdefault("cwd", cwd)
return subprocess.run(args, **kwargs)
def run_read(cwd, *args, **kwargs):
"""Shortcut to run a command in a given directory."""
return run(cwd, *args, **kwargs, stdout=subprocess.PIPE).stdout.decode("utf-8")
def get_repo(org, repo, version):
"""Clone or update a repo to the given version."""
path = CLONE_DIRECTORY / org / repo
if path.exists():
print(f"Updating {org}/{repo}@{version}")
run(path, "git", "checkout", version)
run(path, "git", "pull")
else:
print(f"Cloning {org}/{repo}@{version}")
org_path = CLONE_DIRECTORY / org
path.mkdir(exist_ok=True, parents=True)
run(org_path, "git", "clone", f"https://github.com/{org}/{repo}", "-b", version)
return path
def analyse_module(path):
"""Analyse a module and return a dict of its data."""
manifest = path / "__manifest__.py"
if not manifest.exists():
return
with open(manifest, mode="r") as f:
data = ast.literal_eval(f.read())
if not data.get("installable", True):
return
description_path = path / "static" / "description" / "index.html"
description = ""
if description_path.exists():
with open(description_path, "r") as f:
description = f.read()
return {
"name": data["name"],
"author": data["author"],
"depends": data.get("depends", []),
"maintainers": data.get("mainainers", []),
"development_status": data.get("development_status"),
"summary": data.get("summary"),
"description": description,
}
def analyse_repo(path):
"""Analyse a repo and return a dict of module name and its data."""
return {
module.name: result
for module in path.iterdir()
for result in [analyse_module(path / module)]
if result
}
def make_schema(db):
"""Create the database schema."""
print("Creating database schema (if not exists)")
db.execute("PRAGMA foreign_keys = ON")
db.execute(
"""
CREATE TABLE IF NOT EXISTS "commit" (
id INTEGER PRIMARY KEY,
repo_id INTEGER,
hash TEXT,
date TEXT,
author TEXT,
mail TEXT,
message TEXT,
FOREIGN KEY(repo_id) REFERENCES repo(id),
UNIQUE(repo_id, hash)
)
"""
)
db.execute(
"""
CREATE TABLE IF NOT EXISTS repo (
id INTEGER PRIMARY KEY,
org TEXT,
repo TEXT,
UNIQUE(org, repo)
)
"""
)
db.execute(
"""
CREATE TABLE IF NOT EXISTS module (
id INTEGER PRIMARY KEY,
repo_id INTEGER,
name TEXT,
title TEXT,
author TEXT,
depends TEXT,
maintainers TEXT,
development_status TEXT,
summary TEXT,
FOREIGN KEY(repo_id) REFERENCES repo(id)
UNIQUE(repo_id, name)
)
"""
)
db.execute(
"""
CREATE TABLE IF NOT EXISTS module_commit (
commit_id INTEGER,
module_id INTEGER,
FOREIGN KEY(commit_id) REFERENCES "commit"(id),
FOREIGN KEY(module_id) REFERENCES module(id),
PRIMARY KEY(commit_id, module_id)
)
"""
)
def download(path, url):
"""Download a file from a url and write it to path."""
print(f"Downloading {url}")
response = requests.get(url)
response.raise_for_status()
with open(path, "wb") as f:
f.write(response.content)
@contextlib.contextmanager
def sqlite_db(path, clean=False):
"""Context manager for the sqlite database."""
if clean and path.exists():
path.unlink()
if not clean and not path.exists():
# Try to download last published db version
url = (
f"https://github.com/akretion/odoo-module-tracker/raw/gh-pages/{path.name}"
)
try:
download(path, url)
except Exception as e:
print(f"Fail to download db {url} {e}")
conn = sqlite3.connect(path)
db = conn.cursor()
make_schema(db)
try:
yield db
conn.commit()
finally:
conn.close()
def fetch(db, query, *args):
"""Fetch a single row from the database."""
return db.execute(query, args).fetchone()
def scalar(db, query, *args):
"""Fetch a single value from the database."""
res = fetch(db, query, *args)
return res[0] if res else None
def upsert_repo(db, org, repo):
"""Get the repo id from the database, or insert it if it doesn't exist."""
return scalar(
db,
"""
INSERT INTO repo (org, repo)
VALUES (?, ?)
ON CONFLICT(org, repo)
DO UPDATE SET
org = EXCLUDED.org,
repo = EXCLUDED.repo
RETURNING id
""",
org,
repo,
)
def upsert_module(db, repo_id, module_name, module_data):
"""Get the module id from the database, or insert it if it doesn't exist."""
return scalar(
db,
"""
INSERT INTO module (repo_id, name, title, author, depends, maintainers, development_status, summary)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(repo_id, name)
DO UPDATE SET
title = EXCLUDED.title,
author = EXCLUDED.author,
depends = EXCLUDED.depends,
maintainers = EXCLUDED.maintainers,
development_status = EXCLUDED.development_status,
summary = EXCLUDED.summary
RETURNING id
""",
repo_id,
module_name,
module_data["name"],
module_data["author"],
",".join(module_data["depends"]),
",".join(module_data["maintainers"]),
module_data["development_status"],
module_data["summary"],
)
def upsert_commit(
db, repo_id, commit_hash, commit_date, commit_author, commit_mail, commit_message
):
"""Get the commit id from the database, or insert it if it doesn't exist."""
return scalar(
db,
"""
INSERT INTO "commit" (repo_id, hash, date, author, mail, message)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(repo_id, hash)
DO UPDATE SET
date = EXCLUDED.date,
author = EXCLUDED.author,
mail = EXCLUDED.mail,
message = EXCLUDED.message
RETURNING id
""",
repo_id,
commit_hash,
commit_date,
commit_author,
commit_mail,
commit_message,
)
def insert_module_commit(db, commit_id, module_id):
"""Insert a module commit in the database."""
db.execute(
"""
INSERT INTO module_commit (commit_id, module_id)
VALUES (?, ?)
""",
(
commit_id,
module_id,
),
)
def fetch_last_commit(db, repo_id):
"""Fetch the last commit of a repo."""
# Use order by id and not date since commits can be out of order
return scalar(
db,
"""
SELECT hash
FROM "commit"
WHERE repo_id = ?
ORDER BY id DESC
LIMIT 1
""",
repo_id,
)
def analyse_commits(db, repo_id, path, modules):
"""Analyse the commits of a repo and store it in the database."""
# Update modules
module_ids = {
module_name: upsert_module(db, repo_id, module_name, module_data)
for module_name, module_data in modules.items()
}
last_commit = fetch_last_commit(db, repo_id)
target = f"{last_commit}..HEAD" if last_commit else "HEAD"
print(f"Analyzing commits for {path.name} ({target})")
rev_list = run_read(path, "git", "rev-list", target).strip()
if not rev_list:
return
commits = rev_list.split("\n")
for commit in reversed(commits):
commit_data = run_read(
path,
"git",
"show",
"--pretty=format:%aI%x18%an%x18%s%x18%ae",
"--name-only",
commit,
).strip()
header, *files = commit_data.split("\n")
commit_date, commit_author, commit_message, commit_mail = header.split(
"\x18", 3
)
commit_id = upsert_commit(
db, repo_id, commit, commit_date, commit_author, commit_mail, commit_message
)
modules = {
module
for file in files
for module in [file.split("/")[0]]
if module in module_ids
}
for module in modules:
insert_module_commit(db, commit_id, module_ids[module])
def main():
"""Run the main analysis, writing the results to yaml files and sqlite db."""
result = {}
with open(CONFIG_FILE, "r") as f:
config = yaml.safe_load(f)
version = os.environ.get("ODOO_VERSION", "14.0")
restrict = {
tuple(org_repo.strip().split("/", 1))
for restrict_modules in [os.environ.get("RESTRICT_MODULES", "")]
for org_repo in restrict_modules.split(",")
if restrict_modules
}
clean_db = os.environ.get("CLEAN_DB")
OUTPUT_DIRECTORY.mkdir(exist_ok=True)
CLONE_DIRECTORY.mkdir(exist_ok=True)
with sqlite_db(OUTPUT_DIRECTORY / f"{version}.db", clean=clean_db) as db:
for org, repos in config.items():
result[org] = {}
for repo in repos:
if restrict and (org, repo) not in restrict:
continue
try:
path = get_repo(org, repo, version)
except Exception as e:
print("Fail to fetch repo skip it", e)
continue
result[org][repo] = analyse_repo(path)
analyse_commits(db, upsert_repo(db, org, repo), path, result[org][repo])
data = yaml.safe_dump(result)
with open(OUTPUT_DIRECTORY / f"{version}.yml", "w") as output:
output.write(data)
for org, repos in result.items():
for repo in repos:
result[org][repo] = list(result[org][repo].keys())
data = yaml.safe_dump(result)
with open(OUTPUT_DIRECTORY / f"module-list-{version}.yml", "w") as output:
output.write(data)
if __name__ == "__main__":
main()