-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
76 lines (56 loc) · 2.44 KB
/
handler.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
import re
import git
from database import query
repo = git.Repo('./nixpkgs')
config_writer = repo.config_writer()
config_writer.set_value('feature', 'manyFiles', '1')
config_writer.release()
version_pattern = re.compile(r'version\s*=\s*"(.+?)(?=")')
name_pattern = re.compile(r'name\s*=\s*"((?=[^"]*(\d+\.\d+|\d+-\d+)).+?)"')
def get_package_path(package_name):
commit_tree = repo.head.commit.tree
all_packages = commit_tree['pkgs/top-level/all-packages.nix'].data_stream.read().decode('utf-8')
if not (path := re.search(f'^\s*\\b{package_name}\\b\s*=.*', all_packages, flags=re.MULTILINE)):
return None
path = path.group(0).split()[3].replace('..', 'pkgs') + '/default.nix'
print(path)
return path
def get_versions_from_commits(package_path, package_name, from_commit: str = None):
versions = {}
args = ['--pretty=format:%H', '--name-only', '--follow', package_path]
if from_commit:
args.insert(0, f'{from_commit}..HEAD')
log = repo.git.log(*args)
# reverse list
log = [i for i in log.split('\n')[::-1] if i]
it = iter(log)
for filepath in it:
commit_hash = next(it)
commit = repo.commit(commit_hash)
file_content = commit.tree[filepath].data_stream.read().decode('utf-8')
if version := version_pattern.search(file_content):
version = version.group(1)
else:
if version := name_pattern.search(file_content):
version = version.group(1).replace(f'{package_name}-', '')
else:
version = commit_hash[0:7]
versions[version] = commit_hash
return versions
def get_package_versions(package_name, package_path):
args = [package_path, package_name]
if query("SELECT * FROM version WHERE LOWER(package) = ?", package_name):
# latest commit hash
latest = \
query("SELECT commit_hash FROM version WHERE LOWER(package) = ? ORDER BY timestamp DESC LIMIT 1",
package_name)[
0][0]
args += [latest]
versions = get_versions_from_commits(*args)
if versions:
for version, commit_hash in versions.items():
query('INSERT INTO version(package, version, commit_hash) VALUES (?, ?, ?)', package_name, version,
commit_hash)
result = query('SELECT version, commit_hash FROM version WHERE LOWER(package) = ? ORDER BY timestamp DESC',
package_name)
return result