-
Notifications
You must be signed in to change notification settings - Fork 1
/
rating.py
333 lines (272 loc) · 10.8 KB
/
rating.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import datetime
import json
import os
from functools import cached_property
from pathlib import Path
from typing import TYPE_CHECKING, TypedDict, Optional, Union, List, Tuple, Dict
from platformdirs import user_cache_dir
from pip_rating import __version__
from pip_rating._compat import cache
from pip_rating.sources.audit import Vulnerability
from pip_rating.sources.sourcerank import SourceRankBreakdown
if TYPE_CHECKING:
from pip_rating.packages import Package
RATING_CACHE_DIR = Path(user_cache_dir()) / "pip-rating" / "rating"
MAX_CACHE_AGE = datetime.timedelta(days=7)
class PypiPackage(TypedDict):
latest_upload_iso_dt: Optional[str]
first_upload_iso_dt: Optional[str]
class SourcecodePage(TypedDict):
package_in_readme: Optional[bool]
class PackageRatingParams(TypedDict):
sourcerank_breakdown: SourceRankBreakdown
pypi_package: PypiPackage
sourcecode_page: SourcecodePage
class PackageRatingCache(TypedDict):
package_name: str
updated_at: str
schema_version: str
params: PackageRatingParams
class ScoreBase:
def __add__(self, other: "ScoreBase"):
raise NotImplementedError
def __int__(self) -> int:
raise NotImplementedError
def __repr__(self) -> str:
raise NotImplementedError
def __str__(self) -> str:
return repr(self)
class ScoreValue(ScoreBase):
def __init__(self, value: int):
self.value = value
def __add__(self, other: "ScoreBase") -> "ScoreBase":
if isinstance(other, ScoreValue):
return ScoreValue(int(self) + int(other))
elif isinstance(other, Max):
return other + self
def __int__(self) -> int:
return self.value
def __repr__(self) -> str:
return f"{self.value}"
class Max(ScoreBase):
def __init__(self, max_score: int, current_score: int = 0):
self.max_score = max_score
self.current_score = current_score
def __add__(self, other: ScoreBase):
if isinstance(other, ScoreValue):
score = self.current_score + int(other)
self.current_score = min(self.max_score, score)
if isinstance(other, Max) and other.max_score < self.max_score:
other.current_score += self.current_score
return other
if isinstance(other, Max) and other.max_score > self.max_score:
self.current_score += other.current_score
return self
return self
def __int__(self) -> int:
return min(self.current_score, self.max_score)
def __str__(self):
return f"Max({self.max_score})"
def __repr__(self) -> str:
return f"<Max current: {self.current_score} max: {self.max_score}>"
class BreakdownBase:
breakdown_key: str
def get_score(self, package_rating: "PackageRating") -> ScoreBase:
raise NotImplementedError
def get_breakdown_value(
self, package_rating: "PackageRating"
) -> Union[int, bool, str]:
value = package_rating.params
for subkey in self.breakdown_key.split("."):
value = value[subkey]
return value
class PackageBreakdown(BreakdownBase):
def __init__(self, breakdown_key: str, score: Optional[Union[int, Max]] = None):
self.breakdown_key = breakdown_key
self._score = score
def get_score(self, package_rating: "PackageRating") -> ScoreValue:
value = self.get_breakdown_value(package_rating)
if value and self._score:
return ScoreValue(self._score)
if not value and self._score:
return ScoreValue(0) # the default is 0
if isinstance(value, bool):
raise ValueError("Cannot calculate score for boolean value")
return ScoreValue(value)
class DateBreakdown(BreakdownBase):
def __init__(
self, breakdown_key: str, scores: Dict[datetime.timedelta, int], default: int
):
self.breakdown_key = breakdown_key
self.scores = scores
self.default = default
def get_score(self, package_rating: "PackageRating") -> ScoreValue:
iso_dt = self.get_breakdown_value(package_rating)
if not iso_dt:
return ScoreValue(0)
dt = datetime.datetime.fromisoformat(iso_dt)
dt_delta = datetime.datetime.now(datetime.timezone.utc) - dt
for delta, score in self.scores.items():
if dt_delta < delta:
return ScoreValue(score)
return ScoreValue(self.default)
class NullBoolBreakdown(BreakdownBase):
def __init__(self, breakdown_key: str, scores: Dict[bool, ScoreBase]):
self.breakdown_key = breakdown_key
self.scores = scores
def get_score(self, package_rating: "PackageRating") -> ScoreBase:
value = self.get_breakdown_value(package_rating)
return self.scores[value]
BREAKDOWN_SCORES = [
PackageBreakdown("sourcerank_breakdown.basic_info_present", 1),
PackageBreakdown("sourcerank_breakdown.source_repository_present", 1),
PackageBreakdown("sourcerank_breakdown.readme_present", 1),
PackageBreakdown("sourcerank_breakdown.license_present", 1),
PackageBreakdown("sourcerank_breakdown.has_multiple_versions", 3),
PackageBreakdown("sourcerank_breakdown.dependent_projects"),
PackageBreakdown("sourcerank_breakdown.dependent_repositories"),
PackageBreakdown("sourcerank_breakdown.stars"),
PackageBreakdown("sourcerank_breakdown.contributors"),
DateBreakdown(
"pypi_package.latest_upload_iso_dt",
{
datetime.timedelta(days=30 * 4): 4,
datetime.timedelta(days=30 * 6): 3,
datetime.timedelta(days=365): 2,
datetime.timedelta(days=365 + (30 * 6)): 1,
datetime.timedelta(days=365 * 3): 0,
datetime.timedelta(days=365 * 4): -2,
},
default=-4,
),
DateBreakdown(
"pypi_package.first_upload_iso_dt",
{
datetime.timedelta(days=15): Max(0),
datetime.timedelta(days=30): -3,
datetime.timedelta(days=60): -2,
datetime.timedelta(days=90): -1,
datetime.timedelta(days=180): 0,
datetime.timedelta(days=360): 1,
datetime.timedelta(days=360 * 2): 2,
datetime.timedelta(days=360 * 4): 3,
},
default=4,
),
NullBoolBreakdown(
"sourcecode_page.package_in_readme",
{True: ScoreValue(1), False: Max(0), None: ScoreValue(0)},
),
]
class PackageRatingJson(TypedDict):
rating_score: int
global_rating_score: int
vulnerabilities: List[Vulnerability]
params: PackageRatingParams
class PackageRating:
def __init__(
self, package: "Package", params: Optional[PackageRatingParams] = None
):
self.package = package
if not params and self.is_cache_expired:
params = self.get_params_from_package()
self.save_to_cache()
elif not params:
params = self.get_params_from_cache()
self.params: PackageRatingParams = params
@property
def is_cache_expired(self) -> bool:
return (
not self.cache_path.exists()
or self.cache_path.stat().st_mtime
< (datetime.datetime.now() - MAX_CACHE_AGE).timestamp()
)
@property
def cache_path(self) -> Path:
return RATING_CACHE_DIR / f"{self.package.name}.json"
def get_from_cache(self) -> Optional[PackageRatingCache]:
with open(self.cache_path) as file:
data = json.load(file)
if data["schema_version"] != __version__:
return None
return data
def save_to_cache(self) -> PackageRatingCache:
cache = {
"package_name": self.package.name,
"updated_at": datetime.datetime.now().isoformat(),
"schema_version": __version__,
"params": self.get_params_from_package(),
}
os.makedirs(str(self.cache_path.parent), exist_ok=True)
with open(str(self.cache_path), "w") as file:
json.dump(cache, file)
return cache
def get_params_from_cache(self) -> PackageRatingParams:
cache = self.get_from_cache()
if cache is None:
cache = self.save_to_cache()
return cache["params"]
def get_params_from_package(self) -> PackageRatingParams:
return {
"sourcerank_breakdown": self.package.sourcerank.breakdown,
"pypi_package": {
"latest_upload_iso_dt": self.package.pypi.latest_upload_iso_dt,
"first_upload_iso_dt": self.package.pypi.first_upload_iso_dt,
},
"sourcecode_page": {
"package_in_readme": self.package.sourcecode_page.package_in_readme,
},
}
@cached_property
def breakdown_scores(self) -> List[Tuple[str, ScoreBase]]:
return [
(breakdown.breakdown_key, breakdown.get_score(self))
for breakdown in BREAKDOWN_SCORES
]
@cached_property
def descendant_rating_scores(self) -> List[Tuple["Package", int]]:
return [
(package, package.rating.get_rating_score(self.package))
for package in self.package.get_descendant_packages()
]
@cached_property
def rating_score(self):
scores = dict(self.breakdown_scores).values()
value = ScoreValue(0)
for score in scores:
value += score
return int(value)
@cache
def get_vulnerabilities(
self, from_package: Optional["Package"] = None
) -> List["Vulnerability"]:
node = None
if from_package is not None:
node = self.package.get_node_from_parent(from_package)
elif from_package is None:
node = self.package.first_node
# get_audit requires a node, so we only call it if we have one and this is used
# instead of the package's own rating score
if node is not None:
return self.package.get_audit(node).vulnerabilities
return []
def get_rating_score(self, from_package: Optional["Package"] = None) -> int:
self.package.dependencies.results.analizing_package(
self.package.name, self.package.dependencies.total_size
)
if len(self.get_vulnerabilities(from_package)):
return 0
return self.rating_score
def get_global_rating_score(self, from_package: Optional["Optional"] = None) -> int:
return min(
[self.get_rating_score(from_package)]
+ list(dict(self.descendant_rating_scores).values()),
default=0,
)
def as_json(self, from_package: Optional["Package"] = None) -> PackageRatingJson:
return {
"rating_score": self.get_rating_score(from_package),
"global_rating_score": self.get_global_rating_score(from_package),
"vulnerabilities": self.get_vulnerabilities(from_package),
"params": self.params,
}