-
Notifications
You must be signed in to change notification settings - Fork 1
TransferUtils #288
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
Open
willronchetti
wants to merge
8
commits into
master
Choose a base branch
from
transfer_utils
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
TransferUtils #288
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
210dd43
in progress
willronchetti 3111b87
Merge branch 'master' into transfer_utils
willronchetti ac98067
version, type hinting
willronchetti c59fa0d
finish initial pass + test
willronchetti 46679d4
merge master
willronchetti ca58011
fix module decl
willronchetti b6247ff
build out some remaining functionality
willronchetti 8eb492b
basic test
willronchetti 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import os | ||
| import subprocess | ||
| import concurrent.futures | ||
| from typing import Optional | ||
| from .env_utils import is_cgap_env | ||
| from .creds_utils import CGAPKeyManager, SMaHTKeyManager | ||
| from .ff_utils import search_metadata, get_download_url, get_metadata, patch_metadata | ||
| from .misc_utils import PRINT | ||
|
|
||
|
|
||
| class TransferUtilsError(Exception): | ||
| pass | ||
|
|
||
|
|
||
| class Downloader: | ||
| CURL = 'curl' | ||
| WGET = 'wget' | ||
| RCLONE = 'rclone' | ||
| GLOBUS = 'globus' | ||
| VALID_DOWNLOADERS = [ | ||
| CURL, WGET, RCLONE, GLOBUS | ||
| ] | ||
|
|
||
|
|
||
| class TransferUtils: | ||
| """ Utility class for downloading files to a local system """ | ||
| O2_PATH_FIELD = 'o2_path' # set this field on files to indicate download location on o2 | ||
|
|
||
| def __init__(self, *, ff_env, num_processes=8, download_path='./', downloader=Downloader.CURL): | ||
| """ Builds the TransferUtils object, initializing Auth etc """ | ||
| self.num_processes = num_processes | ||
| self.download_path = download_path | ||
| if downloader not in Downloader.VALID_DOWNLOADERS: | ||
| raise TransferUtilsError(f'Passed invalid/unsupported downloader to TransferUtils: {downloader}') | ||
| self.downloader = downloader.lower() | ||
| self.key = (CGAPKeyManager().get_keydict_for_env(ff_env) if is_cgap_env else | ||
| SMaHTKeyManager().get_keydict_for_env(ff_env)) | ||
|
|
||
| def initialize_download_path(self): | ||
| """ Creates dirs down to the path if they do not exist """ | ||
| if not os.path.exists(self.download_path): | ||
| os.makedirs(self.download_path) | ||
|
|
||
| def extract_file_download_urls_from_search(self, search: str) -> dict: | ||
| """ Returns dictionary mapping file names to URLs from a File search """ | ||
| mapping = {} | ||
| for file_item in search_metadata(search, key=self.key): | ||
| filename = file_item['accession'] | ||
| try: | ||
| download_url = get_download_url(file_item['@id']) | ||
| except Exception as e: | ||
| PRINT(f'Could not retrieve download link for {filename} - is it a file type?') | ||
| mapping[filename] = e | ||
| continue | ||
| # this check may need to be revised in case we start sending folks other places | ||
| if '.s3.amazonaws.com' not in download_url: | ||
| PRINT(f'Potentially bad URL retrieved back from application: {download_url} - continuing') | ||
| mapping[filename] = download_url | ||
| return mapping | ||
|
|
||
| def get_exiting_o2_path(self, atid: str) -> Optional[str]: | ||
| """ Does a GET for a file and checks if there is an existing O2 path, if so return the path, else | ||
| return None | ||
| """ | ||
| # ensure minimal but up to date view | ||
| meta = get_metadata(atid, key=self.key, add_on='?datastore=database&frame=raw') | ||
| if self.O2_PATH_FIELD in meta: | ||
| return True | ||
| return False | ||
|
|
||
| def delete_existing_o2_path(self, atid: str) -> dict: | ||
| """ Deletes an existing download path from a file - do this if it has been removed and you want it | ||
| tracked or you need to re-trigiger a download """ | ||
| return patch_metadata({}, f'{atid}?delete_fields={self.O2_PATH_FIELD}') | ||
|
|
||
| def patch_location_to_portal(self, atid: str, file_path: str) -> bool: | ||
| """ Patches a special field to atid indicating it is redundantly stored at file_path """ | ||
| path = self.get_exiting_o2_path(atid) | ||
| if path and file_path != path: | ||
| PRINT(f'WARNING: patching a new path for file {atid} - ensure file is not present at {path}\n' | ||
| f'new path: {file_path}\n' | ||
| f'Delete o2_path from existing file in order to proceed') | ||
| return False | ||
| elif path and file_path == path: | ||
| PRINT(f'WARNING: potentially triggering duplicate download of {atid} - double check it has not already ' | ||
| f'been downloaded to {path} - if it has remove the o2_path before calling again.') | ||
| return False | ||
| else: | ||
| patch_metadata({self.O2_PATH_FIELD: file_path}, atid) | ||
| return True | ||
|
|
||
| @staticmethod | ||
| def download_curl(url: str, filename: str) -> str: | ||
| """ Downloads from url under filename at the download path using curl """ | ||
| subprocess.run(['curl', '-L', url, '-o', filename], check=True) | ||
| return filename | ||
|
|
||
| @staticmethod | ||
| def download_wget(url: str, filename: str) -> str: | ||
| """ Downloads from url under filename at the download path using wget """ | ||
| subprocess.run(['wget', '-q', url, '-O', filename], check=True) | ||
| return filename | ||
|
|
||
| @staticmethod | ||
| def download_rclone(url: str, filename: str) -> str: | ||
| """ Downloads from url under filename at the download path using rclone """ | ||
| subprocess.run(['rclone', 'copy', url, filename], check=True) | ||
| return filename | ||
|
|
||
| @staticmethod | ||
| def download_globus(url: str, filename: str) -> str: | ||
| """ Downloads from url under filename at the download path using curl """ | ||
| subprocess.run(['globus', 'transfer', 'download', url, filename], check=True) | ||
| return filename | ||
|
|
||
| def download_file(self, url: str, filename: str) -> str: | ||
| """ Entrypoint for general download, will select appropriate downloader depending on what was | ||
| passed to init | ||
| """ | ||
| filename = os.path.join(self.download_path, filename) | ||
| if self.downloader == Downloader.CURL: | ||
| return self.download_curl(url, filename) | ||
| elif self.downloader == Downloader.WGET: | ||
| return self.download_wget(url, filename) | ||
| elif self.downloader == Downloader.GLOBUS: | ||
| return self.download_globus(url, filename) | ||
| else: # rclone | ||
| return self.download_rclone(url, filename) | ||
|
|
||
| def parallel_download(self, filename_to_url_mapping: dict) -> list: | ||
| """ Executes a parallel download given the result of extract_file_download_urls_from_search """ | ||
| download_files = [] | ||
| with concurrent.futures.ProcessPoolExecutor(max_workers=self.num_processes) as executor: | ||
| for filename, download_url in filename_to_url_mapping.items(): | ||
| results = list(executor.map(self.download_file, download_url, filename)) | ||
|
|
||
| for result in results: | ||
| if result is not None: | ||
| download_files.append(result) | ||
| return download_files |
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,6 +1,6 @@ | ||
| [tool.poetry] | ||
| name = "dcicutils" | ||
| version = "7.13.0" | ||
| version = "7.14.0" | ||
| description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources" | ||
| authors = ["4DN-DCIC Team <[email protected]>"] | ||
| license = "MIT" | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import pytest | ||
| from contextlib import contextmanager | ||
| from dcicutils import transfer_utils | ||
| from dcicutils.transfer_utils import TransferUtils | ||
| from unittest import mock | ||
|
|
||
|
|
||
| # TODO: centralize below utilities elsewhere | ||
| def create_dummy_keydict(): | ||
| return {'cgap-dummy': { | ||
| 'key': 'dummy', 'secret': 'dummy', | ||
| 'server': 'cgap-test.com' | ||
| }} | ||
|
|
||
|
|
||
| class CGAPKeyManager: | ||
| def get_keydict_for_env(self, keys_file=None): | ||
| return create_dummy_keydict()['cgap-dummy'] | ||
|
|
||
|
|
||
| @contextmanager | ||
| def mock_key_manager(): | ||
| with mock.patch.object(transfer_utils, 'CGAPKeyManager', new=CGAPKeyManager): | ||
| yield | ||
|
|
||
|
|
||
| class TestTransferUtils: | ||
| """ Tests some basic functionality with mocks for TransferUtils """ | ||
|
|
||
| def test_transfer_utils_basic(self): | ||
| """ Tests that we can instantiate an object with defaults """ | ||
| with mock_key_manager(): | ||
| ts = TransferUtils(ff_env='cgap-dummy') | ||
| assert ts.downloader == 'curl' | ||
| assert ts.download_path == './' |
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.
Should this check for a 301?
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.
Given the
@@downloadat the end it will throw exception prior either way if it doesn't find it, so not sure there is a huge point (there is no scenario where 301 isn't returned and this code is reached, it would have thrown exception prior to this point)