|
| 1 | + |
| 2 | +import argparse |
| 3 | +import os |
| 4 | +from shutil import rmtree |
| 5 | +import sys |
| 6 | +import urllib.request as request |
| 7 | + |
| 8 | +from vcstool.commands.import_ import file_or_url_type, get_repositories |
| 9 | +from vcstool.executor import ansi |
| 10 | +from vcstool.streams import set_streams |
| 11 | + |
| 12 | +from .command import Command, existing_dir |
| 13 | + |
| 14 | + |
| 15 | +class RmAllCommand(Command): |
| 16 | + command = "rm-all" |
| 17 | + help = "Remove the directories indicated by the list of given repositories" |
| 18 | + |
| 19 | + def __init__(self, *args, **kargs): |
| 20 | + super(RmAllCommand, self).__init__(*args, **kargs) |
| 21 | + |
| 22 | + |
| 23 | +_cls = RmAllCommand |
| 24 | + |
| 25 | + |
| 26 | +def get_parser(): |
| 27 | + parser = argparse.ArgumentParser( |
| 28 | + description=_cls.help, prog="vcs {}".format(_cls.command)) |
| 29 | + group = parser.add_argument_group("Command parameters") |
| 30 | + group.add_argument( |
| 31 | + "--input", |
| 32 | + type=file_or_url_type, |
| 33 | + default="-", |
| 34 | + help="Where to read YAML from", |
| 35 | + metavar="FILE_OR_URL", |
| 36 | + ) |
| 37 | + group.add_argument( |
| 38 | + "path", |
| 39 | + nargs="?", |
| 40 | + type=existing_dir, |
| 41 | + default=os.curdir, |
| 42 | + help="Base path to look for repositories", |
| 43 | + ) |
| 44 | + |
| 45 | + group_verification = parser.add_mutually_exclusive_group(required=True) |
| 46 | + group_verification.add_argument( |
| 47 | + "-n", |
| 48 | + "--dry-run", |
| 49 | + action="store_true", |
| 50 | + default=False, |
| 51 | + help="Instead of removing, print the paths instead", |
| 52 | + ) |
| 53 | + group_verification.add_argument( |
| 54 | + "-f", |
| 55 | + "--force", |
| 56 | + action="store_true", |
| 57 | + default=False, |
| 58 | + help="Force the removal ofthe paths", |
| 59 | + ) |
| 60 | + return parser |
| 61 | + |
| 62 | + |
| 63 | +def main(args=None, stdout=None, stderr=None): |
| 64 | + # CLI Parsing ------------------------------------------------------------- |
| 65 | + set_streams(stdout=stdout, stderr=stderr) |
| 66 | + parser = get_parser() |
| 67 | + parser.formatter_class = argparse.ArgumentDefaultsHelpFormatter |
| 68 | + args = parser.parse_args(args) |
| 69 | + try: |
| 70 | + input_ = args.input |
| 71 | + if isinstance(input_, request.Request): |
| 72 | + input_ = request.urlopen(input_) |
| 73 | + repos = get_repositories(input_) |
| 74 | + except (RuntimeError, request.URLError) as e: |
| 75 | + print(ansi("redf") + str(e) + ansi("reset"), file=sys.stderr) |
| 76 | + return 1 |
| 77 | + args = vars(args) |
| 78 | + |
| 79 | + # Get the paths to the repos based on the source and the repo name -------- |
| 80 | + paths = [os.path.join(args["path"], rel_path) for rel_path in repos] |
| 81 | + info_str = ansi("yellowf") + str("Paths to delete:") + ansi("reset") |
| 82 | + info_str += "\n\n- " |
| 83 | + info_str += "\n- ".join(paths) |
| 84 | + print(info_str, file=sys.stderr) |
| 85 | + |
| 86 | + if args["dry_run"]: |
| 87 | + print("\n[Dry Run]", file=sys.stderr) |
| 88 | + return 0 |
| 89 | + |
| 90 | + # Do remove --------------------------------------------------------------- |
| 91 | + for p in paths: |
| 92 | + rmtree(p, ignore_errors=True) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + sys.exit(main()) |
0 commit comments