-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
266 lines (245 loc) · 7.77 KB
/
index.ts
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
import url from 'url';
import changelogFilenameRegex from 'changelog-filename-regex';
import { logger } from '../../logger';
import { parse } from '../../util/html';
import { Http } from '../../util/http';
import { ensureTrailingSlash } from '../../util/url';
import { matches } from '../../versioning/pep440';
import * as pep440 from '../../versioning/pep440';
import { GetReleasesConfig, Release, ReleaseResult } from '../common';
export const id = 'pypi';
export const defaultRegistryUrls = [
process.env.PIP_INDEX_URL || 'https://pypi.org/pypi/',
];
export const registryStrategy = 'merge';
const githubRepoPattern = /^https?:\/\/github\.com\/[^\\/]+\/[^\\/]+$/;
const http = new Http(id);
type PypiJSONRelease = {
requires_python?: string;
upload_time?: string;
yanked?: boolean;
};
type Releases = Record<string, PypiJSONRelease[]>;
type PypiJSON = {
info: {
name: string;
home_page?: string;
project_urls?: Record<string, string>;
};
releases?: Releases;
};
function normalizeName(input: string): string {
return input.toLowerCase().replace(/(-|\.)/g, '_');
}
function compatibleVersions(
releases: Releases,
constraints: Record<string, string>
): string[] {
const versions = Object.keys(releases);
if (!(constraints?.python && pep440.isVersion(constraints.python))) {
return versions;
}
return versions.filter((version) =>
releases[version].some((release) => {
if (!release.requires_python) {
return true;
}
return matches(constraints.python, release.requires_python);
})
);
}
async function getDependency(
packageName: string,
hostUrl: string,
constraints: Record<string, string>
): Promise<ReleaseResult | null> {
const lookupUrl = url.resolve(hostUrl, `${packageName}/json`);
const dependency: ReleaseResult = { releases: null };
logger.trace({ lookupUrl }, 'Pypi api got lookup');
const rep = await http.getJson<PypiJSON>(lookupUrl);
const dep = rep?.body;
if (!dep) {
logger.trace({ dependency: packageName }, 'pip package not found');
return null;
}
logger.trace({ lookupUrl }, 'Got pypi api result');
if (
!(dep.info && normalizeName(dep.info.name) === normalizeName(packageName))
) {
logger.warn(
{ lookupUrl, lookupName: packageName, returnedName: dep.info.name },
'Returned name does not match with requested name'
);
return null;
}
if (dep.info?.home_page) {
dependency.homepage = dep.info.home_page;
if (githubRepoPattern.exec(dep.info.home_page)) {
dependency.sourceUrl = dep.info.home_page.replace('http://', 'https://');
}
}
if (dep.info?.project_urls) {
for (const [name, projectUrl] of Object.entries(dep.info.project_urls)) {
const lower = name.toLowerCase();
if (
!dependency.sourceUrl &&
(lower.startsWith('repo') ||
lower === 'code' ||
lower === 'source' ||
githubRepoPattern.exec(projectUrl))
) {
dependency.sourceUrl = projectUrl;
}
if (
!dependency.changelogUrl &&
([
'changelog',
'change log',
'changes',
'release notes',
'news',
"what's new",
].includes(lower) ||
changelogFilenameRegex.exec(lower))
) {
// from https://github.com/pypa/warehouse/blob/418c7511dc367fb410c71be139545d0134ccb0df/warehouse/templates/packaging/detail.html#L24
dependency.changelogUrl = projectUrl;
}
}
}
dependency.releases = [];
if (dep.releases) {
const versions = compatibleVersions(dep.releases, constraints);
dependency.releases = versions.map((version) => {
const releases = dep.releases[version] || [];
const { upload_time: releaseTimestamp } = releases[0] || {};
const isDeprecated = releases.some(({ yanked }) => yanked);
const result: Release = {
version,
releaseTimestamp,
};
if (isDeprecated) {
result.isDeprecated = isDeprecated;
}
return result;
});
}
return dependency;
}
function extractVersionFromLinkText(
text: string,
packageName: string
): string | null {
const srcPrefixes = [`${packageName}-`, `${packageName.replace(/-/g, '_')}-`];
for (const prefix of srcPrefixes) {
const suffix = '.tar.gz';
if (text.startsWith(prefix) && text.endsWith(suffix)) {
return text.replace(prefix, '').replace(/\.tar\.gz$/, '');
}
}
// pep-0427 wheel packages
// {distribution}-{version}(-{build tag})?-{python tag}-{abi tag}-{platform tag}.whl.
const wheelPrefix = packageName.replace(/[^\w\d.]+/g, '_') + '-';
const wheelSuffix = '.whl';
if (
text.startsWith(wheelPrefix) &&
text.endsWith(wheelSuffix) &&
text.split('-').length > 2
) {
return text.split('-')[1];
}
return null;
}
function cleanSimpleHtml(html: string): string {
return (
html
.replace(/<\/?pre>/, '')
// Certain simple repositories like artifactory don't escape > and <
.replace(
/data-requires-python="(.*?)>(.*?)"/g,
'data-requires-python="$1>$2"'
)
.replace(
/data-requires-python="(.*?)<(.*?)"/g,
'data-requires-python="$1<$2"'
)
);
}
async function getSimpleDependency(
packageName: string,
hostUrl: string,
constraints: Record<string, string>
): Promise<ReleaseResult | null> {
const lookupUrl = url.resolve(hostUrl, `${packageName}`);
const dependency: ReleaseResult = { releases: null };
const response = await http.get(lookupUrl);
const dep = response?.body;
if (!dep) {
logger.trace({ dependency: packageName }, 'pip package not found');
return null;
}
const root: HTMLElement = parse(cleanSimpleHtml(dep));
const links = root.querySelectorAll('a');
const releases: Releases = {};
for (const link of Array.from(links)) {
const version = extractVersionFromLinkText(link.text, packageName);
if (version) {
const release: PypiJSONRelease = {
yanked: link.hasAttribute('data-yanked'),
};
const requiresPython = link.getAttribute('data-requires-python');
if (requiresPython) {
release.requires_python = requiresPython;
}
if (!releases[version]) {
releases[version] = [];
}
releases[version].push(release);
}
}
const versions = compatibleVersions(releases, constraints);
dependency.releases = versions.map((version) => {
const versionReleases = releases[version] || [];
const isDeprecated = versionReleases.some(({ yanked }) => yanked);
const result: Release = { version };
if (isDeprecated) {
result.isDeprecated = isDeprecated;
}
return result;
});
return dependency;
}
export async function getReleases({
constraints,
lookupName,
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const hostUrl = ensureTrailingSlash(registryUrl);
// not all simple indexes use this identifier, but most do
if (hostUrl.endsWith('/simple/') || hostUrl.endsWith('/+simple/')) {
logger.trace({ lookupName, hostUrl }, 'Looking up pypi simple dependency');
return getSimpleDependency(lookupName, hostUrl, constraints);
}
logger.trace({ lookupName, hostUrl }, 'Looking up pypi api dependency');
try {
// we need to resolve early here so we can catch any 404s and fallback to a simple lookup
const releases = await getDependency(lookupName, hostUrl, constraints);
// the dep was found in the json api, return as-is
return releases;
} catch (err) {
if (err.statusCode !== 404) {
throw err;
}
// error contacting json-style api -- attempt to fallback to a simple-style api
logger.trace(
{ lookupName, hostUrl },
'Looking up pypi simple dependency via fallback'
);
const releases = await getSimpleDependency(
lookupName,
hostUrl,
constraints
);
return releases;
}
}