Skip to content

Commit

Permalink
Implement dependency bumping
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeNaccarato committed Aug 16, 2023
1 parent 359cd03 commit dea0c34
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ jobs:
python-version: "3.11"
install-project: false
- run: "copier update --defaults --vcs-ref $(git rev-parse HEAD:template)"
shell: "pwsh"
- run: "python .tools/scripts/update_requirements.py"
- name: "Check whether bumped requirements install properly"
run: |-
pip install .
pip install --requirement .tools/requirements/requirements.txt --requirement .tools/requirements/requirements_ci.txt
pip install --no-deps --requirement .tools/requirements/requirements_nodeps.txt
- uses: "stefanzweifel/git-auto-commit-action@v4.16.0"
with:
commit_message: "Update project from template."
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "typings"]
path = typings
url = https://github.com/blakeNaccarato/pylance-stubs-unofficial.git
[submodule "boilercore"]
path = boilercore
url = https://github.com/blakeNaccarato/boilercore
1 change: 1 addition & 0 deletions .tools/requirements/requirements_both.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# Template
copier==8.1.0
dulwich==0.21.5

# Build
flit==3.9.0
Expand Down
86 changes: 86 additions & 0 deletions .tools/scripts/update_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""Update requirements versions which are coupled to others."""

from contextlib import closing
from dataclasses import dataclass
from pathlib import Path
from re import MULTILINE, VERBOSE, Pattern, compile

from dulwich.porcelain import submodule_list
from dulwich.repo import Repo


def main():
requirements_files = [
Path("pyproject.toml"),
*sorted(Path(".tools/requirements").glob("requirements*.txt")),
]
with closing(repo := Repo(str(Path.cwd()))):
submodules = [Submodule(*item) for item in list(submodule_list(repo))]
dependency_relation = r"(?P<relation>[=~>]=)" # ==, ~=, or >=
for file in requirements_files:
original_content = content = file.read_text("utf-8")
if pandas_match := compile_specific(
rf"""
pandas # pandas
(\[[\w,]+\])? # e.g. [hdf5,performance] (optional)
{dependency_relation} # e.g. ==
(?P<version>[\w\d\.]*) # e.g. 2.0.2
"""
).search(content):
content = compile_specific(
rf"""
(?P<dep>pandas-stubs) # pandas-stubs
{dependency_relation} # e.g. ~=
(?P<version>[\w\d\.]*) # e.g. 2.0.2
"""
).sub(
repl=rf"\g<prefix>\g<dep>\g<relation>{pandas_match['version']}\g<suffix>",
string=content,
)
for sub in submodules:
content = compile_specific(
rf"""
{sub.name}@ # name@
(?P<domain>git\+https://github\.com/) # git+https://github.com/
(?P<org>\w+/) # org/
{sub.name}@ # name@
(?P<commit>\w+) # <commit-hash>
$"""
).sub(
repl=rf"\g<prefix>{sub.name}@\g<domain>\g<org>{sub.name}@{sub.commit}\g<suffix>",
string=content,
)
if content != original_content:
file.write_text(encoding="utf-8", data=content)


@dataclass
class Submodule:
"""Represents a git submodule."""

name: str
"""The submodule name."""
commit: str
"""The commit hash currently tracked by the submodule."""

def __post_init__(self):
"""Handle byte strings reported by some submodule sources, like dulwich."""
# dulwich.porcelain.submodule_list returns bytes
if isinstance(self.name, bytes):
self.name = self.name.decode("utf-8")


def compile_specific(pattern: str) -> Pattern[str]:
"""Compile verbose, multi-line regex pattern with a specific prefix and suffix."""
return compile(
flags=VERBOSE | MULTILINE,
pattern=rf"""^
(?P<prefix>\s*['"])? # Optional `"` as in pyproject.toml
{pattern}
(?P<suffix>['"],)? # Optional `",` as in pyproject.toml
$""",
)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions boilercore
Submodule boilercore added at 78aba4

0 comments on commit dea0c34

Please sign in to comment.