Skip to content

Commit 1ada123

Browse files
committed
relink.py: Refactor to improve testability.
1 parent f88de06 commit 1ada123

3 files changed

Lines changed: 433 additions & 226 deletions

File tree

relink.py

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -180,50 +180,66 @@ def replace_files_with_symlinks(
180180

181181
# Use efficient scandir-based search
182182
for file_path in find_owned_files_scandir(source_dir, user_uid, inputdata_root):
183-
logger.info("Found owned file: %s", file_path)
184-
185-
# Determine the relative path and the new link's destination
186-
relative_path = os.path.relpath(file_path, source_dir)
187-
link_target = os.path.join(target_dir, relative_path)
188-
189-
# Check if the target file actually exists
190-
if not os.path.exists(link_target):
191-
logger.warning(
192-
"Warning: Corresponding file not found in '%s' for '%s'. Skipping.",
193-
target_dir,
194-
file_path,
195-
)
196-
continue
197-
198-
# Get the link name
199-
link_name = file_path
200-
201-
if dry_run:
202-
logger.info(
203-
"[DRY RUN] Would create symbolic link: %s -> %s",
204-
link_name,
205-
link_target,
206-
)
207-
continue
208-
209-
# Remove the original file
210-
try:
211-
os.rename(link_name, link_name + ".tmp")
212-
logger.info("Deleted original file: %s", link_name)
213-
except OSError as e:
214-
logger.error("Error deleting file %s: %s. Skipping.", link_name, e)
215-
continue
216-
217-
# Create the symbolic link, handling necessary parent directories
218-
try:
219-
# Create parent directories for the link if they don't exist
220-
os.makedirs(os.path.dirname(link_name), exist_ok=True)
221-
os.symlink(link_target, link_name)
222-
os.remove(link_name + ".tmp")
223-
logger.info("Created symbolic link: %s -> %s", link_name, link_target)
224-
except OSError as e:
225-
os.rename(link_name + ".tmp", link_name)
226-
logger.error("Error creating symlink for %s: %s. Skipping.", link_name, e)
183+
replace_one_file_with_symlink(source_dir, target_dir, file_path, dry_run=dry_run)
184+
185+
186+
def replace_one_file_with_symlink(
187+
source_dir, target_dir, file_path, dry_run=False
188+
):
189+
"""
190+
Given a file, replaces it with a symbolic link to the same relative path in a target directory
191+
tree.
192+
193+
Args:
194+
source_dir (str): The root of the directory tree to search for files.
195+
target_dir (str): The root of the directory tree containing the new files.
196+
file_path (str): The path of the file to be replaced.
197+
dry_run (bool): If True, only show what would be done without making changes.
198+
"""
199+
logger.info("Found owned file: %s", file_path)
200+
201+
# Determine the relative path and the new link's destination
202+
relative_path = os.path.relpath(file_path, source_dir)
203+
link_target = os.path.join(target_dir, relative_path)
204+
205+
# Check if the target file actually exists
206+
if not os.path.exists(link_target):
207+
logger.warning(
208+
"Warning: Corresponding file not found in '%s' for '%s'. Skipping.",
209+
target_dir,
210+
file_path,
211+
)
212+
return
213+
214+
# Get the link name
215+
link_name = file_path
216+
217+
if dry_run:
218+
logger.info(
219+
"[DRY RUN] Would create symbolic link: %s -> %s",
220+
link_name,
221+
link_target,
222+
)
223+
return
224+
225+
# Remove the original file
226+
try:
227+
os.rename(link_name, link_name + ".tmp")
228+
logger.info("Deleted original file: %s", link_name)
229+
except OSError as e:
230+
logger.error("Error deleting file %s: %s. Skipping.", link_name, e)
231+
return
232+
233+
# Create the symbolic link, handling necessary parent directories
234+
try:
235+
# Create parent directories for the link if they don't exist
236+
os.makedirs(os.path.dirname(link_name), exist_ok=True)
237+
os.symlink(link_target, link_name)
238+
os.remove(link_name + ".tmp")
239+
logger.info("Created symbolic link: %s -> %s", link_name, link_target)
240+
except OSError as e:
241+
os.rename(link_name + ".tmp", link_name)
242+
logger.error("Error creating symlink for %s: %s. Skipping.", link_name, e)
227243

228244

229245
def validate_directory(path):

0 commit comments

Comments
 (0)