From dfa0fe3efbec32343bab9bac7de772e162bb06b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20Bal=C3=A1zs?= Date: Sun, 20 Dec 2020 00:44:34 +0100 Subject: [PATCH] Download frontend asset from github --- .github/workflows/releases.yml | 2 +- policy/frontend/.version | 2 +- policy/openbot/server/frontend.py | 33 ++++++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/.github/workflows/releases.yml b/.github/workflows/releases.yml index 82b0c874a..542ecb0c5 100644 --- a/.github/workflows/releases.yml +++ b/.github/workflows/releases.yml @@ -54,7 +54,7 @@ jobs: node-version: '12' - run: yarn install - run: yarn build - - run: cd build && tar -zcf ../build.zip * + - run: zip -qq -r build.zip build - name: Upload Release Asset id: upload-release-asset uses: actions/upload-release-asset@v1 diff --git a/policy/frontend/.version b/policy/frontend/.version index f6cc860a3..8c43fb439 100644 --- a/policy/frontend/.version +++ b/policy/frontend/.version @@ -1 +1 @@ -20201219.0 +v0.1.4 diff --git a/policy/openbot/server/frontend.py b/policy/openbot/server/frontend.py index f6800eb9b..00fcfaa62 100644 --- a/policy/openbot/server/frontend.py +++ b/policy/openbot/server/frontend.py @@ -1,5 +1,7 @@ import os from subprocess import Popen +import urllib.request +import zipfile from aiohttp import web @@ -11,11 +13,24 @@ async def init_frontend(app: web.Application): await run_frontend_dev_server(app) return - # todo: - # get version from file - # download from github if needed - # unzip - pass + frontend_dir = os.path.join(base_dir, "frontend") + zip_path = os.path.join(base_dir, "frontend.zip") + version_target = read_version(frontend_dir, ".version") + version_current = read_version(frontend_dir, "build", ".version") + if version_current == version_target: + print("Frontend is up to date!") + return + + # todo fix URL + url = f"https://github.com/sanyatuning/OpenBot/releases/download/{version_target}/frontend.zip" + print("Downloading frontend...") + urllib.request.urlretrieve(url, zip_path) + with zipfile.ZipFile(zip_path, 'r') as zip_ref: + zip_ref.extractall(frontend_dir) + with open(os.path.join(frontend_dir, "build", ".version")) as f: + f.write(version_target) + os.unlink(zip_path) + print("Frontend is ready!") async def run_frontend_dev_server(app: web.Application): @@ -28,6 +43,14 @@ async def run_frontend_dev_server(app: web.Application): ) +def read_version(*args): + try: + with open(os.path.join(*args)) as f: + return f.read().strip() + except FileNotFoundError: + return None + + def is_port_in_use(port): import socket with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: