-
Notifications
You must be signed in to change notification settings - Fork 4
/
update_precommit.py
executable file
·260 lines (188 loc) · 7.49 KB
/
update_precommit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python3
import re
from abc import ABC, abstractmethod
from copy import deepcopy
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable
import requests
import yaml
REPOSITORIES = "repos"
REPOSITORY = "repo"
MYPY_REPOSITORY = "https://github.com/pre-commit/mirrors-mypy"
HOOKS = "hooks"
ADDITIONAL_DEPENDENCIES = "additional_dependencies"
CAPTURE_GROUP_URL = "url"
CAPTURE_GROUP_PACKAGE = "package"
CAPTURE_GROUP_VERSION = "version"
class AdditionalMypyDependency(ABC):
@abstractmethod
def __hash__(self) -> int:
raise NotImplementedError
@abstractmethod
def __eq__(self, other: object) -> bool:
raise NotImplementedError
@abstractmethod
def serialize(self) -> str:
raise NotImplementedError
class Package(AdditionalMypyDependency):
@property
@abstractmethod
def name(self) -> str:
raise NotImplementedError
@property
@abstractmethod
def version(self) -> str | None:
raise NotImplementedError
def __hash__(self) -> int:
return hash((self.name, self.version))
def __eq__(self, other: object) -> bool:
if not isinstance(other, Package):
return False
return (self.name, self.version) == (other.name, other.version)
class TypeStubPackage(Package):
@property
def name(self) -> str:
return self._name
@property
def version(self) -> str | None:
return self._version
def __init__(self, name: str, version: str | None) -> None:
self._name = name
self._version = version
def serialize(self) -> str:
return self.name
class NormalPackage(Package):
@property
def name(self) -> str:
return self._name
@property
def version(self) -> str | None:
return self._version
def __init__(self, name: str, version: str | None) -> None:
self._name = name
self._version = version
def serialize(self) -> str:
if self.version:
return self.name + "==" + self.version
return self.name
@dataclass
class ExtraIndexUrl(AdditionalMypyDependency):
url: str
def __hash__(self) -> int:
return hash(self.url)
def __eq__(self, other: object) -> bool:
if not isinstance(other, ExtraIndexUrl):
return False
return self.url == other.url
def serialize(self) -> str:
return f"--extra-index-url={self.url}"
class CustomDumper(yaml.SafeDumper):
def increase_indent(self, flow: bool = False, indentless: bool = False) -> None:
return super(CustomDumper, self).increase_indent(flow, False)
def parse_multiple_requirements_file(
files: Iterable[Path],
) -> set[AdditionalMypyDependency]:
packages = set()
for _file in files:
packages.update(parse_requirements_file(_file))
return packages
def parse_requirements_file(requirements_file: Path) -> set[AdditionalMypyDependency]:
"""Parse requirements.txt and extract package names using regex."""
with open(requirements_file, "r") as file:
lines = file.readlines()
packages = set()
for line in lines:
line = line.strip()
if (
line and not line.startswith("#") and line != "-r requirements.txt"
): # Ignore empty lines, comments '-r requirements.txt'
package_name = parse_requirement(line)
if package_name:
packages.add(package_name)
return packages
pattern_package = re.compile(
r"^(?!--extra-index-url)(?P<package>[a-zA-Z0-9_\-\.]+)(?:[<>=~!]+(?P<version>\S*))?"
)
pattern_extra_index_url = re.compile(r"^--extra-index-url\s+(?P<url>\S+)")
def parse_requirement(requirement_line: str) -> AdditionalMypyDependency | None:
"""Extract package name from a requirement line using regex."""
# Regex pattern to capture the package name, ignoring version specifiers
if match_extra_index_url := pattern_extra_index_url.match(requirement_line):
return create_extra_index_url(
match_extra_index_url.group(CAPTURE_GROUP_URL).strip()
)
match = pattern_package.match(requirement_line)
if not match:
return None
package_name = match.group(CAPTURE_GROUP_PACKAGE).strip()
if package_version := match.group(CAPTURE_GROUP_VERSION):
package_version = package_version.strip()
return create_package(name=package_name, version=package_version)
def create_extra_index_url(url: str) -> AdditionalMypyDependency:
return ExtraIndexUrl(url)
def create_package(name: str, version: str | None) -> AdditionalMypyDependency:
"""Check if a type stub exists for a given package name and return it."""
types_package_name = f"types-{name}"
if __check_types_for_package_exists(types_package_name):
return create_type_stub_package(name=types_package_name, version=version)
# Some packages already provide type stubs with their package
# If they don't pre-commit mypy won't fail
return create_normal_package(name=name, version=version)
def __check_types_for_package_exists(package_name: str) -> bool:
response = requests.get(f"https://pypi.org/pypi/{package_name}/json")
return response.status_code == 200
def create_type_stub_package(
name: str, version: str | None
) -> AdditionalMypyDependency:
return TypeStubPackage(name=name, version=version)
def create_normal_package(name: str, version: str | None) -> AdditionalMypyDependency:
return NormalPackage(name=name, version=version)
def serialize_packages(packages: Iterable[AdditionalMypyDependency]) -> list[str]:
"""Converts packages to a serializable format."""
return sorted([package.serialize() for package in packages])
def read_precommit_file(precommit_file: Path) -> dict:
with open(precommit_file, "r") as stream:
yaml_config = yaml.safe_load(stream)
return yaml_config
def update_precommit_config(config: dict, type_stubs: list[str]) -> dict:
updated_config = deepcopy(config)
for repo in updated_config[REPOSITORIES]:
if repo[REPOSITORY] == MYPY_REPOSITORY:
repo[HOOKS][0][ADDITIONAL_DEPENDENCIES] = type_stubs
break
return updated_config
def save_precommit_config(config: dict, save_path: Path) -> None:
with open(save_path, "w") as yaml_file:
yaml.dump(
data=config,
stream=yaml_file,
Dumper=CustomDumper,
explicit_start=True,
default_flow_style=False,
sort_keys=False,
)
def display_available_type_stubs(type_stubs: list[str]) -> None:
if type_stubs:
print("\nType stubs that can be added to your pre-commit configuration:")
for stub in type_stubs:
print(f"- {stub}")
else:
print("\n No type stubs to be added to your pre-commit configuration.")
def type_stubs_have_changed(actual: dict, to_compare: dict) -> bool:
return actual != to_compare
def main() -> None:
requirements_file = Path("requirements.txt")
requirements_dev_file = Path("requirements-dev.txt")
precommit_file = Path(".pre-commit-config.yaml")
additional_dependencies = parse_multiple_requirements_file(
[requirements_file, requirements_dev_file]
)
serializable_dependencies = serialize_packages(additional_dependencies)
precommit_config = read_precommit_file(precommit_file)
updated_precommit_config = update_precommit_config(
precommit_config, serializable_dependencies
)
save_precommit_config(updated_precommit_config, precommit_file)
if __name__ == "__main__":
main()