Skip to content

Commit

Permalink
add custom command
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-thomas committed Jun 23, 2013
1 parent 8fc49d3 commit f9ac71b
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 0 deletions.
6 changes: 6 additions & 0 deletions scripts/vcs-custom
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python

import sys
from vcstool.commands.custom 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 @@ -34,6 +34,7 @@
'console_scripts': [
'vcs = vcstool.commands.vcs:main',
'vcs-branch = vcstool.commands.branch:main',
'vcs-custom = vcstool.commands.custom:main',
'vcs-diff = vcstool.commands.diff:main',
'vcs-export = vcstool.commands.export:main',
'vcs-help = vcstool.commands.help:main',
Expand Down
4 changes: 4 additions & 0 deletions vcstool/clients/bzr.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def __init__(self, path):
def branch(self, _command):
return self._get_parent_branch()

def custom(self, command):
cmd = [BzrClient._executable] + command.args
return self._run_command(cmd)

def diff(self, _command):
cmd = [BzrClient._executable, 'diff']
return self._run_command(cmd)
Expand Down
4 changes: 4 additions & 0 deletions vcstool/clients/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def branch(self, _command):
cmd = [GitClient._executable, 'branch']
return self._run_command(cmd)

def custom(self, command):
cmd = [GitClient._executable] + command.args
return self._run_command(cmd)

def diff(self, command):
cmd = [GitClient._executable, 'diff']
self._check_color(cmd)
Expand Down
4 changes: 4 additions & 0 deletions vcstool/clients/hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def branch(self, _command):
cmd = [HgClient._executable, 'branch']
return self._run_command(cmd)

def custom(self, command):
cmd = [HgClient._executable] + command.args
return self._run_command(cmd)

def diff(self, command):
cmd = [HgClient._executable, 'diff']
self._check_color(cmd)
Expand Down
4 changes: 4 additions & 0 deletions vcstool/clients/svn.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def branch(self, _command):
'returncode': 0,
}

def custom(self, command):
cmd = [SvnClient._executable] + command.args
return self._run_command(cmd)

def diff(self, command):
cmd = [SvnClient._executable, 'diff']
if command.context:
Expand Down
2 changes: 2 additions & 0 deletions vcstool/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .branch import BranchCommand
from .custom import CustomCommand
from .diff import DiffCommand
from .export import ExportCommand
from .import_ import ImportCommand
Expand All @@ -10,6 +11,7 @@

vcstool_commands = []
vcstool_commands.append(BranchCommand)
vcstool_commands.append(CustomCommand)
vcstool_commands.append(DiffCommand)
vcstool_commands.append(ExportCommand)
vcstool_commands.append(ImportCommand)
Expand Down
77 changes: 77 additions & 0 deletions vcstool/commands/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import argparse
import sys

from vcstool.clients import vcstool_clients
from vcstool.crawler import find_repositories
from vcstool.executor import execute_jobs, generate_jobs, output_repositories, output_results

from .command import add_common_arguments, Command


class CustomCommand(Command):

command = 'custom'
help = 'Run a custom command'

def __init__(self, args):
super(CustomCommand, self).__init__(args)
self.args = args.args


def get_parser():
parser = argparse.ArgumentParser(description='Run a custom command', prog='vcs custom')
group = parser.add_argument_group('"custom" command parameters restricting the repositories')
for client_type in [c.type for c in vcstool_clients if c.type not in ['tar']]:
group.add_argument('--%s' % client_type, action='store_true', default=False, help="Run command on '%s' repositories" % client_type)
group = parser.add_argument_group('"custom" command parameters')
group.add_argument('--args', required=True, nargs='*', help='Arbitrary arguments passed to each vcs invocation. '
'It must be passed after other arguments since it collects all following options.')
return parser


def main(args=None):
parser = get_parser()
add_common_arguments(parser)

# separate anything followed after --args to not confuse argparse
if args is None:
args = sys.argv[1:]
try:
index = args.index('--args') + 1
except ValueError:
# should generate error due to missing --args
parser.parse_known_args(args)

client_args = args[index:]
args = parser.parse_args(args[0:index])
args.args = client_args

# check if any client type is specified
any_client_type = False
for client in vcstool_clients:
if client.type in args and args.__dict__[client.type]:
any_client_type = True
break
# if no client type is specified enable all client types
if not any_client_type:
for client in vcstool_clients:
if client.type in args:
args.__dict__[client.type] = True

command = CustomCommand(args)

# filter repositories by specified client types
clients = find_repositories(command.paths)
clients = [c for c in clients if c.type in args and args.__dict__[c.type]]

if command.output_repos:
output_repositories(clients)
jobs = generate_jobs(clients, command)
results = execute_jobs(jobs, show_progress=True)

output_results(results)
return 0


if __name__ == '__main__':
sys.exit(main())

0 comments on commit f9ac71b

Please sign in to comment.