Skip to content

tar_commands

Prepare tar commands for task-subfolder compression/extraction.

get_tar_compression_cmd(subfolder_path, filelist_path)

Prepare command to compress e.g. /path/dir into /path/dir.tar.gz.

Note that /path/dir.tar.gz may already exist. In this case, it will be overwritten.

Parameters:

Name Type Description Default
subfolder_path Path

Absolute path to the folder to compress.

required
filelist_path Path | None

If set, to be used in the --files-from option.

required
expected_tarfile

If set, it should match to the returned one.

required

Returns:

Type Description
tuple[str, str]

tar command

Source code in fractal_server/app/runner/executors/slurm_ssh/tar_commands.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def get_tar_compression_cmd(
    subfolder_path: Path,
    filelist_path: Path | None,
) -> tuple[str, str]:
    """
    Prepare command to compress e.g. `/path/dir` into `/path/dir.tar.gz`.

    Note that `/path/dir.tar.gz` may already exist. In this case, it will
    be overwritten.

    Args:
        subfolder_path: Absolute path to the folder to compress.
        filelist_path: If set, to be used in the `--files-from` option.
        expected_tarfile: If set, it should match to the returned one.

    Returns:
        tar command
    """
    tarfile_path = subfolder_path.with_suffix(".tar.gz")
    if filelist_path is None:
        cmd_tar = (
            f"tar -c -z "
            f"-f {tarfile_path} "
            f"--directory={subfolder_path.as_posix()} "
            "."
        )
    else:
        cmd_tar = (
            f"tar -c -z -f {tarfile_path} "
            f"--directory={subfolder_path.as_posix()} "
            f"--files-from={filelist_path.as_posix()} --ignore-failed-read"
        )

    return cmd_tar

get_tar_extraction_cmd(archive_path)

Prepare command to extract e.g. /path/dir.tar.gz into /path/dir.

Parameters:

Name Type Description Default
archive_path Path

Absolute path to the tar.gz archive.

required

Returns:

Type Description
tuple[Path, str]

Target extraction folder and tar command

Source code in fractal_server/app/runner/executors/slurm_ssh/tar_commands.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def get_tar_extraction_cmd(archive_path: Path) -> tuple[Path, str]:
    """
    Prepare command to extract e.g. `/path/dir.tar.gz` into `/path/dir`.

    Args:
        archive_path: Absolute path to the tar.gz archive.

    Returns:
        Target extraction folder and tar command
    """

    # Prepare subfolder path
    if archive_path.suffixes[-2:] != [".tar", ".gz"]:
        raise ValueError(
            "Archive path must end with `.tar.gz` "
            f"(given: {archive_path.as_posix()})"
        )
    subfolder_path = archive_path.with_suffix("").with_suffix("")

    cmd_tar = (
        f"tar -xzvf {archive_path} --directory={subfolder_path.as_posix()}"
    )
    return subfolder_path, cmd_tar