From 81f343dbe8116af1c7eb4fa5492ea15953f8d65f Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Sat, 19 Mar 2022 18:12:36 +0200 Subject: [PATCH] Cleanup dev-platform package installer --- platformio/commands/platform.py | 71 ++++++++++++++---------- platformio/package/commands/install.py | 6 +- platformio/package/commands/uninstall.py | 2 +- platformio/package/manager/platform.py | 25 ++------- platformio/platform/_packages.py | 59 ++------------------ tests/commands/pkg/test_install.py | 8 +-- tests/package/test_manager.py | 2 +- 7 files changed, 63 insertions(+), 110 deletions(-) diff --git a/platformio/commands/platform.py b/platformio/commands/platform.py index 827b17ca..238b1f01 100644 --- a/platformio/commands/platform.py +++ b/platformio/commands/platform.py @@ -20,6 +20,8 @@ import click from platformio.cache import cleanup_content_cache from platformio.commands.boards import print_boards +from platformio.exception import UserSideException +from platformio.package.exception import UnknownPackageError from platformio.package.manager.platform import PlatformPackageManager from platformio.package.meta import PackageItem, PackageSpec from platformio.package.version import get_original_version @@ -179,7 +181,7 @@ def platform_show(platform, json_output): # pylint: disable=too-many-branches is_flag=True, help="Reinstall/redownload dev/platform and its packages if exist", ) -def platform_install( # pylint: disable=too-many-arguments +def platform_install( # pylint: disable=too-many-arguments,too-many-locals platforms, with_package, without_package, @@ -188,37 +190,50 @@ def platform_install( # pylint: disable=too-many-arguments silent, force, ): - return _platform_install( - platforms, - with_package, - without_package, - skip_default_package, - with_all_packages, - silent, - force, - ) + def _find_pkg_names(p, candidates): + result = [] + for candidate in candidates: + found = False + # lookup by package types + for _name, _opts in p.packages.items(): + if _opts.get("type") == candidate: + result.append(_name) + found = True + if ( + p.frameworks + and candidate.startswith("framework-") + and candidate[10:] in p.frameworks + ): + result.append(p.frameworks[candidate[10:]]["package"]) + found = True + if not found: + result.append(candidate) + return result - -def _platform_install( # pylint: disable=too-many-arguments - platforms, - with_package=None, - without_package=None, - skip_default_package=False, - with_all_packages=False, - silent=False, - force=False, -): pm = PlatformPackageManager() pm.set_log_level(logging.WARN if silent else logging.DEBUG) for platform in platforms: - pkg = pm.install( - spec=platform, - with_packages=with_package or [], - without_packages=without_package or [], - skip_default_package=skip_default_package, - with_all_packages=with_all_packages, - force=force, - ) + if with_package or without_package or with_all_packages: + pkg = pm.install(platform, skip_dependencies=True) + p = PlatformFactory.new(pkg) + if with_all_packages: + with_package = list(p.packages) + with_package = set(_find_pkg_names(p, with_package or [])) + without_package = set(_find_pkg_names(p, without_package or [])) + upkgs = with_package | without_package + ppkgs = set(p.packages) + if not upkgs.issubset(ppkgs): + raise UnknownPackageError(", ".join(upkgs - ppkgs)) + for name, options in p.packages.items(): + if name in without_package: + continue + if name in with_package or not ( + skip_default_package or options.get("optional", False) + ): + p.pm.install(p.get_package_spec(name), force=force) + else: + pkg = pm.install(platform, skip_dependencies=skip_default_package) + if pkg and not silent: click.secho( "The platform '%s' has been successfully installed!\n" diff --git a/platformio/package/commands/install.py b/platformio/package/commands/install.py index a060de88..5f5d216b 100644 --- a/platformio/package/commands/install.py +++ b/platformio/package/commands/install.py @@ -70,7 +70,7 @@ def install_global_dependencies(options): for spec in options.get("platforms"): pm.install( spec, - skip_default_package=options.get("skip_dependencies"), + skip_dependencies=options.get("skip_dependencies"), force=options.get("force"), ) for spec in options.get("tools"): @@ -145,7 +145,7 @@ def _install_project_env_platform(project_env, options): spec, project_env=project_env, project_targets=options.get("project_targets"), - skip_default_package=options.get("skip_dependencies"), + skip_dependencies=options.get("skip_dependencies"), force=options.get("force"), ) return not already_up_to_date @@ -163,7 +163,7 @@ def _install_project_env_custom_platforms(project_env, options): spec, project_env=project_env, project_targets=options.get("project_targets"), - skip_default_package=options.get("skip_dependencies"), + skip_dependencies=options.get("skip_dependencies"), force=options.get("force"), ) return not already_up_to_date diff --git a/platformio/package/commands/uninstall.py b/platformio/package/commands/uninstall.py index b4391c6a..050d23a4 100644 --- a/platformio/package/commands/uninstall.py +++ b/platformio/package/commands/uninstall.py @@ -134,7 +134,7 @@ def _uninstall_project_env_platform(project_env, options): pm.set_log_level(logging.WARN) spec = config.get(f"env:{project_env}", "platform") if not spec: - return False + return None already_up_to_date = True if not pm.get_package(spec): return None diff --git a/platformio/package/manager/platform.py b/platformio/package/manager/platform.py index 6d6ece5f..71cd5ddd 100644 --- a/platformio/package/manager/platform.py +++ b/platformio/package/manager/platform.py @@ -38,13 +38,10 @@ class PlatformPackageManager(BasePackageManager): # pylint: disable=too-many-an def manifest_names(self): return PackageType.get_manifest_map()[PackageType.PLATFORM] - def install( # pylint: disable=arguments-differ, too-many-arguments + def install( # pylint: disable=arguments-differ,too-many-arguments self, spec, - with_packages=None, - without_packages=None, - skip_default_package=False, - with_all_packages=False, + skip_dependencies=False, force=False, project_env=None, project_targets=None, @@ -55,26 +52,16 @@ class PlatformPackageManager(BasePackageManager): # pylint: disable=too-many-an ) try: p = PlatformFactory.new(pkg) + # set logging level for underlying tool manager + p.pm.set_log_level(self.log.getEffectiveLevel()) p.ensure_engine_compatible() except IncompatiblePlatform as e: super(PlatformPackageManager, self).uninstall(pkg, skip_dependencies=True) raise e - - # set logging level for underlying tool manager - p.pm.set_log_level(self.log.getEffectiveLevel()) - if project_env: p.configure_project_packages(project_env, project_targets) - - if with_all_packages: - with_packages = list(p.packages) - - p.install_packages( - with_packages, - without_packages, - skip_default_package, - force=force, - ) + if not skip_dependencies: + p.install_required_packages(force=force) if not already_installed: p.on_installed() return pkg diff --git a/platformio/platform/_packages.py b/platformio/platform/_packages.py index 95ecd402..b0fbd8d6 100644 --- a/platformio/platform/_packages.py +++ b/platformio/platform/_packages.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from platformio.package.exception import UnknownPackageError from platformio.package.meta import PackageSpec @@ -65,67 +64,19 @@ class PlatformPackagesMixin(object): result.append(item) return result - def install_packages( # pylint: disable=too-many-arguments - self, - with_packages=None, - without_packages=None, - skip_default_package=False, - force=False, - ): - with_packages = set(self._find_pkg_names(with_packages or [])) - without_packages = set(self._find_pkg_names(without_packages or [])) - - upkgs = with_packages | without_packages - ppkgs = set(self.packages) - if not upkgs.issubset(ppkgs): - raise UnknownPackageError(", ".join(upkgs - ppkgs)) - + def install_required_packages(self, force=False): for name, options in self.packages.items(): - if name in without_packages: + if options.get("optional"): continue - if name in with_packages or not ( - skip_default_package or options.get("optional", False) - ): - self.pm.install(self.get_package_spec(name), force=force) - - return True - - def _find_pkg_names(self, candidates): - result = [] - for candidate in candidates: - found = False - - # lookup by package types - for _name, _opts in self.packages.items(): - if _opts.get("type") == candidate: - result.append(_name) - found = True - - if ( - self.frameworks - and candidate.startswith("framework-") - and candidate[10:] in self.frameworks - ): - result.append(self.frameworks[candidate[10:]]["package"]) - found = True - - if not found: - result.append(candidate) - - return result + self.pm.install(self.get_package_spec(name), force=force) def uninstall_packages(self): for pkg in self.get_installed_packages(): self.pm.uninstall(pkg) - def update_packages(self, only_check=False): + def update_packages(self): for pkg in self.get_installed_packages(): - self.pm.update( - pkg, - to_spec=self.get_package_spec(pkg.metadata.name), - only_check=only_check, - show_incompatible=False, - ) + self.pm.update(pkg, to_spec=self.get_package_spec(pkg.metadata.name)) def are_outdated_packages(self): for pkg in self.get_installed_packages(): diff --git a/tests/commands/pkg/test_install.py b/tests/commands/pkg/test_install.py index 1ceab50e..e539dba3 100644 --- a/tests/commands/pkg/test_install.py +++ b/tests/commands/pkg/test_install.py @@ -56,13 +56,13 @@ def test_global_packages( [ "--global", "-l", - "milesburton/DallasTemperature@^3.9.1", + "https://github.com/milesburton/Arduino-Temperature-Control-Library.git#3.9.0", "--skip-dependencies", ], ) validate_cliresult(result) assert pkgs_to_specs(LibraryPackageManager().get_installed()) == [ - PackageSpec("DallasTemperature@3.9.1") + PackageSpec("DallasTemperature@3.9.0+sha.964939d") ] # with dependencies result = clirunner.invoke( @@ -70,7 +70,7 @@ def test_global_packages( [ "--global", "-l", - "milesburton/DallasTemperature@^3.9.1", + "https://github.com/milesburton/Arduino-Temperature-Control-Library.git#3.9.0", "-l", "bblanchon/ArduinoJson@^6.19.2", ], @@ -78,7 +78,7 @@ def test_global_packages( validate_cliresult(result) assert pkgs_to_specs(LibraryPackageManager().get_installed()) == [ PackageSpec("ArduinoJson@6.19.3"), - PackageSpec("DallasTemperature@3.9.1"), + PackageSpec("DallasTemperature@3.9.0+sha.964939d"), PackageSpec("OneWire@2.3.6"), ] # custom storage diff --git a/tests/package/test_manager.py b/tests/package/test_manager.py index 29dec26a..89132b07 100644 --- a/tests/package/test_manager.py +++ b/tests/package/test_manager.py @@ -455,7 +455,7 @@ def test_update_with_metadata(isolated_pio_core, tmpdir_factory): assert outdated.latest > semantic_version.Version("1.10.0") pkg = lm.install("ArduinoJson @ 5.10.1") - # tesy latest + # test latest outdated = lm.outdated(pkg) assert str(outdated.current) == "5.10.1" assert outdated.wanted is None