Skip to content
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

Files missing in upload clearly marked as such #510

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions viewer/download_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import uuid
import zipfile
from datetime import datetime, timedelta, timezone
from io import BytesIO
from pathlib import Path
from typing import Any, Dict

Expand Down Expand Up @@ -139,21 +140,21 @@ def _add_file_to_zip(ziparchive, param, filepath):
Returns:
[boolean]: [True of record added]
"""
logger.debug('+_add_file_to_zip: %s, %s', param, filepath)
if not filepath:
# Odd - assume success
logger.error('No filepath value')
return True

fullpath = os.path.join(settings.MEDIA_ROOT, filepath)

cleaned_filename = clean_filename(filepath)
archive_path = os.path.join(_ZIP_FILEPATHS[param], cleaned_filename)
if os.path.isfile(fullpath):
cleaned_filename = clean_filename(filepath)
ziparchive.write(
fullpath, os.path.join(_ZIP_FILEPATHS[param], cleaned_filename)
)
ziparchive.write(fullpath, archive_path)
return True
else:
logger.warning('filepath "%s" is not a file', filepath)
_add_empty_file(ziparchive, archive_path)

return False

Expand All @@ -165,6 +166,17 @@ def _is_mol_or_sdf(path):
return Path(path).suffix.lower() in ('.sdf', '.mol')


def _add_empty_file(ziparchive, archive_path):
"""When file is missing, add an empty file to the archive.


Used to send an explicit signal to the downloader that the file is
missing.
"""
logger.debug('+_add_empty_file: %s', archive_path)
ziparchive.writestr(f'{archive_path}_FILE_NOT_IN_UPLOAD', BytesIO(b'').getvalue())


def _read_and_patch_molecule_name(path, molecule_name=None):
"""Patches the source file (expected to be a MOL or SDF file)
by adding the molecule name (the basename of the file) and returning the
Expand Down Expand Up @@ -218,6 +230,7 @@ def _add_file_to_zip_aligned(ziparchive, code, filepath):
Returns:
[boolean]: [True of record added to archive]
"""
logger.debug('+_add_file_to_zip_aligned: %s, %s', code, filepath)
if not filepath:
# Odd - assume success
logger.error('No filepath value')
Expand All @@ -229,9 +242,9 @@ def _add_file_to_zip_aligned(ziparchive, code, filepath):
except AttributeError:
filepath = str(Path(settings.MEDIA_ROOT).joinpath(filepath))

# strip off the leading parts of path
archive_path = str(Path(*Path(filepath).parts[7:]))
if Path(filepath).is_file():
# strip off the leading parts of path
archive_path = str(Path(*Path(filepath).parts[7:]))
if _is_mol_or_sdf(filepath):
# It's a MOL or SD file.
# Read and (potentially) adjust the file
Expand All @@ -244,6 +257,7 @@ def _add_file_to_zip_aligned(ziparchive, code, filepath):
return True
else:
logger.warning('filepath "%s" is not a file', filepath)
_add_empty_file(ziparchive, archive_path)

return False

Expand Down Expand Up @@ -370,16 +384,19 @@ def _trans_matrix_files_zip(ziparchive, target):
experiment_upload.reference_structure_transforms,
)
for tmf in trans_matrix_files:
if tmf:
filepath = Path(settings.MEDIA_ROOT).joinpath(str(tmf))
archive_path = os.path.join(
_ZIP_FILEPATHS['trans_matrix_info'],
Path(str(tmf)).name,
)
if filepath.is_file():
ziparchive.write(
Path(settings.MEDIA_ROOT).joinpath(str(tmf)),
os.path.join(
_ZIP_FILEPATHS['trans_matrix_info'],
Path(str(tmf)).name,
),
filepath,
archive_path,
)
else:
logger.info('File %s does not exist', Path(str(tmf)).name)
logger.warning('File %s does not exist', Path(str(tmf)).name)
_add_empty_file(ziparchive, archive_path)


def _extra_files_zip(ziparchive, target):
Expand Down Expand Up @@ -561,7 +578,7 @@ def _create_structures_zip(target, zip_contents, file_url, original_search, host
errors += 1
logger.warning('After _add_file_to_zip() errors=%s', errors)

if zip_contents['metadata_info']:
if zip_contents['trans_matrix_info']:
_trans_matrix_files_zip(ziparchive, target)

_extra_files_zip(ziparchive, target)
Expand Down
Loading