From df2f96828eeefc2ed60fce9697843ebc751d11d6 Mon Sep 17 00:00:00 2001 From: grossmj Date: Sat, 12 Aug 2023 18:48:43 +1000 Subject: [PATCH] Use the user data dir to store built-in appliances --- gns3server/controller/__init__.py | 2 ++ gns3server/controller/appliance_manager.py | 13 +++++++------ requirements.txt | 1 + 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 2220f7e5..19d7976e 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -255,6 +255,8 @@ class Controller: if not previous_version or \ parse_version(__version__.split("+")[0]) > parse_version(previous_version.split("+")[0]): self._appliance_manager.install_builtin_appliances() + elif not os.listdir(self._appliance_manager.builtin_appliances_path()): + self._appliance_manager.install_builtin_appliances() self._appliance_manager.appliances_etag = controller_settings.get("appliances_etag") self._appliance_manager.load_appliances() diff --git a/gns3server/controller/appliance_manager.py b/gns3server/controller/appliance_manager.py index 76cadfbf..d6878d71 100644 --- a/gns3server/controller/appliance_manager.py +++ b/gns3server/controller/appliance_manager.py @@ -21,6 +21,7 @@ import uuid import asyncio import aiohttp import shutil +import platformdirs try: @@ -81,13 +82,13 @@ class ApplianceManager: os.makedirs(appliances_path, exist_ok=True) return appliances_path - def _builtin_appliances_path(self, delete_first=False): + def builtin_appliances_path(self, delete_first=False): """ Get the built-in appliance storage directory """ - config = Config.instance() - appliances_dir = os.path.join(config.config_dir, "appliances") + appname = vendor = "GNS3" + appliances_dir = os.path.join(platformdirs.user_data_dir(appname, vendor, roaming=True), "appliances") if delete_first: shutil.rmtree(appliances_dir, ignore_errors=True) os.makedirs(appliances_dir, exist_ok=True) @@ -98,7 +99,7 @@ class ApplianceManager: At startup we copy the built-in appliances files. """ - dst_path = self._builtin_appliances_path(delete_first=True) + dst_path = self.builtin_appliances_path(delete_first=True) log.info(f"Installing built-in appliances in '{dst_path}'") from . import Controller try: @@ -112,7 +113,7 @@ class ApplianceManager: """ self._appliances = {} - for directory, builtin in ((self._builtin_appliances_path(), True,), (self._custom_appliances_path(), False,)): + for directory, builtin in ((self.builtin_appliances_path(), True,), (self._custom_appliances_path(), False,)): if directory and os.path.isdir(directory): for file in os.listdir(directory): if not file.endswith('.gns3a') and not file.endswith('.gns3appliance'): @@ -215,7 +216,7 @@ class ApplianceManager: from . import Controller Controller.instance().save() json_data = await response.json() - appliances_dir = self._builtin_appliances_path() + appliances_dir = self.builtin_appliances_path() downloaded_appliance_files = [] for appliance in json_data: if appliance["type"] == "file": diff --git a/requirements.txt b/requirements.txt index de280d34..392d35df 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,6 +11,7 @@ psutil==5.9.5 async-timeout>=4.0.2,<4.1 distro>=1.8.0 py-cpuinfo>=9.0.0,<10.0 +platformdirs>=3.10.0 importlib-resources>=1.3; python_version <= '3.9' truststore>=0.7.0; python_version >= '3.10' setuptools>=60.8.1; python_version >= '3.7'