1919sys .path .append ("adabot" )
2020import adabot .github_requests as github
2121
22- SUPPORTED_PORTS = ["atmel-samd" , "cxd56" , "esp32s2" , "litex" , "mimxrt10xx" , "nrf" , "stm" ]
23-
24- BIN = ('bin' ,)
25- UF2 = ('uf2' ,)
26- BIN_UF2 = ('bin' , 'uf2' )
27- HEX = ('hex' ,)
28- HEX_UF2 = ('hex' , 'uf2' )
29- SPK = ('spk' ,)
30- DFU = ('dfu' ,)
31- BIN_DFU = ('bin' , 'dfu' )
32-
33- # Example:
34- # https://downloads.circuitpython.org/bin/trinket_m0/en_US/adafruit-circuitpython-trinket_m0-en_US-5.0.0-rc.0.uf2
35- DOWNLOAD_BASE_URL = "https://downloads.circuitpython.org/bin"
22+ SUPPORTED_PORTS = [
23+ "atmel-samd" ,
24+ "cxd56" ,
25+ "esp32s2" ,
26+ "litex" ,
27+ "mimxrt10xx" ,
28+ "nrf" ,
29+ "stm" ,
30+ ]
31+
32+ BIN = ("bin" ,)
33+ UF2 = ("uf2" ,)
34+ BIN_UF2 = ("bin" , "uf2" )
35+ HEX = ("hex" ,)
36+ HEX_UF2 = ("hex" , "uf2" )
37+ SPK = ("spk" ,)
38+ DFU = ("dfu" ,)
39+ BIN_DFU = ("bin" , "dfu" )
3640
3741# Default extensions
3842extension_by_port = {
4246 "cxd56" : SPK ,
4347 "mimxrt10xx" : HEX_UF2 ,
4448 "litex" : DFU ,
45- "esp32s2" : BIN_UF2
49+ "esp32s2" : BIN_UF2 ,
4650}
4751
4852# Per board overrides
5761 "feather_m0_rfm69" : BIN_UF2 ,
5862 "feather_m0_rfm9x" : BIN_UF2 ,
5963 "uchip" : BIN_UF2 ,
60-
6164 # nRF52840 dev kits that may not have UF2 bootloaders,
6265 "makerdiary_nrf52840_mdk" : HEX ,
6366 "makerdiary_nrf52840_mdk_usb_dongle" : HEX_UF2 ,
6467 "pca10056" : BIN_UF2 ,
6568 "pca10059" : BIN_UF2 ,
6669 "electronut_labs_blip" : HEX ,
67-
6870 # stm32
69- "meowbit_v121" : UF2
71+ "meowbit_v121" : UF2 ,
7072}
7173
7274aliases_by_board = {
73- "circuitplayground_express" : ["circuitplayground_express_4h" , "circuitplayground_express_digikey_pycon2019" ],
75+ "circuitplayground_express" : [
76+ "circuitplayground_express_4h" ,
77+ "circuitplayground_express_digikey_pycon2019" ,
78+ ],
7479 "pybadge" : ["edgebadge" ],
7580 "pyportal" : ["pyportal_pynt" ],
7681 "gemma_m0" : ["gemma_m0_pycon2018" ],
77- "pewpew10" : ["pewpew13" ]
82+ "pewpew10" : ["pewpew13" ],
7883}
7984
85+
8086def get_languages ():
8187 languages = []
8288 for f in os .scandir ("../locale" ):
8389 if f .name .endswith (".po" ):
8490 languages .append (f .name [:- 3 ])
8591 return languages
8692
93+
8794def get_board_mapping ():
8895 boards = {}
8996 for port in SUPPORTED_PORTS :
@@ -95,23 +102,30 @@ def get_board_mapping():
95102 extensions = extension_by_port [port ]
96103 extensions = extension_by_board .get (board_path .name , extensions )
97104 aliases = aliases_by_board .get (board_path .name , [])
98- boards [board_id ] = {"port" : port ,
99- "extensions" : extensions ,
100- "download_count" : 0 ,
101- "aliases" : aliases }
105+ boards [board_id ] = {
106+ "port" : port ,
107+ "extensions" : extensions ,
108+ "download_count" : 0 ,
109+ "aliases" : aliases ,
110+ }
102111 for alias in aliases :
103- boards [alias ] = {"port" : port ,
104- "extensions" : extensions ,
105- "download_count" : 0 ,
106- "alias" : True ,
107- "aliases" : []}
112+ boards [alias ] = {
113+ "port" : port ,
114+ "extensions" : extensions ,
115+ "download_count" : 0 ,
116+ "alias" : True ,
117+ "aliases" : [],
118+ }
108119 return boards
109120
121+
110122def get_version_info ():
111123 version = None
112124 sha = git ("rev-parse" , "--short" , "HEAD" ).stdout .decode ("utf-8" )
113125 try :
114- version = git ("describe" , "--tags" , "--exact-match" ).stdout .decode ("utf-8" ).strip ()
126+ version = (
127+ git ("describe" , "--tags" , "--exact-match" ).stdout .decode ("utf-8" ).strip ()
128+ )
115129 except sh .ErrorReturnCode_128 :
116130 # No exact match
117131 pass
@@ -120,18 +134,21 @@ def get_version_info():
120134 sha = os .environ ["GITHUB_SHA" ]
121135
122136 if not version :
123- version = "{}-{}" .format (date .today ().strftime ("%Y%m%d" ), sha [:7 ])
137+ version = "{}-{}" .format (date .today ().strftime ("%Y%m%d" ), sha [:7 ])
124138
125139 return sha , version
126140
141+
127142def get_current_info ():
128143 response = github .get ("/repos/adafruit/circuitpython-org/git/refs/heads/master" )
129144 if not response .ok :
130145 print (response .text )
131146 raise RuntimeError ("cannot get master sha" )
132147 commit_sha = response .json ()["object" ]["sha" ]
133148
134- response = github .get ("/repos/adafruit/circuitpython-org/contents/_data/files.json?ref=" + commit_sha )
149+ response = github .get (
150+ "/repos/adafruit/circuitpython-org/contents/_data/files.json?ref=" + commit_sha
151+ )
135152 if not response .ok :
136153 print (response .text )
137154 raise RuntimeError ("cannot get previous files.json" )
@@ -145,6 +162,7 @@ def get_current_info():
145162 current_info [info ["id" ]] = info
146163 return git_info , current_info
147164
165+
148166def create_pr (changes , updated , git_info , user ):
149167 commit_sha , original_blob_sha = git_info
150168 branch_name = "new_release_" + changes ["new_release" ]
@@ -158,7 +176,7 @@ def create_pr(changes, updated, git_info, user):
158176 updated_list .append (info )
159177
160178 updated = json .dumps (updated_list , sort_keys = True , indent = 1 ).encode ("utf-8" ) + b"\n "
161- #print(updated.decode("utf-8"))
179+ # print(updated.decode("utf-8"))
162180 pr_title = "Automated website update for release {}" .format (changes ["new_release" ])
163181 boards = ""
164182 if changes ["new_boards" ]:
@@ -167,16 +185,13 @@ def create_pr(changes, updated, git_info, user):
167185 if changes ["new_languages" ]:
168186 languages = "New languages:\n * " + "\n * " .join (changes ["new_languages" ])
169187 message = "Automated website update for release {} by Blinka.\n \n {}\n \n {}\n " .format (
170- changes ["new_release" ],
171- boards ,
172- languages
188+ changes ["new_release" ], boards , languages
173189 )
174190
175- create_branch = {
176- "ref" : "refs/heads/" + branch_name ,
177- "sha" : commit_sha
178- }
179- response = github .post ("/repos/{}/circuitpython-org/git/refs" .format (user ), json = create_branch )
191+ create_branch = {"ref" : "refs/heads/" + branch_name , "sha" : commit_sha }
192+ response = github .post (
193+ "/repos/{}/circuitpython-org/git/refs" .format (user ), json = create_branch
194+ )
180195 if not response .ok and response .json ()["message" ] != "Reference already exists" :
181196 print ("unable to create branch" )
182197 print (response .text )
@@ -186,10 +201,13 @@ def create_pr(changes, updated, git_info, user):
186201 "message" : message ,
187202 "content" : base64 .b64encode (updated ).decode ("utf-8" ),
188203 "sha" : original_blob_sha ,
189- "branch" : branch_name
204+ "branch" : branch_name ,
190205 }
191206
192- response = github .put ("/repos/{}/circuitpython-org/contents/_data/files.json" .format (user ), json = update_file )
207+ response = github .put (
208+ "/repos/{}/circuitpython-org/contents/_data/files.json" .format (user ),
209+ json = update_file ,
210+ )
193211 if not response .ok :
194212 print ("unable to post new file" )
195213 print (response .text )
@@ -199,7 +217,7 @@ def create_pr(changes, updated, git_info, user):
199217 "head" : user + ":" + branch_name ,
200218 "base" : "master" ,
201219 "body" : message ,
202- "maintainer_can_modify" : True
220+ "maintainer_can_modify" : True ,
203221 }
204222 response = github .post ("/repos/adafruit/circuitpython-org/pulls" , json = pr_info )
205223 if not response .ok :
@@ -220,17 +238,14 @@ def print_active_user():
220238 print ("Not logged in" )
221239 return None
222240
241+
223242def generate_download_info ():
224243 boards = {}
225244 errors = []
226245
227246 new_tag = os .environ ["RELEASE_TAG" ]
228247
229- changes = {
230- "new_release" : new_tag ,
231- "new_boards" : [],
232- "new_languages" : []
233- }
248+ changes = {"new_release" : new_tag , "new_boards" : [], "new_languages" : []}
234249
235250 user = print_active_user ()
236251
@@ -254,8 +269,9 @@ def generate_download_info():
254269 info = current_info [board ]
255270 for version in info ["versions" ]:
256271 previous_releases .add (version ["version" ])
257- previous_languages .update (version ["files" ].keys ())
258- if version ["stable" ] == new_stable :
272+ if version ["stable" ] == new_stable or (
273+ new_stable and version ["version" ].startswith (this_version )
274+ ):
259275 info ["versions" ].remove (version )
260276
261277 board_mapping = get_board_mapping ()
@@ -272,29 +288,15 @@ def generate_download_info():
272288 alias_info = board_mapping [alias ]
273289 if alias not in current_info :
274290 changes ["new_boards" ].append (alias )
275- current_info [alias ] = {"downloads" : 0 ,
276- "versions" : []}
291+ current_info [alias ] = {"downloads" : 0 , "versions" : []}
277292
278293 new_version = {
279294 "stable" : new_stable ,
280295 "version" : new_tag ,
281296 "modules" : support_matrix .get (alias , "[]" ),
282- "files" : {},
283297 "languages" : languages ,
284- "extensions" : board_info ["extensions" ]
298+ "extensions" : board_info ["extensions" ],
285299 }
286- for language in languages :
287- files = []
288- new_version ["files" ][language ] = files
289- for extension in board_info ["extensions" ]:
290- files .append (
291- "{base_url}/{alias}/{language}/adafruit-circuitpython-{alias}-{language}-{tag}.{extension}"
292- .format (
293- base_url = DOWNLOAD_BASE_URL ,
294- tag = new_tag ,
295- alias = alias ,
296- language = language ,
297- extension = extension ))
298300 current_info [alias ]["downloads" ] = alias_info ["download_count" ]
299301 current_info [alias ]["versions" ].append (new_version )
300302
@@ -305,6 +307,7 @@ def generate_download_info():
305307 else :
306308 print ("No new release to update" )
307309
310+
308311if __name__ == "__main__" :
309312 if "RELEASE_TAG" in os .environ and os .environ ["RELEASE_TAG" ]:
310313 generate_download_info ()
0 commit comments