def test_checks_uuid(self): repos_url = self.make_svn_repository('d') dc = self.get_commit_editor(repos_url) dc.add_dir("bp") dc.close() mapping = mapping_registry.get_default()() ra = RemoteAccess(repos_url.encode("utf-8"), auth=Auth([get_username_provider()])) revnum = ra.get_latest_revnum() revprops = { SVN_REVPROP_BZR_REPOS_UUID: "otheruuid", "svn:log": "bla", SVN_REVPROP_BZR_ROOT: "bp", SVN_REVPROP_BZR_MAPPING_VERSION: mapping.name, SVN_REVPROP_BZR_BASE_REVISION: "therealbaserevid" } dc = TestCommitEditor(ra.get_commit_editor(revprops), ra.url, revnum) dc.open_dir("bp").add_file("bp/la").modify() dc.close() repos = Repository.open(repos_url) revmeta1 = repos._revmeta_provider.get_revision(u"bp", 1) revmeta2 = repos._revmeta_provider.get_revision(u"bp", 2) self.assertEquals( mapping.revision_id_foreign_to_bzr((repos.uuid, "bp", 1)), revmeta2.get_lhs_parent_revid(mapping, revmeta1))
def Connection(url, auth=None, config=None, readonly=False): progress_cb = SubversionProgressReporter(url).update try: ret = RemoteAccess( _url_escape_uri(url), auth=auth, client_string_func=breezy.plugins.svn.get_client_string, progress_cb=progress_cb, config=config) if 'transport' in debug.debug_flags: ret = MutteringRemoteAccess(ret) if readonly: ret = ReadonlyRemoteAccess(ret) except subvertpy.SubversionException as e: msg, num = e.args if num in (subvertpy.ERR_RA_SVN_REPOS_NOT_FOUND, ): raise NoSvnRepositoryPresent(url=url) if num == subvertpy.ERR_BAD_URL: raise urlutils.InvalidURL(url) if num in (subvertpy.ERR_RA_DAV_PATH_NOT_FOUND, subvertpy.ERR_FS_NOT_FOUND): raise NoSuchFile(url) if num == subvertpy.ERR_RA_ILLEGAL_URL: raise urlutils.InvalidURL(url, msg) if num == subvertpy.ERR_RA_DAV_RELOCATED: raise convert_relocate_error(url, num, msg) raise convert_error(e) from . import lazy_check_versions lazy_check_versions() return ret
def test_remote_access_retry_failure( mocker, tmp_path, sample_repo_url, exception_to_retry ): nb_failed_calls = SVN_RETRY_MAX_ATTEMPTS mock_ra = mocker.patch("swh.loader.svn.svn.RemoteAccess") remote_access = RemoteAccess(sample_repo_url, auth=Auth([get_username_provider()])) mock_ra.side_effect = ( [exception_to_retry] * nb_failed_calls + [remote_access] + [exception_to_retry] * nb_failed_calls + [remote_access] ) mock_sleep = mocker.patch.object(SvnRepo.remote_access.retry, "sleep") with pytest.raises(type(exception_to_retry)): SvnRepo( sample_repo_url, sample_repo_url, tmp_path, max_content_length=100000, ) assert_sleep_calls(mock_sleep, mocker, nb_failed_calls - 1)
def find_root(self, uuid, url): for root in self._roots[uuid]: if url.startswith(root): return root try: from subvertpy.ra import RemoteAccess except ImportError: return None c = RemoteAccess(url) root = c.get_repos_root() self._roots[uuid].add(root) return root
def get_commit_editor(self, url, message="Test commit"): """Obtain a commit editor. :param url: URL to connect to :param message: Commit message :return: Commit editor object """ ra_ctx = RemoteAccess(url.encode("utf-8"), auth=Auth([ra.get_username_provider()])) revnum = ra_ctx.get_latest_revnum() return TestCommitEditor(ra_ctx.get_commit_editor({"svn:log": message}), ra_ctx.url, revnum)
def add_commit(repo_url: str, message: str, changes: List[CommitChange]) -> None: conn = RemoteAccess(repo_url, auth=Auth([get_username_provider()])) editor = conn.get_commit_editor({"svn:log": message}) root = editor.open_root() for change in changes: if change["change_type"] == CommitChangeType.Delete: root.delete_entry(change["path"].rstrip("/")) else: dir_change = change["path"].endswith("/") split_path = change["path"].rstrip("/").split("/") for i in range(len(split_path)): path = "/".join(split_path[0 : i + 1]) if i < len(split_path) - 1: try: root.add_directory(path).close() except SubversionException: pass else: if dir_change: try: dir = root.add_directory(path) except SubversionException: dir = root.open_directory(path) if "properties" in change: for prop, value in change["properties"].items(): dir.change_prop(prop, value) dir.close() else: try: file = root.add_file(path) except SubversionException: file = root.open_file(path) if "properties" in change: for prop, value in change["properties"].items(): file.change_prop(prop, value) if "data" in change: txdelta = file.apply_textdelta() delta.send_stream(BytesIO(change["data"]), txdelta) file.close() root.close() editor.close()
def main(local_url, svn_url, revision_start, revision_end, debug, cleanup): """Script to present how to use Replay class.""" conn = RemoteAccess(svn_url.encode("utf-8"), auth=Auth([get_username_provider()])) os.makedirs(local_url, exist_ok=True) rootpath = tempfile.mkdtemp( prefix=local_url, suffix="-" + os.path.basename(svn_url) ) rootpath = os.fsencode(rootpath) # Do not go beyond the repository's latest revision revision_end_max = conn.get_latest_revnum() if revision_end == -1: revision_end = revision_end_max revision_end = min(revision_end, revision_end_max) try: replay = Replay(conn, rootpath) for rev in range(revision_start, revision_end + 1): contents, skipped_contents, directories = replay.compute_objects(rev) print( "r%s %s (%s new contents, %s new directories)" % ( rev, hashutil.hash_to_hex(replay.directory.hash), len(contents) + len(skipped_contents), len(directories), ) ) if debug: print("%s" % rootpath.decode("utf-8")) finally: if cleanup: if os.path.exists(rootpath): shutil.rmtree(rootpath)
def test_remote_access_retry_success( mocker, tmp_path, sample_repo_url, exception_to_retry ): nb_failed_calls = 2 mock_ra = mocker.patch("swh.loader.svn.svn.RemoteAccess") remote_access = RemoteAccess(sample_repo_url, auth=Auth([get_username_provider()])) mock_ra.side_effect = ( [exception_to_retry] * nb_failed_calls + [remote_access] + [exception_to_retry] * nb_failed_calls + [remote_access] ) mock_sleep = mocker.patch.object(SvnRepo.remote_access.retry, "sleep") SvnRepo( sample_repo_url, sample_repo_url, tmp_path, max_content_length=100000, ) assert_sleep_calls(mock_sleep, mocker, nb_failed_calls)
def __init__(self, *args, **kwargs): self.actual = RemoteAccess(*args, **kwargs)
#!/usr/bin/python import cmd import subvertpy from subvertpy.ra import RemoteAccess import sys if len(sys.argv) == 1: print("Usage: %s <url>" % sys.argv) url = sys.argv[1] conn = RemoteAccess(url) def log_printer(changed_paths, rev, revprops, has_children=None): print("=" * 79) print("%d:" % rev) print("Revision properties:") for entry in revprops.items(): print(" %s: %s" % entry) print("") if changed_paths is None: return print("Changed paths:") for path, (action, from_path, from_rev) in changed_paths.items(): print(" %s (%s)" % (path, action)) class RaCmd(cmd.Cmd):
#!/usr/bin/python # Demonstrates how to use the replay function to fetch the # changes made in a revision. from subvertpy.ra import RemoteAccess, Auth, get_username_provider conn = RemoteAccess("svn://svn.gnome.org/svn/gnome-specimen/trunk", auth=Auth([get_username_provider()])) class MyFileEditor: def change_prop(self, key, value): print("Change prop: %s -> %r" % (key, value)) def apply_textdelta(self, base_checksum): # This should return a function that can receive delta windows def apply_window(x): pass return apply_window def close(self): pass class MyDirEditor: def open_directory(self, *args): print("Open dir: %s (base revnum: %r)" % args) return MyDirEditor() def add_directory(self, path, copyfrom_path=None, copyfrom_rev=-1):
#!/usr/bin/python # Demonstrates how to do a new commit using Subvertpy import os from cStringIO import StringIO from subvertpy import delta, repos from subvertpy.ra import RemoteAccess, Auth, get_username_provider # Create a repository repos.create("tmprepo") # Connect to the "remote" repository using the file transport. # Note that a username provider needs to be provided, so that Subversion # knows who to record as the author of new commits made over this connection. repo_url = "file://%s" % os.path.abspath("tmprepo") conn = RemoteAccess(repo_url, auth=Auth([get_username_provider()])) # Simple commit that adds a directory editor = conn.get_commit_editor({"svn:log": "Commit message"}) root = editor.open_root() # Add a directory dir = root.add_directory("somedir") dir.close() # Add and edit a file file = root.add_file("somefile") # Set the svn:executable attribute file.change_prop("svn:executable", "*") # Obtain a textdelta handler and send the new file contents txdelta = file.apply_textdelta() delta.send_stream(StringIO("new file contents"), txdelta) file.close()
#!/usr/bin/python # Demonstrates how to iterate over the log of a Subversion repository. from subvertpy.ra import RemoteAccess conn = RemoteAccess("svn://svn.samba.org/subvertpy/trunk") for (changed_paths, rev, revprops, has_children) in conn.iter_log(paths=None, start=0, end=conn.get_latest_revnum(), discover_changed_paths=True): print("=" * 79) print("%d:" % rev) print("Revision properties:") for entry in revprops.items(): print(" %s: %s" % entry) print("") print("Changed paths") for path, (action, from_path, from_rev, node_kind) in (changed_paths.items()): print(" %s (%s)" % (path, action))
def remote_access(self, auth: Auth) -> RemoteAccess: """Simple wrapper around subvertpy.ra.RemoteAccess creation enabling to retry the operation if a network error occurs.""" return RemoteAccess(self.remote_url, auth=auth)