def setup_client_ctx(config_dir): # Ensure that the configuration directory exists. core.svn_config_ensure(config_dir) # Fetch the configuration (and 'config' bit thereof). cfg = core.svn_config_get_config(config_dir) config = cfg.get(core.SVN_CONFIG_CATEGORY_CONFIG) # Here's the compat-sensitive part: try to use # svn_cmdline_create_auth_baton(), and fall back to making our own # if that fails. try: auth_baton = core.svn_cmdline_create_auth_baton(1, None, None, config_dir, 1, 1, config, None) except AttributeError: auth_baton = core.svn_auth_open([ client.svn_client_get_simple_provider(), client.svn_client_get_username_provider(), client.svn_client_get_ssl_server_trust_file_provider(), client.svn_client_get_ssl_client_cert_file_provider(), client.svn_client_get_ssl_client_cert_pw_file_provider(), ]) if config_dir is not None: core.svn_auth_set_parameter(auth_baton, core.SVN_AUTH_PARAM_CONFIG_DIR, config_dir) # Create, setup, and return the client context baton. ctx = client.svn_client_create_context() ctx.config = cfg ctx.auth_baton = auth_baton return ctx
def init_ra_and_client(self): """Initializes the RA and client layers, because sometimes getting unified diffs runs the remote server out of open files. """ # while we're in here we'll recreate our pool self.pool = core.Pool() if self.username: core.svn_auth_set_parameter(self.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_USERNAME, self.username) if self.password: core.svn_auth_set_parameter(self.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_PASSWORD, self.password) self.client_context = client.create_context() self.client_context.auth_baton = self.auth_baton self.client_context.config = svn_config callbacks = RaCallbacks() callbacks.auth_baton = self.auth_baton self.callbacks = callbacks try: self.ra = ra.open2(self.svn_url, callbacks, svn_config, self.pool) except SubversionException, e: # e.child contains a detailed error messages msglist = [] svn_exc = e while svn_exc: if svn_exc.args[0]: msglist.append(svn_exc.args[0]) svn_exc = svn_exc.child msg = '\n'.join(msglist) raise common.SubversionConnectionException(msg)
def init_ra_and_client(self): """Initializes the RA and client layers, because sometimes getting unified diffs runs the remote server out of open files. """ # while we're in here we'll recreate our pool self.pool = core.Pool() if self.username: core.svn_auth_set_parameter(self.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_USERNAME, self.username) if self.password: core.svn_auth_set_parameter(self.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_PASSWORD, self.password) self.client_context = client.create_context() self.client_context.auth_baton = self.auth_baton self.client_context.config = svn_config callbacks = RaCallbacks() callbacks.auth_baton = self.auth_baton self.callbacks = callbacks try: url = self.svn_url.encode('utf-8') scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) path=urllib.quote(path) url = urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) self.ra = ra.open2(url, callbacks, svn_config, self.pool) except core.SubversionException, e: raise hgutil.Abort(e.args[0])
def setup_client_ctx(config_dir): # Ensure that the configuration directory exists. core.svn_config_ensure(config_dir) # Fetch the configuration (and 'config' bit thereof). cfg = core.svn_config_get_config(config_dir) config = cfg.get(core.SVN_CONFIG_CATEGORY_CONFIG) # Here's the compat-sensitive part: try to use # svn_cmdline_create_auth_baton(), and fall back to making our own # if that fails. try: auth_baton = core.svn_cmdline_create_auth_baton( 1, None, None, config_dir, 1, 1, config, None) except AttributeError: auth_baton = core.svn_auth_open([ client.svn_client_get_simple_provider(), client.svn_client_get_username_provider(), client.svn_client_get_ssl_server_trust_file_provider(), client.svn_client_get_ssl_client_cert_file_provider(), client.svn_client_get_ssl_client_cert_pw_file_provider(), ]) if config_dir is not None: core.svn_auth_set_parameter(auth_baton, core.SVN_AUTH_PARAM_CONFIG_DIR, config_dir) # Create, setup, and return the client context baton. ctx = client.svn_client_create_context() ctx.config = cfg ctx.auth_baton = auth_baton return ctx
def open(self): # Setup the client context baton, complete with non-prompting authstuffs. # TODO: svn_cmdline_setup_auth_baton() is mo' better (when available) core.svn_config_ensure(self.config_dir) self.ctx = client.svn_client_create_context() self.ctx.auth_baton = core.svn_auth_open([ client.svn_client_get_simple_provider(), client.svn_client_get_username_provider(), client.svn_client_get_ssl_server_trust_file_provider(), client.svn_client_get_ssl_client_cert_file_provider(), client.svn_client_get_ssl_client_cert_pw_file_provider(), ]) self.ctx.config = core.svn_config_get_config(self.config_dir) if self.config_dir is not None: core.svn_auth_set_parameter(self.ctx.auth_baton, core.SVN_AUTH_PARAM_CONFIG_DIR, self.config_dir) ra_callbacks = ra.svn_ra_callbacks_t() ra_callbacks.auth_baton = self.ctx.auth_baton self.ra_session = ra.svn_ra_open(self.rootpath, ra_callbacks, None, self.ctx.config) self.youngest = ra.svn_ra_get_latest_revnum(self.ra_session) self._dirent_cache = {} self._revinfo_cache = {} # See if a universal read access determination can be made. if self.auth and self.auth.check_universal_access(self.name) == 1: self.auth = None
def open(self): # Setup the client context baton, complete with non-prompting authstuffs. # TODO: svn_cmdline_setup_auth_baton() is mo' better (when available) core.svn_config_ensure(self.config_dir) self.ctx = client.svn_client_create_context() self.ctx.auth_baton = core.svn_auth_open([ client.svn_client_get_simple_provider(), client.svn_client_get_username_provider(), client.svn_client_get_ssl_server_trust_file_provider(), client.svn_client_get_ssl_client_cert_file_provider(), client.svn_client_get_ssl_client_cert_pw_file_provider(), ]) self.ctx.config = core.svn_config_get_config(self.config_dir) if self.config_dir is not None: core.svn_auth_set_parameter(self.ctx.auth_baton, core.SVN_AUTH_PARAM_CONFIG_DIR, self.config_dir) ra_callbacks = ra.svn_ra_callbacks_t() ra_callbacks.auth_baton = self.ctx.auth_baton self.ra_session = ra.svn_ra_open(self.rootpath, ra_callbacks, None, self.ctx.config) self.youngest = ra.svn_ra_get_latest_revnum(self.ra_session) self._dirent_cache = { } self._revinfo_cache = { } # See if a universal read access determination can be made. if self.auth and self.auth.check_universal_access(self.name) == 1: self.auth = None
def svn_move(src_path, dst_path, username='', commitmsg=''): '''Move src_path to dst_path, where each is the url to a file or directory in a Subversion repository. Apply the change as username, and log with commitmsg. ''' def log_message(items, pool): '''Return a commit log message, use as a callback ''' def fname(s): return s.rstrip('/').rsplit('/', 1)[1] src_fname = fname(items[1][2]) dst_fname = fname(items[0][2]) default_msg = 'Moved %s to %s' % (src_fname, dst_fname) return commitmsg or default_msg src_path = core.svn_path_canonicalize(src_path) dst_path = core.svn_path_canonicalize(dst_path) force = False # Ignored for repository -> repository moves move_as_child = False # If dst_path exists don't attempt to move src_path # as it's child make_parents = False # Make parents of dst_path as needed (like mkdir -p) revprop_tbl = None # Use a dict of str prop: vals to set custom svn props # The move operation is coordinated by a client context, suitbly populated # To set the commit message we provide a callback that returns commitmsg client_ctx = client.create_context() client_ctx.log_msg_func3 = client.svn_swig_py_get_commit_log_func client_ctx.log_msg_baton3 = log_message # Configure minimal authentication, this is an example only auth_providers = [ client.svn_client_get_simple_provider(), client.svn_client_get_username_provider(), ] client_ctx.auth_baton = core.svn_auth_open(auth_providers) # libsvn normally infers the username from the environment the working copy # and the configuration. If requested override all that. if username is not None: core.svn_auth_set_parameter(client_ctx.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_USERNAME, username) # Move one directory or file to another location in the same repository # svn_client_move5 can mv a number of files/directories at once if dst_path # is a directory, we ignore this and pass a 1-tuple commit_info = client.svn_client_move5( (src_path, ), dst_path, force, # Ignored move_as_child, make_parents, revprop_tbl, client_ctx, ) print commit_info.revision
def svn_move(src_path, dst_path, username='', commitmsg=''): '''Move src_path to dst_path, where each is the url to a file or directory in a Subversion repository. Apply the change as username, and log with commitmsg. ''' def log_message(items, pool): '''Return a commit log message, use as a callback ''' def fname(s): return s.rstrip('/').rsplit('/', 1)[1] src_fname = fname(items[1][2]) dst_fname = fname(items[0][2]) default_msg = 'Moved %s to %s' % (src_fname, dst_fname) return commitmsg or default_msg src_path = core.svn_path_canonicalize(src_path) dst_path = core.svn_path_canonicalize(dst_path) force = False # Ignored for repository -> repository moves move_as_child = False # If dst_path exists don't attempt to move src_path # as it's child make_parents = False # Make parents of dst_path as needed (like mkdir -p) revprop_tbl = None # Use a dict of str prop: vals to set custom svn props # The move operation is coordinated by a client context, suitbly populated # To set the commit message we provide a callback that returns commitmsg client_ctx = client.create_context() client_ctx.log_msg_func3 = client.svn_swig_py_get_commit_log_func client_ctx.log_msg_baton3 = log_message # Configure minimal authentication, this is an example only auth_providers = [client.svn_client_get_simple_provider(), client.svn_client_get_username_provider(), ] client_ctx.auth_baton = core.svn_auth_open(auth_providers) # libsvn normally infers the username from the environment the working copy # and the configuration. If requested override all that. if username is not None: core.svn_auth_set_parameter(client_ctx.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_USERNAME, username) # Move one directory or file to another location in the same repository # svn_client_move5 can mv a number of files/directories at once if dst_path # is a directory, we ignore this and pass a 1-tuple commit_info = client.svn_client_move5((src_path,), dst_path, force, # Ignored move_as_child, make_parents, revprop_tbl, client_ctx, ) print commit_info.revision
def test_lock(self): self.calls = 0 self.locks = 0 self.errors = 0 def callback(path, do_lock, lock, ra_err, pool): self.calls += 1 self.assertEqual(path, "trunk/README2.txt") if lock: self.assertEqual(lock.owner, "jrandom") self.locks += 1 if ra_err: self.assert_( ra_err.apr_err == core.SVN_ERR_FS_PATH_ALREADY_LOCKED or ra_err.apr_err == core.SVN_ERR_FS_NO_SUCH_LOCK) self.errors += 1 providers = [core.svn_auth_get_username_provider()] self.callbacks.auth_baton = core.svn_auth_open(providers) core.svn_auth_set_parameter(self.callbacks.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_USERNAME, "jrandom") self.ra_ctx = ra.open2(self.repos_uri, self.callbacks, {}) rev = fs.youngest_rev(self.fs) ra.lock(self.ra_ctx, {"trunk/README2.txt": rev}, "sleutel", False, callback) self.assertEqual(self.calls, 1) self.assertEqual(self.locks, 1) self.assertEqual(self.errors, 0) self.calls = 0 self.locks = 0 ra.lock(self.ra_ctx, {"trunk/README2.txt": rev}, "sleutel", False, callback) self.assertEqual(self.calls, 1) self.assertEqual(self.locks, 0) self.assertEqual(self.errors, 1) self.calls = 0 self.errors = 0 the_lock = fs.get_lock(self.fs, "/trunk/README2.txt") ra.unlock(self.ra_ctx, {"trunk/README2.txt": the_lock.token}, False, callback) self.assertEqual(self.calls, 1) self.assertEqual(self.locks, 0) self.assertEqual(self.errors, 0) self.calls = 0 ra.unlock(self.ra_ctx, {"trunk/README2.txt": the_lock.token}, False, callback) self.assertEqual(self.calls, 1) self.assertEqual(self.locks, 0) self.assertEqual(self.errors, 1)
def test_set_parameter(self): baton = core.svn_auth_open([]) core.svn_auth_set_parameter(baton, "name", "somedata") core.svn_auth_set_parameter(baton, "name", None) core.svn_auth_set_parameter(baton, "name", 2) core.svn_auth_set_parameter(baton, "name", core.svn_auth_ssl_server_cert_info_t())
def test_set_parameter(self): baton = core.svn_auth_open([]) core.svn_auth_set_parameter(baton, b"name", b"somedata") core.svn_auth_set_parameter(baton, b"name", None) core.svn_auth_set_parameter(baton, b"name", 2) core.svn_auth_set_parameter(baton, b"name", core.svn_auth_ssl_server_cert_info_t())
def test_credentials_get_ssl_server_trust(self): def myfunc(realm, failures, cert_info, may_save, pool): self.assertEquals("somerealm", realm) ssl_trust = core.svn_auth_cred_ssl_server_trust_t() ssl_trust.accepted_failures = 0 ssl_trust.may_save = False return ssl_trust baton = core.svn_auth_open([core.svn_auth_get_ssl_server_trust_prompt_provider(myfunc)]) core.svn_auth_set_parameter(baton, core.SVN_AUTH_PARAM_SSL_SERVER_FAILURES, "2") cert_info = core.svn_auth_ssl_server_cert_info_t() core.svn_auth_set_parameter(baton, core.SVN_AUTH_PARAM_SSL_SERVER_CERT_INFO, cert_info) creds = core.svn_auth_first_credentials(core.SVN_AUTH_CRED_SSL_SERVER_TRUST, "somerealm", baton) self.assert_(creds is not None)
def test_lock(self): self.calls = 0 self.locks = 0 self.errors = 0 def callback(path, do_lock, lock, ra_err, pool): self.calls += 1 self.assertEqual(path, "trunk/README2.txt") if lock: self.assertEqual(lock.owner, "jrandom") self.locks += 1 if ra_err: self.assert_(ra_err.apr_err == core.SVN_ERR_FS_PATH_ALREADY_LOCKED or ra_err.apr_err == core.SVN_ERR_FS_NO_SUCH_LOCK) self.errors += 1 providers = [core.svn_auth_get_username_provider()] self.callbacks.auth_baton = core.svn_auth_open(providers) core.svn_auth_set_parameter(self.callbacks.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_USERNAME, "jrandom") self.ra_ctx = ra.open2(self.repos_uri, self.callbacks, {}) rev = fs.youngest_rev(self.fs) ra.lock(self.ra_ctx, {"trunk/README2.txt":rev}, "sleutel", False, callback) self.assertEqual(self.calls, 1) self.assertEqual(self.locks, 1) self.assertEqual(self.errors, 0) self.calls = 0 self.locks = 0 ra.lock(self.ra_ctx, {"trunk/README2.txt":rev}, "sleutel", False, callback) self.assertEqual(self.calls, 1) self.assertEqual(self.locks, 0) self.assertEqual(self.errors, 1) self.calls = 0 self.errors = 0 the_lock = fs.get_lock(self.fs, "/trunk/README2.txt") ra.unlock(self.ra_ctx, {"trunk/README2.txt":the_lock.token}, False, callback) self.assertEqual(self.calls, 1) self.assertEqual(self.locks, 0) self.assertEqual(self.errors, 0) self.calls = 0 ra.unlock(self.ra_ctx, {"trunk/README2.txt":the_lock.token}, False, callback) self.assertEqual(self.calls, 1) self.assertEqual(self.locks, 0) self.assertEqual(self.errors, 1)
def test_credentials_get_ssl_server_trust(self): def myfunc(realm, failures, cert_info, may_save, pool): self.assertEqual(b"somerealm", realm) ssl_trust = core.svn_auth_cred_ssl_server_trust_t() ssl_trust.accepted_failures = 0 ssl_trust.may_save = False return ssl_trust baton = core.svn_auth_open([core.svn_auth_get_ssl_server_trust_prompt_provider(myfunc)]) core.svn_auth_set_parameter(baton, core.SVN_AUTH_PARAM_SSL_SERVER_FAILURES, b"2") cert_info = core.svn_auth_ssl_server_cert_info_t() core.svn_auth_set_parameter(baton, core.SVN_AUTH_PARAM_SSL_SERVER_CERT_INFO, cert_info) creds = core.svn_auth_first_credentials( core.SVN_AUTH_CRED_SSL_SERVER_TRUST, b"somerealm", baton) self.assertTrue(creds is not None)
def __init__(self, config_dir=None): core.svn_config_ensure(config_dir) self.ctx = client.ctx_t() self.ctx.auth_baton = core.svn_auth_open([ client.get_simple_provider(), client.get_username_provider(), client.get_ssl_server_trust_file_provider(), client.get_ssl_client_cert_file_provider(), client.get_ssl_client_cert_pw_file_provider(), ]) self.ctx.config = core.svn_config_get_config(config_dir) if config_dir is not None: core.svn_auth_set_parameter(self.ctx.auth_baton, core.SVN_AUTH_PARAM_CONFIG_DIR, config_dir) self.ra_callbacks = ra.callbacks_t() self.ra_callbacks.auth_baton = self.ctx.auth_baton self.base_optrev = make_optrev('BASE')
def init_ra_and_client(self): """Initializes the RA and client layers, because sometimes getting unified diffs runs the remote server out of open files. """ # while we're in here we'll recreate our pool self.pool = core.Pool() if self.username: core.svn_auth_set_parameter(self.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_USERNAME, self.username) if self.password: core.svn_auth_set_parameter(self.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_PASSWORD, self.password) self.client_context = client.create_context() self.client_context.auth_baton = self.auth_baton self.client_context.config = svn_config callbacks = RaCallbacks() callbacks.auth_baton = self.auth_baton self.callbacks = callbacks try: self.ra = ra.open2(self.svn_url, callbacks, svn_config, self.pool) except SubversionException, e: if e.apr_err == core.SVN_ERR_RA_SERF_SSL_CERT_UNTRUSTED: msg = ('Subversion does not trust the SSL certificate for this ' 'site; please try running \'svn ls %s\' first.' % self.svn_url) elif e.apr_err == core.SVN_ERR_RA_DAV_REQUEST_FAILED: msg = ('Failed to open Subversion repository; please try ' 'running \'svn ls %s\' for details.' % self.svn_url) else: msg = e.args[0] for k, v in vars(core).iteritems(): if k.startswith('SVN_ERR_') and v == e.apr_err: msg = '%s (%s)' % (msg, k) break raise common.SubversionConnectionException(msg)
def init_ra_and_client(self): """Initializes the RA and client layers, because sometimes getting unified diffs runs the remote server out of open files. """ # while we're in here we'll recreate our pool self.pool = core.Pool() if self.username: core.svn_auth_set_parameter(self.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_USERNAME, self.username) if self.password: core.svn_auth_set_parameter(self.auth_baton, core.SVN_AUTH_PARAM_DEFAULT_PASSWORD, self.password) self.client_context = client.create_context() self.client_context.auth_baton = self.auth_baton self.client_context.config = svn_config callbacks = RaCallbacks() callbacks.auth_baton = self.auth_baton self.callbacks = callbacks try: self.ra = ra.open2(self.svn_url, callbacks, svn_config, self.pool) except SubversionException, e: if e.apr_err == core.SVN_ERR_RA_SERF_SSL_CERT_UNTRUSTED: msg = ( 'Subversion does not trust the SSL certificate for this ' 'site; please try running \'svn ls %s\' first.' % self.svn_url) elif e.apr_err == core.SVN_ERR_RA_DAV_REQUEST_FAILED: msg = ('Failed to open Subversion repository; please try ' 'running \'svn ls %s\' for details.' % self.svn_url) else: msg = e.args[0] for k, v in vars(core).iteritems(): if k.startswith('SVN_ERR_') and v == e.apr_err: msg = '%s (%s)' % (msg, k) break raise common.SubversionConnectionException(msg)
def test_set_parameter(self): baton = core.svn_auth_open([]) core.svn_auth_set_parameter(baton, "name", "somedata")