Newer
Older
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
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { regEx } from '../../../util/regex';
import { PypiDatasource } from '../../datasource/pypi';
import type { PackageDependency } from '../types';
import type { Pep508ParseResult } from './types';
const pep508Regex = regEx(
/^(?<packageName>[A-Z0-9._-]+)\s*(\[(?<extras>[A-Z0-9,._-]+)\])?\s*(?<currentValue>[^;]+)?(;\s*(?<marker>.*))?/i
);
export function parsePEP508(
value: string | null | undefined
): Pep508ParseResult | null {
if (is.nullOrUndefined(value)) {
return null;
}
const regExpExec = pep508Regex.exec(value);
if (
is.nullOrUndefined(regExpExec) ||
is.nullOrUndefined(regExpExec?.groups)
) {
logger.trace(`Pep508 could not be extracted`);
return null;
}
const result: Pep508ParseResult = {
packageName: regExpExec.groups.packageName,
};
if (is.nonEmptyString(regExpExec.groups.currentValue)) {
result.currentValue = regExpExec.groups.currentValue;
}
if (is.nonEmptyString(regExpExec.groups.marker)) {
result.marker = regExpExec.groups.marker;
}
if (is.nonEmptyString(regExpExec.groups.extras)) {
result.extras = regExpExec.groups.extras.split(',');
}
return result;
}
export function pep508ToPackageDependency(
depType: string,
value: string
): PackageDependency | null {
const parsed = parsePEP508(value);
if (is.nullOrUndefined(parsed)) {
return null;
}
const dep: PackageDependency = {
packageName: parsed.packageName,
depName: parsed.packageName,
datasource: PypiDatasource.id,
depType,
};
if (is.nullOrUndefined(parsed.currentValue)) {
dep.skipReason = 'unspecified-version';
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
} else {
dep.currentValue = parsed.currentValue;
}
return dep;
}
export function parseDependencyGroupRecord(
depType: string,
records: Record<string, string[]> | null | undefined
): PackageDependency[] {
if (is.nullOrUndefined(records)) {
return [];
}
const deps: PackageDependency[] = [];
for (const [groupName, pep508Strings] of Object.entries(records)) {
for (const dep of parseDependencyList(depType, pep508Strings)) {
deps.push({ ...dep, depName: `${groupName}/${dep.packageName!}` });
}
}
return deps;
}
export function parseDependencyList(
depType: string,
list: string[] | null | undefined
): PackageDependency[] {
if (is.nullOrUndefined(list)) {
return [];
}
const deps: PackageDependency[] = [];
for (const element of list) {
const dep = pep508ToPackageDependency(depType, element);
if (is.truthy(dep)) {
deps.push(dep);
}
}
return deps;
}