Fix parsing for @pip target aliases
I tried adding `glog==0.1` to the requirements file. That ended up
generating an alias of `@pip//glog==0_1`. That's definitly not what we
want. We just want `@pip//glog`.
This patch fixes the issue by simplifying the parsing logic. Now all
comments and empty lines are handled by the same logic. A single regex
extracts the first package name on a line.
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: I9036ff668a74b06f3ec1b72e398e634eba5924f4
diff --git a/tools/python/pip_configure.py b/tools/python/pip_configure.py
index 4f9dff1..132cc24 100644
--- a/tools/python/pip_configure.py
+++ b/tools/python/pip_configure.py
@@ -13,10 +13,21 @@
in BUILD files.
"""
+import re
import sys
import textwrap
from pathlib import Path
+# Regex to parse the lines in a requirements file.
+# - Ignore line comments.
+# - Remove any inline comments that may or may not exist.
+# - Also remove any version specifiers. We don't use it.
+#
+# E.g:
+# numpy==1.2.3 # needed because we like it.
+# turns into "numpy".
+REQUIREMENT_MATCH = re.compile(r"[-_.a-zA-Z0-9]+")
+
def parse_requirements(requirements_path: Path) -> list[str]:
"""Parses tools/python/requirements.txt.
@@ -25,18 +36,9 @@
depend on explicitly requested pip packages. We don't want users to depend
on transitive dependencies of our requested pip packages.
"""
- result = []
- for line in requirements_path.read_text().splitlines():
- # Ignore line comments.
- if not line or line.startswith("#"):
- continue
-
- # Remove any inline comments that may or may not exist.
- # E.g:
- # numpy==1.2.3 # needed because we like it.
- result.append(line.split()[0])
-
- return result
+ lines = requirements_path.read_text().splitlines()
+ matches = map(REQUIREMENT_MATCH.match, lines)
+ return [match.group(0) for match in matches if match]
def generate_build_files(requirements: list[str]) -> None: