Skip to content

Commit eb5bb3a

Browse files
StevenClontzoscarlevinCopilotCopilot
authored
Refactor version extraction with regex (#1116)
* Refactor version extraction with regex Updated version extraction logic from requirements.txt to use regex. * Update pretext/utils.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update pretext/utils.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix regex-based version extraction in requirements.txt parsing (#1122) * Initial plan * Fix review comments: remove duplicate import, fix regex syntax, add tests Co-authored-by: oscarlevin <6504596+oscarlevin@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: oscarlevin <6504596+oscarlevin@users.noreply.github.com> Co-authored-by: Oscar Levin <oscar.levin@gmail.com> * format * lint --------- Co-authored-by: Oscar Levin <oscar.levin@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: oscarlevin <6504596+oscarlevin@users.noreply.github.com> Co-authored-by: Oscar Levin <oscar.levin@unco.edu>
1 parent e833c0a commit eb5bb3a

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

pretext/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ def requirements_version(dirpath: Optional[Path] = None) -> Optional[str]:
133133
return None
134134
try:
135135
with open(pp / "requirements.txt", "r") as f:
136+
regex = r"\s*pretext(book)?(\[.*\])?\s*==\s*(?P<version>[\d\.]+)\s*"
136137
for line in f.readlines():
137-
if ("pretext ==" in line) or ("pretextbook ==" in line):
138-
return line.split("==")[1].strip()
138+
match = re.match(regex, line)
139+
if match:
140+
return match.group("version")
139141
except Exception as e:
140142
log.debug("Could not read `requirements.txt`:")
141143
log.debug(e)

tests/test_utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,34 @@ def test_is_unmodified() -> None:
4242
b"foo\n<!-- Managed automatically by PreTeXt authoring tools -->\nbar"
4343
)
4444
assert utils.is_unmodified("foo", magic_comment)
45+
46+
47+
def test_requirements_version(tmp_path: Path) -> None:
48+
# Create a minimal project.ptx so project_path() can find the project root
49+
(tmp_path / "project.ptx").write_text("")
50+
51+
cases = [
52+
("pretext == 2.36.0", "2.36.0"),
53+
("pretextbook == 1.2.3", "1.2.3"),
54+
("pretext[prefigure] == 2.36.0", "2.36.0"),
55+
(" pretext == 3.0.0 ", "3.0.0"),
56+
("pretext[prefigure,extra] == 0.9.1", "0.9.1"),
57+
]
58+
59+
for line, expected in cases:
60+
req_file = tmp_path / "requirements.txt"
61+
req_file.write_text(line + "\n")
62+
assert utils.requirements_version(tmp_path) == expected, f"Failed for: {line!r}"
63+
64+
# Lines that should NOT match
65+
non_matching = [
66+
"numpy == 1.0.0",
67+
"pretext ==",
68+
"pretext",
69+
]
70+
for line in non_matching:
71+
req_file = tmp_path / "requirements.txt"
72+
req_file.write_text(line + "\n")
73+
assert (
74+
utils.requirements_version(tmp_path) is None
75+
), f"Should not match: {line!r}"

0 commit comments

Comments
 (0)