modm_data.utils.helper

 1# Copyright 2022, Niklas Hauser
 2# SPDX-License-Identifier: MPL-2.0
 3
 4import subprocess
 5from pathlib import Path
 6from importlib.resources import files, as_file
 7
 8def list_lstrip(input_values: list, strip_fn) -> list:
 9    ii = 0
10    for value in input_values:
11        if strip_fn(value):
12            ii += 1
13        else:
14            break
15    return input_values[ii:]
16
17
18def list_rstrip(input_values: list, strip_fn) -> list:
19    ii = 0
20    for value in reversed(input_values):
21        if strip_fn(value):
22            ii += 1
23        else:
24            break
25    return input_values[:len(input_values) - ii]
26
27
28def list_strip(input_values: list, strip_fn) -> list:
29    return list_rstrip(list_lstrip(input_values, strip_fn), strip_fn)
30
31
32def pkg_file_exists(pkg, file: Path) -> bool:
33    return files(pkg).joinpath(file).is_file()
34
35
36def pkg_apply_patch(pkg, patch: Path, base_dir: Path) -> bool:
37    with as_file(files(pkg).joinpath(patch)) as patch_file:
38        return apply_patch(patch_file, base_dir)
39
40
41def apply_patch(patch_file: Path, base_dir: Path) -> bool:
42    cmds = ["patch",
43            "--strip=1", "--silent", "--ignore-whitespace",
44            "--reject-file=-", "--forward", "--posix",
45            "--directory", Path(base_dir).absolute(),
46            "--input", Path(patch_file).absolute()]
47    if subprocess.run(cmds + ["--dry-run"]).returncode:
48        return False
49    return subprocess.run(cmds).returncode == 0
def list_lstrip(input_values: list, strip_fn) -> list:
 9def list_lstrip(input_values: list, strip_fn) -> list:
10    ii = 0
11    for value in input_values:
12        if strip_fn(value):
13            ii += 1
14        else:
15            break
16    return input_values[ii:]
def list_rstrip(input_values: list, strip_fn) -> list:
19def list_rstrip(input_values: list, strip_fn) -> list:
20    ii = 0
21    for value in reversed(input_values):
22        if strip_fn(value):
23            ii += 1
24        else:
25            break
26    return input_values[:len(input_values) - ii]
def list_strip(input_values: list, strip_fn) -> list:
29def list_strip(input_values: list, strip_fn) -> list:
30    return list_rstrip(list_lstrip(input_values, strip_fn), strip_fn)
def pkg_file_exists(pkg, file: pathlib.Path) -> bool:
33def pkg_file_exists(pkg, file: Path) -> bool:
34    return files(pkg).joinpath(file).is_file()
def pkg_apply_patch(pkg, patch: pathlib.Path, base_dir: pathlib.Path) -> bool:
37def pkg_apply_patch(pkg, patch: Path, base_dir: Path) -> bool:
38    with as_file(files(pkg).joinpath(patch)) as patch_file:
39        return apply_patch(patch_file, base_dir)
def apply_patch(patch_file: pathlib.Path, base_dir: pathlib.Path) -> bool:
42def apply_patch(patch_file: Path, base_dir: Path) -> bool:
43    cmds = ["patch",
44            "--strip=1", "--silent", "--ignore-whitespace",
45            "--reject-file=-", "--forward", "--posix",
46            "--directory", Path(base_dir).absolute(),
47            "--input", Path(patch_file).absolute()]
48    if subprocess.run(cmds + ["--dry-run"]).returncode:
49        return False
50    return subprocess.run(cmds).returncode == 0