gcs.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 PackData, Pack, load_pack_index_file
  25. # TODO(jelmer): For performance, read ranges?
  26. class GcsObjectStore(BucketBasedObjectStore):
  27. def __init__(self, bucket, subpath=''):
  28. super(GcsObjectStore, self).__init__()
  29. self.bucket = bucket
  30. self.subpath = subpath
  31. def __repr__(self):
  32. return "%s(%r, subpath=%r)" % (
  33. type(self).__name__, self.bucket, self.subpath)
  34. def _remove_pack(self, name):
  35. self.bucket.delete_blobs([
  36. posixpath.join(self.subpath, name) + '.' + ext
  37. for ext in ['pack', 'idx']])
  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 == set(['.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()
  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()
  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),
  61. lambda: self._load_pack_index(name))
  62. def _upload_pack(self, basename, pack_file, index_file):
  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)