-
Notifications
You must be signed in to change notification settings - Fork 8
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
Fix TemporaryDirectory arguments #670
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -766,26 +766,27 @@ def upload_run_anonymous(self, file_path: Path, dataset_id: str): | |
# TODO use format from upload_to_tira instead of hard-coded run.txt | ||
check_format(file_path, "run.txt") | ||
|
||
zip_file = tempfile.TemporaryDirectory(prefix="tira-upload", delete=False).name | ||
zip_file = Path(zip_file) | ||
zip_file.mkdir(parents=True, exist_ok=True) | ||
zip_file = zip_file / "tira-upload.zip" | ||
|
||
zf = zipfile.ZipFile(zip_file, "w", compression=zipfile.ZIP_DEFLATED, compresslevel=9) | ||
for root, _, files in os.walk(file_path): | ||
for name in files: | ||
filePath = os.path.join(root, name) | ||
zf.write(filePath, arcname=name) | ||
|
||
zf.close() | ||
headers = {"Accept": "application/json"} | ||
files = {"file": open(zip_file, "rb")} | ||
|
||
resp = requests.post( | ||
url=f"{self.base_url}/api/v1/anonymous-uploads/{upload_to_tira['dataset_id']}", | ||
files=files, | ||
headers=headers | ||
) | ||
with TemporaryDirectory(prefix="tira-upload") as tmp_dir: | ||
zip_file = tmp_dir.name | ||
zip_file = Path(zip_file) | ||
zip_file.mkdir(parents=True, exist_ok=True) | ||
zip_file = zip_file / "tira-upload.zip" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could a TemporaryFile be used instead of a TemporaryDirectory that we add a file to? |
||
|
||
zf = zipfile.ZipFile(zip_file, "w", compression=zipfile.ZIP_DEFLATED, compresslevel=9) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should a context manager be used here? |
||
for root, _, files in os.walk(file_path): | ||
for name in files: | ||
filePath = os.path.join(root, name) | ||
zf.write(filePath, arcname=name) | ||
Comment on lines
+770
to
+779
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the advantage over something like:
I have not tested this code so it may not behave exactly the same but I am wondering if we are reimplementing something that already exists here. |
||
|
||
zf.close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will not be called if an exception (e.g., IOError?) is thrown above |
||
headers = {"Accept": "application/json"} | ||
files = {"file": open(zip_file, "rb")} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. zip_file is never closed |
||
|
||
resp = requests.post( | ||
url=f"{self.base_url}/api/v1/anonymous-uploads/{upload_to_tira['dataset_id']}", | ||
files=files, | ||
headers=headers | ||
) | ||
|
||
if resp.status_code not in {200, 202}: | ||
message = resp.content.decode() | ||
|
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.
mkdir
should not be needed since the context manager for TemporaryDirectory should automatically create the folder right? (https://docs.python.org/3/library/tempfile.html#examples)