diff --git a/.gitignore b/.gitignore index c6b6498..f2ecd5a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -secrets.py \ No newline at end of file +secrets.py +pymakr.conf \ No newline at end of file diff --git a/app/httpclient.py b/app/httpclient.py index 2525ba2..4b6e277 100644 --- a/app/httpclient.py +++ b/app/httpclient.py @@ -1,5 +1,4 @@ -import usocket, os, time - +import usocket, os class Response: def __init__(self, socket, saveToFile=None): @@ -7,19 +6,13 @@ def __init__(self, socket, saveToFile=None): self._saveToFile = saveToFile self._encoding = 'utf-8' if saveToFile is not None: - print('Saving to file', saveToFile) - time.sleep(1) CHUNK_SIZE = 512 # bytes with open(saveToFile, 'w') as outfile: - print('Opened file', saveToFile) - time.sleep(1) data = self._socket.read(CHUNK_SIZE) while data: outfile.write(data) data = self._socket.read(CHUNK_SIZE) outfile.close() - print('Data written to file', saveToFile) - time.sleep(1) self.close() @@ -33,26 +26,23 @@ def content(self): if self._saveToFile is not None: raise SystemError('You cannot get the content from the response as you decided to save it in {}'.format(self._saveToFile)) - result = None try: result = self._socket.read() + return result finally: self.close() - return result @property def text(self): return str(self.content, self._encoding) def json(self): - result = None try: import ujson result = ujson.load(self._socket) + return result finally: self.close() - return result - class HttpClient: @@ -61,8 +51,6 @@ def __init__(self, headers={}): self._headers = headers def request(self, method, url, data=None, json=None, file=None, custom=None, saveToFile=None, headers={}, stream=None): - print('Req', url) - def _write_headers(sock, _headers): for k in _headers: sock.write(b'{}: {}\r\n'.format(k, _headers[k])) @@ -84,34 +72,16 @@ def _write_headers(sock, _headers): host, port = host.split(':', 1) port = int(port) - print('Getting addr info') - time.sleep(1) ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM) if len(ai) < 1: raise ValueError('You are not connected to the internet...') ai = ai[0] - print('Opening socket', ai) - time.sleep(1) s = usocket.socket(ai[0], ai[1], ai[2]) - print('Socket open') - time.sleep(1) try: - print('Connecting to socket') - time.sleep(1) s.connect(ai[-1]) - print('Connected to socket') - time.sleep(1) if proto == 'https:': - try: - print('Wrapping socket with ssl') - time.sleep(1) - s = ussl.wrap_socket(s) - print('Wrapped socket with ssl') - time.sleep(1) - except: - print("An error occurred.") - + s = ussl.wrap_socket(s, server_hostname=host) s.write(b'%s /%s HTTP/1.0\r\n' % (method, path)) if not 'Host' in headers: s.write(b'Host: %s\r\n' % host) @@ -184,4 +154,4 @@ def patch(self, url, **kw): return self.request('PATCH', url, **kw) def delete(self, url, **kw): - return self.request('DELETE', url, **kw) + return self.request('DELETE', url, **kw) \ No newline at end of file diff --git a/app/ota_updater.py b/app/ota_updater.py index e364955..f642985 100644 --- a/app/ota_updater.py +++ b/app/ota_updater.py @@ -1,4 +1,4 @@ -import machine, usocket, os, gc +import os, gc from .httpclient import HttpClient class OTAUpdater: @@ -9,7 +9,7 @@ class OTAUpdater: def __init__(self, github_repo, github_src_dir='', module='', main_dir='main', new_version_dir='next', secrets_file=None, headers={}): self.http_client = HttpClient(headers=headers) - self.github_repo = github_repo.rstrip('/').replace('https://github.com', 'https://api.github.com/repos') + self.github_repo = github_repo.rstrip('/').replace('https://github.com/', '') self.github_src_dir = '' if len(github_src_dir) < 1 else github_src_dir.rstrip('/') + '/' self.module = module.rstrip('/') self.main_dir = main_dir @@ -122,7 +122,7 @@ def get_version(self, directory, version_file_name='.version'): return '0.0' def get_latest_version(self): - latest_release = self.http_client.get(self.github_repo + '/releases/latest') + latest_release = self.http_client.get('https://api.github.com/repos/{}/releases/latest'.format(self.github_repo)) version = latest_release.json()['tag_name'] latest_release.close() return version @@ -133,10 +133,9 @@ def _download_new_version(self, version): print('Version {} downloaded to {}'.format(version, self.modulepath(self.new_version_dir))) def _download_all_files(self, version, sub_dir=''): - root_url = self.github_repo + '/contents/' + self.github_src_dir + self.main_dir + sub_dir - print(root_url) - gc.collect() - file_list = self.http_client.get(root_url + '?ref=refs/tags/' + version) + url = 'https://api.github.com/repos/{}/contents{}{}{}?ref=refs/tags/{}'.format(self.github_repo, self.github_src_dir, self.main_dir, sub_dir, version) + gc.collect() + file_list = self.http_client.get(url) for file in file_list.json(): path = self.modulepath(self.new_version_dir + '/' + file['path'].replace(self.main_dir + '/', '').replace(self.github_src_dir, '')) if file['type'] == 'file': @@ -147,18 +146,12 @@ def _download_all_files(self, version, sub_dir=''): print('Creating dir', path) self.mkdir(path) self._download_all_files(version, sub_dir + '/' + file['name']) + gc.collect() file_list.close() def _download_file(self, version, gitPath, path): - repo_name = self.github_repo.replace('https://api.github.com/repos/', '') - try: - self.http_client.get('https://cdn.jsdelivr.net/gh/{}@{}/{}'.format(repo_name, version, gitPath), saveToFile=path) - except OSError as err: - gc.collect() - self.http_client.get('https://raw.githubusercontent.com/{}/{}/{}'.format(repo_name, version, gitPath), saveToFile=path) - pass - + self.http_client.get('https://raw.githubusercontent.com/{}/{}/{}'.format(self.github_repo, version, gitPath), saveToFile=path) def _copy_secrets_file(self): if self.secrets_file: @@ -195,8 +188,7 @@ def _os_supports_rename(self) -> bool: self._mk_dirs('otaUpdater/osRenameTest') os.rename('otaUpdater', 'otaUpdated') result = len(os.listdir('otaUpdated')) > 0 - os.rmdir('otaUpdated/osRenameTest') - os.rmdir('otaUpdated') + self._rmtree('otaUpdated') return result def _copy_directory(self, fromPath, toPath):