Skip to content

Commit

Permalink
Add corrupted zip error cleanup and checksum validation
Browse files Browse the repository at this point in the history
  • Loading branch information
yma96 committed Dec 2, 2024
1 parent 6f23e57 commit 65c8328
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
3 changes: 1 addition & 2 deletions sidecarinit/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def run(env_yml):
Path(suite.local_repository).mkdir(parents=True, exist_ok=True)

if archive_name:
download.download_archive(suite.archive_api + "/" + archive_name,
suite.local_repository)
download.download_archive(suite.archive_api, suite.local_repository)
else:
logger.info('BUILD_CONFIG_ID does not exist, exit now.')

Expand Down
48 changes: 41 additions & 7 deletions sidecarinit/download.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import urllib.request
import shutil
import hashlib
import os
from zipfile import ZipFile
from zipfile import BadZipFile
import zlib
from zipfile import ZipFile, BadZipFile

def download_archive(url, repository):
build_id = os.environ.get('BUILD_CONFIG_ID')
archive_path = f"/{build_id}.zip"
archive_path = f"{build_id}.zip"
zip_path = os.path.join(repository, archive_path)

try:
url = f"{url}/{build_id}"
print(f"Trying to download archive from url {url}\n")
with urllib.request.urlopen(url) as response:
with open(repository + archive_path, 'wb') as out_file:
with open(zip_path, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
downloaded = True
except urllib.error.HTTPError as e:
Expand All @@ -22,10 +25,41 @@ def download_archive(url, repository):
print(f"{e.code}: {e.reason}\n\n{e.headers}")

if downloaded:
checksum = get_file_sha256(zip_path)
if checksum:
print(f"ZIP file SHA-256: {checksum}")

print(f"Archive is downloaded successfully, unpacking it in {repository}\n")
try:
with ZipFile(repository + archive_path) as archive_zip:
with ZipFile(zip_path) as archive_zip:
archive_zip.extractall(repository)
print("Unpacked successfully.\n")
except BadZipFile:
print("Downloaded archive can not be extracted since it's a bad zip file.\n")
except (BadZipFile, zlib.error) as e:
print(f"Error extracting zip file: {str(e)}\n")

print("Start cleaning up extracted files from local FS...")
try:
shutil.rmtree(repository)
print(f"Cleanup completed: {repository}\n")
except Exception as cleanup_e:
print(f"Error during cleanup: {str(cleanup_e)}\n")

print("Start cleaning up zip file from Archive FS...")
clean_archive_url = f"{url}/{checksum}"
print(f"URL request: {clean_archive_url}")
req = urllib.request.Request(clean_archive_url, method='DELETE')
try:
urllib.request.urlopen(req)
except Exception as e:
print(f"DELETE Archive Error: {str(e)}")

def get_file_sha256(file_path):
sha256_hash = hashlib.sha256()
try:
with open(file_path, "rb") as f:
for byte_block in iter(lambda: f.read(100 * 1024 * 1024), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
except Exception as e:
print(f"Error calculating SHA-256: {str(e)}")
return None

0 comments on commit 65c8328

Please sign in to comment.