Skip to content

Commit 6dbfb84

Browse files
committed
add a requirements.txt generator for the slips light script because docker is unable to use xargs to install the binary of river
1 parent cf873c7 commit 6dbfb84

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

docker/light/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ RUN cd modules \
6262
CHANGELOG.md \
6363
conftest.py
6464

65-
RUN pip3 install --no-cache-dir --upgrade pip \
66-
&& grep -v -f docker/light/excluded_libs.txt install/requirements.txt | xargs -n 1 pip install \
65+
RUN python3 docker/light/generate_requirements.py \
66+
&& pip3 install --no-cache-dir --upgrade pip \
67+
&& pip install --ignore-installed --no-cache-dir -r docker/light/requirements.txt \
6768
&& chmod 774 slips.py \
6869
&& git init \
6970
&& git remote add origin https://github.com/stratosphereips/StratosphereLinuxIPS.git
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)