From aa006b66d187311b22cf83e8900408368e5a85c7 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 19 Jan 2023 10:15:56 +0200 Subject: [PATCH] feat: update pre-installed extensions on re-create --- lnbits/app.py | 12 +++++------- lnbits/core/views/api.py | 1 - lnbits/extension_manger.py | 22 ++++++++++++++++++++-- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lnbits/app.py b/lnbits/app.py index 598f3258a..56614b186 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -7,9 +7,7 @@ import shutil import signal import sys import traceback -import zipfile from http import HTTPStatus -from pathlib import Path from typing import Callable from fastapi import FastAPI, Request @@ -146,12 +144,12 @@ async def check_installed_extensions(app: FastAPI): def check_installed_extension(ext: InstallableExtension) -> bool: - extensions_data_dir = os.path.join(settings.lnbits_data_folder, "extensions") - extensions_dir = os.path.join("lnbits", "extensions") - zip_files = glob.glob(f"{extensions_data_dir}/*.zip") + zip_files = glob.glob( + os.path.join(settings.lnbits_data_folder, "extensions", "*.zip") + ) - if Path(os.path.join(extensions_dir, ext.id)).is_dir(): - return True # todo: pre-installed that require upgrade + if ext.has_installed_version: + return True if ext.zip_path in zip_files: ext.extract_archive() else: diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index 3e12e809e..a68d8580a 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -5,7 +5,6 @@ import time import uuid from http import HTTPStatus from io import BytesIO -from pathlib import Path from typing import Dict, List, Optional, Tuple, Union from urllib.parse import ParseResult, parse_qs, urlencode, urlparse, urlunparse diff --git a/lnbits/extension_manger.py b/lnbits/extension_manger.py index e0c8cf725..4ac4705d3 100644 --- a/lnbits/extension_manger.py +++ b/lnbits/extension_manger.py @@ -6,6 +6,7 @@ import sys import urllib.request import zipfile from http import HTTPStatus +from pathlib import Path from typing import Any, List, NamedTuple, Optional import httpx @@ -108,7 +109,6 @@ class ExtensionRelease(BaseModel): archive: str source_repo: str hash: Optional[str] - published_at: Optional[str] html_url: Optional[str] description: Optional[str] details_html: Optional[str] = None @@ -122,7 +122,6 @@ class ExtensionRelease(BaseModel): archive=r["zipball_url"], source_repo=source_repo, # description=r["body"], # bad for JSON - published_at=r["published_at"], html_url=r["html_url"], ) @@ -184,6 +183,14 @@ class InstallableExtension(BaseModel): def module_installed(self) -> bool: return self.module_name in sys.modules + @property + def has_installed_version(self) -> bool: + if not Path(self.ext_dir).is_dir(): + return False + with open(os.path.join(self.ext_dir, "config.json"), "r") as json_file: + config_json = json.load(json_file) + return config_json.get("is_installed") == True + def download_archive(self): ext_zip_file = self.zip_path if os.path.isfile(ext_zip_file): @@ -218,6 +225,17 @@ class InstallableExtension(BaseModel): os.path.join(self.ext_upgrade_dir, self.id), ) + # Pre-packed extensions can be upgraded + # Mark the extension as installed so we know it is not the pre-packed version + with open( + os.path.join(self.ext_upgrade_dir, self.id, "config.json"), "r+" + ) as json_file: + config_json = json.load(json_file) + config_json["is_installed"] = True + json_file.seek(0) + json.dump(config_json, json_file) + json_file.truncate() + shutil.rmtree(self.ext_dir, True) shutil.copytree( os.path.join(self.ext_upgrade_dir, self.id),