Skip to content

Commit 48c520f

Browse files
committed
Improve unzip_like and remove bash-based tempdirs
1 parent 4cb8bf6 commit 48c520f

3 files changed

Lines changed: 33 additions & 17 deletions

File tree

tests/integration/Snakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ rule download_netherlands_shapes:
1919

2020
rule download_netherlands_protected_areas:
2121
message:
22-
"Download and unzip a dummy drop-in dataset for Netherlands protected areas (not based on WDPA)."
22+
"Download a dummy drop-in dataset for Netherlands protected areas (not based on WDPA)."
2323
output:
2424
"results/integration_test/resources/user/wdpa.gdb.zip",
2525
log:
@@ -34,7 +34,7 @@ rule download_netherlands_protected_areas:
3434

3535
rule unzip_netherlands_protected_areas:
3636
message:
37-
"Download and unzip a dummy drop-in dataset for Netherlands protected areas (not based on WDPA)."
37+
"Download protected areas data."
3838
input:
3939
script=workflow.source_path("../../workflow/scripts/unzip_like.py"),
4040
zipfile=rules.download_netherlands_protected_areas.output,

workflow/rules/automatic.smk

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ rule download_globcover:
4343
conda:
4444
"../envs/shell.yaml"
4545
shell:
46-
'curl -sSLo {output} "{params.url}"'
46+
"""
47+
curl -sSLo "{output}" "{params.url}"
48+
"""
4749

4850

4951
rule unzip_globcover:
@@ -62,10 +64,7 @@ rule unzip_globcover:
6264
"../envs/shell.yaml"
6365
shell:
6466
"""
65-
temp_dir=$(mktemp -d)
66-
python {input.script} {input.zipfile} -f {params.target_file} -t $temp_dir
67-
mv $temp_dir/{params.target_file} {output}
68-
rm -R $temp_dir
67+
python "{input.script}" "{input.zipfile}" -f "{params.target_file}" -o "{output}" 2> "{log}"
6968
"""
7069

7170

@@ -81,7 +80,9 @@ rule download_ghsl:
8180
conda:
8281
"../envs/shell.yaml"
8382
shell:
84-
'curl -sSLo {output} "{params.url}"'
83+
"""
84+
curl -sSLo "{output}" "{params.url}"
85+
"""
8586

8687

8788
rule unzip_ghsl:
@@ -100,8 +101,5 @@ rule unzip_ghsl:
100101
"../envs/shell.yaml"
101102
shell:
102103
"""
103-
temp_dir=$(mktemp -d)
104-
python {input.script} {input.zipfile} -f {params.target_file} -t $temp_dir
105-
mv $temp_dir/{params.target_file} {output}
106-
rm -R $temp_dir
104+
python "{input.script}" "{input.zipfile}" -f "{params.target_file}" -o "{output}" 2> "{log}"
107105
"""

workflow/scripts/unzip_like.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Emulates the `unzip` command across platforms."""
22

33
import os
4+
import shutil
45
import zipfile
56

67
import click
@@ -15,8 +16,18 @@
1516
default=".",
1617
help="Target directory to extract to.",
1718
)
18-
@click.option("--file", "-f", help="Specific file inside the zip to extract.")
19-
def unzip(zip_path, target, file):
19+
@click.option(
20+
"--file",
21+
"-f",
22+
help="Specific file inside the zip to extract.",
23+
)
24+
@click.option(
25+
"--output",
26+
"-o",
27+
type=click.Path(),
28+
help="Output filename to save the extracted file as (used with -f).",
29+
)
30+
def unzip(zip_path, target, file, output):
2031
"""Emulates the `unzip` command across platforms.
2132
2233
ZIP_PATH: Path to the .zip file
@@ -25,12 +36,19 @@ def unzip(zip_path, target, file):
2536

2637
with zipfile.ZipFile(zip_path, "r") as zip_ref:
2738
if file:
28-
# Check if file exists in zip
2939
if file not in zip_ref.namelist():
3040
click.echo(f"Error: '{file}' not found in archive.")
3141
return
32-
zip_ref.extract(file, target)
33-
click.echo(f"Extracted '{file}' to '{target}'.")
42+
43+
extracted_path = zip_ref.extract(file, target)
44+
45+
if output:
46+
output_path = os.path.join(target, output)
47+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
48+
shutil.move(extracted_path, output_path)
49+
click.echo(f"Extracted '{file}' to '{output_path}'.")
50+
else:
51+
click.echo(f"Extracted '{file}' to '{extracted_path}'.")
3452
else:
3553
zip_ref.extractall(target)
3654
click.echo(f"Extracted all files to '{target}'.")

0 commit comments

Comments
 (0)