Skip to content

Commit

Permalink
Fix hardlink bug (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
tayden authored Jul 24, 2024
1 parent bcdb04a commit 343a933
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions kelp_o_matic/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
import tempfile
import urllib.request
from pathlib import Path
Expand Down Expand Up @@ -37,21 +38,27 @@ def download_file(url: str, filename: Path):
task = progress.add_task(f"Downloading {filename.name}...", total=file_size)

# Download the file
with tempfile.NamedTemporaryFile("wb") as f:
# Read data in chunks (e.g., 1024 bytes)
while True:
chunk = response.read(1024)
if not chunk:
break
f.write(chunk)

# Update progress bar
progress.update(task, advance=len(chunk))

# Move the file to the cache directory once downloaded
f.flush()
os.fsync(f.fileno())
filename.hardlink_to(f.name)
try:
with tempfile.NamedTemporaryFile("wb", delete=False) as f:
# Read data in chunks (e.g., 1024 bytes)
while True:
chunk = response.read(1024)
if not chunk:
break
f.write(chunk)

# Update progress bar
progress.update(task, advance=len(chunk))

# Move the file to the cache directory once downloaded
f.flush()
os.fsync(f.fileno())

shutil.move(f.name, filename)
except Exception as e:
if f and os.path.exists(f.name):
os.remove(f.name)
raise e


def all_same(items):
Expand Down

0 comments on commit 343a933

Please sign in to comment.