-
-
Notifications
You must be signed in to change notification settings - Fork 1
Add icon uploading to objectstore #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+153
−80
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a60ebbe
Add icon uploading to objectstore
rbro112 3b964c9
Add icon ID creation
rbro112 07560fb
Update to use published objectstore client
rbro112 dd79f49
Update objectstore client
rbro112 2ec1e83
Fix tests
rbro112 219cbdf
Safety
rbro112 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,100 +1,78 @@ | ||
| """File utilities for app size analyzer.""" | ||
|
|
||
| import hashlib | ||
| import shutil | ||
| import tempfile | ||
|
|
||
| from enum import Enum | ||
| from io import BytesIO | ||
| from pathlib import Path | ||
| from typing import IO | ||
|
|
||
| from .logging import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| _HASH_CHUNK_SIZE = 8192 | ||
|
|
||
| def calculate_file_hash(file_path: Path, algorithm: str = "md5") -> str: | ||
| """Calculate hash of a file. | ||
| Args: | ||
| file_path: Path to the file | ||
| algorithm: Hash algorithm to use ("md5", "sha1", "sha256") | ||
|
|
||
| Returns: | ||
| Hexadecimal hash string | ||
| class IdPrefix(Enum): | ||
| ICON = "icn" | ||
| SNAPSHOT = "snap" | ||
|
|
||
| Raises: | ||
| ValueError: If algorithm is not supported | ||
| FileNotFoundError: If file doesn't exist | ||
| """ | ||
| if not file_path.exists(): | ||
| raise FileNotFoundError(f"File not found: {file_path}") | ||
|
|
||
| def _calculate_hash(data: IO[bytes], algorithm: str) -> str: | ||
| hasher = None | ||
| if algorithm == "md5": | ||
| hasher = hashlib.md5() | ||
| elif algorithm == "sha1": | ||
| hasher = hashlib.sha1() | ||
| elif algorithm == "sha256": | ||
| hasher = hashlib.sha256() | ||
| else: | ||
|
|
||
| if hasher is None: | ||
| raise ValueError(f"Unsupported hash algorithm: {algorithm}") | ||
|
|
||
| for chunk in iter(lambda: data.read(_HASH_CHUNK_SIZE), b""): | ||
| hasher.update(chunk) | ||
|
|
||
| return hasher.hexdigest() | ||
|
|
||
|
|
||
| def id_from_bytes(data: bytes, prefix: IdPrefix) -> str: | ||
| return f"{prefix.value}_{_calculate_hash(BytesIO(data), 'sha256')[:12]}" | ||
|
|
||
|
|
||
| def calculate_file_hash(file_path: Path, algorithm: str = "md5") -> str: | ||
| if not file_path.exists(): | ||
| raise FileNotFoundError(f"File not found: {file_path}") | ||
|
|
||
| try: | ||
| with open(file_path, "rb") as f: | ||
| # Read file in chunks to handle large files efficiently | ||
| for chunk in iter(lambda: f.read(8192), b""): | ||
| hasher.update(chunk) | ||
|
|
||
| return hasher.hexdigest() | ||
| return _calculate_hash(f, algorithm) | ||
| except Exception as e: | ||
| raise RuntimeError(f"Failed to calculate hash for {file_path}: {e}") | ||
|
|
||
|
|
||
| def get_file_size(file_path: Path) -> int: | ||
| """Get file size in bytes. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason a bunch of the comments in this file were removed? |
||
| Args: | ||
| file_path: Path to the file | ||
| Returns: | ||
| File size in bytes | ||
| Raises: | ||
| FileNotFoundError: If file doesn't exist | ||
| """ | ||
| if not file_path.exists(): | ||
| raise FileNotFoundError(f"File not found: {file_path}") | ||
|
|
||
| return file_path.stat().st_size | ||
|
|
||
|
|
||
| def to_nearest_block_size(file_size: int, block_size: int) -> int: | ||
| """Round file size up to the nearest filesystem block size.""" | ||
|
|
||
| if file_size == 0: | ||
| return 0 | ||
|
|
||
| return ((file_size - 1) // block_size + 1) * block_size | ||
|
|
||
|
|
||
| def create_temp_directory(prefix: str = "app-analyzer-") -> Path: | ||
| """Create a temporary directory. | ||
| Args: | ||
| prefix: Prefix for the temporary directory name | ||
| Returns: | ||
| Path to the created temporary directory | ||
| """ | ||
| temp_dir = Path(tempfile.mkdtemp(prefix=prefix)) | ||
| logger.debug(f"Created temporary directory: {temp_dir}") | ||
| return temp_dir | ||
|
|
||
|
|
||
| def cleanup_directory(directory: Path) -> None: | ||
| """Remove a directory and all its contents. | ||
| Args: | ||
| directory: Directory to remove | ||
| """ | ||
| if directory.exists() and directory.is_dir(): | ||
| shutil.rmtree(directory) | ||
| logger.debug(f"Cleaned up directory: {directory}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If for some reason objectstore_client is None (should only ever happen if the
OBJECSTORE_URLenv variable isn't set), this will gracefully fail and log.We should have everything set up properly following https://github.com/getsentry/ops/blob/bc63aef2e313ac482f1ffbca826fb9273b1aa643/k8s/services/launchpad/deployment.yaml#L34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe it should log an error not info?