Skip to content

Commit

Permalink
fix: don't use Path.rename if files are on different filesystems
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Nov 8, 2024
1 parent a3b6a1d commit c11f827
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 11 additions & 7 deletions openfoodfacts_exports/exports/csv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import shutil
import tempfile
from pathlib import Path

import duckdb
Expand Down Expand Up @@ -34,11 +36,13 @@ def generate_mobile_app_dump(parquet_path: Path, output_path: Path) -> None:
if not parquet_path.exists():
raise FileNotFoundError(f"{str(parquet_path)} was not found.")

query = MOBILE_APP_DUMP_SQL_QUERY.replace(
"{dataset_path}", str(parquet_path)
).replace("{output_path}", str(output_path))
try:
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_file_path = Path(tmp_dir) / "mobile_dump.csv"
query = MOBILE_APP_DUMP_SQL_QUERY.replace(
"{dataset_path}", str(parquet_path)
).replace("{output_path}", str(tmp_file_path))
duckdb.sql(query)
except duckdb.Error as e:
logger.error(f"Error executing query: {query}\nError message: {e}")
raise
# Move dataset file to output_path
shutil.move(tmp_file_path, output_path)

logger.info("Mobile app dump generation done")
5 changes: 3 additions & 2 deletions openfoodfacts_exports/exports/parquet.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import shutil
import tempfile
from pathlib import Path

Expand Down Expand Up @@ -161,8 +162,8 @@ def export_parquet(dataset_path: Path, output_path: Path) -> None:
convert_jsonl_to_parquet(
output_file_path=tmp_file_path, dataset_path=dataset_path
)
# Copy dataset file to datasets directory
tmp_file_path.rename(output_path)
# Move dataset file to output_path
shutil.move(tmp_file_path, output_path)

if settings.ENABLE_HF_PUSH:
push_parquet_file_to_hf(data_path=output_path)
Expand Down

0 comments on commit c11f827

Please sign in to comment.