Skip to content

Commit

Permalink
🎨 option to symlink all resoures
Browse files Browse the repository at this point in the history
  • Loading branch information
Wytamma committed May 30, 2023
1 parent eb2c41c commit fea0e3e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
50 changes: 28 additions & 22 deletions snk/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def copy_resources(
self,
resources: List[Path],
cleanup: bool,
symlink_resources_folder: bool = False
symlink_resources: bool = False
):
"""
Copy resources to the current working directory.
Expand All @@ -215,16 +215,15 @@ def copy_resources(
"""
copied_resources = []

def copy_resource(src, dst):
if src.is_dir():
def copy_resource(src, dst, symlink=False):
target_is_directory = src.is_dir()
if symlink:
os.symlink(src, dst, target_is_directory=target_is_directory)
elif target_is_directory:
shutil.copytree(src, dst)
else:
shutil.copy(src, dst)

def symlink_resource(src, dst):
target_is_directory = src.is_dir()
os.symlink(src, dst, target_is_directory=target_is_directory)

def remove_resource(resource: Path):
if resource.is_symlink():
resource.unlink()
Expand All @@ -236,26 +235,33 @@ def remove_resource(resource: Path):
try:
#
resources_folder = self.pipeline.path / 'resources'
destination = Path(".") / 'resources'
if destination.exists():
typer.secho(
"Resources folder exists! Skipping...\n",
fg=typer.colors.RED,
)
else:
if resources_folder.exists():
if symlink_resources_folder:
symlink_resource(resources_folder, destination)
else:
copy_resource(resources_folder, destination)
copied_resources.append(destination)
# destination = Path(".") / 'resources'
# if destination.exists():
# typer.secho(
# "Resources folder exists! Skipping...\n",
# fg=typer.colors.RED,
# )
# else:
# if resources_folder.exists():
# if symlink_resources:
# symlink_resource(resources_folder, destination)
# else:
# copy_resource(resources_folder, destination)
# copied_resources.append(destination)
if resources_folder.exists():
resources.insert(0, Path('resources'))
for resource in resources:
abs_path = self.pipeline.path / resource
destination = Path(".") / resource.name
if not destination.exists():
# make sure you don't delete files that are already there...
copy_resource(abs_path, destination)
copy_resource(abs_path, destination, symlink=symlink_resources)
copied_resources.append(destination)
elif destination.exists() and not cleanup:
typer.secho(
f"Resource '{resource.name}' already exists! Skipping...",
fg=typer.colors.RED,
)
else:
raise FileExistsError(f"Resource '{resource.name}' already exists!")

Expand Down Expand Up @@ -432,7 +438,7 @@ def run(
with self.copy_resources(
self.snk_config.resources,
cleanup=not keep_resources,
symlink_resources_folder=self.snk_config.symlink_resources_folder
symlink_resources=self.snk_config.symlink_resources
):
try:
snakemake.main(args)
Expand Down
4 changes: 2 additions & 2 deletions snk/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class SnkConfig:

resources: List[Path] = field(default_factory=list)
annotations: dict = field(default_factory=dict)
symlink_resources_folder: bool = False
symlink_resources: bool = False

@classmethod
def from_path(cls, snk_config_path: Path):
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/data/pipeline/.snk
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ annotations:
text:
type: str
help: "A string to save to a file"
symlink_resources_folder: true
symlink_resources: true

0 comments on commit fea0e3e

Please sign in to comment.