Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/self upgrade check #153

Merged
merged 5 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,4 @@

- added guard on determining host IP address in `mmpm.constants.paths`
- added import guard for Python3.8 when using `importlib.resources` in `mmpm.ui`
- added check for MMPM being a docker image when self upgrading
5 changes: 4 additions & 1 deletion mmpm/api/endpoints/ep_mmpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import mmpm.utils
from mmpm.api.constants import http
from mmpm.api.endpoints.endpoint import Endpoint
from mmpm.env import MMPMEnv
from mmpm.log.factory import MMPMLogFactory

logger = MMPMLogFactory.get_logger(__name__)
Expand All @@ -18,7 +19,7 @@ class Mmpm(Endpoint):
def __init__(self):
self.name = "mmpm"
self.blueprint = Blueprint(self.name, __name__, url_prefix=f"/api/{self.name}")
self.handler = None
self.env = MMPMEnv()

@self.blueprint.route("/version", methods=[http.GET])
def version() -> Response:
Expand All @@ -44,6 +45,8 @@ def upgrade() -> Response:
Returns:
Response: A Flask Response object indicating success or failure of the upgrade operation.
"""
if self.env.MMPM_IS_DOCKER_IMAGE:
self.failure("MMPM is a Docker image. MMPM must be upgraded using the Docker CLI.")
if mmpm.utils.upgrade():
return self.success("Upgrade MMPM")

Expand Down
6 changes: 6 additions & 0 deletions mmpm/subcommands/_sub_cmd_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from time import sleep

from mmpm.constants import urls
from mmpm.env import MMPMEnv
from mmpm.log.factory import MMPMLogFactory
from mmpm.magicmirror.database import MagicMirrorDatabase
from mmpm.subcommands.sub_cmd import SubCmd
Expand All @@ -31,6 +32,7 @@ def __init__(self, app_name):
self.usage = f"{self.app_name} {self.name} [--url] [--status] <install/remove>"
self.database = MagicMirrorDatabase()
self.ui = MMPMui()
self.env = MMPMEnv()

def register(self, subparser):
self.parser = subparser.add_parser(self.name, usage=self.usage, help=self.help)
Expand Down Expand Up @@ -112,6 +114,10 @@ def exec(self, args, extra):
if not self.database.is_initialized():
self.database.load()

if self.env.MMPM_IS_DOCKER_IMAGE:
logger.error("MMPM is a Docker image. Unable to interact with MMPM UI from CLI.")
return

if extra:
logger.error(f"Extra arguments are not accepted. See '{self.app_name} {self.name} --help'")
return
Expand Down
8 changes: 7 additions & 1 deletion mmpm/subcommands/_sub_cmd_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import json
from typing import List

from mmpm import utils
from mmpm.constants import paths
from mmpm.env import MMPMEnv
from mmpm.log.factory import MMPMLogFactory
from mmpm.magicmirror.database import MagicMirrorDatabase
from mmpm.magicmirror.magicmirror import MagicMirror
Expand All @@ -29,6 +31,7 @@ def __init__(self, app_name):
self.usage = f"{self.app_name} {self.name} <package(s)> [--yes]"
self.database = MagicMirrorDatabase()
self.magicmirror = MagicMirror()
self.env = MMPMEnv()

def register(self, subparser):
self.parser = subparser.add_parser(self.name, usage=self.usage, help=self.help)
Expand Down Expand Up @@ -76,7 +79,10 @@ def exec(self, args, extra):
upgradable["MagicMirror"] = upgradable["MagicMirror"] and self.magicmirror.upgrade()

if upgradable["mmpm"]:
print("Run 'pip install --upgrade --no-cache-dir mmpm' to install the latest version of MMPM. Run 'mmpm update' after upgrading.")
if self.env.MMPM_IS_DOCKER_IMAGE:
logger.warning("Cannot perform self-upgrade because MMPM is a Docker image. Stop MMPM and run `docker pull karsten13/mmpm:latest`")
else:
upgradable["mmpm"] = utils.upgrade()

with open(paths.MMPM_AVAILABLE_UPGRADES_FILE, mode="w", encoding="utf-8") as upgrade_file:
json.dump(upgradable, upgrade_file)
2 changes: 1 addition & 1 deletion mmpm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def upgrade() -> bool:
"""

error_code, stdout, stderr = run_cmd(
["python3", "-m", "pip", "install", "--upgrade", "--no-cache-dir", "mmpm"],
["python3", "-m", "pip", "install", "--upgrade", "mmpm"],
message="Upgrading MMPM",
)

Expand Down
Binary file modified ui/bun.lockb
Binary file not shown.
Loading