Skip to content

utils_pixi

parse_collect_stdout(stdout)

Parse standard output of pixi/1_collect.sh

Source code in fractal_server/tasks/v2/utils_pixi.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def parse_collect_stdout(stdout: str) -> ParsedOutput:
    """
    Parse standard output of `pixi/1_collect.sh`
    """
    searches = [
        ("Package folder:", "package_root"),
        ("Disk usage:", "venv_size"),
        ("Number of files:", "venv_file_number"),
        ("Project Python wrapper:", "project_python_wrapper"),
    ]
    stdout_lines = stdout.splitlines()
    attributes = dict()
    for search, attribute_name in searches:
        matching_lines = [_line for _line in stdout_lines if search in _line]
        if len(matching_lines) == 0:
            raise ValueError(f"String '{search}' not found in stdout.")
        elif len(matching_lines) > 1:
            raise ValueError(
                f"String '{search}' found too many times "
                f"({len(matching_lines)})."
            )
        else:
            actual_line = matching_lines[0]
            attribute_value = actual_line.split(search)[-1].strip(" ")
            attributes[attribute_name] = attribute_value
    return attributes

simplify_pyproject_toml(*, original_toml_string, pixi_environment, pixi_platform)

Simplify pyproject.toml contents to make pixi install less heavy.

Parameters:

Name Type Description Default
original_toml_string str

Original pyproject.toml contents

required
pixi_environment str

Name of the pixi environment to use (e.g. default).

required
pixi_platform str

Name of the platform (e.g. linux-64)

required

Returns:

Type Description
str

New pyproject.toml contents

Source code in fractal_server/tasks/v2/utils_pixi.py
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def simplify_pyproject_toml(
    *,
    original_toml_string: str,
    pixi_environment: str,
    pixi_platform: str,
) -> str:
    """
    Simplify `pyproject.toml` contents to make `pixi install` less heavy.

    Args:
        original_toml_string: Original `pyproject.toml` contents
        pixi_environment: Name of the pixi environment to use (e.g. `default`).
        pixi_platform: Name of the platform (e.g. `linux-64`)

    Returns:
        New `pyproject.toml` contents
    """

    # Parse TOML string
    data = tomllib.loads(original_toml_string)
    try:
        pixi_data = data["tool"]["pixi"]
    except KeyError:
        logger.warning(
            "KeyError when looking for tool/pixi path. Return original value."
        )
        return original_toml_string

    # Use a single platform (or skip, if not set)
    try:
        pixi_data["workspace"]["platforms"] = [pixi_platform]
    except KeyError:
        logger.info("KeyError for workspace/platforms - skip.")
    try:
        pixi_data["project"]["platforms"] = [pixi_platform]
    except KeyError:
        logger.info("KeyError for project/platforms - skip.")

    # Keep a single environment (or skip, if not set)
    try:
        current_environments = pixi_data["environments"]
        pixi_data["environments"] = {
            key: value
            for key, value in current_environments.items()
            if key == pixi_environment
        }
        if pixi_data["environments"] == {}:
            raise ValueError(
                f"No '{pixi_environment}' pixi environment found."
            )
    except KeyError:
        logger.info("KeyError for workspace/platforms - skip.")

    # Drop pixi.tasks
    pixi_data.pop("tasks", None)

    # Prepare and validate new `pyprojectl.toml` contents
    data["tool"]["pixi"] = pixi_data
    new_toml_string = tomli_w.dumps(data)
    tomllib.loads(new_toml_string)

    return new_toml_string