Skip to content

Commit

Permalink
Merge pull request #96 from MannLabs/fix-fasta-install-location
Browse files Browse the repository at this point in the history
Fix issues with file availability
  • Loading branch information
mschwoer authored Feb 20, 2025
2 parents f62a00d + e21ebd5 commit e7e23d5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
53 changes: 41 additions & 12 deletions alphamap/organisms_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,46 @@ def import_uniprot_annotation(organism: str):

return pd.read_csv(file_path)


import tempfile
def _download_file(data_path: str, file_name: str) -> str:
"""Download a file from github if not present and return its local path."""
file_path = os.path.join(data_path, file_name)
if not os.path.exists(file_path):
github_file_url = os.path.join(GITHUB_URL_DATA_FOLDER, file_name)

print(f"Downloading {github_file_url} to {file_path}..")
with urllib.request.urlopen(github_file_url) as response, \
open(file_path, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
print(".. done")
"""Download a file `file_name` from `data_path` (e.g. GitHub) if not present and return its local path.
return file_path
In case that it is not possible to download to the package-internal folder (e.g. due to permission issues),
a temporary directory will be created and used as a target for downloading.
"""
file_path = os.path.join(data_path, file_name)
temp_dir_path = os.path.join(tempfile.gettempdir(), "alphamap_temp")
temp_file_path = os.path.join(temp_dir_path, file_name)

if os.path.exists(file_path):
print(f"Using cached {file_path}")
return file_path

if os.path.exists(temp_file_path):
print(f"Using cached {temp_file_path}")
return temp_file_path

github_file_url = os.path.join(GITHUB_URL_DATA_FOLDER, file_name)

print(f"Downloading {github_file_url} ..")
with urllib.request.urlopen(github_file_url) as response:
try:
_safe_download(file_path, response)
return file_path
except Exception as e:
# in case the user has to rights to write to the package folder
print(f"Exception {e}: falling back to temporary location '{temp_file_path}'. Depending on your OS, you will need to clean up manually.")
os.makedirs(temp_dir_path, exist_ok=True)
_safe_download(temp_file_path, response)

return temp_file_path

def _safe_download(file_path: str, response) -> None:
"""Download a file from a response to a local path in a 'safe' manner by creating the destination file only after download is completed."""
# TODO find a way to clean up failed downloads
download_file_path = file_path + ".tmp"
with open(download_file_path, 'wb') as out_file:
print(f".. to {file_path} ..")
shutil.copyfileobj(response, out_file)
os.rename(download_file_path, file_path)
print(".. done")
6 changes: 3 additions & 3 deletions release/windows/build_installer_windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ pip install "dist/$WHL_NAME[stable,structuremap-stable]"
pyinstaller release/pyinstaller/alphamap.spec --distpath dist_pyinstaller --workpath build_pyinstaller -y

# copy files excluded in MANIFEST.in
New-Item -ItemType Directory -Force -Path dist_pyinstaller/alphamap/data
Copy-Item alphamap/data/*.fasta dist_pyinstaller/alphamap/data
Copy-Item alphamap/data/*.csv dist_pyinstaller/alphamap/data
New-Item -ItemType Directory -Force -Path dist_pyinstaller/alphamap_gui/_internal/alphamap/data
Copy-Item alphamap/data/*.fasta dist_pyinstaller/alphamap_gui/_internal/alphamap/data
Copy-Item alphamap/data/*.csv dist_pyinstaller/alphamap_gui/_internal/alphamap/data

0 comments on commit e7e23d5

Please sign in to comment.