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

Fix TemporaryDirectory arguments #670

Merged
merged 1 commit into from
Dec 2, 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
41 changes: 21 additions & 20 deletions python-client/tira/rest_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

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)

zip_file = zip_file / "tira-upload.zip"
Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the advantage over something like:

with TemporaryFile() as tmpfile:
  shutil.make_archive(tmpfile, "zip", file_path)

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()
Copy link
Member

Choose a reason for hiding this comment

The 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")}
Copy link
Member

Choose a reason for hiding this comment

The 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()
Expand Down
Loading