Exemplo n.º 1
0
def _populate_environment(environment: Optional[Dict[str, Optional[str]]],
                          prime_dir: Path, arch_triplet: str):
    """Populate default app environmental variables.

    Three cases for LD_LIBRARY_PATH and PATH variables:
        - If LD_LIBRARY_PATH or PATH are defined, keep user-defined values.
        - If LD_LIBRARY_PATH or PATH are not defined, set to default values.
        - If LD_LIBRARY_PATH or PATH are null, do not use default values.
    """
    if environment is None:
        return {
            "LD_LIBRARY_PATH": get_ld_library_paths(prime_dir, arch_triplet),
            "PATH": "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH",
        }

    try:
        if not environment["LD_LIBRARY_PATH"]:
            environment.pop("LD_LIBRARY_PATH")
    except KeyError:
        environment["LD_LIBRARY_PATH"] = get_ld_library_paths(
            prime_dir, arch_triplet)

    try:
        if not environment["PATH"]:
            environment.pop("PATH")
    except KeyError:
        environment[
            "PATH"] = "$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"

    if len(environment):
        return environment

    # if the environment only contained a null LD_LIBRARY_PATH and a null PATH, return None
    return None
Exemplo n.º 2
0
def test_get_ld_library_paths(tmp_path, lib_dirs, expected_env):
    for d in lib_dirs:
        (tmp_path / d).mkdir(parents=True)

    expected_env = (
        f"${{SNAP_LIBRARY_PATH}}${{LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}}:{expected_env}"
    )
    assert utils.get_ld_library_paths(tmp_path, "i286-none-none") == expected_env