From 11a43b26930c87c4b853f4dde756f7217d838c51 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Tue, 31 May 2022 17:31:21 +0300 Subject: [PATCH] Regroup "system" commands --- platformio/system/cli.py | 185 ++--------------------- platformio/system/commands/__init__.py | 13 ++ platformio/system/commands/completion.py | 69 +++++++++ platformio/system/commands/info.py | 85 +++++++++++ platformio/system/commands/prune.py | 70 +++++++++ 5 files changed, 250 insertions(+), 172 deletions(-) create mode 100644 platformio/system/commands/__init__.py create mode 100644 platformio/system/commands/completion.py create mode 100644 platformio/system/commands/info.py create mode 100644 platformio/system/commands/prune.py diff --git a/platformio/system/cli.py b/platformio/system/cli.py index 605624c0..dd39ce87 100644 --- a/platformio/system/cli.py +++ b/platformio/system/cli.py @@ -12,180 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json -import platform -import sys - import click -from tabulate import tabulate -from platformio import __version__, compat, fs, proc, util -from platformio.package.manager.library import LibraryPackageManager -from platformio.package.manager.platform import PlatformPackageManager -from platformio.package.manager.tool import ToolPackageManager -from platformio.project.config import ProjectConfig -from platformio.system.completion import ( - ShellType, - get_completion_install_path, - install_completion_code, - uninstall_completion_code, +from platformio.system.commands.completion import system_completion_cmd +from platformio.system.commands.info import system_info_cmd +from platformio.system.commands.prune import system_prune_cmd + + +@click.group( + "system", + commands=[ + system_completion_cmd, + system_info_cmd, + system_prune_cmd, + ], + short_help="Miscellaneous system commands", ) -from platformio.system.prune import ( - prune_cached_data, - prune_core_packages, - prune_platform_packages, -) - - -@click.group("system", short_help="Miscellaneous system commands") def cli(): pass - - -@cli.command("info", short_help="Display system-wide information") -@click.option("--json-output", is_flag=True) -def system_info(json_output): - project_config = ProjectConfig() - data = {} - data["core_version"] = {"title": "PlatformIO Core", "value": __version__} - data["python_version"] = { - "title": "Python", - "value": "{0}.{1}.{2}-{3}.{4}".format(*list(sys.version_info)), - } - data["system"] = {"title": "System Type", "value": util.get_systype()} - data["platform"] = {"title": "Platform", "value": platform.platform(terse=True)} - data["filesystem_encoding"] = { - "title": "File System Encoding", - "value": compat.get_filesystem_encoding(), - } - data["locale_encoding"] = { - "title": "Locale Encoding", - "value": compat.get_locale_encoding(), - } - data["core_dir"] = { - "title": "PlatformIO Core Directory", - "value": project_config.get("platformio", "core_dir"), - } - data["platformio_exe"] = { - "title": "PlatformIO Core Executable", - "value": proc.where_is_program( - "platformio.exe" if compat.IS_WINDOWS else "platformio" - ), - } - data["python_exe"] = { - "title": "Python Executable", - "value": proc.get_pythonexe_path(), - } - data["global_lib_nums"] = { - "title": "Global Libraries", - "value": len(LibraryPackageManager().get_installed()), - } - data["dev_platform_nums"] = { - "title": "Development Platforms", - "value": len(PlatformPackageManager().get_installed()), - } - data["package_tool_nums"] = { - "title": "Tools & Toolchains", - "value": len( - ToolPackageManager( - project_config.get("platformio", "packages_dir") - ).get_installed() - ), - } - - click.echo( - json.dumps(data) - if json_output - else tabulate([(item["title"], item["value"]) for item in data.values()]) - ) - - -@cli.command("prune", short_help="Remove unused data") -@click.option("--force", "-f", is_flag=True, help="Do not prompt for confirmation") -@click.option( - "--dry-run", is_flag=True, help="Do not prune, only show data that will be removed" -) -@click.option("--cache", is_flag=True, help="Prune only cached data") -@click.option( - "--core-packages", is_flag=True, help="Prune only unnecessary core packages" -) -@click.option( - "--platform-packages", - is_flag=True, - help="Prune only unnecessary development platform packages", -) -def system_prune(force, dry_run, cache, core_packages, platform_packages): - if dry_run: - click.secho( - "Dry run mode (do not prune, only show data that will be removed)", - fg="yellow", - ) - click.echo() - - reclaimed_cache = 0 - reclaimed_core_packages = 0 - reclaimed_platform_packages = 0 - prune_all = not any([cache, core_packages, platform_packages]) - - if cache or prune_all: - reclaimed_cache = prune_cached_data(force, dry_run) - click.echo() - - if core_packages or prune_all: - reclaimed_core_packages = prune_core_packages(force, dry_run) - click.echo() - - if platform_packages or prune_all: - reclaimed_platform_packages = prune_platform_packages(force, dry_run) - click.echo() - - click.secho( - "Total reclaimed space: %s" - % fs.humanize_file_size( - reclaimed_cache + reclaimed_core_packages + reclaimed_platform_packages - ), - fg="green", - ) - - -@cli.group("completion", short_help="Shell completion support") -def completion(): - pass - - -@completion.command("install", short_help="Install shell completion files/code") -@click.argument("shell", type=click.Choice([t.value for t in ShellType])) -@click.option( - "--path", - type=click.Path(file_okay=True, dir_okay=False, readable=True, resolve_path=True), - help="Custom installation path of the code to be evaluated by the shell. " - "The standard installation path is used by default.", -) -def completion_install(shell, path): - shell = ShellType(shell) - path = path or get_completion_install_path(shell) - install_completion_code(shell, path) - click.echo( - "PlatformIO CLI completion has been installed for %s shell to %s \n" - "Please restart a current shell session." - % (click.style(shell.name, fg="cyan"), click.style(path, fg="blue")) - ) - - -@completion.command("uninstall", short_help="Uninstall shell completion files/code") -@click.argument("shell", type=click.Choice([t.value for t in ShellType])) -@click.option( - "--path", - type=click.Path(file_okay=True, dir_okay=False, readable=True, resolve_path=True), - help="Custom installation path of the code to be evaluated by the shell. " - "The standard installation path is used by default.", -) -def completion_uninstall(shell, path): - shell = ShellType(shell) - path = path or get_completion_install_path(shell) - uninstall_completion_code(shell, path) - click.echo( - "PlatformIO CLI completion has been uninstalled for %s shell from %s \n" - "Please restart a current shell session." - % (click.style(shell.name, fg="cyan"), click.style(path, fg="blue")) - ) diff --git a/platformio/system/commands/__init__.py b/platformio/system/commands/__init__.py new file mode 100644 index 00000000..b0514903 --- /dev/null +++ b/platformio/system/commands/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/platformio/system/commands/completion.py b/platformio/system/commands/completion.py new file mode 100644 index 00000000..aac68592 --- /dev/null +++ b/platformio/system/commands/completion.py @@ -0,0 +1,69 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import click + +from platformio.system.completion import ( + ShellType, + get_completion_install_path, + install_completion_code, + uninstall_completion_code, +) + + +@click.group("completion", short_help="Shell completion support") +def system_completion_cmd(): + pass + + +@system_completion_cmd.command( + "install", short_help="Install shell completion files/code" +) +@click.argument("shell", type=click.Choice([t.value for t in ShellType])) +@click.option( + "--path", + type=click.Path(file_okay=True, dir_okay=False, readable=True, resolve_path=True), + help="Custom installation path of the code to be evaluated by the shell. " + "The standard installation path is used by default.", +) +def system_completion_install(shell, path): + shell = ShellType(shell) + path = path or get_completion_install_path(shell) + install_completion_code(shell, path) + click.echo( + "PlatformIO CLI completion has been installed for %s shell to %s \n" + "Please restart a current shell session." + % (click.style(shell.name, fg="cyan"), click.style(path, fg="blue")) + ) + + +@system_completion_cmd.command( + "uninstall", short_help="Uninstall shell completion files/code" +) +@click.argument("shell", type=click.Choice([t.value for t in ShellType])) +@click.option( + "--path", + type=click.Path(file_okay=True, dir_okay=False, readable=True, resolve_path=True), + help="Custom installation path of the code to be evaluated by the shell. " + "The standard installation path is used by default.", +) +def system_completion_uninstall(shell, path): + shell = ShellType(shell) + path = path or get_completion_install_path(shell) + uninstall_completion_code(shell, path) + click.echo( + "PlatformIO CLI completion has been uninstalled for %s shell from %s \n" + "Please restart a current shell session." + % (click.style(shell.name, fg="cyan"), click.style(path, fg="blue")) + ) diff --git a/platformio/system/commands/info.py b/platformio/system/commands/info.py new file mode 100644 index 00000000..52a06636 --- /dev/null +++ b/platformio/system/commands/info.py @@ -0,0 +1,85 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import json +import platform +import sys + +import click +from tabulate import tabulate + +from platformio import __version__, compat, proc, util +from platformio.package.manager.library import LibraryPackageManager +from platformio.package.manager.platform import PlatformPackageManager +from platformio.package.manager.tool import ToolPackageManager +from platformio.project.config import ProjectConfig + + +@click.command("info", short_help="Display system-wide information") +@click.option("--json-output", is_flag=True) +def system_info_cmd(json_output): + project_config = ProjectConfig() + data = {} + data["core_version"] = {"title": "PlatformIO Core", "value": __version__} + data["python_version"] = { + "title": "Python", + "value": "{0}.{1}.{2}-{3}.{4}".format(*list(sys.version_info)), + } + data["system"] = {"title": "System Type", "value": util.get_systype()} + data["platform"] = {"title": "Platform", "value": platform.platform(terse=True)} + data["filesystem_encoding"] = { + "title": "File System Encoding", + "value": compat.get_filesystem_encoding(), + } + data["locale_encoding"] = { + "title": "Locale Encoding", + "value": compat.get_locale_encoding(), + } + data["core_dir"] = { + "title": "PlatformIO Core Directory", + "value": project_config.get("platformio", "core_dir"), + } + data["platformio_exe"] = { + "title": "PlatformIO Core Executable", + "value": proc.where_is_program( + "platformio.exe" if compat.IS_WINDOWS else "platformio" + ), + } + data["python_exe"] = { + "title": "Python Executable", + "value": proc.get_pythonexe_path(), + } + data["global_lib_nums"] = { + "title": "Global Libraries", + "value": len(LibraryPackageManager().get_installed()), + } + data["dev_platform_nums"] = { + "title": "Development Platforms", + "value": len(PlatformPackageManager().get_installed()), + } + data["package_tool_nums"] = { + "title": "Tools & Toolchains", + "value": len( + ToolPackageManager( + project_config.get("platformio", "packages_dir") + ).get_installed() + ), + } + + click.echo( + json.dumps(data) + if json_output + else tabulate([(item["title"], item["value"]) for item in data.values()]) + ) diff --git a/platformio/system/commands/prune.py b/platformio/system/commands/prune.py new file mode 100644 index 00000000..1559aab3 --- /dev/null +++ b/platformio/system/commands/prune.py @@ -0,0 +1,70 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import click + +from platformio import fs +from platformio.system.prune import ( + prune_cached_data, + prune_core_packages, + prune_platform_packages, +) + + +@click.command("prune", short_help="Remove unused data") +@click.option("--force", "-f", is_flag=True, help="Do not prompt for confirmation") +@click.option( + "--dry-run", is_flag=True, help="Do not prune, only show data that will be removed" +) +@click.option("--cache", is_flag=True, help="Prune only cached data") +@click.option( + "--core-packages", is_flag=True, help="Prune only unnecessary core packages" +) +@click.option( + "--platform-packages", + is_flag=True, + help="Prune only unnecessary development platform packages", +) +def system_prune_cmd(force, dry_run, cache, core_packages, platform_packages): + if dry_run: + click.secho( + "Dry run mode (do not prune, only show data that will be removed)", + fg="yellow", + ) + click.echo() + + reclaimed_cache = 0 + reclaimed_core_packages = 0 + reclaimed_platform_packages = 0 + prune_all = not any([cache, core_packages, platform_packages]) + + if cache or prune_all: + reclaimed_cache = prune_cached_data(force, dry_run) + click.echo() + + if core_packages or prune_all: + reclaimed_core_packages = prune_core_packages(force, dry_run) + click.echo() + + if platform_packages or prune_all: + reclaimed_platform_packages = prune_platform_packages(force, dry_run) + click.echo() + + click.secho( + "Total reclaimed space: %s" + % fs.humanize_file_size( + reclaimed_cache + reclaimed_core_packages + reclaimed_platform_packages + ), + fg="green", + )