Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Allow link_only for posix file sources #19125

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/galaxy/files/sources/posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
DEFAULT_ENFORCE_SYMLINK_SECURITY = True
DEFAULT_DELETE_ON_REALIZE = False
DEFAULT_ALLOW_SUBDIR_CREATION = True
DEFAULT_LINK_ONLY = False


class PosixFilesSourceProperties(FilesSourceProperties, total=False):
root: str
enforce_symlink_security: bool
delete_on_realize: bool
allow_subdir_creation: bool
link_only: bool


class PosixFilesSource(BaseFilesSource):
Expand All @@ -50,6 +52,7 @@ def __init__(self, **kwd: Unpack[PosixFilesSourceProperties]):
self.root = props.get("root")
if not self.root:
self.writable = False
self.link_only = props.get("link_only", DEFAULT_LINK_ONLY)
self.enforce_symlink_security = props.get("enforce_symlink_security", DEFAULT_ENFORCE_SYMLINK_SECURITY)
self.delete_on_realize = props.get("delete_on_realize", DEFAULT_DELETE_ON_REALIZE)
self.allow_subdir_creation = props.get("allow_subdir_creation", DEFAULT_ALLOW_SUBDIR_CREATION)
Expand Down Expand Up @@ -103,7 +106,11 @@ def _realize_to(
source_native_path = os.path.normpath(source_native_path)
assert source_native_path.startswith(os.path.normpath(effective_root))

if not self.delete_on_realize:
if self.link_only:
if os.path.exists(native_path) or os.path.islink(native_path):
os.remove(native_path)
os.symlink(source_native_path, native_path)
elif not self.delete_on_realize:
shutil.copyfile(source_native_path, native_path)
else:
shutil.move(source_native_path, native_path)
Expand Down Expand Up @@ -133,9 +140,14 @@ def _write_from(

# Use a temporary name while writing so anything that consumes written files can detect when they've completed,
# and identify interrupted writes
target_native_path_part = os.path.join(target_native_path_parent, f"_{target_native_path_name}.part")
shutil.copyfile(native_path, target_native_path_part)
os.rename(target_native_path_part, target_native_path)
if self.link_only:
if os.path.exists(native_path) or os.path.islink(native_path):
os.remove(native_path)
os.symlink(target_native_path, native_path)
else:
target_native_path_part = os.path.join(target_native_path_parent, f"_{target_native_path_name}.part")
shutil.copyfile(native_path, target_native_path_part)
os.rename(target_native_path_part, target_native_path)

def _to_native_path(self, source_path: str, user_context: OptionalUserContext = None):
source_path = os.path.normpath(source_path)
Expand Down Expand Up @@ -182,6 +194,7 @@ def _serialization_props(self, user_context: OptionalUserContext = None) -> Posi
"enforce_symlink_security": self.enforce_symlink_security,
"delete_on_realize": self.delete_on_realize,
"allow_subdir_creation": self.allow_subdir_creation,
"link_only": self.link_only,
}

@property
Expand Down
Loading