-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgetghrelease.py
More file actions
135 lines (96 loc) · 3.92 KB
/
getghrelease.py
File metadata and controls
135 lines (96 loc) · 3.92 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
import requests
import subprocess
import os
import shutil
import datetime
import pathlib
import sys
import re
RELEASE_PER_PAGE = 30
TEMP_DIR = 'temp'
ZIP_NAME = "release.zip"
GEN1_DIR = "gen1"
GEN2_DIR = "gen2"
SUPER_BASIC_BINARIES = ['sb01.bin', 'sb02.bin', 'sb03.bin', 'sb04.bin']
TARGET_DIR = "shipping/firmware"
def get_std_headers(token = None):
headers = {
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28'
}
if token != None:
headers['Authorization'] = f'Bearer {token}'
return headers
def get_all_releases(repo_url, token = None):
res = []
re_exp = r'^.*<(https://api.github.com/.+)>;\s+rel="next".*$'
exp = re.compile(re_exp)
next_found = True
# Implement full pagination
url = f'{repo_url}/releases?per_page={RELEASE_PER_PAGE}'
while next_found:
response = requests.get(url, headers=get_std_headers(token))
response.raise_for_status()
json_data = response.json()
res = res + json_data
# There is no Link header if list fits on one page
if 'Link' in response.headers:
match = exp.search(response.headers['Link'])
next_found = match != None
if next_found:
url = match.group(1)
else:
next_found = False
return res
def get_latest_release(repo_url, file_extension, token = None):
all_releases = get_all_releases(repo_url, token)
latest = max(all_releases, key=lambda i: datetime.datetime.fromisoformat(i['published_at']))
return list(map(lambda j: j['browser_download_url'], filter(lambda i: i['browser_download_url'].endswith(file_extension), latest['assets'])))
def download_asset(release_url, file_name, token=None):
response = requests.get(release_url, headers=get_std_headers(token))
response.raise_for_status()
with open(file_name, 'wb') as f:
f.write(response.content)
def create_temp_dir(dir_name):
try:
shutil.rmtree(dir_name)
except:
pass
dir_path = pathlib.Path(dir_name)
dir_path.mkdir()
def unzip_release(release_file_name, temp_dir):
cmd = ["unzip", '-d', temp_dir, release_file_name]
subprocess.run(cmd, check=True)
subfolders = [ f.path for f in os.scandir(temp_dir) if f.is_dir() ]
if len(subfolders) != 1:
raise Exception("Not enough or too many subfolders in temp dir")
return pathlib.Path(subfolders[0])
def process_super_basic_release(latest_release_files, temp_dir):
# We assume that there is exactly one ZIP
if len(latest_release_files) != 1:
raise Exception("Not enough or too many zip files in release")
release_zip_url = latest_release_files[0]
# Donwload zip file
download_asset(release_zip_url, temp_dir / ZIP_NAME)
# unzip release data in temp directory and return name of subdir into which the binaries were
# unpacked
release_dir_name = unzip_release(temp_dir / ZIP_NAME, temp_dir)
# Copy Gen1 and Gen2 SuperBASIC binaries in to shipping/firmware/genx directory
copy_super_basic_binaries(release_dir_name, pathlib.Path(TARGET_DIR))
def copy_super_basic_binaries(src_dir, target_dir):
for i in SUPER_BASIC_BINARIES:
shutil.copy(src_dir / GEN1_DIR / i, target_dir / GEN1_DIR / i)
shutil.copy(src_dir / GEN2_DIR / i, target_dir / GEN2_DIR / i)
def main():
if len(sys.argv) < 4:
print("usage: python3 getghrelease.py <owner> <repo> <file_extension_to_get>")
repo_name = sys.argv[2]
# Clear and recreate temp dir
temp_dir = pathlib.Path(TEMP_DIR)
create_temp_dir(temp_dir)
# determine URLs of all files with selected postfix (e.g. zip) in latest release
latest_release_files = get_latest_release(f'https://api.github.com/repos/{sys.argv[1]}/{repo_name}', sys.argv[3])
if repo_name == "f256-superbasic":
process_super_basic_release(latest_release_files, temp_dir)
if __name__ == "__main__":
main()