-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
70 lines (56 loc) · 1.94 KB
/
generate.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
import sys
import requests
import hashlib
DOWNLOAD_URL = "https://github.com/zineland/zine/releases/download/v{version}/{artifact}"
TAP_FORMULA_TEMPATE = """class Zine < Formula
desc "Simple and opinionated tool to build your own magazine"
homepage "https://github.com/zineland/zine"
version "{version}"
if OS.mac? && Hardware::CPU.intel?
url "https://github.com/zineland/zine/releases/download/v{version}/zine-x86_64-apple-darwin.tar.gz"
sha256 "{mac_sha256}"
end
if OS.linux? && Hardware::CPU.intel?
url "https://github.com/zineland/zine/releases/download/v{version}/zine-x86_64-unknown-linux-gnu.tar.gz"
sha256 "{linux_sha256}"
end
def install
bin.install "zine"
end
test do
system "#{{bin}}/zine"
end
end
"""
def download_and_check_sha256(url: str) -> str:
r = requests.get(url)
if r.status_code == 200:
sha256 = hashlib.sha256(r.content).hexdigest()
return sha256
else:
print(f"download faild: {r.text}")
raise Exception("Download failed: ", url)
def get_download_urls(version: str) -> dict:
return {
"mac": DOWNLOAD_URL.format(
version=version, artifact="zine-x86_64-apple-darwin.tar.gz"),
"linux": DOWNLOAD_URL.format(
version=version, artifact="zine-x86_64-unknown-linux-gnu.tar.gz"),
}
if __name__ == "__main__":
if len(sys.argv) < 2:
print("version argument required")
sys.exit()
print("Start generate latest formula file...")
version = sys.argv[1].strip('v')
print('version: ', version)
shasums = {}
for os, url in get_download_urls(version).items():
sha256 = download_and_check_sha256(url)
shasums[os] = sha256
formula = TAP_FORMULA_TEMPATE.format(
version=version, mac_sha256=shasums["mac"], linux_sha256=shasums["linux"])
print(formula)
with open("Formula/zine.rb", "w") as file:
file.write(formula)
print("Generate success!")