|
1 | 1 | import re
|
2 | 2 | from importlib.metadata import version
|
3 |
| -from typing import List |
4 | 3 |
|
5 | 4 | new_pins = {"pre-commit": version("pre-commit")}
|
6 | 5 | pre_commit_path = ".pre-commit-config.yaml"
|
7 | 6 | with open(pre_commit_path) as f:
|
8 | 7 | tools = f.read().split("repo")
|
9 |
| - for tool in tools: |
10 |
| - package_match = re.search("id: (.*)", tool) |
11 |
| - if not package_match: |
12 |
| - continue |
13 |
| - version_match = re.search("rev: (.*)", tool) |
14 |
| - if not version_match: |
15 |
| - continue |
16 |
| - new_pins[package_match.group(1)] = version_match.group(1) |
| 8 | +for tool in tools: |
| 9 | + package_match = re.search("id: (.*)", tool) |
| 10 | + if not package_match: |
| 11 | + continue |
| 12 | + version_match = re.search("rev: (.*)", tool) |
| 13 | + if not version_match: |
| 14 | + continue |
| 15 | + new_pins[package_match.group(1).lstrip()] = version_match.group(1).lstrip() |
17 | 16 |
|
18 |
| -lines: List[str] = [] |
19 | 17 | requirements_path = "dev-requirements.txt"
|
20 | 18 | with open(requirements_path) as f:
|
21 |
| - lines = f.readlines() |
22 |
| - for ind, line in enumerate(lines): |
23 |
| - package_match = re.search("(.*)==(.*)", line) |
24 |
| - if not package_match: |
25 |
| - continue |
26 |
| - package_name = package_match.group(1) |
27 |
| - if package_name in new_pins: |
28 |
| - lines[ind] = re.sub("==(.*)", f"=={new_pins[package_name]}", line) |
| 19 | + tools = f.readlines() |
| 20 | +for ind, tool in enumerate(tools): |
| 21 | + package_match = re.search("(.*)==(.*)", tool) |
| 22 | + if not package_match: |
| 23 | + continue |
| 24 | + package_name = package_match.group(1) |
| 25 | + if package_name in new_pins: |
| 26 | + tools[ind] = re.sub("==(.*)", f"=={new_pins[package_name]}", tool) |
29 | 27 | with open(requirements_path, "w") as f:
|
30 |
| - f.writelines(lines) |
| 28 | + f.writelines(tools) |
0 commit comments