|
| 1 | +"""Command to delete directories of repositories listed in a YAML file.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import urllib.request as request |
| 7 | +from urllib.error import URLError |
| 8 | + |
| 9 | +from vcs2l.commands.import_ import file_or_url_type, get_repositories |
| 10 | +from vcs2l.executor import ansi |
| 11 | +from vcs2l.streams import set_streams |
| 12 | +from vcs2l.util import rmtree |
| 13 | + |
| 14 | +from .command import Command, existing_dir |
| 15 | + |
| 16 | + |
| 17 | +class DeleteCommand(Command): |
| 18 | + """Delete directories of repositories listed in a YAML file.""" |
| 19 | + |
| 20 | + command = 'delete' |
| 21 | + help = 'Remove the directories indicated by the list of given repositories.' |
| 22 | + |
| 23 | + |
| 24 | +def get_parser(): |
| 25 | + """CLI parser for the 'delete' command.""" |
| 26 | + _cls = DeleteCommand |
| 27 | + |
| 28 | + parser = argparse.ArgumentParser( |
| 29 | + description=_cls.help, prog='vcs {}'.format(_cls.command) |
| 30 | + ) |
| 31 | + group = parser.add_argument_group('Command parameters') |
| 32 | + group.add_argument( |
| 33 | + '--input', |
| 34 | + type=file_or_url_type, |
| 35 | + default='-', |
| 36 | + help='Where to read YAML from', |
| 37 | + metavar='FILE_OR_URL', |
| 38 | + ) |
| 39 | + group.add_argument( |
| 40 | + 'path', |
| 41 | + nargs='?', |
| 42 | + type=existing_dir, |
| 43 | + default=os.curdir, |
| 44 | + help='Base path to look for repositories', |
| 45 | + ) |
| 46 | + group.add_argument( |
| 47 | + '-f', |
| 48 | + '--force', |
| 49 | + action='store_true', |
| 50 | + default=False, |
| 51 | + help='Do the deletion instead of a dry-run', |
| 52 | + ) |
| 53 | + return parser |
| 54 | + |
| 55 | + |
| 56 | +def get_repository_paths(input_source, base_path): |
| 57 | + """Get repository paths from input source.""" |
| 58 | + try: |
| 59 | + if isinstance(input_source, request.Request): |
| 60 | + input_source = request.urlopen(input_source) |
| 61 | + repos = get_repositories(input_source) |
| 62 | + return [os.path.join(base_path, rel_path) for rel_path in repos] |
| 63 | + except (RuntimeError, URLError) as e: |
| 64 | + raise RuntimeError(f'Failed to read repositories: {e}') from e |
| 65 | + |
| 66 | + |
| 67 | +def validate_paths(paths): |
| 68 | + """Validate that paths exist and are directories.""" |
| 69 | + valid_paths = [] |
| 70 | + missing_paths = [] |
| 71 | + |
| 72 | + for path in paths: |
| 73 | + if os.path.exists(path) and os.path.isdir(path): |
| 74 | + valid_paths.append(path) |
| 75 | + else: |
| 76 | + missing_paths.append(path) |
| 77 | + |
| 78 | + return valid_paths, missing_paths |
| 79 | + |
| 80 | + |
| 81 | +def main(args=None, stdout=None, stderr=None): |
| 82 | + """Entry point for the 'delete' command.""" |
| 83 | + |
| 84 | + set_streams(stdout=stdout, stderr=stderr) |
| 85 | + parser = get_parser() |
| 86 | + args = parser.parse_args(args) |
| 87 | + |
| 88 | + try: |
| 89 | + paths = get_repository_paths(args.input, args.path) |
| 90 | + except RuntimeError as e: |
| 91 | + print(ansi('redf') + f'Error: {e}' + ansi('reset'), file=sys.stderr) |
| 92 | + return 1 |
| 93 | + |
| 94 | + if not paths: |
| 95 | + print( |
| 96 | + ansi('yellowf') + 'No repositories found to delete' + ansi('reset'), |
| 97 | + file=sys.stderr, |
| 98 | + ) |
| 99 | + return 0 |
| 100 | + |
| 101 | + # Validate paths existence |
| 102 | + valid_paths, missing_paths = validate_paths(paths) |
| 103 | + |
| 104 | + if not valid_paths: |
| 105 | + print( |
| 106 | + ansi('redf') + 'No valid directories to delete.' + ansi('reset'), |
| 107 | + file=sys.stderr, |
| 108 | + ) |
| 109 | + return 1 |
| 110 | + else: |
| 111 | + if missing_paths: |
| 112 | + print( |
| 113 | + ansi('yellowf') |
| 114 | + + 'Warning: The following directories do not exist:' |
| 115 | + + ansi('reset'), |
| 116 | + file=sys.stderr, |
| 117 | + ) |
| 118 | + for path in missing_paths: |
| 119 | + print(f' {path}', file=sys.stderr) |
| 120 | + |
| 121 | + print( |
| 122 | + ansi('cyanf') |
| 123 | + + 'The following directories will be deleted:' |
| 124 | + + ansi('reset'), |
| 125 | + file=sys.stderr, |
| 126 | + ) |
| 127 | + for path in valid_paths: |
| 128 | + print(f' {path}', file=sys.stderr) |
| 129 | + |
| 130 | + if not args.force: |
| 131 | + print( |
| 132 | + ansi('yellowf') |
| 133 | + + 'Dry-run mode: No directories were deleted. Use -f/--force to delete them.' |
| 134 | + + ansi('reset'), |
| 135 | + file=sys.stderr, |
| 136 | + ) |
| 137 | + return 0 |
| 138 | + |
| 139 | + # Actual deletion |
| 140 | + for path in valid_paths: |
| 141 | + try: |
| 142 | + rmtree(path) |
| 143 | + except OSError as e: |
| 144 | + print( |
| 145 | + ansi('redf') + f'Failed to delete {path}: {e}' + ansi('reset'), |
| 146 | + file=sys.stderr, |
| 147 | + ) |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + sys.exit(main()) |
0 commit comments