diff options
Diffstat (limited to 'obalie/client.py')
-rw-r--r-- | obalie/client.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/obalie/client.py b/obalie/client.py new file mode 100644 index 0000000..d943bb8 --- /dev/null +++ b/obalie/client.py | |||
@@ -0,0 +1,92 @@ | |||
1 | # vim: set filencoding=utf8 | ||
2 | """ | ||
3 | Subversion Client | ||
4 | |||
5 | @author: Mike Crute (mcrute@ag.com) | ||
6 | @organization: SoftGroup Interactive, Inc. | ||
7 | @date: April 20, 2010 | ||
8 | """ | ||
9 | |||
10 | from urlparse import urlparse | ||
11 | from obalie.commands import load_commands | ||
12 | from obalie.exceptions import UnsupportedCommand | ||
13 | from obalie.log import inject_logger | ||
14 | from obalie.utils import run_command | ||
15 | |||
16 | |||
17 | class Client(object): | ||
18 | |||
19 | @inject_logger | ||
20 | def __init__(self, repo_url, trust_server=True, config_dir=None, | ||
21 | svn_command='svn', logger=None, **config): | ||
22 | self.logger = logger | ||
23 | self.command = svn_command | ||
24 | self.config_dir = config_dir | ||
25 | self.trust_server = trust_server | ||
26 | self.additional_config = config | ||
27 | self.verbosity = None | ||
28 | self.commands = {} | ||
29 | |||
30 | self._unpack_url(repo_url) | ||
31 | |||
32 | def __getattr__(self, name): | ||
33 | """ | ||
34 | Proxy commands through to a command object. | ||
35 | """ | ||
36 | if not self.commands: | ||
37 | self.commands = load_commands() | ||
38 | |||
39 | try: | ||
40 | cmd = self.commands[name](self) | ||
41 | return cmd | ||
42 | except KeyError: | ||
43 | raise UnsupportedCommand(name) | ||
44 | |||
45 | def _unpack_url(self, url): | ||
46 | """ | ||
47 | Parses a repository URL and loads its parts into the instance. | ||
48 | """ | ||
49 | parsed = urlparse(url) | ||
50 | |||
51 | self.repo_url = "{0}://{1}".format(parsed.scheme, parsed.hostname) | ||
52 | if parsed.port: | ||
53 | self.repo_url += ":{0}".format(parsed.port) | ||
54 | self.repo_url += parsed.path | ||
55 | |||
56 | self.logger.debug('Repository URL: %r', self.repo_url) | ||
57 | |||
58 | self.username = parsed.username | ||
59 | self.password = parsed.password | ||
60 | |||
61 | def _get_svn_args(self): | ||
62 | """ | ||
63 | Gets a string of global options for the subversion command. | ||
64 | """ | ||
65 | args = [] | ||
66 | if self.username: | ||
67 | args.append('--username={0}'.format(self.username)) | ||
68 | |||
69 | if self.password: | ||
70 | args.append('--password={0}'.format(self.password)) | ||
71 | args.append('--no-auth-cache') | ||
72 | |||
73 | if self.config_dir: | ||
74 | args.append('--config-dir={0}'.format(self.config_dir)) | ||
75 | |||
76 | if self.trust_server: | ||
77 | args.append('--trust-server-cert') | ||
78 | args.append('--non-interactive') | ||
79 | |||
80 | for key, value in self.additional_config.items(): | ||
81 | option = '{0}={1}'.format(key, value) | ||
82 | args.append('--config-option={0}'.format(option)) | ||
83 | |||
84 | return ' '.join(args) | ||
85 | |||
86 | def run_raw_command(self, subcommand, *args): | ||
87 | command = [self.command, self._get_svn_args(), subcommand] | ||
88 | command = ' '.join(command + list(args)) | ||
89 | |||
90 | self.logger.debug("Command: %r", command) | ||
91 | |||
92 | return run_command(command) | ||