gcs.py 3.0 KB

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