Skip to content

Commit

Permalink
feat: add subcommand to clean cache files (#4)
Browse files Browse the repository at this point in the history
* add silent subcommand to clean cache files

* add news fragment

* add tests for ww3 clean command

* prompt before removing all files

* remove lint
  • Loading branch information
mcflugen authored Jun 3, 2022
1 parent 0ba549a commit 12c1a0b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 2 additions & 0 deletions news/4.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added the ``ww3 clean`` subcommand that removes cached data files.

19 changes: 18 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pathlib

import pytest
from click.testing import CliRunner

Expand All @@ -15,7 +17,7 @@ def test_command_line_interface():
assert "version" in result.output


@pytest.mark.parametrize("subcommand", ("url", "fetch"))
@pytest.mark.parametrize("subcommand", ("url", "fetch", "clean"))
def test_subcommand_help(subcommand):
runner = CliRunner()
result = runner.invoke(ww3, [subcommand, "--help"])
Expand All @@ -27,3 +29,18 @@ def test_noop(subcommand):
runner = CliRunner()
result = runner.invoke(ww3, [subcommand])
assert result.exit_code == 0


def test_clean(tmpdir):
runner = CliRunner()

with tmpdir.as_cwd():
data_file = pathlib.Path("multi_1.glo_30m.dp.201712.grb2")
data_file.touch()

assert data_file.is_file()
runner.invoke(ww3, ["clean", "--cache-dir=.", "--dry-run"])
assert data_file.is_file()

runner.invoke(ww3, ["clean", "--cache-dir=.", "--yes"])
assert not data_file.is_file()
48 changes: 48 additions & 0 deletions wavewatch3/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools
import os
import pathlib
import sys
Expand Down Expand Up @@ -96,6 +97,53 @@ def fetch(ctx, date, dry_run, force, file):
print(local_file.absolute())


@ww3.command()
@click.option("--dry-run", is_flag=True, help="only display what would have been done")
@click.option(
"--cache-dir",
type=click.Path(file_okay=False, path_type=pathlib.Path),
help="cache folder to clean",
default="~/.wavewatch3/data",
)
@click.option("--yes", is_flag=True, help="remove files without prompting")
# @click.confirmation_option(prompt="Are you sure you want to remove all cached files?")
@click.pass_context
def clean(ctx, dry_run, cache_dir, yes):
verbose = ctx.parent.params["verbose"]
silent = ctx.parent.params["silent"]

region = "*"
quantity = "*"
date = "*"
pattern = f"multi_1.{region}.{quantity}.{date}.grb2"

cache_dir = cache_dir.expanduser()
cache_files = list(
itertools.chain(cache_dir.glob(pattern), cache_dir.glob(pattern + ".*.idx"))
)

total_bytes = sum([cache_file.stat().st_size for cache_file in cache_files])

if not silent and not dry_run:
for cache_file in cache_files:
out(f"{cache_file}")
out(f"Total size: {total_bytes // 2**20} MB")

if not dry_run and len(cache_files):
yes = yes or click.confirm(
"Are you sure you want to remove all files?", abort=True
)

for cache_file in cache_files:
if dry_run:
out(f"rm {cache_file}")
else:
cache_file.unlink()

if not dry_run and (verbose and not silent):
out(f"Removed {len(cache_files)} files ({total_bytes} bytes)")


def _retreive_urls(urls, disable=False, force=False):
tqdm.set_lock(RLock())
p = Pool(initializer=tqdm.set_lock, initargs=(tqdm.get_lock(),))
Expand Down

0 comments on commit 12c1a0b

Please sign in to comment.