-
Notifications
You must be signed in to change notification settings - Fork 1
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
Implement download #1
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
33530c6
Add download capabilities
maximusunc 4a00838
Incorporate Tests model, tqdm, and instance of logger
maximusunc 2d5b7b3
Run black formatter
maximusunc 70e0c0a
Fix download suite argument type
maximusunc ccee7e7
Put the downloaded files into a temp directory that gets cleaned up
maximusunc 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 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
__pycache__ | ||
*.egg-info | ||
logs/ | ||
Translator-Tests/ |
This file contains 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,11 +1,61 @@ | ||
"""Download tests.""" | ||
import glob | ||
import httpx | ||
import io | ||
import json | ||
import logging | ||
from pathlib import Path | ||
from typing import List | ||
import tempfile | ||
from typing import List, Union | ||
import zipfile | ||
|
||
from .models import TestCase | ||
from .models import TestCase, TestSuite | ||
|
||
|
||
def download_tests(url: Path) -> List[TestCase]: | ||
def download_tests( | ||
suite: Union[str, List[str]], url: Path, logger: logging.Logger | ||
) -> List[TestCase]: | ||
"""Download tests from specified location.""" | ||
raise NotImplementedError() | ||
assert Path(url).suffix == ".zip" | ||
logger.info(f"Downloading tests from {url}...") | ||
# download file from internet | ||
with httpx.Client(follow_redirects=True) as client: | ||
tests_zip = client.get(url) | ||
tests_zip.raise_for_status() | ||
# we already checked if zip before download, so now unzip | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
with zipfile.ZipFile(io.BytesIO(tests_zip.read())) as zip_ref: | ||
zip_ref.extractall(tmpdir) | ||
|
||
# Find all json files in the downloaded zip | ||
tests_paths = glob.glob(f"{tmpdir}/**/*.json", recursive=True) | ||
|
||
all_tests = [] | ||
suites = suite if type(suite) == list else [suite] | ||
test_case_ids = [] | ||
|
||
logger.info(f"Reading in {len(tests_paths)} tests...") | ||
|
||
# do the reading of the tests and make a tests list | ||
for test_path in tests_paths: | ||
with open(test_path, "r") as f: | ||
test_json = json.load(f) | ||
try: | ||
test_suite = TestSuite.parse_obj(test_json) | ||
if test_suite.id in suites: | ||
# if suite is selected, grab all its test cases | ||
test_case_ids.extend(test_suite.case_ids) | ||
except Exception as e: | ||
# not a Test Suite | ||
pass | ||
try: | ||
test_case = TestCase.parse_obj(test_json) | ||
all_tests.append(test_case) | ||
except Exception as e: | ||
# not a Test Case | ||
pass | ||
|
||
# only return the tests from the specified suites | ||
tests = list(filter(lambda x: x in test_case_ids, all_tests)) | ||
logger.info(f"Passing along {len(tests)} tests") | ||
return tests |
This file contains 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 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 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.
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.
I've been using a file storage package called pystow - it handles compression/uncompression, storing everything in a consistent folder location that is programmatically retrievable, retrieving from URLs/FTP, refreshing the file when it changes - or not - etc. e.g. https://github.com/geneontology/gopreprocess/blob/d258d475dfe65886d39cc795e1fae78c2bab8d36/src/utils/download.py#L44
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.
That looked interesting, so I just tried it out. It looks like it uses urllib underneath and on a Mac, I get an SSL certificate verification failed error. And then I was looking at their documentation and wasn't seeing an option for ensure_zip, without knowing the specific files we want to download (
ensure_open_zip
opens a specific file in the zip). We want to download the whole thing so we can parse out the tests we want to run. Do you know how we might use pystow for our specific use-case?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.
yeah no worries, its awesome that you tried it out! :)
https://stackoverflow.com/questions/52805115/certificate-verify-failed-unable-to-get-local-issuer-certificate -- that's the Mac fix SO that has worked for me.
Definitely would still need some software to iterate through the downloaded files (or even to iterate through all files in a source URL - e.g. a folder on an FTP site), but instead of keeping track of the path amongst methods, you can always retrieve the path that pystow stores the files by querying its metadata (and then managing the download URLs/etc in a conf file).
pystow does manage zip/unzip and refresh nicely. here's a example of the file structure it creates for me:
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.
I did a little more playing around with this. I got the SSL certificate error fixed from that SO above, but there doesn't seem to be a method in pystow to download and unzip an entire zip file. The only options are to download the zip but leave it unzipped, or download it and unzip but only save a specific file inside, which doesn't quite meet our needs.