mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-29 13:22:37 +02:00
feat: update pre-installed extensions on re-create
This commit is contained in:
@@ -7,9 +7,7 @@ import shutil
|
|||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
import zipfile
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from pathlib import Path
|
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
from fastapi import FastAPI, Request
|
from fastapi import FastAPI, Request
|
||||||
@@ -146,12 +144,12 @@ async def check_installed_extensions(app: FastAPI):
|
|||||||
|
|
||||||
|
|
||||||
def check_installed_extension(ext: InstallableExtension) -> bool:
|
def check_installed_extension(ext: InstallableExtension) -> bool:
|
||||||
extensions_data_dir = os.path.join(settings.lnbits_data_folder, "extensions")
|
zip_files = glob.glob(
|
||||||
extensions_dir = os.path.join("lnbits", "extensions")
|
os.path.join(settings.lnbits_data_folder, "extensions", "*.zip")
|
||||||
zip_files = glob.glob(f"{extensions_data_dir}/*.zip")
|
)
|
||||||
|
|
||||||
if Path(os.path.join(extensions_dir, ext.id)).is_dir():
|
if ext.has_installed_version:
|
||||||
return True # todo: pre-installed that require upgrade
|
return True
|
||||||
if ext.zip_path in zip_files:
|
if ext.zip_path in zip_files:
|
||||||
ext.extract_archive()
|
ext.extract_archive()
|
||||||
else:
|
else:
|
||||||
|
@@ -5,7 +5,6 @@ import time
|
|||||||
import uuid
|
import uuid
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pathlib import Path
|
|
||||||
from typing import Dict, List, Optional, Tuple, Union
|
from typing import Dict, List, Optional, Tuple, Union
|
||||||
from urllib.parse import ParseResult, parse_qs, urlencode, urlparse, urlunparse
|
from urllib.parse import ParseResult, parse_qs, urlencode, urlparse, urlunparse
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@ import sys
|
|||||||
import urllib.request
|
import urllib.request
|
||||||
import zipfile
|
import zipfile
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
from pathlib import Path
|
||||||
from typing import Any, List, NamedTuple, Optional
|
from typing import Any, List, NamedTuple, Optional
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
@@ -108,7 +109,6 @@ class ExtensionRelease(BaseModel):
|
|||||||
archive: str
|
archive: str
|
||||||
source_repo: str
|
source_repo: str
|
||||||
hash: Optional[str]
|
hash: Optional[str]
|
||||||
published_at: Optional[str]
|
|
||||||
html_url: Optional[str]
|
html_url: Optional[str]
|
||||||
description: Optional[str]
|
description: Optional[str]
|
||||||
details_html: Optional[str] = None
|
details_html: Optional[str] = None
|
||||||
@@ -122,7 +122,6 @@ class ExtensionRelease(BaseModel):
|
|||||||
archive=r["zipball_url"],
|
archive=r["zipball_url"],
|
||||||
source_repo=source_repo,
|
source_repo=source_repo,
|
||||||
# description=r["body"], # bad for JSON
|
# description=r["body"], # bad for JSON
|
||||||
published_at=r["published_at"],
|
|
||||||
html_url=r["html_url"],
|
html_url=r["html_url"],
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -184,6 +183,14 @@ class InstallableExtension(BaseModel):
|
|||||||
def module_installed(self) -> bool:
|
def module_installed(self) -> bool:
|
||||||
return self.module_name in sys.modules
|
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):
|
def download_archive(self):
|
||||||
ext_zip_file = self.zip_path
|
ext_zip_file = self.zip_path
|
||||||
if os.path.isfile(ext_zip_file):
|
if os.path.isfile(ext_zip_file):
|
||||||
@@ -218,6 +225,17 @@ class InstallableExtension(BaseModel):
|
|||||||
os.path.join(self.ext_upgrade_dir, self.id),
|
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.rmtree(self.ext_dir, True)
|
||||||
shutil.copytree(
|
shutil.copytree(
|
||||||
os.path.join(self.ext_upgrade_dir, self.id),
|
os.path.join(self.ext_upgrade_dir, self.id),
|
||||||
|
Reference in New Issue
Block a user