base.py
187 lines
| 6.0 KiB
| text/x-python
|
PythonLexer
/ vcsserver / base.py
r0 | # RhodeCode VCSServer provides access to different vcs backends via network. | |||
r1327 | # Copyright (C) 2014-2024 RhodeCode GmbH | |||
r0 | # | |||
# This program is free software; you can redistribute it and/or modify | ||||
# it under the terms of the GNU General Public License as published by | ||||
# the Free Software Foundation; either version 3 of the License, or | ||||
# (at your option) any later version. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU General Public License | ||||
# along with this program; if not, write to the Free Software Foundation, | ||||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
r894 | import os | |||
r171 | import sys | |||
r1122 | import tempfile | |||
r0 | import logging | |||
r987 | import urllib.parse | |||
r0 | ||||
r1251 | from vcsserver.lib.archive_cache import get_archival_cache_store | |||
r0 | ||||
r894 | from vcsserver import exceptions | |||
from vcsserver.exceptions import NoContentException | ||||
r1072 | from vcsserver.hgcompat import archival | |||
r1249 | from vcsserver.lib.str_utils import safe_bytes | |||
r1144 | from vcsserver.lib.exc_tracking import format_exc | |||
r0 | log = logging.getLogger(__name__) | |||
r1152 | class RepoFactory: | |||
r0 | """ | |||
Utility to create instances of repository | ||||
It provides internal caching of the `repo` object based on | ||||
the :term:`call context`. | ||||
""" | ||||
r483 | repo_type = None | |||
r0 | ||||
r483 | def __init__(self): | |||
r1135 | pass | |||
r0 | ||||
def _create_config(self, path, config): | ||||
config = {} | ||||
return config | ||||
def _create_repo(self, wire, create): | ||||
raise NotImplementedError() | ||||
def repo(self, wire, create=False): | ||||
r725 | raise NotImplementedError() | |||
r106 | ||||
def obfuscate_qs(query_string): | ||||
r107 | if query_string is None: | |||
return None | ||||
r106 | parsed = [] | |||
r987 | for k, v in urllib.parse.parse_qsl(query_string, keep_blank_values=True): | |||
r106 | if k in ['auth_token', 'api_key']: | |||
v = "*****" | ||||
parsed.append((k, v)) | ||||
r107 | return '&'.join('{}{}'.format( | |||
r1114 | k, f'={v}' if v else '') for k, v in parsed) | |||
r171 | ||||
r1048 | def raise_from_original(new_type, org_exc: Exception): | |||
r171 | """ | |||
Raise a new exception type with original args and traceback. | ||||
""" | ||||
r1144 | exc_info = sys.exc_info() | |||
exc_type, exc_value, exc_traceback = exc_info | ||||
r621 | new_exc = new_type(*exc_value.args) | |||
r1048 | ||||
r621 | # store the original traceback into the new exc | |||
r1144 | new_exc._org_exc_tb = format_exc(exc_info) | |||
r171 | ||||
try: | ||||
r987 | raise new_exc.with_traceback(exc_traceback) | |||
r171 | finally: | |||
del exc_traceback | ||||
r894 | ||||
r1152 | class ArchiveNode: | |||
r894 | def __init__(self, path, mode, is_link, raw_bytes): | |||
self.path = path | ||||
self.mode = mode | ||||
self.is_link = is_link | ||||
self.raw_bytes = raw_bytes | ||||
r1122 | def store_archive_in_cache(node_walker, archive_key, kind, mtime, archive_at_path, archive_dir_name, | |||
commit_id, write_metadata=True, extra_metadata=None, cache_config=None): | ||||
r894 | """ | |||
r1135 | Function that would store generate archive and send it to a dedicated backend store | |||
r1122 | In here we use diskcache | |||
:param node_walker: a generator returning nodes to add to archive | ||||
:param archive_key: key used to store the path | ||||
:param kind: archive kind | ||||
:param mtime: time of creation | ||||
r1135 | :param archive_at_path: default '/' the path at archive was started. | |||
If this is not '/' it means it's a partial archive | ||||
r1122 | :param archive_dir_name: inside dir name when creating an archive | |||
:param commit_id: commit sha of revision archive was created at | ||||
:param write_metadata: | ||||
:param extra_metadata: | ||||
:param cache_config: | ||||
r1135 | walker should be a file walker, for example, | |||
r1122 | def node_walker(): | |||
r894 | for file_info in files: | |||
yield ArchiveNode(fn, mode, is_link, ctx[fn].data) | ||||
""" | ||||
extra_metadata = extra_metadata or {} | ||||
r1122 | ||||
d_cache = get_archival_cache_store(config=cache_config) | ||||
if archive_key in d_cache: | ||||
r1241 | reader, metadata = d_cache.fetch(archive_key) | |||
return reader.name | ||||
r1122 | ||||
archive_tmp_path = safe_bytes(tempfile.mkstemp()[1]) | ||||
log.debug('Creating new temp archive in %s', archive_tmp_path) | ||||
r894 | ||||
if kind == "tgz": | ||||
r1122 | archiver = archival.tarit(archive_tmp_path, mtime, b"gz") | |||
r894 | elif kind == "tbz2": | |||
r1122 | archiver = archival.tarit(archive_tmp_path, mtime, b"bz2") | |||
r894 | elif kind == 'zip': | |||
r1122 | archiver = archival.zipit(archive_tmp_path, mtime) | |||
r894 | else: | |||
raise exceptions.ArchiveException()( | ||||
r1072 | f'Remote does not support: "{kind}" archive type.') | |||
r894 | ||||
r1122 | for f in node_walker(commit_id, archive_at_path): | |||
r1085 | f_path = os.path.join(safe_bytes(archive_dir_name), safe_bytes(f.path).lstrip(b'/')) | |||
r1241 | ||||
r894 | try: | |||
archiver.addfile(f_path, f.mode, f.is_link, f.raw_bytes()) | ||||
except NoContentException: | ||||
# NOTE(marcink): this is a special case for SVN so we can create "empty" | ||||
r1135 | # directories which are not supported by archiver | |||
r1085 | archiver.addfile(os.path.join(f_path, b'.dir'), f.mode, f.is_link, b'') | |||
r894 | ||||
r1241 | metadata = dict([ | |||
('commit_id', commit_id), | ||||
('mtime', mtime), | ||||
]) | ||||
metadata.update(extra_metadata) | ||||
r894 | if write_metadata: | |||
r1070 | meta = [safe_bytes(f"{f_name}:{value}") for f_name, value in metadata.items()] | |||
f_path = os.path.join(safe_bytes(archive_dir_name), b'.archival.txt') | ||||
archiver.addfile(f_path, 0o644, False, b'\n'.join(meta)) | ||||
r894 | ||||
r1122 | archiver.done() | |||
r1241 | with open(archive_tmp_path, 'rb') as archive_file: | |||
add_result = d_cache.store(archive_key, archive_file, metadata=metadata) | ||||
if not add_result: | ||||
log.error('Failed to store cache for key=%s', archive_key) | ||||
r1122 | ||||
r1241 | os.remove(archive_tmp_path) | |||
r1122 | ||||
r1241 | reader, metadata = d_cache.fetch(archive_key) | |||
r1122 | ||||
r1241 | return reader.name | |||
r1089 | ||||
r1152 | class BinaryEnvelope: | |||
r1122 | def __init__(self, val): | |||
self.val = val | ||||
r1094 | ||||
r1122 | class BytesEnvelope(bytes): | |||
def __new__(cls, content): | ||||
if isinstance(content, bytes): | ||||
return super().__new__(cls, content) | ||||
else: | ||||
r1139 | raise TypeError('BytesEnvelope content= param must be bytes. Use BinaryEnvelope to wrap other types') | |||
r1094 | ||||
r1122 | ||||
class BinaryBytesEnvelope(BytesEnvelope): | ||||
pass | ||||