diff --git a/nomad/cli/__init__.py b/nomad/cli/__init__.py index 075a6c12dd63106a18638b18941f21cd17f21706..698288e6bf2cc1612369e9904b3fc80adc8476f4 100644 --- a/nomad/cli/__init__.py +++ b/nomad/cli/__init__.py @@ -24,5 +24,5 @@ Use it from the command line with ``nomad --help`` or ``python -m nomad.cli --he more. """ -from . import dev, parse, client, admin # noqa +from . import dev, parse, client, admin, clean # noqa from .cli import run_cli, cli # noqa diff --git a/nomad/cli/clean.py b/nomad/cli/clean.py new file mode 100644 index 0000000000000000000000000000000000000000..90433258c64492c5acfeefd5a635994490644ca2 --- /dev/null +++ b/nomad/cli/clean.py @@ -0,0 +1,59 @@ +# +# Copyright The NOMAD Authors. +# +# This file is part of NOMAD. See https://nomad-lab.eu for further info. +# +# 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 os + +import click +from tqdm import tqdm + +from .cli import cli + + +@cli.command( + help='Cleanse the given path by removing empty folders.', + name='clean', +) +@click.option( + '--path', + type=str, + help='Cleanse the given path by removing empty folders.', +) +def clean_staging(path): + if 'staging' not in path: + print('Path must contain "staging".') + return + + print(f'Cleaning path: "{path}".') + print('Are you sure you want to continue? (y/N)', end=' ') + response = input() + if response.lower() != 'y': + print('Exiting...') + return + + print('Cleaning...') + + def safe_remove(_p): + try: + os.rmdir(_p) + except Exception: # noqa + pass + + for root, folders, _ in tqdm(os.walk(path, topdown=False)): + for folder in folders: + if not os.listdir(full_path := os.path.join(root, folder)): # noqa + safe_remove(full_path)