From 9ea3efe7bfba9a6182bd99d31c5e6874ec869850 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 22 Nov 2026 00:30:22 +0900 Subject: [PATCH 1/6] refactor: normalize the names with pypackaging.bzl Use `pypackaging.utils.canonicalize_name` instead of the hand written loop. The result is the same, we only keep the conversion of the dashes to underscores so that the name can be used in the labels. --- python/private/BUILD.bazel | 11 ++++++----- python/private/normalize_name.bzl | 14 +++----------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index 4360048304..067c8f29c0 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -997,6 +997,12 @@ bzl_library( ], ) +bzl_library( + name = "normalize_name", + srcs = ["normalize_name.bzl"], + deps = ["@pypackaging.bzl//:pypackaging"], +) + bzl_library( name = "artifacts_manifest", srcs = ["artifacts_manifest.bzl"], @@ -1032,11 +1038,6 @@ bzl_library( srcs = ["full_version.bzl"], ) -bzl_library( - name = "normalize_name", - srcs = ["normalize_name.bzl"], -) - bzl_library( name = "pbs_manifest", srcs = ["pbs_manifest.bzl"], diff --git a/python/private/normalize_name.bzl b/python/private/normalize_name.bzl index 7898222e41..7d3886f9ca 100644 --- a/python/private/normalize_name.bzl +++ b/python/private/normalize_name.bzl @@ -38,6 +38,8 @@ Also see Python spec on normalizing package names: https://packaging.python.org/en/latest/specifications/name-normalization/ """ +load("@pypackaging.bzl", "pypackaging") + def normalize_name(name): """normalize a PyPI package name and return a valid bazel label. @@ -47,14 +49,4 @@ def normalize_name(name): Returns: a normalized name as a string. """ - name = name.replace("-", "_").replace(".", "_").lower() - if "__" not in name: - return name - - # Handle the edge-case where there are consecutive `-`, `_` or `.` characters, - # which is a valid Python package name. - return "_".join([ - part - for part in name.split("_") - if part - ]) + return pypackaging.utils.canonicalize_name(name).replace("-", "_") From 0c44a8267dd3a8078ccccd1045dad4ab30c4ce57 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 22 Nov 2026 00:31:39 +0900 Subject: [PATCH 2/6] refactor(pypi): parse the requirements with pypackaging.bzl Use `pypackaging.requirements.parse` instead of the hand written partitioning of the requirement lines. The extras are now sorted and the unused `version` field is dropped. `name==version @ url` lines are not valid as per PEP 508, but they are still accepted because they are used in the requirements files. Whilst at it, parse each requirement line once instead of once per platform. --- news/pypackaging-bzl.changed.md | 5 +++ python/private/pypi/BUILD.bazel | 2 +- python/private/pypi/parse_requirements.bzl | 8 +++- python/private/pypi/pep508_requirement.bzl | 50 +++++++++------------- tests/pypi/pep508/requirement_tests.bzl | 29 +++++++------ 5 files changed, 49 insertions(+), 45 deletions(-) create mode 100644 news/pypackaging-bzl.changed.md diff --git a/news/pypackaging-bzl.changed.md b/news/pypackaging-bzl.changed.md new file mode 100644 index 0000000000..b233f37b2d --- /dev/null +++ b/news/pypackaging-bzl.changed.md @@ -0,0 +1,5 @@ +(pypi) The requirements are now parsed by +[`pypackaging.bzl`](https://github.com/jvolkman/pypackaging.bzl), which is a +Starlark port of [`packaging`](https://github.com/pypa/packaging). This means +that invalid input, which was silently accepted before, is now an error. +`WORKSPACE` users get `pypackaging.bzl` from `py_repositories`. diff --git a/python/private/pypi/BUILD.bazel b/python/private/pypi/BUILD.bazel index c5331fd899..a8050dcef2 100644 --- a/python/private/pypi/BUILD.bazel +++ b/python/private/pypi/BUILD.bazel @@ -312,7 +312,7 @@ bzl_library( bzl_library( name = "pep508_requirement", srcs = ["pep508_requirement.bzl"], - deps = ["//python/private:normalize_name"], + deps = ["@pypackaging.bzl//:pypackaging"], ) bzl_library( diff --git a/python/private/pypi/parse_requirements.bzl b/python/private/pypi/parse_requirements.bzl index 2d8c4b4774..f82d6f850e 100644 --- a/python/private/pypi/parse_requirements.bzl +++ b/python/private/pypi/parse_requirements.bzl @@ -511,6 +511,9 @@ def _parse_requirements_from_req_files( reqs_by_name = {} requirements_by_platform = {} + + # The same lines are used on many platforms, so parse them once. + names = {} for plat, parse_results in requirements.items(): requirements_dict = {} for entry in sorted( @@ -518,9 +521,10 @@ def _parse_requirements_from_req_files( key = lambda x: (len(x[1].partition("==")[0]), x), ): req_line = entry[1] - req = requirement(req_line) + if req_line not in names: + names[req_line] = requirement(req_line).name - requirements_dict[req.name] = entry + requirements_dict[names[req_line]] = entry extra_pip_args_for_plat = options[plat] diff --git a/python/private/pypi/pep508_requirement.bzl b/python/private/pypi/pep508_requirement.bzl index 7552642572..9c145bcbf5 100644 --- a/python/private/pypi/pep508_requirement.bzl +++ b/python/private/pypi/pep508_requirement.bzl @@ -15,44 +15,36 @@ """This module is for parsing PEP508 requires-dist and requirements lines. """ -load("//python/private:normalize_name.bzl", "normalize_name") - -_STRIP = ["(", " ", ">", "=", "<", "~", "!", "@"] +load("@pypackaging.bzl", "pypackaging") def requirement(spec): """Parse a PEP508 requirement line Args: - spec: {type}`str` requirement line that will be parsed. + spec: {type}`str` requirement line that will be parsed. The options + of a `requirements.txt` line, e.g. `--hash`, are ignored. Returns: A struct with the information. """ - spec = spec.strip() - requires, _, maybe_hashes = spec.partition(";") - - version_start = requires.find("==") - version = None - if version_start != -1: - # Extract everything after '==' until the next space or end of the string - version, _, _ = requires[version_start + 2:].partition(" ") - - # Remove any trailing characters from the version string - version = version.strip(" ") - - marker, _, _ = maybe_hashes.partition("--hash") - requires, _, extras_unparsed = requires.partition("[") - extras_unparsed, _, _ = extras_unparsed.partition("]") - for char in _STRIP: - requires, _, _ = requires.partition(char) - extras = [normalize_name(e) for e in extras_unparsed.replace(" ", "").split(",") if e] - name = requires.strip(" ") - name = normalize_name(name) + + # The options are not a part of PEP 508. + spec, _, _ = spec.partition(" --") + + head, semicolon, marker = spec.partition(";") + name, at, url = head.partition("@") + if at and "==" in name: + # PEP 508 does not allow a version for the direct URL requirements, + # but `name==version @ url` has always been accepted here, so that + # the version of the distribution is known. + name, _, _ = name.partition("==") + spec = name + at + url + semicolon + marker + + req = pypackaging.requirements.parse(spec) return struct( - name = name.replace("_", "-"), - name_ = name, - marker = marker.strip(" "), - extras = extras, - version = version, + name = req.name, + name_ = req.name.replace("-", "_"), + marker = req.marker_str or "", + extras = [e.replace("-", "_") for e in req.extras], ) diff --git a/tests/pypi/pep508/requirement_tests.bzl b/tests/pypi/pep508/requirement_tests.bzl index 2ce45922da..92cfa32caa 100644 --- a/tests/pypi/pep508/requirement_tests.bzl +++ b/tests/pypi/pep508/requirement_tests.bzl @@ -20,22 +20,25 @@ _tests = [] def _test_requirement_line_parsing(env): want = { - " name1[ foo ] ": ("name1", ["foo"], None, ""), - "Name[foo]": ("name", ["foo"], None, ""), - "name [fred,bar] @ http://foo.com ; python_version=='2.7'": ("name", ["fred", "bar"], None, "python_version=='2.7'"), - "name; (os_name=='a' or os_name=='b') and os_name=='c'": ("name", [], None, "(os_name=='a' or os_name=='b') and os_name=='c'"), - "name@http://foo.com": ("name", [], None, ""), - "name[ Foo123 ]": ("name", ["foo123"], None, ""), - "name[extra-one,extra-two.three]==1.0": ("name", ["extra_one", "extra_two_three"], "1.0", ""), - "name[extra]@http://foo.com": ("name", ["extra"], None, ""), - "name[foo]": ("name", ["foo"], None, ""), - "name[quux, strange];python_version<'2.7' and platform_version=='2'": ("name", ["quux", "strange"], None, "python_version<'2.7' and platform_version=='2'"), - "name_foo[bar]": ("name-foo", ["bar"], None, ""), - "name_foo[bar]==0.25": ("name-foo", ["bar"], "0.25", ""), + " name1[ foo ] ": ("name1", ["foo"], ""), + "Name[foo]": ("name", ["foo"], ""), + "name [fred,bar] @ http://foo.com ; python_version=='2.7'": ("name", ["bar", "fred"], "python_version=='2.7'"), + "name; (os_name=='a' or os_name=='b') and os_name=='c'": ("name", [], "(os_name=='a' or os_name=='b') and os_name=='c'"), + "name@http://foo.com": ("name", [], ""), + "name[ Foo123 ]": ("name", ["foo123"], ""), + "name[extra-one,extra-two.three]==1.0": ("name", ["extra_one", "extra_two_three"], ""), + "name[extra]@http://foo.com": ("name", ["extra"], ""), + "name[foo]": ("name", ["foo"], ""), + "name[quux, strange];python_version<'2.7' and platform_version=='2'": ("name", ["quux", "strange"], "python_version<'2.7' and platform_version=='2'"), + "name_foo==0.25 ; os_name=='a' --hash=sha256:deadbeef": ("name-foo", [], "os_name=='a'"), + "name_foo[bar]": ("name-foo", ["bar"], ""), + "name_foo[bar]==0.25": ("name-foo", ["bar"], ""), + "name_foo[bar]==0.25 --hash=sha256:deadbeef": ("name-foo", ["bar"], ""), + "name_foo[bar]==0.25 @ http://foo.com ; os_name=='a'": ("name-foo", ["bar"], "os_name=='a'"), } got = { - i: (parsed.name, parsed.extras, parsed.version, parsed.marker) + i: (parsed.name, parsed.extras, parsed.marker) for i, parsed in {case: requirement(case) for case in want}.items() } env.expect.that_dict(got).contains_exactly(want) From d6ce4ba9710273bbf868dca3dcbce039f04a4769 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:41:05 +0900 Subject: [PATCH 3/6] revert: keep normalize_name free of pypackaging.bzl It is loaded by the files that Bazel 8 autoloads, which cannot load from the pypackaging.bzl repository. --- python/private/BUILD.bazel | 11 +++++------ python/private/normalize_name.bzl | 14 +++++++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index 067c8f29c0..4360048304 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -997,12 +997,6 @@ bzl_library( ], ) -bzl_library( - name = "normalize_name", - srcs = ["normalize_name.bzl"], - deps = ["@pypackaging.bzl//:pypackaging"], -) - bzl_library( name = "artifacts_manifest", srcs = ["artifacts_manifest.bzl"], @@ -1038,6 +1032,11 @@ bzl_library( srcs = ["full_version.bzl"], ) +bzl_library( + name = "normalize_name", + srcs = ["normalize_name.bzl"], +) + bzl_library( name = "pbs_manifest", srcs = ["pbs_manifest.bzl"], diff --git a/python/private/normalize_name.bzl b/python/private/normalize_name.bzl index 7d3886f9ca..7898222e41 100644 --- a/python/private/normalize_name.bzl +++ b/python/private/normalize_name.bzl @@ -38,8 +38,6 @@ Also see Python spec on normalizing package names: https://packaging.python.org/en/latest/specifications/name-normalization/ """ -load("@pypackaging.bzl", "pypackaging") - def normalize_name(name): """normalize a PyPI package name and return a valid bazel label. @@ -49,4 +47,14 @@ def normalize_name(name): Returns: a normalized name as a string. """ - return pypackaging.utils.canonicalize_name(name).replace("-", "_") + name = name.replace("-", "_").replace(".", "_").lower() + if "__" not in name: + return name + + # Handle the edge-case where there are consecutive `-`, `_` or `.` characters, + # which is a valid Python package name. + return "_".join([ + part + for part in name.split("_") + if part + ]) From c15b709d5c39c497396048ec91bc8b7d3b3c12bd Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 22 Nov 2026 16:17:33 +0900 Subject: [PATCH 4/6] fix(pypi): accept tabs before the options and markers right after the URLs Whilst at it, really parse each requirement line once. --- python/private/pypi/parse_requirements.bzl | 13 ++++++------- python/private/pypi/pep508_requirement.bzl | 12 +++++++++--- tests/pypi/pep508/requirement_tests.bzl | 5 +++++ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/python/private/pypi/parse_requirements.bzl b/python/private/pypi/parse_requirements.bzl index f82d6f850e..0ace719ac3 100644 --- a/python/private/pypi/parse_requirements.bzl +++ b/python/private/pypi/parse_requirements.bzl @@ -462,6 +462,9 @@ def _parse_requirements_from_req_files( all_files_parsed = {} index_url = None extra_index_urls = [] + + # The same lines are used on many platforms, so parse them once. + names = {} for file, plats in requirements_by_platform.items(): logger.trace(lambda: "Using {} for {}".format(file, plats)) contents = ctx.read(file) @@ -491,6 +494,8 @@ def _parse_requirements_from_req_files( # Pre-parse requirements once per file to avoid redundant parsing in loops parsed_reqs = [(entry, requirement(entry[1])) for entry in parse_result.requirements] + for entry, req in parsed_reqs: + names[entry[1]] = req.name for plat in plats: plat_env = platforms.get(plat) @@ -512,19 +517,13 @@ def _parse_requirements_from_req_files( reqs_by_name = {} requirements_by_platform = {} - # The same lines are used on many platforms, so parse them once. - names = {} for plat, parse_results in requirements.items(): requirements_dict = {} for entry in sorted( parse_results, key = lambda x: (len(x[1].partition("==")[0]), x), ): - req_line = entry[1] - if req_line not in names: - names[req_line] = requirement(req_line).name - - requirements_dict[names[req_line]] = entry + requirements_dict[names[entry[1]]] = entry extra_pip_args_for_plat = options[plat] diff --git a/python/private/pypi/pep508_requirement.bzl b/python/private/pypi/pep508_requirement.bzl index 9c145bcbf5..a5c86a2a38 100644 --- a/python/private/pypi/pep508_requirement.bzl +++ b/python/private/pypi/pep508_requirement.bzl @@ -29,16 +29,22 @@ def requirement(spec): """ # The options are not a part of PEP 508. - spec, _, _ = spec.partition(" --") + for sep in (" --", "\t--"): + spec, _, _ = spec.partition(sep) head, semicolon, marker = spec.partition(";") name, at, url = head.partition("@") - if at and "==" in name: + if at: # PEP 508 does not allow a version for the direct URL requirements, # but `name==version @ url` has always been accepted here, so that # the version of the distribution is known. name, _, _ = name.partition("==") - spec = name + at + url + semicolon + marker + + # PEP 508 needs a whitespace between the URL and the marker, but + # `name @ url; marker` is accepted by `pip` in the requirements files. + spec = name + at + url.rstrip() + if semicolon: + spec += " ;" + marker req = pypackaging.requirements.parse(spec) diff --git a/tests/pypi/pep508/requirement_tests.bzl b/tests/pypi/pep508/requirement_tests.bzl index 92cfa32caa..b13691bdf7 100644 --- a/tests/pypi/pep508/requirement_tests.bzl +++ b/tests/pypi/pep508/requirement_tests.bzl @@ -22,6 +22,8 @@ def _test_requirement_line_parsing(env): want = { " name1[ foo ] ": ("name1", ["foo"], ""), "Name[foo]": ("name", ["foo"], ""), + # No whitespace between the URL and the marker + "foo @ https://example.org/foo-1.0-py3-none-any.whl; os_name == 'a'": ("foo", [], "os_name == 'a'"), "name [fred,bar] @ http://foo.com ; python_version=='2.7'": ("name", ["bar", "fred"], "python_version=='2.7'"), "name; (os_name=='a' or os_name=='b') and os_name=='c'": ("name", [], "(os_name=='a' or os_name=='b') and os_name=='c'"), "name@http://foo.com": ("name", [], ""), @@ -30,6 +32,9 @@ def _test_requirement_line_parsing(env): "name[extra]@http://foo.com": ("name", ["extra"], ""), "name[foo]": ("name", ["foo"], ""), "name[quux, strange];python_version<'2.7' and platform_version=='2'": ("name", ["quux", "strange"], "python_version<'2.7' and platform_version=='2'"), + "name_foo==0.25\t--hash=sha256:deadbeef": ("name-foo", [], ""), + # The options may be indented with tabs in the requirements files + "name_foo==0.25 \t--hash=sha256:deadbeef": ("name-foo", [], ""), "name_foo==0.25 ; os_name=='a' --hash=sha256:deadbeef": ("name-foo", [], "os_name=='a'"), "name_foo[bar]": ("name-foo", ["bar"], ""), "name_foo[bar]==0.25": ("name-foo", ["bar"], ""), From b90e56fbfcf8a9f18a92cd2e8f746b995b812624 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Sun, 22 Nov 2026 20:33:17 -0800 Subject: [PATCH 5/6] refactor(pypi): cache parsed requirements across files and polish docs Avoid re-parsing shared requirement lines across per-platform files and align the Starlark structs, tests, and news entry with project style. Cache `requirement()` structs in a single dict across files in `parse_requirements.bzl`, document `requirement()` return fields, use an explicit loop in `requirement_tests.bzl`, and cross-reference `py_repositories` in the tightened news entry. --- news/pypackaging-bzl.changed.md | 10 ++++----- python/private/pypi/parse_requirements.bzl | 24 ++++++++++++---------- python/private/pypi/pep508_requirement.bzl | 4 ++++ tests/pypi/pep508/requirement_tests.bzl | 8 ++++---- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/news/pypackaging-bzl.changed.md b/news/pypackaging-bzl.changed.md index b233f37b2d..5248d98ad0 100644 --- a/news/pypackaging-bzl.changed.md +++ b/news/pypackaging-bzl.changed.md @@ -1,5 +1,5 @@ -(pypi) The requirements are now parsed by -[`pypackaging.bzl`](https://github.com/jvolkman/pypackaging.bzl), which is a -Starlark port of [`packaging`](https://github.com/pypa/packaging). This means -that invalid input, which was silently accepted before, is now an error. -`WORKSPACE` users get `pypackaging.bzl` from `py_repositories`. +(pypi) Invalid PEP 508 requirements now fail instead of being silently accepted, +as requirements are now parsed by +[`pypackaging.bzl`](https://github.com/jvolkman/pypackaging.bzl) (a Starlark +port of [`packaging`](https://github.com/pypa/packaging)). `WORKSPACE` users +get `pypackaging.bzl` from {obj}`py_repositories`. diff --git a/python/private/pypi/parse_requirements.bzl b/python/private/pypi/parse_requirements.bzl index 0ace719ac3..dfc4501c76 100644 --- a/python/private/pypi/parse_requirements.bzl +++ b/python/private/pypi/parse_requirements.bzl @@ -464,7 +464,7 @@ def _parse_requirements_from_req_files( extra_index_urls = [] # The same lines are used on many platforms, so parse them once. - names = {} + parsed_reqs = {} for file, plats in requirements_by_platform.items(): logger.trace(lambda: "Using {} for {}".format(file, plats)) contents = ctx.read(file) @@ -492,19 +492,21 @@ def _parse_requirements_from_req_files( # (3.0). get_index_urls = None - # Pre-parse requirements once per file to avoid redundant parsing in loops - parsed_reqs = [(entry, requirement(entry[1])) for entry in parse_result.requirements] - for entry, req in parsed_reqs: - names[entry[1]] = req.name + for _, req_line in parse_result.requirements: + if req_line not in parsed_reqs: + parsed_reqs[req_line] = requirement(req_line) for plat in plats: plat_env = platforms.get(plat) - requirements[plat] = [ - entry - for entry, req in parsed_reqs - if not req.marker or (plat_env and evaluate(req.marker, env = plat_env.env)) - ] + plat_reqs = [] + for entry in parse_result.requirements: + marker = parsed_reqs[entry[1]].marker + if not marker or ( + plat_env and evaluate(marker, env = plat_env.env) + ): + plat_reqs.append(entry) + requirements[plat] = plat_reqs options[plat] = pip_args @@ -523,7 +525,7 @@ def _parse_requirements_from_req_files( parse_results, key = lambda x: (len(x[1].partition("==")[0]), x), ): - requirements_dict[names[entry[1]]] = entry + requirements_dict[parsed_reqs[entry[1]].name] = entry extra_pip_args_for_plat = options[plat] diff --git a/python/private/pypi/pep508_requirement.bzl b/python/private/pypi/pep508_requirement.bzl index a5c86a2a38..7dab8caf41 100644 --- a/python/private/pypi/pep508_requirement.bzl +++ b/python/private/pypi/pep508_requirement.bzl @@ -49,8 +49,12 @@ def requirement(spec): req = pypackaging.requirements.parse(spec) return struct( + # str: normalized PEP 503 package name using `-` separators. name = req.name, + # str: normalized package name using `_` separators for Bazel labels. name_ = req.name.replace("-", "_"), + # str: PEP 508 environment marker expression, or `""` if absent. marker = req.marker_str or "", + # list[str]: sorted normalized extra names using `_` separators. extras = [e.replace("-", "_") for e in req.extras], ) diff --git a/tests/pypi/pep508/requirement_tests.bzl b/tests/pypi/pep508/requirement_tests.bzl index b13691bdf7..2660886d57 100644 --- a/tests/pypi/pep508/requirement_tests.bzl +++ b/tests/pypi/pep508/requirement_tests.bzl @@ -42,10 +42,10 @@ def _test_requirement_line_parsing(env): "name_foo[bar]==0.25 @ http://foo.com ; os_name=='a'": ("name-foo", ["bar"], "os_name=='a'"), } - got = { - i: (parsed.name, parsed.extras, parsed.marker) - for i, parsed in {case: requirement(case) for case in want}.items() - } + got = {} + for case in want: + parsed = requirement(case) + got[case] = (parsed.name, parsed.extras, parsed.marker) env.expect.that_dict(got).contains_exactly(want) _tests.append(_test_requirement_line_parsing) From d30d0f215b3924e675b94e2fe88595d7bf988847 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:57:43 +0900 Subject: [PATCH 6/6] fix(pypi): only strip the options outside of the quoted strings of the requirement lines --- python/private/pypi/pep508_requirement.bzl | 28 +++++++++++++++++++--- tests/pypi/pep508/requirement_tests.bzl | 5 ++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/python/private/pypi/pep508_requirement.bzl b/python/private/pypi/pep508_requirement.bzl index 7dab8caf41..6798e06bd6 100644 --- a/python/private/pypi/pep508_requirement.bzl +++ b/python/private/pypi/pep508_requirement.bzl @@ -28,9 +28,7 @@ def requirement(spec): A struct with the information. """ - # The options are not a part of PEP 508. - for sep in (" --", "\t--"): - spec, _, _ = spec.partition(sep) + spec = _strip_options(spec) head, semicolon, marker = spec.partition(";") name, at, url = head.partition("@") @@ -58,3 +56,27 @@ def requirement(spec): # list[str]: sorted normalized extra names using `_` separators. extras = [e.replace("-", "_") for e in req.extras], ) + +def _strip_options(line): + """Remove the `requirements.txt` options, which are not a part of PEP 508. + + The options, e.g. `--hash=sha256:...` or `-C key=value`, start with a `-` + that follows a whitespace. The quoted strings of the markers are skipped, + because they may have such text as well. + """ + if " -" not in line and "\t-" not in line: + return line + + quote = None + prev = "" + for i, c in enumerate(line.elems()): + if quote: + if c == quote: + quote = None + elif c in ("\"", "'"): + quote = c + elif c == "-" and prev in (" ", "\t"): + return line[:i] + prev = c + + return line diff --git a/tests/pypi/pep508/requirement_tests.bzl b/tests/pypi/pep508/requirement_tests.bzl index 2660886d57..fd81a321cb 100644 --- a/tests/pypi/pep508/requirement_tests.bzl +++ b/tests/pypi/pep508/requirement_tests.bzl @@ -32,9 +32,14 @@ def _test_requirement_line_parsing(env): "name[extra]@http://foo.com": ("name", ["extra"], ""), "name[foo]": ("name", ["foo"], ""), "name[quux, strange];python_version<'2.7' and platform_version=='2'": ("name", ["quux", "strange"], "python_version<'2.7' and platform_version=='2'"), + "name_foo; platform_release != \"custom -x\"": ("name-foo", [], "platform_release != \"custom -x\""), + # The text that looks like an option in a quoted marker value is kept + "name_foo; platform_release != 'custom -- kernel' --hash=sha256:deadbeef": ("name-foo", [], "platform_release != 'custom -- kernel'"), "name_foo==0.25\t--hash=sha256:deadbeef": ("name-foo", [], ""), # The options may be indented with tabs in the requirements files "name_foo==0.25 \t--hash=sha256:deadbeef": ("name-foo", [], ""), + # The short options, e.g. `-C` for `--config-settings` + "name_foo==0.25 -Ckey=value --hash=sha256:deadbeef": ("name-foo", [], ""), "name_foo==0.25 ; os_name=='a' --hash=sha256:deadbeef": ("name-foo", [], "os_name=='a'"), "name_foo[bar]": ("name-foo", ["bar"], ""), "name_foo[bar]==0.25": ("name-foo", ["bar"], ""),