-
Notifications
You must be signed in to change notification settings - Fork 7
/
update
executable file
·118 lines (91 loc) · 3.47 KB
/
update
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
#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p "python3.withPackages(ps: [ ps.beautifulsoup4 ps.requests ])"
from __future__ import annotations
import json
import re
import subprocess
from dataclasses import dataclass
import requests
from bs4 import BeautifulSoup
@dataclass(frozen=True)
class Item:
name: str
url: str
scrape: Callable[[BeatifulSoup], str]
download_url: Callable[[str, str], str]
PACKAGES_FILE = "./data/packages.json"
DEBIAN_ARCHS = {
"x86_64-linux": "amd64",
"i686-linux": "i386",
"aarch64-linux": "arm64",
"armv7l-linux": "armhf",
}
SYSTEMS = DEBIAN_ARCHS.keys()
def adept2_deb_download_url(system: str, version: str):
return f"https://files.digilent.com/Software/Adept2%20Runtime/{version}/digilent.adept.runtime_{version}-{DEBIAN_ARCHS[system]}.deb"
def waveforms_deb_download_url(system: str, version: str):
return f"https://files.digilent.com/Software/Waveforms2015/{version}/digilent.waveforms_{version}_{DEBIAN_ARCHS[system]}.deb"
def scrape_adept2_runtime(html):
runtime = html.find(string=re.compile("\s*Linux Runtime - Latest Downloads\s*"))
version = (
runtime.find_parent(class_="section")
.find(string=re.compile("\s*Latest Version\s*"))
.find_parent(class_="key-value")
.find(class_="value")
.text.strip()
.lstrip("v")
)
return "2.27.9" if version == "2.27.29" else version
def scrape_waveforms(html):
pattern = re.compile(r"\s*Latest Downloads \(([0-9.]+)\) .*")
if (version_text := html.find(string=pattern)) and (
m := pattern.match(version_text)
):
return m.group(1)
CONTROL = [
Item(
name="adept2-runtime",
url="https://reference.digilentinc.com/reference/software/adept/start",
scrape=scrape_adept2_runtime,
download_url=adept2_deb_download_url,
),
Item(
name="waveforms",
url="https://reference.digilentinc.com/reference/software/waveforms/waveforms-3/start",
scrape=scrape_waveforms,
download_url=waveforms_deb_download_url,
),
]
def fetch_html(url):
response = requests.get(url)
response.raise_for_status()
return BeautifulSoup(response.text, "html.parser")
if __name__ == "__main__":
with open(PACKAGES_FILE, "rb") as inp:
current_data = json.load(inp)
for item in CONTROL:
latest_version = item.scrape(fetch_html(item.url))
if not latest_version:
raise RuntimeError(f"Could not find version text from {item.url}")
if current_data[item.name]["version"] == latest_version:
print(f"{item.name} up to date at {latest_version}.")
else:
print(
f"Update {item.name} from {current_data[item.name]['version']} to {latest_version}"
)
for system in SYSTEMS:
url = item.download_url(system, latest_version)
proc = subprocess.run(
["nix", "--quiet", "store", "prefetch-file", "--json", url],
capture_output=True,
)
proc.check_returncode()
result = json.loads(proc.stdout)
current_data[item.name]["version"] = latest_version
current_data[item.name]["systems"][system]["url"] = url
current_data[item.name]["systems"][system]["hash"] = result["hash"]
with open(PACKAGES_FILE, "w") as out:
json.dump(current_data, out, indent=4)
# Local Variables:
# mode: python
# End: