Bootstrap 'app'

philcrump-phil-add-ntp
Marek Ventur 2018-08-23 23:07:25 +01:00
parent 68e0c97cbf
commit 5b8189a015
5 changed files with 53 additions and 24 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -6,7 +6,7 @@ To publish apps use https://badge.emfcamp.org"""
___license___ = "MIT"
___title___ = "Badge Store"
___dependencies___ = ["app", "badge_store", "dialogs", "ugfx_helper"]
___dependencies___ = ["app", "badge_store", "dialogs", "ugfx_helper", "app"]
___categories___ = ["System"]
___bootstrapped___ = True
@ -56,7 +56,7 @@ def show_app(a):
if install:
with WaitingMessage(title="Installing %s" % a, text="Please wait...") as message:
installers = store.install(a)
installers = store.install(_get_current_apps + [a])
n = len(installers)
for i, installer in enumerate(installers):
message.text = "%s (%s/%s)" % (installer.path, i + 1, n)
@ -89,6 +89,9 @@ def main_menu():
else:
app.restart_to_default()
def _get_current_apps():
return [a.name for a in app.get_apps()]
wifi.connect(show_wait_message=True)
main_menu()
#show_app("launcher")

26
bootstrap/main.py Normal file
View File

@ -0,0 +1,26 @@
"""App that gets backed into the firmware.
It's only purpose is to download the base operating system on first boot.
It's not meant to be executed from the launcher"""
___license___ = "MIT"
___title___ = "Bootstrap"
___categories___ = ["System"]
___dependencies___ = ["badge_store", "dialogs"]
___launchable___ = False
___builtin___ = True
import ugfx, wifi, badge_store, machine, dialogs
ugfx.init()
machine.Pin(machine.Pin.PWM_LCD_BLIGHT).on()
wifi.connect(show_wait_message=True)
with dialogs.WaitingMessage(title="Setting up TiLDA Mk4", text="Please wait...") as message:
installers = badge_store.BadgeStore().bootstrap()
n = len(installers)
for i, installer in enumerate(installers):
message.text = "%s (%s/%s)" % (installer.path, i + 1, n)
installer.download()
machine.reset()

View File

@ -1,11 +1,11 @@
"""Library to interact with the badge store"""
___license___ = "MIT"
___dependencies___ = ["http", "ospath", "app"]
___dependencies___ = ["http", "ospath"]
from ospath import *
from http import *
import hashlib, binascii, app
import hashlib, binascii
class BadgeStore:
def __init__(self, url = "http://badge.marekventur.com", repo="emfcamp/Mk4-Apps", ref="master"):
@ -28,8 +28,19 @@ class BadgeStore:
def get_app(self, app):
return self._call("app", {"app": app})
def call_install(self, apps):
files = self._call("install", {"apps": ",".join(apps)})
def install(self, apps):
return self._create_installers(self._call("install", {"apps": ",".join(apps)}))
def bootstrap(self):
return self._create_installers(self._call("bootstrap"))
def _call(self, command, params = {}):
params["repo"] = self.repo
params["ref"] = self.ref
with get("%s/%s" % (self.url, command), params=params).raise_for_status() as response:
return response.json() # todo: error handling
def _create_installers(self, files):
installers = []
url = "%s/download" % (self.url)
for path, hash in files.items():
@ -39,24 +50,9 @@ class BadgeStore:
installers.append(Installer(path, url, params, hash))
return installers
def install(self, app):
return self.call_install(self._get_current_apps() + [app])
def update(self):
return self.call_install(self._get_current_apps())
def _call(self, command, params = {}):
params["repo"] = self.repo
params["ref"] = self.ref
with get("%s/%s" % (self.url, command), params=params).raise_for_status() as response:
return response.json() # todo: error handling
def _is_file_up_to_date(self, path, hash):
return hash == _get_hash(path)
def _get_current_apps(self):
return [a.name for a in app.get_apps()]
TEMP_FILE = ".tmp.download"
class Installer:
@ -70,7 +66,6 @@ class Installer:
count = 0
while get_hash(TEMP_FILE) != self.hash:
count += 1
print(count)
if count > 5:
try:
os.remove(TEMP_FILE)
@ -78,7 +73,6 @@ class Installer:
pass
raise OSError("Aborting download of %s after 5 unsuccessful attempts" % self.path)
try:
print("download ", self.url, self.params)
get(self.url, params=self.params).raise_for_status().download_to(TEMP_FILE)
except OSError as e:
if "404" in str(e):

View File

@ -39,13 +39,19 @@ class TestBadgeStore(unittest.TestCase):
def test_install_integration(self):
self._remove_download_file()
store = BadgeStore(url="http://badge.marekventur.com", repo="emfcamp/Mk4-Apps", ref="dont-delete-test-download-branch")
for installer in store.call_install(["launcher"]):
for installer in store.install(["launcher"]):
if installer.path == "shared/test/download.txt":
installer.download()
with open(self.download_file, "rt") as response:
self.assertIn("I'm a download test", response.read())
def test_bootstrap_integration(self):
self._remove_download_file()
store = BadgeStore(url="http://badge.marekventur.com")
installers = store.bootstrap()
self.assertTrue(len(installers) > 0)
def _remove_download_file(self):
if isdir(self.download_file) or isfile(self.download_file):
os.remove(self.download_file)