Skip to content

Commit

Permalink
make repository non-required for ArchiveService, add bulk delete to A…
Browse files Browse the repository at this point in the history
…rchiveService, remove default value from paths
  • Loading branch information
nora-codecov committed Dec 28, 2024
1 parent 96d0b0c commit 2331c4a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
20 changes: 17 additions & 3 deletions shared/api_archive/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def __init__(self, repository, ttl=None):
self.ttl = ttl or int(get_config("services", "minio", "ttl", default=self.ttl))

self.storage = StorageService()
self.storage_hash = self.get_archive_hash(repository)
if repository:
self.storage_hash = self.get_archive_hash(repository)
else:
self.storage_hash = None

@classmethod
def get_archive_hash(cls, repository) -> str:
Expand Down Expand Up @@ -98,6 +101,8 @@ def write_json_data_to_storage(
*,
encoder=ReportEncoder,
):
if not self.storage_hash:
raise ValueError("No hash key provided")
if commit_id is None:
# Some classes don't have a commit associated with them
# For example Pull belongs to multiple commits.
Expand Down Expand Up @@ -147,20 +152,29 @@ def read_file(self, path: str) -> str:
return contents.decode()

@sentry_sdk.trace
def delete_file(self, path):
def delete_file(self, path: str) -> None:
"""
Generic method to delete a file from the archive.
"""
self.storage.delete_file(self.root, path)

@sentry_sdk.trace
def delete_files(self, paths: list[str]) -> None:
"""
Generic method to delete files from the archive.
"""
self.storage.delete_files(bucket_name=self.root, paths=paths)

def read_chunks(self, commit_sha: str) -> str:
"""
Convenience method to read a chunks file from the archive.
"""
if not self.storage_hash:
raise ValueError("No hash key provided")
path = MinioEndpoints.chunks.get_path(
version="v4", repo_hash=self.storage_hash, commitid=commit_sha
)
return self.read_file(path)

def create_presigned_put(self, path):
def create_presigned_put(self, path: str) -> str:
return self.storage.create_presigned_put(self.root, path, self.ttl)
2 changes: 1 addition & 1 deletion shared/storage/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def delete_file(self, bucket_name, path):
except ClientError:
raise

def delete_files(self, bucket_name, paths=[]):
def delete_files(self, bucket_name: str, paths: list[str]) -> list[bool]:
"""Batch deletes a list of files from a given bucket
Note:
Expand Down
2 changes: 1 addition & 1 deletion shared/storage/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def delete_file(self, bucket_name, path):
second_deletion = self.fallback_service.delete_file(bucket_name, path)
return first_deletion and second_deletion

def delete_files(self, bucket_name, paths=[]):
def delete_files(self, bucket_name: str, paths: list[str]) -> list[bool]:
"""Batch deletes a list of files from a given bucket
(what happens to the files that don't exist?)
Expand Down
2 changes: 1 addition & 1 deletion shared/storage/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def delete_file(self, bucket_name, path):
raise FileNotInStorageError()
return True

def delete_files(self, bucket_name, paths=[]):
def delete_files(self, bucket_name: str, paths: list[str]) -> list[bool]:
"""Batch deletes a list of files from a given bucket
(what happens to the files that don't exist?)
Expand Down
12 changes: 6 additions & 6 deletions shared/storage/minio.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,21 @@ def read_file(
deletion, returns a ResponseError otherwise.
"""

def delete_file(self, bucket_name, url):
def delete_file(self, bucket_name: str, path: str) -> bool:
try:
# delete a file given a bucket name and a url
self.minio_client.remove_object(bucket_name, url)
# delete a file given a bucket name and a path
self.minio_client.remove_object(bucket_name, path)
return True
except MinioException:
raise

def delete_files(self, bucket_name, urls=[]):
def delete_files(self, bucket_name: str, paths: list[str]) -> list[bool]:
try:
for del_err in self.minio_client.remove_objects(
bucket_name, [DeleteObject(url) for url in urls]
bucket_name, [DeleteObject(path) for path in paths]
):
print("Deletion error: {}".format(del_err))
return [True] * len(urls)
return [True] * len(paths)
except MinioException:
raise

Expand Down

0 comments on commit 2331c4a

Please sign in to comment.