Debian Codegraph @

hgext/convert/transport.py

raw
1 # Copyright (C) 2007 Daniel Holth <[email protected]>
2 # This is a stripped-down version of the original bzr-svn transport.py,
3 # Copyright (C) 2006 Jelmer Vernooij <[email protected]>
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 # pytype: disable=import-error
21 import svn.client
22 import svn.core
23 import svn.ra
24
25 # pytype: enable=import-error
26
27 Pool = svn.core.Pool
28 SubversionException = svn.core.SubversionException
29
30 # Some older versions of the Python bindings need to be
31 # explicitly initialized. But what we want to do probably
32 # won't work worth a darn against those libraries anyway!
33 svn.ra.initialize()
34
35 svn_config = None
36
37
38 def _create_auth_baton(pool):
39 """Create a Subversion authentication baton."""
40 import svn.client # pytype: disable=import-error
41
42 # Give the client context baton a suite of authentication
43 # providers.h
44 providers = [
45 svn.client.get_simple_provider(pool),
46 svn.client.get_username_provider(pool),
47 svn.client.get_ssl_client_cert_file_provider(pool),
48 svn.client.get_ssl_client_cert_pw_file_provider(pool),
49 svn.client.get_ssl_server_trust_file_provider(pool),
50 ]
51 # Platform-dependent authentication methods
52 getprovider = getattr(
53 svn.core, 'svn_auth_get_platform_specific_provider', None
54 )
55 if getprovider:
56 # Available in svn >= 1.6
57 for name in (b'gnome_keyring', b'keychain', b'kwallet', b'windows'):
58 for type in (b'simple', b'ssl_client_cert_pw', b'ssl_server_trust'):
59 p = getprovider(name, type, pool)
60 if p:
61 providers.append(p)
62 else:
63 if hasattr(svn.client, 'get_windows_simple_provider'):
64 providers.append(svn.client.get_windows_simple_provider(pool))
65
66 return svn.core.svn_auth_open(providers, pool)
67
68
69 class NotBranchError(SubversionException):
70 pass
71
72
73 class SvnRaTransport:
74 """
75 Open an ra connection to a Subversion repository.
76 """
77
78 def __init__(self, url=b"", ra=None):
79 self.pool = Pool()
80 self.svn_url = url
81 self.username = b''
82 self.password = b''
83
84 # Only Subversion 1.4 has reparent()
85 if ra is None or not hasattr(svn.ra, 'reparent'):
86 self.client = svn.client.create_context(self.pool)
87 ab = _create_auth_baton(self.pool)
88 self.client.auth_baton = ab
89 global svn_config
90 if svn_config is None:
91 svn_config = svn.core.svn_config_get_config(None)
92 self.client.config = svn_config
93 try:
94 self.ra = svn.client.open_ra_session(
95 self.svn_url, self.client, self.pool
96 )
97 except SubversionException as xxx_todo_changeme:
98 (inst, num) = xxx_todo_changeme.args
99 if num in (
100 svn.core.SVN_ERR_RA_ILLEGAL_URL,
101 svn.core.SVN_ERR_RA_LOCAL_REPOS_OPEN_FAILED,
102 svn.core.SVN_ERR_BAD_URL,
103 ):
104 raise NotBranchError(url)
105 raise
106 else:
107 self.ra = ra
108 svn.ra.reparent(self.ra, self.svn_url.encode('utf8'))
109
110 class Reporter:
111 def __init__(self, reporter_data):
112 self._reporter, self._baton = reporter_data
113
114 def set_path(self, path, revnum, start_empty, lock_token, pool=None):
115 svn.ra.reporter2_invoke_set_path(
116 self._reporter,
117 self._baton,
118 path,
119 revnum,
120 start_empty,
121 lock_token,
122 pool,
123 )
124
125 def delete_path(self, path, pool=None):
126 svn.ra.reporter2_invoke_delete_path(
127 self._reporter, self._baton, path, pool
128 )
129
130 def link_path(
131 self, path, url, revision, start_empty, lock_token, pool=None
132 ):
133 svn.ra.reporter2_invoke_link_path(
134 self._reporter,
135 self._baton,
136 path,
137 url,
138 revision,
139 start_empty,
140 lock_token,
141 pool,
142 )
143
144 def finish_report(self, pool=None):
145 svn.ra.reporter2_invoke_finish_report(
146 self._reporter, self._baton, pool
147 )
148
149 def abort_report(self, pool=None):
150 svn.ra.reporter2_invoke_abort_report(
151 self._reporter, self._baton, pool
152 )
153
154 def do_update(self, revnum, path, *args, **kwargs):
155 return self.Reporter(
156 svn.ra.do_update(self.ra, revnum, path, *args, **kwargs)
157 )