# vim: set filencoding=utf8 """ Subversion Client @author: Mike Crute (mcrute@ag.com) @organization: SoftGroup Interactive, Inc. @date: April 20, 2010 """ from urlparse import urlparse from obalie.commands import load_commands from obalie.exceptions import UnsupportedCommand from obalie.log import inject_logger from obalie.utils import run_command class Client(object): @inject_logger def __init__(self, repo_url, trust_server=True, config_dir=None, svn_command='svn', logger=None, **config): self.logger = logger self.command = svn_command self.config_dir = config_dir self.trust_server = trust_server self.additional_config = config self.verbosity = None self.commands = {} self._unpack_url(repo_url) def __getattr__(self, name): """ Proxy commands through to a command object. """ if not self.commands: self.commands = load_commands() try: cmd = self.commands[name](self) return cmd except KeyError: raise UnsupportedCommand(name) def _unpack_url(self, url): """ Parses a repository URL and loads its parts into the instance. """ parsed = urlparse(url) self.repo_url = "{0}://{1}".format(parsed.scheme, parsed.hostname) if parsed.port: self.repo_url += ":{0}".format(parsed.port) self.repo_url += parsed.path self.logger.debug('Repository URL: %r', self.repo_url) self.username = parsed.username self.password = parsed.password def _get_svn_args(self): """ Gets a string of global options for the subversion command. """ args = [] if self.username: args.append('--username={0}'.format(self.username)) if self.password: args.append('--password={0}'.format(self.password)) args.append('--no-auth-cache') if self.config_dir: args.append('--config-dir={0}'.format(self.config_dir)) if self.trust_server: args.append('--trust-server-cert') args.append('--non-interactive') for key, value in self.additional_config.items(): option = '{0}={1}'.format(key, value) args.append('--config-option={0}'.format(option)) return ' '.join(args) def run_raw_command(self, subcommand, *args): command = [self.command, self._get_svn_args(), subcommand] command = ' '.join(command + list(args)) self.logger.debug("Command: %r", command) return run_command(command)