-
Notifications
You must be signed in to change notification settings - Fork 1
/
repository.py
56 lines (48 loc) · 1.83 KB
/
repository.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
import io
import tarfile
from datetime import datetime, timedelta
import requests
class Repository:
def __init__(self):
self.packages = {}
self.last_update = None
self.update_threshold = timedelta(hours=12)
self.name = "arch4edu"
self.mirror = "https://mirrors.tuna.tsinghua.edu.cn/arch4edu"
self.arch = ["aarch64", "x86_64"]
self.session = requests.session()
def update(self):
_packages = {}
for arch in self.arch:
content = self.session.get(
f"{self.mirror}/{arch}/{self.name}.db.tar.gz"
).content
content = io.BytesIO(content)
tar = tarfile.open(fileobj=content, mode="r:gz")
for member in tar.getmembers():
if member.isfile():
f = tar.extractfile(member)
lines = f.read().decode("utf-8").split("\n")
arch = lines[lines.index("%ARCH%") + 1]
pkgname = lines[lines.index("%NAME%") + 1]
version = lines[lines.index("%VERSION%") + 1]
key = f"{arch}/{pkgname}"
if not key in _packages:
_packages[key] = (pkgname, version, arch)
self.packages = _packages
self.last_update = datetime.now()
def need_update(self):
if self.last_update is None:
return True
if datetime.now() - self.last_update > self.update_threshold:
return True
return False
def search(self, package):
if self.need_update():
self.update()
result = [i for i in self.packages.values() if i[0] == package]
return result
if __name__ == "__main__":
repository = Repository()
result = repository.search("yay")
print("\n".join(" ".join(i) for i in result))