gcs.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # object_store.py -- Object store for git objects
  2. # Copyright (C) 2021 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Storage of repositories on GCS."""
  22. import posixpath
  23. import tempfile
  24. from ..object_store import BucketBasedObjectStore
  25. from ..pack import PACK_SPOOL_FILE_MAX_SIZE, Pack, PackData, load_pack_index_file
  26. # TODO(jelmer): For performance, read ranges?
  27. class GcsObjectStore(BucketBasedObjectStore):
  28. def __init__(self, bucket, subpath="") -> None:
  29. super().__init__()
  30. self.bucket = bucket
  31. self.subpath = subpath
  32. def __repr__(self) -> str:
  33. return f"{type(self).__name__}({self.bucket!r}, subpath={self.subpath!r})"
  34. def _remove_pack(self, name) -> None:
  35. self.bucket.delete_blobs(
  36. [posixpath.join(self.subpath, name) + "." + ext for ext in ["pack", "idx"]]
  37. )
  38. def _iter_pack_names(self):
  39. packs = {}
  40. for blob in self.bucket.list_blobs(prefix=self.subpath):
  41. name, ext = posixpath.splitext(posixpath.basename(blob.name))
  42. packs.setdefault(name, set()).add(ext)
  43. for name, exts in packs.items():
  44. if exts == {".pack", ".idx"}:
  45. yield name
  46. def _load_pack_data(self, name):
  47. b = self.bucket.blob(posixpath.join(self.subpath, name + ".pack"))
  48. f = tempfile.SpooledTemporaryFile(max_size=PACK_SPOOL_FILE_MAX_SIZE)
  49. b.download_to_file(f)
  50. f.seek(0)
  51. return PackData(name + ".pack", f)
  52. def _load_pack_index(self, name):
  53. b = self.bucket.blob(posixpath.join(self.subpath, name + ".idx"))
  54. f = tempfile.SpooledTemporaryFile(max_size=PACK_SPOOL_FILE_MAX_SIZE)
  55. b.download_to_file(f)
  56. f.seek(0)
  57. return load_pack_index_file(name + ".idx", f)
  58. def _get_pack(self, name):
  59. return Pack.from_lazy_objects(
  60. lambda: self._load_pack_data(name), lambda: self._load_pack_index(name)
  61. )
  62. def _upload_pack(self, basename, pack_file, index_file) -> None:
  63. idxblob = self.bucket.blob(posixpath.join(self.subpath, basename + ".idx"))
  64. datablob = self.bucket.blob(posixpath.join(self.subpath, basename + ".pack"))
  65. idxblob.upload_from_file(index_file)
  66. datablob.upload_from_file(pack_file)