-
Notifications
You must be signed in to change notification settings - Fork 0
/
clyde_server
executable file
·63 lines (57 loc) · 2.36 KB
/
clyde_server
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
#!/usr/bin/env python
import tarfile
import tempfile
import urllib.request
import re
import os
import toml
def mirror_status(url, wanted):
with tempfile.TemporaryDirectory() as tmp:
filename = os.path.join(tmp, os.path.basename(url))
urllib.request.urlretrieve(url, filename)
tar = tarfile.open(filename, mode='r')
for member in tar.getmembers():
pkgname, _ = package_version(member.name)
if pkgname in wanted:
tar.extract(member, tmp)
pkgnames = [x for x in os.listdir(tmp) if x != os.path.basename(url)]
pkgname2filename = {}
for pkgname in pkgnames:
with open(os.path.join(tmp, pkgname, "desc")) as f:
assert f.readline() == "%FILENAME%\n"
filename = f.readline().rstrip()
pkgname2filename[pkgname] = filename
return [(package_version(x), pkgname2filename[x]) for x in pkgnames]
def package_version(s):
m = re.match(r"^(.*)(-.*-.*)$", s)
return m.group(1), m.group(2)[1:]
def updates(wanted, locally_stored, url):
mstatus = mirror_status(url, wanted)
for (pkgname, _), filename in mstatus:
if pkgname in wanted and not filename in locally_stored:
yield filename
def main():
with open("/etc/clyde_server/config.toml") as f:
config = toml.load(f)
with open("/etc/cpcache/cpcache.toml") as f:
cache_dir = toml.load(f)["cache_directory"]
for dist in config.keys():
arch = "x86_64"
wanted = set()
path = os.path.join(cache_dir, "wanted_packages")
for host in os.listdir(path):
with open(os.path.join(cache_dir, "wanted_packages", host)) as f:
for package_name in f:
wanted.add(package_name.rstrip())
for repo, attribs in config.items():
pkg_url = attribs["pkg_url"]
db_url = os.path.join(pkg_url, repo + ".db")
print("Check for new files in repository", repo)
locally_stored = os.listdir(attribs["local_directory"])
for filename in updates(wanted, locally_stored, db_url):
print("Downloading {}…".format(filename))
pkgurl = os.path.join(pkg_url, filename)
urllib.request.urlretrieve(pkgurl, os.devnull)
print("Download completed.")
if __name__ == "__main__":
main()