-
Notifications
You must be signed in to change notification settings - Fork 8
Added option to delete repositories listed in a YAML file. #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+222
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import sys | ||
|
|
||
| from vcs2l.commands.delete import main | ||
|
|
||
| sys.exit(main() or 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| branch custom diff export import log pull push remotes status validate | ||
| branch custom delete diff export import log pull push remotes status validate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| The following directories will be deleted: | ||
| ./immutable/hash | ||
| ./immutable/hash_tar | ||
| ./immutable/hash_zip | ||
| ./immutable/tag | ||
| ./vcs2l | ||
| ./without_version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| """Command to delete directories of repositories listed in a YAML file.""" | ||
|
|
||
| import argparse | ||
| import os | ||
| import sys | ||
| import urllib.request as request | ||
| from urllib.error import URLError | ||
|
|
||
| from vcs2l.commands.import_ import file_or_url_type, get_repositories | ||
| from vcs2l.executor import ansi | ||
| from vcs2l.streams import set_streams | ||
| from vcs2l.util import rmtree | ||
|
|
||
| from .command import Command, existing_dir | ||
|
|
||
|
|
||
| class DeleteCommand(Command): | ||
| """Delete directories of repositories listed in a YAML file.""" | ||
|
|
||
| command = 'delete' | ||
| help = 'Remove the directories indicated by the list of given repositories.' | ||
|
|
||
|
|
||
| def get_parser(): | ||
| """CLI parser for the 'delete' command.""" | ||
| _cls = DeleteCommand | ||
|
|
||
| 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.add_argument( | ||
| '-f', | ||
| '--force', | ||
| action='store_true', | ||
| default=False, | ||
| help='Do the deletion instead of a dry-run', | ||
| ) | ||
| return parser | ||
|
|
||
|
|
||
| def get_repository_paths(input_source, base_path): | ||
| """Get repository paths from input source.""" | ||
| try: | ||
| if isinstance(input_source, request.Request): | ||
| input_source = request.urlopen(input_source) | ||
| repos = get_repositories(input_source) | ||
| return [os.path.join(base_path, rel_path) for rel_path in repos] | ||
| except (RuntimeError, URLError) as e: | ||
| raise RuntimeError(f'Failed to read repositories: {e}') from e | ||
|
|
||
|
|
||
| def validate_paths(paths): | ||
| """Validate that paths exist and are directories.""" | ||
| valid_paths = [] | ||
| missing_paths = [] | ||
|
|
||
| for path in paths: | ||
| if os.path.exists(path) and os.path.isdir(path): | ||
| valid_paths.append(path) | ||
| else: | ||
| missing_paths.append(path) | ||
|
|
||
| return valid_paths, missing_paths | ||
|
|
||
|
|
||
| def main(args=None, stdout=None, stderr=None): | ||
| """Entry point for the 'delete' command.""" | ||
|
|
||
| set_streams(stdout=stdout, stderr=stderr) | ||
| parser = get_parser() | ||
| args = parser.parse_args(args) | ||
|
|
||
| try: | ||
| paths = get_repository_paths(args.input, args.path) | ||
| except RuntimeError as e: | ||
| print(ansi('redf') + f'Error: {e}' + ansi('reset'), file=sys.stderr) | ||
| return 1 | ||
|
|
||
| if not paths: | ||
| print( | ||
| ansi('yellowf') + 'No repositories found to delete' + ansi('reset'), | ||
| file=sys.stderr, | ||
| ) | ||
| return 0 | ||
|
|
||
| # Validate paths existence | ||
| valid_paths, missing_paths = validate_paths(paths) | ||
|
|
||
| if not valid_paths: | ||
| print( | ||
| ansi('redf') + 'No valid directories to delete.' + ansi('reset'), | ||
| file=sys.stderr, | ||
| ) | ||
| return 1 | ||
| else: | ||
| if missing_paths: | ||
| print( | ||
| ansi('yellowf') | ||
| + 'Warning: The following directories do not exist:' | ||
| + ansi('reset'), | ||
| file=sys.stderr, | ||
| ) | ||
| for path in missing_paths: | ||
| print(f' {path}', file=sys.stderr) | ||
|
|
||
| print( | ||
| ansi('cyanf') | ||
| + 'The following directories will be deleted:' | ||
| + ansi('reset'), | ||
| file=sys.stderr, | ||
| ) | ||
| for path in valid_paths: | ||
| print(f' {path}', file=sys.stderr) | ||
|
|
||
| if not args.force: | ||
| print( | ||
| ansi('yellowf') | ||
| + 'Dry-run mode: No directories were deleted. Use -f/--force to delete them.' | ||
| + ansi('reset'), | ||
| file=sys.stderr, | ||
| ) | ||
| return 0 | ||
|
|
||
| # Actual deletion | ||
| for path in valid_paths: | ||
| try: | ||
| rmtree(path) | ||
| except OSError as e: | ||
| print( | ||
| ansi('redf') + f'Failed to delete {path}: {e}' + ansi('reset'), | ||
| file=sys.stderr, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| sys.exit(main()) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would happen if the YAML is empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You would get the following error:
This is captured from the following line in delete.py.