forked from frank-w/BPI-Router-Images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownloadfiles.py
143 lines (125 loc) · 4.37 KB
/
downloadfiles.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
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
135
136
137
138
139
140
141
142
143
import requests
from urllib.parse import urlparse
import os
import sys, json
import re
board=None
kernel=None
argc=len(sys.argv)-1
if argc>0:
board=sys.argv[1]
if argc>1:
kernel=sys.argv[2]
print(f"board : {board}")
print(f"kernel : {kernel}")
def read_settings(infile):
config={}
with open(infile) as f:
for line in f:
# ignore comments
if line[0] == '#':
continue
key, value = line.split("=", 1)
key = key.lower()
config[key]=value.replace("\n", "")
return config
config=None
conffile='sourcefiles_'+board+'.conf'
if os.path.isfile(conffile):
config=read_settings(conffile)
print(config)
def download(url, file_name=None):
# get request
response = requests.get(url)
if file_name:
# open in binary mode
# write to file
with open(file_name, "wb") as file:
file.write(response.content)
else:
return response.content
uboot_release_url="https://api.github.com/repos/frank-w/u-boot/releases/latest"
kernel_releases_url="https://api.github.com/repos/frank-w/BPI-Router-Linux/releases"
uboot_data=download(uboot_release_url)
uj=json.loads(uboot_data)
if uj:
ubootfiles={}
uname=uj.get("name")
#print("name:",uname)
ufiles=uj.get("assets")
#print("files:",json.dumps(ufiles,indent=2))
for uf in ufiles:
ufname=uf.get("name")
if ufname.endswith("img.gz"):
ufurl=uf.get("browser_download_url")
board_=re.sub('^(bpi-r[2346pro]+).*$',r'\1',ufname)
#print(board,ufurl)
ubootfiles[board_]=ufurl
#print("file:",json.dumps(uf,indent=2))
#print("files:",json.dumps(ubootfiles,indent=2))
kernel_releases=download(kernel_releases_url)
krj=json.loads(kernel_releases)
if krj:
kfiles={}
for rel in krj:
kname=rel.get("name")
if re.search('CI-BUILD-.*-main',kname):
branch=re.sub('^CI-BUILD-([56]\.[0-9]+-main).*$',r'\1',kname)
#print("branch:",branch)
rel["body"]=""
if branch=='5.15-main' or branch=='6.1-main': #catch 5.15 for r2 for internal wifi-support
#print("kernel-release",kname)
if not branch in kfiles:
rdata={}
for kf in rel.get("assets"):
kfname=kf.get("name")
if re.search("^bpi-r.*\.tar.gz$",kfname):
board_=re.sub('^(bpi-r[2346pro]+).*$',r'\1',kfname)
#if not board in rdata:
# rdata[board]={}
#rdata[board][kfname]=kf.get("browser_download_url")
rdata[board_]=kf.get("browser_download_url")
kfiles[branch]=rdata
#print("release-data:",json.dumps(rel,indent=2))
#print("files:",json.dumps(kfiles,indent=2))
ufile=None
kfile=None
if board and board in ubootfiles:
ufile=ubootfiles[board]
print(f"board:{board} ubootfile: {ufile}")
if kernel:
if kernel+"-main" in kfiles:
if board in kfiles[kernel+"-main"]:
kfile=kfiles[kernel+"-main"][board]
print(f"board:{board} kernelfile: {kfile}")
else: print(f"board not in kfiles[kernel]")
else: print(f"kernel not in kfiles")
else: print(f"kernel not set!")
else: print(f"{board} not found in ubootfiles")
if ufile:
a = urlparse(ufile)
fname=os.path.basename(a.path)
print(f"ubootfile: {ufile} filename: {fname}")
if os.path.isfile(fname):
print(fname,"already exists")
c=input('overwrite it [yn]? ').lower()
else: c='y'
if c=='y':
download(ufile,fname)
with open(conffile, 'w') as f:
f.write("imgfile="+fname+'\n')
else: print("no uboot image defined!")
if config and config.get("skipkerneldownload"):
with open(conffile, 'a') as f:
f.write("skipkerneldownload="+config.get("skipkerneldownload")+'\n')
f.write("kernelfile="+config.get("kernelfile")+'\n')
elif kfile:
a = urlparse(kfile)
fname=os.path.basename(a.path)
print(f"kernelfile: {kfile} filename: {fname}")
if not os.path.isfile(fname):
download(kfile,fname)
else: print(fname,"already exists")
with open(conffile, 'a') as f:
f.write("kernelfile="+fname+'\n')
else: print("no kernel defined!")