Skip to content

Refactor version extraction with regex#1116

Open
StevenClontz wants to merge 4 commits intomainfrom
StevenClontz-patch-2
Open

Refactor version extraction with regex#1116
StevenClontz wants to merge 4 commits intomainfrom
StevenClontz-patch-2

Conversation

@StevenClontz
Copy link
Member

Closes #1115

Hacked this out on GitHub.com so who knows if it works lol

Updated version extraction logic from requirements.txt to use regex.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the CLI’s requirements.txt version detection to support PreTeXt requirements with extras (e.g., pretext[prefigure] == 2.36.0), addressing issue #1115 where such lines were previously mishandled.

Changes:

  • Adds regex-based parsing for pretext / pretextbook pinned versions in requirements.txt.
  • Updates logic for extracting the pinned version from matching requirement lines.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

pretext/utils.py Outdated
Comment on lines +136 to +140
with open(pp / "requirements.txt", "r") as f:
REGEX = r"\s*pretext(book)?(\[.*\])?\s*==\s*(?P<version>[\d\.]*)\s*"gm
for line in f.readlines():
if ("pretext ==" in line) or ("pretextbook ==" in line):
return line.split("==")[1].strip()
if re.match(REGEX, line):
return re.match(REGEX, line).group("version")
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requirements_version() parsing logic changed, but there are no targeted tests covering supported requirement lines (e.g., pretext[prefigure] == 2.36.0, pretextbook == ..., whitespace variations). Add a unit test to lock in the intended matching behavior and prevent regressions.

Copilot uses AI. Check for mistakes.
pretext/utils.py Outdated
import logging
import logging.handlers
import psutil
import re
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re is imported twice in this module (already imported earlier). This will trip linters and is unnecessary; remove the newly added duplicate import.

Suggested change
import re

Copilot uses AI. Check for mistakes.
pretext/utils.py Outdated
return None
try:
with open(pp / "requirements.txt", "r") as f:
REGEX = r"\s*pretext(book)?(\[.*\])?\s*==\s*(?P<version>[\d\.]*)\s*"gm
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REGEX = r"..."gm is invalid Python syntax (it will raise a SyntaxError at import time). If you intended regex flags, pass them via re.compile(..., flags=...) (e.g., re.MULTILINE) instead of appending gm to the literal.

Suggested change
REGEX = r"\s*pretext(book)?(\[.*\])?\s*==\s*(?P<version>[\d\.]*)\s*"gm
REGEX = r"\s*pretext(book)?(\[.*\])?\s*==\s*(?P<version>[\d\.]*)\s*"

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@oscarlevin
Copy link
Member

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link

Copilot AI commented Mar 5, 2026

@oscarlevin I've opened a new pull request, #1122, to work on those changes. Once the pull request is ready, I'll request review from you.

oscarlevin and others added 2 commits March 4, 2026 19:17
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mishandling of requirements.txt

4 participants