Skip to content

Commit

Permalink
Added new check_prune_system_threshold setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankravets committed Jan 24, 2021
1 parent 15ff8f9 commit 2c389ae
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ PlatformIO Core 5
+ ``--core-packages`` option to remove unnecessary core packages
+ ``--platform-packages`` option to remove unnecessary development platform packages (`issue #923 <https://github.com/platformio/platformio-core/issues/923>`_)

- Added new `check_prune_system_threshold <https://docs.platformio.org/page/core/userguide/cmd_settings.html#check-prune-system-threshold>`__ setting
- Disabled automatic removal of unnecessary development platform packages (`issue #3708 <https://github.com/platformio/platformio-core/issues/3708>`_, `issue #3770 <https://github.com/platformio/platformio-core/issues/3770>`_)
- Fixed an issue when unnecessary packages were removed in ``update --dry-run`` mode (`issue #3809 <https://github.com/platformio/platformio-core/issues/3809>`_)

Expand Down
2 changes: 1 addition & 1 deletion docs
4 changes: 4 additions & 0 deletions platformio/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def projects_dir_validate(projects_dir):
"description": "Check for the platform updates interval (days)",
"value": 7,
},
"check_prune_system_threshold": {
"description": "Check for pruning unnecessary data threshold (megabytes)",
"value": 1024,
},
"enable_cache": {
"description": "Enable caching for HTTP API requests",
"value": True,
Expand Down
46 changes: 30 additions & 16 deletions platformio/commands/system/prune.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,40 @@
from platformio.project.helpers import get_project_cache_dir


def prune_cached_data(force, dry_run):
def prune_cached_data(force=False, dry_run=False, silent=False):
reclaimed_space = 0
click.secho("Prune cached data:", bold=True)
click.echo(" - cached API requests")
click.echo(" - cached package downloads")
click.echo(" - temporary data")
if not silent:
click.secho("Prune cached data:", bold=True)
click.echo(" - cached API requests")
click.echo(" - cached package downloads")
click.echo(" - temporary data")
cache_dir = get_project_cache_dir()
if os.path.isdir(cache_dir):
reclaimed_space += fs.calculate_folder_size(cache_dir)
if not dry_run:
if not force:
click.confirm("Do you want to continue?", abort=True)
fs.rmtree(cache_dir)
click.secho("Space on disk: %s" % fs.humanize_file_size(reclaimed_space))
if not silent:
click.secho("Space on disk: %s" % fs.humanize_file_size(reclaimed_space))
return reclaimed_space


def prune_core_packages(force, dry_run):
click.secho("Prune unnecessary core packages:", bold=True)
return _prune_packages(force, dry_run, remove_unnecessary_core_packages)
def prune_core_packages(force=False, dry_run=False, silent=False):
if not silent:
click.secho("Prune unnecessary core packages:", bold=True)
return _prune_packages(force, dry_run, silent, remove_unnecessary_core_packages)


def prune_platform_packages(force, dry_run):
click.secho("Prune unnecessary development platform packages:", bold=True)
return _prune_packages(force, dry_run, remove_unnecessary_platform_packages)
def prune_platform_packages(force=False, dry_run=False, silent=False):
if not silent:
click.secho("Prune unnecessary development platform packages:", bold=True)
return _prune_packages(force, dry_run, silent, remove_unnecessary_platform_packages)


def _prune_packages(force, dry_run, handler):
click.echo("Calculating...")
def _prune_packages(force, dry_run, silent, handler):
if not silent:
click.echo("Calculating...")
items = [
(
pkg,
Expand All @@ -62,7 +67,7 @@ def _prune_packages(force, dry_run, handler):
]
items = sorted(items, key=itemgetter(1), reverse=True)
reclaimed_space = sum([item[1] for item in items])
if items:
if items and not silent:
click.echo(
tabulate(
[
Expand All @@ -80,5 +85,14 @@ def _prune_packages(force, dry_run, handler):
if not force:
click.confirm("Do you want to continue?", abort=True)
handler(dry_run=False)
click.secho("Space on disk: %s" % fs.humanize_file_size(reclaimed_space))
if not silent:
click.secho("Space on disk: %s" % fs.humanize_file_size(reclaimed_space))
return reclaimed_space


def calculate_unnecessary_system_data():
return (
prune_cached_data(force=True, dry_run=True, silent=True)
+ prune_core_packages(force=True, dry_run=True, silent=True)
+ prune_platform_packages(force=True, dry_run=True, silent=True)
)
30 changes: 30 additions & 0 deletions platformio/maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from platformio.commands.lib.command import CTX_META_STORAGE_DIRS_KEY
from platformio.commands.lib.command import lib_update as cmd_lib_update
from platformio.commands.platform import platform_update as cmd_platform_update
from platformio.commands.system.prune import calculate_unnecessary_system_data
from platformio.commands.upgrade import get_latest_version
from platformio.compat import ensure_python3
from platformio.package.manager.core import update_core_packages
Expand Down Expand Up @@ -73,6 +74,7 @@ def on_platformio_end(ctx, result): # pylint: disable=unused-argument
check_platformio_upgrade()
check_internal_updates(ctx, "platforms")
check_internal_updates(ctx, "libraries")
check_prune_system()
except (
http.HTTPClientError,
http.InternetIsOffline,
Expand Down Expand Up @@ -347,3 +349,31 @@ def check_internal_updates(ctx, what): # pylint: disable=too-many-branches

click.echo("*" * terminal_width)
click.echo("")


def check_prune_system():
last_check = app.get_state_item("last_check", {})
interval = 30 * 3600 * 24 # 1 time per month
if (time() - interval) < last_check.get("prune_system", 0):
return

last_check["prune_system"] = int(time())
app.set_state_item("last_check", last_check)
threshold_mb = int(app.get_setting("check_prune_system_threshold") or 0)
if threshold_mb <= 0:
return

unnecessary_mb = calculate_unnecessary_system_data() / 1024
if unnecessary_mb < threshold_mb:
return

terminal_width, _ = click.get_terminal_size()
click.echo()
click.echo("*" * terminal_width)
click.secho(
"We found %s of unnecessary PlatformIO system data (temporary files, "
"unnecessary packages, etc.).\nUse `pio system prune --dry-run` to list "
"them or `pio system prune` to save disk space."
% fs.humanize_file_size(unnecessary_mb),
fg="yellow",
)

0 comments on commit 2c389ae

Please sign in to comment.