-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindexer.py
122 lines (114 loc) · 4.72 KB
/
indexer.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
# indexer.py
import logging
import sqlite3
import requests
from config import DB_PATH, BRANCHES, REPO_COMPONENTS
logger = logging.getLogger("AptIndexer")
class AptIndexer:
def __init__(self):
self.db = sqlite3.connect(DB_PATH)
self.cursor = self.db.cursor()
self.cursor.execute(
"""
CREATE TABLE IF NOT EXISTS packages (
id INTEGER PRIMARY KEY,
name TEXT,
version TEXT,
description TEXT,
section TEXT,
homepage TEXT,
maintainer TEXT,
depends TEXT,
recommends TEXT,
suggests TEXT,
conflicts TEXT,
replaces TEXT,
provides TEXT,
filename TEXT,
branch TEXT
)
"""
)
self.db.commit()
def cleanup(self):
self.cursor.execute("DELETE FROM packages")
self.db.commit()
def index(self):
for branch, base_url in BRANCHES.items():
for component in REPO_COMPONENTS:
url = base_url.replace("@", component)
logger.info(
f"Indexing branch '{branch}' component '{component}' at {url}"
)
data = requests.get(url).text.split("\n\n")
values = []
for pkg_raw in data:
if not pkg_raw.strip():
continue
lines = pkg_raw.split("\n")
name = version = description = section = homepage = maintainer = (
None
)
depends = recommends = suggests = conflicts = replaces = (
provides
) = filename = None
for line in lines:
if line.startswith("Package: "):
name = line.split(": ")[1]
elif line.startswith("Version: "):
version = line.split(": ")[1]
elif line.startswith("Description: "):
description = line.split(": ")[1]
elif line.startswith("Section: "):
section = line.split(": ")[1]
elif line.startswith("Homepage: "):
homepage = line.split(": ")[1]
elif line.startswith("Maintainer: "):
maintainer = line.split(": ")[1]
elif line.startswith("Depends: "):
depends = line.split(": ")[1]
elif line.startswith("Recommends: "):
recommends = line.split(": ")[1]
elif line.startswith("Suggests: "):
suggests = line.split(": ")[1]
elif line.startswith("Conflicts: "):
conflicts = line.split(": ")[1]
elif line.startswith("Replaces: "):
replaces = line.split(": ")[1]
elif line.startswith("Provides: "):
provides = line.split(": ")[1]
elif line.startswith("Filename: "):
filename = line.split(": ")[1]
if name:
values.append(
(
name,
version,
description,
section,
homepage,
maintainer,
depends,
recommends,
suggests,
conflicts,
replaces,
provides,
filename,
branch,
)
)
self.cursor.executemany(
"""
INSERT INTO packages (
name, version, description, section, homepage, maintainer,
depends, recommends, suggests, conflicts, replaces, provides,
filename, branch
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
values,
)
self.db.commit()
logger.info(
f"Finished indexing {len(values)} packages for branch '{branch}' component '{component}'."
)