1- import machine , usocket , os , gc
1+ import os , gc
22from .httpclient import HttpClient
33
44class OTAUpdater :
@@ -9,7 +9,7 @@ class OTAUpdater:
99
1010 def __init__ (self , github_repo , github_src_dir = '' , module = '' , main_dir = 'main' , new_version_dir = 'next' , secrets_file = None , headers = {}):
1111 self .http_client = HttpClient (headers = headers )
12- self .github_repo = github_repo .rstrip ('/' ).replace ('https://github.com' , 'https://api.github.com/repos ' )
12+ self .github_repo = github_repo .rstrip ('/' ).replace ('https://github.com/ ' , '' )
1313 self .github_src_dir = '' if len (github_src_dir ) < 1 else github_src_dir .rstrip ('/' ) + '/'
1414 self .module = module .rstrip ('/' )
1515 self .main_dir = main_dir
@@ -122,7 +122,7 @@ def get_version(self, directory, version_file_name='.version'):
122122 return '0.0'
123123
124124 def get_latest_version (self ):
125- latest_release = self .http_client .get (self . github_repo + '/ releases/latest' )
125+ latest_release = self .http_client .get ('https://api.github.com/repos/{}/ releases/latest'. format ( self . github_repo ) )
126126 version = latest_release .json ()['tag_name' ]
127127 latest_release .close ()
128128 return version
@@ -133,10 +133,9 @@ def _download_new_version(self, version):
133133 print ('Version {} downloaded to {}' .format (version , self .modulepath (self .new_version_dir )))
134134
135135 def _download_all_files (self , version , sub_dir = '' ):
136- root_url = self .github_repo + '/contents/' + self .github_src_dir + self .main_dir + sub_dir
137- print (root_url )
138- gc .collect ()
139- file_list = self .http_client .get (root_url + '?ref=refs/tags/' + version )
136+ url = 'https://api.github.com/repos/{}/contents{}{}{}?ref=refs/tags/{}' .format (self .github_repo , self .github_src_dir , self .main_dir , sub_dir , version )
137+ gc .collect ()
138+ file_list = self .http_client .get (url )
140139 for file in file_list .json ():
141140 path = self .modulepath (self .new_version_dir + '/' + file ['path' ].replace (self .main_dir + '/' , '' ).replace (self .github_src_dir , '' ))
142141 if file ['type' ] == 'file' :
@@ -147,18 +146,12 @@ def _download_all_files(self, version, sub_dir=''):
147146 print ('Creating dir' , path )
148147 self .mkdir (path )
149148 self ._download_all_files (version , sub_dir + '/' + file ['name' ])
149+ gc .collect ()
150150
151151 file_list .close ()
152152
153153 def _download_file (self , version , gitPath , path ):
154- repo_name = self .github_repo .replace ('https://api.github.com/repos/' , '' )
155- try :
156- self .http_client .get ('https://cdn.jsdelivr.net/gh/{}@{}/{}' .format (repo_name , version , gitPath ), saveToFile = path )
157- except OSError as err :
158- gc .collect ()
159- self .http_client .get ('https://raw.githubusercontent.com/{}/{}/{}' .format (repo_name , version , gitPath ), saveToFile = path )
160- pass
161-
154+ self .http_client .get ('https://raw.githubusercontent.com/{}/{}/{}' .format (self .github_repo , version , gitPath ), saveToFile = path )
162155
163156 def _copy_secrets_file (self ):
164157 if self .secrets_file :
@@ -195,8 +188,7 @@ def _os_supports_rename(self) -> bool:
195188 self ._mk_dirs ('otaUpdater/osRenameTest' )
196189 os .rename ('otaUpdater' , 'otaUpdated' )
197190 result = len (os .listdir ('otaUpdated' )) > 0
198- os .rmdir ('otaUpdated/osRenameTest' )
199- os .rmdir ('otaUpdated' )
191+ self ._rmtree ('otaUpdated' )
200192 return result
201193
202194 def _copy_directory (self , fromPath , toPath ):
0 commit comments