Skip to content

Commit

Permalink
fix tempfile
Browse files Browse the repository at this point in the history
  • Loading branch information
mam10eks committed Dec 2, 2024
1 parent d9f334b commit dc447d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
39 changes: 17 additions & 22 deletions python-client/tira/rest_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import logging
import os
import tempfile
import time
import zipfile
from functools import lru_cache
Expand All @@ -21,6 +20,7 @@
from tira.pandas_integration import PandasIntegration
from tira.profiling_integration import ProfilingIntegration
from tira.pyterrier_integration import PyTerrierAnceIntegration, PyTerrierIntegration, PyTerrierSpladeIntegration
from tira.third_party_integrations import temporary_directory
from tira.tira_redirects import (
RESOURCE_REDIRECTS,
TASKS_WITH_REDIRECT_MERGING,
Expand Down Expand Up @@ -766,27 +766,22 @@ 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")

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"

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
)
zip_file = temporary_directory()
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
)

if resp.status_code not in {200, 202}:
message = resp.content.decode()
Expand Down
14 changes: 14 additions & 0 deletions python-client/tira/third_party_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ def persist_and_normalize_run(run, system_name, default_output=None, output_file
tira.upload_run_anonymous(output_file, upload_to_tira["dataset_id"])


def temporary_directory():
import tempfile

try:
ret = tempfile.TemporaryDirectory(prefix="tira-", delete=False)
ret = Path(ret.name)
except:
ret = tempfile.TemporaryDirectory(prefix="tira-")
ret = Path(ret.name)

ret.mkdir(parents=True, exist_ok=True)
return ret


def normalize_run(run, system_name, depth=1000):
try:
run["qid"] = run["qid"].astype(int)
Expand Down

0 comments on commit dc447d0

Please sign in to comment.