Skip to content

Commit

Permalink
Fix: Windows compatibility for file operations
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcMarc94 committed Nov 14, 2024
1 parent 7954f5a commit 5ff0dde
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/scportrait/tools/parse/_parse_phenix.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import xml.etree.ElementTree as ET
from datetime import datetime
from pathlib import Path
import platform
import tempfile

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -458,40 +460,44 @@ def replace_missing_images(self):
)

def define_copy_functions(self):
# define function for copying depending on if symlinks should be used or not
"""Define function for copying depending on if symlinks should be used or not"""
if self.export_symlinks:

def copyfunction(input, output):
try:
os.symlink(input, output)
if platform.system() == 'Windows':
# On Windows, use hard links when symlinks are requested
if not os.path.exists(output):
os.link(input, output)
else:
# Original Unix symlink behavior
os.symlink(input, output)
except OSError as e:
print("Error: ", e)
return ()
else:

def copyfunction(input, output):
shutil.copyfile(input, output)

try:
# Create destination directory if it doesn't exist
os.makedirs(os.path.dirname(output), exist_ok=True)
shutil.copyfile(input, output)
except OSError as e:
print("Error: ", e)
return ()
self.copyfunction = copyfunction

def copy_files(self, metadata):
"""
Copy files from the source directory to the output directory. The new file names are defined in the metadata.
Parameters
----------
metadata : pd.DataFrame
Expected columns are: filename, new_file_name, source, dest
Returns
-------
None
"""
print("Starting copy process...")
self.define_copy_functions()

# actually perform the copy process
for old, new, source, dest in tqdm(
zip(
Expand All @@ -507,7 +513,6 @@ def copy_files(self, metadata):
# define old and new paths for copy process
old_path = os.path.join(source, old)
new_path = os.path.join(dest, new)

# check if old path exists
if os.path.exists(old_path):
self.copyfunction(old_path, new_path)
Expand Down

0 comments on commit 5ff0dde

Please sign in to comment.