1# 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19# 20# 21import os.path, sys, tempfile 22from svn import core, repos 23from io import BytesIO 24try: 25 # Python >=3.0 26 from urllib.request import pathname2url 27except ImportError: 28 # Python <3.0 29 from urllib import pathname2url 30 31class Temper(object): 32 """Class to simplify allocation and cleanup of dummy Subversion 33 structures, such as repositories and working copies.""" 34 35 def __init__(self): 36 self._cleanup_list = [] 37 38 def __del__(self): 39 self.cleanup() 40 41 def cleanup(self): 42 """Destroy everything that was allocated so far.""" 43 for (target, clean_func) in self._cleanup_list: 44 clean_func(target) 45 self._cleanup_list = [] 46 47 def alloc_empty_dir(self, suffix = ""): 48 """Create an empty temporary directory. Returns its full path 49 in canonical internal form.""" 50 if isinstance(suffix, bytes): 51 suffix = suffix.decode('UTF-8') 52 temp_dir_name = tempfile.mkdtemp(suffix).encode('UTF-8') 53 temp_dir_name = core.svn_dirent_internal_style(temp_dir_name) 54 self._cleanup_list.append((temp_dir_name, core.svn_io_remove_dir)) 55 return temp_dir_name 56 57 def alloc_empty_repo(self, suffix = ""): 58 """Create an empty repository. Returns a tuple of its handle, path and 59 file: URI in canonical internal form.""" 60 if isinstance(suffix, bytes): 61 suffix = suffix.decode('UTF-8') 62 temp_path = tempfile.mkdtemp(suffix).encode('UTF-8') 63 repo_path = core.svn_dirent_internal_style(temp_path) 64 repo_uri = core.svn_uri_canonicalize(file_uri_for_path(temp_path)) 65 handle = repos.create(repo_path, None, None, None, None) 66 self._cleanup_list.append((repo_path, repos.svn_repos_delete)) 67 return (handle, repo_path, repo_uri) 68 69 def alloc_known_repo(self, repo_id, suffix = ""): 70 """Create a temporary repository and fill it with the contents of the 71 specified dump. repo_id is the path to the dump, relative to the script's 72 location. Returns the same as alloc_empty_repo.""" 73 dump_path = os.path.join(os.path.dirname(sys.argv[0]), repo_id) 74 (handle, repo_path, repo_uri) = self.alloc_empty_repo(suffix=suffix) 75 with open(dump_path, 'rb') as dump_fp: 76 repos.svn_repos_load_fs2(handle, dump_fp, BytesIO(), 77 repos.load_uuid_default, None, False, False, None) 78 return (handle, repo_path, repo_uri) 79 80def file_uri_for_path(path): 81 """Return the file: URI corresponding to the given path.""" 82 if not isinstance(path, str): 83 path = path.decode('UTF-8') 84 uri_path = pathname2url(path).encode('UTF-8') 85 86 # pathname2url claims to return the path part of the URI, but on Windows 87 # it returns both the authority and path parts for no reason, which 88 # means we have to trim the leading slashes to "normalize" the result. 89 return b'file:///' + uri_path.lstrip(b'/') 90