# Copyright (C) 2008 Lukas Lalinsky <lalinsky@gmail.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Support for integrating external commands into bzr
If you want to add new command, e.g. `bzr sed`, you can either create a script named `bzr-sed`::
#!/bin/sh bzr ls --versioned --kind=file | xargs sed -i $1
and put it somewhere to $PATH or define an alias in ~/.bazaar/bazaar.conf::
[EXTERNAL_ALIASES] sed = bzr ls --versioned --kind=file | xargs sed -i $1
In both cases you will be able to execute the command using::
$ bzr sed s/foo/bar/g """
from bzrlib import externalcommand
class ExternalAlias(externalcommand.Command):
def __init__(self, name, cmdline): self.name = lambda: name self.cmdline = cmdline
def run_argv_aliases(self, argv, alias_argv=None): import re, subprocess def _replace_arg(match): space = match.group(1) i = int(match.group(2)) - 1 try: return space + '"%s"' % argv[i].replace(r'"', r'\"') except IndexError: return space cmdline = re.sub(r'([^\\])\$(\d+)', _replace_arg, self.cmdline) subprocess.call(cmdline, shell=True)
def help(self): return 'external command: %s\n' % self.cmdline
class ExternalCommand(externalcommand.ExternalCommand):
@classmethod def find_command(cls, name): import os, sys, shlex, subprocess # Try to find an alias from bzrlib.config import GlobalConfig config = GlobalConfig() try: cmdline = config._get_parser().get_value('EXTERNAL_ALIASES', name) return ExternalAlias(name, cmdline) except KeyError: pass return None # Try to find an executable named "bzr-COMMAND" executable = 'bzr-%s' % cmd if sys.platform == 'win32': executables = [executable + '.bat', executable + '.cmd', executable + '.exe'] else: executables = [executable] for path in os.environ.get('PATH', '').split(os.pathsep): for executable in executables: f = os.path.join(path, executable) if os.path.isfile(f): return cls(f) # Use bzrlib's own ExternalCommand return externalcommand.ExternalCommand(cls, cmd)
def help(self): return 'external command from %s\n' % self.path
externalcommand.ExternalCommand = ExternalCommand
|