Skip to content

Commit

Permalink
Memory improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Dehuysser committed Dec 28, 2020
1 parent b24de1f commit d1b49e0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
secrets.py
secrets.py
pymakr.conf
40 changes: 5 additions & 35 deletions app/httpclient.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import usocket, os, time

import usocket, os
class Response:

def __init__(self, socket, saveToFile=None):
self._socket = socket
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()

Expand All @@ -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:
Expand All @@ -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]))
Expand All @@ -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)
Expand Down Expand Up @@ -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)
26 changes: 9 additions & 17 deletions app/ota_updater.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import machine, usocket, os, gc
import os, gc
from .httpclient import HttpClient

class OTAUpdater:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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':
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit d1b49e0

Please sign in to comment.