gcs.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 collections.abc import Iterator
  25. from typing import TYPE_CHECKING, BinaryIO
  26. from ..object_store import BucketBasedObjectStore
  27. from ..pack import (
  28. PACK_SPOOL_FILE_MAX_SIZE,
  29. Pack,
  30. PackData,
  31. PackIndex,
  32. load_pack_index_file,
  33. )
  34. if TYPE_CHECKING:
  35. from google.cloud.storage import Bucket
  36. # TODO(jelmer): For performance, read ranges?
  37. class GcsObjectStore(BucketBasedObjectStore):
  38. def __init__(self, bucket: "Bucket", subpath: str = "") -> None:
  39. super().__init__()
  40. self.bucket = bucket
  41. self.subpath = subpath
  42. def __repr__(self) -> str:
  43. """Return string representation of GcsObjectStore."""
  44. return f"{type(self).__name__}({self.bucket!r}, subpath={self.subpath!r})"
  45. def _remove_pack_by_name(self, name: str) -> None:
  46. self.bucket.delete_blobs(
  47. [posixpath.join(self.subpath, name) + "." + ext for ext in ["pack", "idx"]]
  48. )
  49. def _iter_pack_names(self) -> Iterator[str]:
  50. packs: dict[str, set[str]] = {}
  51. for blob in self.bucket.list_blobs(prefix=self.subpath):
  52. name, ext = posixpath.splitext(posixpath.basename(blob.name))
  53. packs.setdefault(name, set()).add(ext)
  54. for name, exts in packs.items():
  55. if exts == {".pack", ".idx"}:
  56. yield name
  57. def _load_pack_data(self, name: str) -> PackData:
  58. b = self.bucket.blob(posixpath.join(self.subpath, name + ".pack"))
  59. from typing import cast
  60. from ..file import _GitFile
  61. f = tempfile.SpooledTemporaryFile(max_size=PACK_SPOOL_FILE_MAX_SIZE)
  62. b.download_to_file(f)
  63. f.seek(0)
  64. return PackData(name + ".pack", cast(_GitFile, f))
  65. def _load_pack_index(self, name: str) -> PackIndex:
  66. b = self.bucket.blob(posixpath.join(self.subpath, name + ".idx"))
  67. f = tempfile.SpooledTemporaryFile(max_size=PACK_SPOOL_FILE_MAX_SIZE)
  68. b.download_to_file(f)
  69. f.seek(0)
  70. return load_pack_index_file(name + ".idx", f)
  71. def _get_pack(self, name: str) -> Pack:
  72. return Pack.from_lazy_objects( # type: ignore[no-untyped-call]
  73. lambda: self._load_pack_data(name), lambda: self._load_pack_index(name)
  74. )
  75. def _upload_pack(
  76. self, basename: str, pack_file: BinaryIO, index_file: BinaryIO
  77. ) -> None:
  78. idxblob = self.bucket.blob(posixpath.join(self.subpath, basename + ".idx"))
  79. datablob = self.bucket.blob(posixpath.join(self.subpath, basename + ".pack"))
  80. idxblob.upload_from_file(index_file)
  81. datablob.upload_from_file(pack_file)