Skip to content
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

Add an rm-all option to vcs #216

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,18 @@ Alternatively the ``-e/--editable`` flag of ``pip`` can be used::

# from the top level of this repo
pip3 install --user -e .


Running tests
--------------

To run the unittests locally you shuld also have the following PyPI packages
installed:

* coverage
* flake8
* flake8-import-order
* flake8-docstrings
* pytest

To run the tests, run ``pytest`` from the root of the repository.
7 changes: 7 additions & 0 deletions scripts/vcs-rm-all
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3

import sys

from vcstool.commands.rm_all import main

sys.exit(main() or 0)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion test/commands.txt
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 diff export import log pull push remotes rm-all status validate
2 changes: 2 additions & 0 deletions vcstool/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down
96 changes: 96 additions & 0 deletions vcstool/commands/rm_all.py
Original file line number Diff line number Diff line change
@@ -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())