From 5952935b8a140e9fa24477e7bbf7cb0ed046a668 Mon Sep 17 00:00:00 2001 From: Nikos Koukis Date: Wed, 26 May 2021 17:57:32 +0300 Subject: [PATCH] Implement rm-all command --- scripts/vcs-rm-all | 7 +++ setup.py | 1 + test/commands.txt | 2 +- vcstool/commands/__init__.py | 2 + vcstool/commands/rm_all.py | 96 ++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100755 scripts/vcs-rm-all create mode 100644 vcstool/commands/rm_all.py diff --git a/scripts/vcs-rm-all b/scripts/vcs-rm-all new file mode 100755 index 00000000..c7f8d6cd --- /dev/null +++ b/scripts/vcs-rm-all @@ -0,0 +1,7 @@ +#!/usr/bin/env python3 + +import sys + +from vcstool.commands.rm_all import main + +sys.exit(main() or 0) diff --git a/setup.py b/setup.py index ed8c0759..884a8cda 100644 --- a/setup.py +++ b/setup.py @@ -50,6 +50,7 @@ 'vcs-pull = vcstool.commands.pull:main', 'vcs-push = vcstool.commands.push:main', 'vcs-remotes = vcstool.commands.remotes:main', + 'vcs-rm-all = vcstool.commands.rm_all:main', 'vcs-status = vcstool.commands.status:main', 'vcs-svn = vcstool.commands.custom:svn_main', 'vcs-validate = vcstool.commands.validate:main', diff --git a/test/commands.txt b/test/commands.txt index fd5ea645..5d9134bc 100644 --- a/test/commands.txt +++ b/test/commands.txt @@ -1 +1 @@ -branch custom diff export import log pull push remotes status validate +branch custom diff export import log pull push remotes rm-all status validate diff --git a/vcstool/commands/__init__.py b/vcstool/commands/__init__.py index f96d55ba..8ea6e228 100644 --- a/vcstool/commands/__init__.py +++ b/vcstool/commands/__init__.py @@ -7,6 +7,7 @@ from .pull import PullCommand from .push import PushCommand from .remotes import RemotesCommand +from .rm_all import RmAllCommand from .status import StatusCommand from .validate import ValidateCommand @@ -20,6 +21,7 @@ vcstool_commands.append(PullCommand) vcstool_commands.append(PushCommand) vcstool_commands.append(RemotesCommand) +vcstool_commands.append(RmAllCommand) vcstool_commands.append(StatusCommand) vcstool_commands.append(ValidateCommand) diff --git a/vcstool/commands/rm_all.py b/vcstool/commands/rm_all.py new file mode 100644 index 00000000..481dd6c4 --- /dev/null +++ b/vcstool/commands/rm_all.py @@ -0,0 +1,96 @@ + +import argparse +import os +from shutil import rmtree +import sys +import urllib.request as request + +from vcstool.commands.import_ import file_or_url_type, get_repositories +from vcstool.executor import ansi +from vcstool.streams import set_streams + +from .command import Command, existing_dir + + +class RmAllCommand(Command): + command = "rm-all" + help = "Remove the directories indicated by the list of given repositories" + + def __init__(self, *args, **kargs): + super(RmAllCommand, self).__init__(*args, **kargs) + + +_cls = RmAllCommand + + +def get_parser(): + parser = argparse.ArgumentParser( + description=_cls.help, prog="vcs {}".format(_cls.command)) + group = parser.add_argument_group("Command parameters") + group.add_argument( + "--input", + type=file_or_url_type, + default="-", + help="Where to read YAML from", + metavar="FILE_OR_URL", + ) + group.add_argument( + "path", + nargs="?", + type=existing_dir, + default=os.curdir, + help="Base path to look for repositories", + ) + + group_verification = parser.add_mutually_exclusive_group(required=True) + group_verification.add_argument( + "-n", + "--dry-run", + action="store_true", + default=False, + help="Instead of removing, print the paths instead", + ) + group_verification.add_argument( + "-f", + "--force", + action="store_true", + default=False, + help="Force the removal ofthe paths", + ) + return parser + + +def main(args=None, stdout=None, stderr=None): + # CLI Parsing ----------------------------------------------------------------------------- + set_streams(stdout=stdout, stderr=stderr) + parser = get_parser() + parser.formatter_class = argparse.ArgumentDefaultsHelpFormatter + args = parser.parse_args(args) + try: + input_ = args.input + if isinstance(input_, request.Request): + input_ = request.urlopen(input_) + repos = get_repositories(input_) + except (RuntimeError, request.URLError) as e: + print(ansi("redf") + str(e) + ansi("reset"), file=sys.stderr) + return 1 + args = vars(args) + + # Get the paths to the repos based on the source and the repo name ------------------------ + paths = [os.path.join(args["path"], rel_path) for rel_path in repos] + info_str = ansi("yellowf") + str("Paths to delete:") + ansi("reset") + info_str += "\n\n- " + info_str += "\n- ".join(paths) + print(info_str, file=sys.stderr) + + if args["dry_run"]: + print("\n[Dry Run]", file=sys.stderr) + return 0 + + # Do remove ------------------------------------------------------------------------------- + for p in paths: + rmtree(p, ignore_errors=True) + + +if __name__ == "__main__": + sys.exit(main())