|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Generate docker/light/requirements.txt from install/requirements.txt |
| 3 | +with excluding docker/light/excluded_libs.txt.""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from pathlib import Path |
| 8 | +import re |
| 9 | + |
| 10 | + |
| 11 | +ROOT_DIR = Path(__file__).resolve().parents[2] |
| 12 | +SOURCE_FILE = ROOT_DIR / "install" / "requirements.txt" |
| 13 | +EXCLUDED_FILE = ROOT_DIR / "docker" / "light" / "excluded_libs.txt" |
| 14 | +OUTPUT_FILE = ROOT_DIR / "docker" / "light" / "requirements.txt" |
| 15 | + |
| 16 | +VERSION_SEPARATORS = ("==", ">=", "<=", "~=", "!=", ">", "<", "===") |
| 17 | +NAME_SPLIT_RE = re.compile(r"[\s\[@;]") |
| 18 | + |
| 19 | + |
| 20 | +def normalize_name(name: str) -> str: |
| 21 | + return re.sub(r"[-_.]+", "-", name.strip().lower()) |
| 22 | + |
| 23 | + |
| 24 | +def extract_requirement_name(line: str) -> str | None: |
| 25 | + requirement = line.strip() |
| 26 | + if not requirement or requirement.startswith("#"): |
| 27 | + return None |
| 28 | + if requirement.startswith(("-r", "--requirement")): |
| 29 | + return None |
| 30 | + if requirement.startswith(("-c", "--constraint")): |
| 31 | + return None |
| 32 | + if requirement.startswith(("-", "--")): |
| 33 | + return None |
| 34 | + if "#egg=" in requirement: |
| 35 | + return requirement.rsplit("#egg=", maxsplit=1)[-1].strip() or None |
| 36 | + |
| 37 | + name_part = requirement |
| 38 | + for separator in VERSION_SEPARATORS: |
| 39 | + if separator in name_part: |
| 40 | + name_part = name_part.split(separator, maxsplit=1)[0] |
| 41 | + break |
| 42 | + |
| 43 | + name_part = NAME_SPLIT_RE.split(name_part, maxsplit=1)[0] |
| 44 | + return name_part.strip() or None |
| 45 | + |
| 46 | + |
| 47 | +def load_excluded_names() -> set[str]: |
| 48 | + excluded_names = set() |
| 49 | + for raw_line in EXCLUDED_FILE.read_text().splitlines(): |
| 50 | + line = raw_line.strip() |
| 51 | + if not line or line.startswith("#"): |
| 52 | + continue |
| 53 | + excluded_names.add(normalize_name(line)) |
| 54 | + return excluded_names |
| 55 | + |
| 56 | + |
| 57 | +def main() -> None: |
| 58 | + excluded_names = load_excluded_names() |
| 59 | + filtered_lines = [] |
| 60 | + |
| 61 | + for raw_line in SOURCE_FILE.read_text().splitlines(): |
| 62 | + requirement_name = extract_requirement_name(raw_line) |
| 63 | + if ( |
| 64 | + requirement_name |
| 65 | + and normalize_name(requirement_name) in excluded_names |
| 66 | + ): |
| 67 | + continue |
| 68 | + filtered_lines.append(raw_line) |
| 69 | + |
| 70 | + OUTPUT_FILE.write_text("\n".join(filtered_lines) + "\n") |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + main() |
0 commit comments