Skip to content

Commit ee80a49

Browse files
Implement rm-all command
1 parent 24b546d commit ee80a49

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

scripts/vcs-rm-all

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
from vcstool.commands.rm_all import main
6+
7+
sys.exit(main() or 0)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
'vcs-pull = vcstool.commands.pull:main',
5151
'vcs-push = vcstool.commands.push:main',
5252
'vcs-remotes = vcstool.commands.remotes:main',
53+
'vcs-rm-all = vcstool.commands.rm_all:main',
5354
'vcs-status = vcstool.commands.status:main',
5455
'vcs-svn = vcstool.commands.custom:svn_main',
5556
'vcs-validate = vcstool.commands.validate:main',

test/commands.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
branch custom diff export import log pull push remotes status validate
1+
branch custom diff export import log pull push remotes rm-all status validate

vcstool/commands/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .pull import PullCommand
88
from .push import PushCommand
99
from .remotes import RemotesCommand
10+
from .rm_all import RmAllCommand
1011
from .status import StatusCommand
1112
from .validate import ValidateCommand
1213

@@ -20,6 +21,7 @@
2021
vcstool_commands.append(PullCommand)
2122
vcstool_commands.append(PushCommand)
2223
vcstool_commands.append(RemotesCommand)
24+
vcstool_commands.append(RmAllCommand)
2325
vcstool_commands.append(StatusCommand)
2426
vcstool_commands.append(ValidateCommand)
2527

vcstool/commands/rm_all.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)