From 8d01b8f1876485a77a8ca44e754be16e963af585 Mon Sep 17 00:00:00 2001 From: Christophe Combelles Date: Wed, 20 Nov 2024 10:17:28 +0100 Subject: [PATCH] refactor: unzip in a temporary folder instead (#834) ## :wrench: Problem We cannot run two imports in parallel because the csv files are unzipped in the same directory. ## :cake: Solution Unzip in a temporary folder instead ## :desert_island: How to test Extract two clones of ecobalyse in two different folders, and launch the make process at the same time. Check both work. --- data/common/import_.py | 50 +++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/data/common/import_.py b/data/common/import_.py index cc0d0b757..1f144883e 100644 --- a/data/common/import_.py +++ b/data/common/import_.py @@ -1,9 +1,9 @@ import functools import json -import os import re import sys -from os.path import dirname +import tempfile +from os.path import basename, join, splitext from subprocess import call from zipfile import ZipFile @@ -186,29 +186,29 @@ def import_simapro_csv( """ print(f"### Importing {datapath}...") # unzip - with ZipFile(datapath) as zf: - print("### Extracting the zip file...") - zf.extractall(path=dirname(datapath)) - unzipped = datapath[0:-4] - - if "AGB" in datapath: - print("### Patching Agribalyse...") - # `yield` is used as a variable in some Simapro parameters. bw2parameters cannot handle it: - # (sed is faster than Python) - call("sed -i 's/yield/Yield_/g' " + unzipped, shell=True) - # Fix some errors in Agribalyse: - call("sed -i 's/01\\/03\\/2005/1\\/3\\/5/g' " + unzipped, shell=True) - call("sed -i 's/\"0;001172\"/0,001172/' " + unzipped, shell=True) - - print(f"### Importing into {dbname}...") - # Do the import - database = bw2io.importers.simapro_csv.SimaProCSVImporter( - unzipped, dbname, normalize_biosphere=True - ) - if source: - for ds in database: - ds["source"] = source - os.unlink(unzipped) + with tempfile.TemporaryDirectory() as tempdir: + with ZipFile(datapath) as zf: + print(f"### Extracting the zip file in {tempdir}...") + zf.extractall(path=tempdir) + unzipped, _ = splitext(join(tempdir, basename(datapath))) + + if "AGB" in datapath: + print("### Patching Agribalyse...") + # `yield` is used as a variable in some Simapro parameters. bw2parameters cannot handle it: + # (sed is faster than Python) + call("sed -i 's/yield/Yield_/g' " + unzipped, shell=True) + # Fix some errors in Agribalyse: + call("sed -i 's/01\\/03\\/2005/1\\/3\\/5/g' " + unzipped, shell=True) + call("sed -i 's/\"0;001172\"/0,001172/' " + unzipped, shell=True) + + print(f"### Importing into {dbname}...") + # Do the import + database = bw2io.importers.simapro_csv.SimaProCSVImporter( + unzipped, dbname, normalize_biosphere=True + ) + if source: + for ds in database: + ds["source"] = source print("### Applying migrations...") # Apply provided migrations