-
Notifications
You must be signed in to change notification settings - Fork 313
Expand file tree
/
Copy path_ecosystems.py
More file actions
195 lines (165 loc) · 5.48 KB
/
_ecosystems.py
File metadata and controls
195 lines (165 loc) · 5.48 KB
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
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Ecosystem helpers."""
import re
from .ecosystems_base import EnumerableEcosystem, OrderedEcosystem
from .alpine import Alpine, APK
from .bioconductor import Bioconductor
from .cran import CRAN
from .debian import Debian
from .echo import Echo
from .haskell import Hackage, GHC
from .hex import Hex
from .maven import Maven
from .nuget import NuGet
from .opam import Opam
from .packagist import Packagist
from .pub import Pub
from .pypi import PyPI
from .redhat import RPM
from .root import Root
from .rubygems import RubyGems
from .semver_ecosystem_helper import SemverEcosystem, SemverLike
from .ubuntu import Ubuntu
_ecosystems = {
'AlmaLinux': RPM,
'Alpaquita': APK,
'Azure Linux': RPM,
'Alpine': Alpine,
'BellSoft Hardened Containers': APK,
'Bioconductor': Bioconductor,
'Bitnami': SemverEcosystem,
'Chainguard': APK,
'CleanStart': APK,
'CRAN': CRAN,
'crates.io': SemverEcosystem,
'Debian': Debian,
'Docker Hardened Images': SemverEcosystem,
'Echo': Echo,
'GHC': GHC,
'Go': SemverEcosystem,
'Hackage': Hackage,
'Hex': Hex,
'Julia': SemverEcosystem,
'Mageia': RPM,
'Maven': Maven,
'MinimOS': APK,
'npm': SemverEcosystem,
'NuGet': NuGet,
'opam': Opam,
'openEuler': RPM,
'openSUSE': RPM,
'Packagist': Packagist,
'Pub': Pub,
'PyPI': PyPI,
'Red Hat': RPM,
'Rocky Linux': RPM,
'Root': Root,
'RubyGems': RubyGems,
'SUSE': RPM,
'SwiftURL': SemverEcosystem,
'Ubuntu': Ubuntu,
'VSCode': SemverLike,
'Wolfi': APK,
# Ecosystems known in the schema, but without implementations.
# Must be kept in sync with osv-schema.
'Android': None,
'ConanCenter': None,
'GIT': None,
'GitHub Actions': None,
'Kubernetes': None,
'Linux': None,
'OSS-Fuzz': None,
'Photon OS': None,
'TuxCare': None,
}
def is_semver(ecosystem: str) -> bool:
"""Returns whether an ecosystem uses 'SEMVER' range types"""
return isinstance(get(ecosystem), SemverEcosystem)
def is_known(ecosystem: str) -> bool:
"""Returns whether an ecosystem is known to OSV
(even if ordering is not supported)."""
name, _, _ = ecosystem.partition(':')
return name in _ecosystems
package_urls = {
'Android': 'https://android.googlesource.com/',
'CRAN': 'https://cran.r-project.org/web/packages/',
'crates.io': 'https://crates.io/crates/',
'Debian': 'https://packages.debian.org/src:',
'GitHub Actions': 'https://github.com/marketplace/actions/',
'Go': 'https://',
'Hackage': 'https://hackage.haskell.org/package/',
'Hex': 'https://hex.pm/packages/',
'Mageia': 'https://madb.mageia.org/show?rpm=',
'npm': 'https://www.npmjs.com/package/',
'NuGet': 'https://www.nuget.org/packages/',
'Packagist': 'https://packagist.org/packages/',
'Pub': 'https://pub.dev/packages/',
'PyPI': 'https://pypi.org/project/',
'Rocky Linux': 'https://pkgs.org/download/',
'RubyGems': 'https://rubygems.org/gems/',
}
_OSV_TO_DEPS_ECOSYSTEMS_MAP = {
'npm': 'npm',
'Go': 'go',
'Maven': 'maven',
'PyPI': 'pypi',
'NuGet': 'nuget',
'crates.io': 'cargo'
}
def get(name: str) -> OrderedEcosystem | EnumerableEcosystem | None:
"""Get ecosystem helpers for a given ecosystem."""
name, _, suffix = name.partition(':')
ecosys = _ecosystems.get(name)
if ecosys is None:
return None
return ecosys(suffix)
def normalize(ecosystem_name: str):
return ecosystem_name.split(':')[0]
def remove_variants(ecosystem_name: str) -> str | None:
result = None
# For Ubuntu, remove ":Pro" and ":LTS"
if ecosystem_name.startswith('Ubuntu'):
result = ecosystem_name.replace(':Pro', '').replace(':LTS', '')
return result
def add_matching_ecosystems(original_set: set[str]) -> set[str]:
"""
For Linux distributions, some release versions may have different variants.
For example, Ubuntu:22.04 is equivalent to Ubuntu:22.04:LTS.
This function adds all matching ecosystems
to the datastore to facilitate API queries.
For example:
- "Ubuntu:Pro:18.04:LTS" would also be "Ubuntu:18.04"
Args:
original_set: The original ecosystem set
Returns:
A new set with the added matching ecosystems.
"""
new_set = set(original_set)
for ecosystem in original_set:
# For Ubuntu, remove ":Pro" and ":LTS"
new_ecosystem = remove_variants(ecosystem)
if new_ecosystem:
new_set.add(new_ecosystem)
return new_set
def is_supported_in_deps_dev(ecosystem_name: str) -> bool:
return ecosystem_name in _OSV_TO_DEPS_ECOSYSTEMS_MAP
def map_ecosystem_to_deps_dev(ecosystem_name: str) -> str | None:
return _OSV_TO_DEPS_ECOSYSTEMS_MAP.get(ecosystem_name)
def maybe_normalize_package_names(package_name: str, ecosystem: str) -> str:
"""Normalize package names as necessary."""
if ecosystem in ('PyPI', 'Echo:PyPI'):
# per https://peps.python.org/pep-0503/#normalized-names
package_name = re.sub(r'[-_.]+', '-', package_name).lower()
return package_name