/
extract.ts
169 lines (154 loc) · 4.77 KB
/
extract.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
import toml from '@iarna/toml';
import { RANGE_PATTERN } from '@renovate/pep440/lib/specifier';
import is from '@sindresorhus/is';
import * as datasourcePypi from '../../datasource/pypi';
import { logger } from '../../logger';
import { SkipReason } from '../../types';
import { localPathExists } from '../../util/fs';
import type { PackageDependency, PackageFile } from '../types';
// based on https://www.python.org/dev/peps/pep-0508/#names
const packageRegex = /^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$/i;
const rangePattern: string = RANGE_PATTERN;
const specifierPartPattern = `\\s*${rangePattern.replace(
/\?<\w+>/g,
'?:'
)}\\s*`;
const specifierPattern = `${specifierPartPattern}(?:,${specifierPartPattern})*`;
interface PipSource {
name: string;
url: string;
}
interface PipFile {
source: PipSource[];
packages?: Record<string, PipRequirement>;
'dev-packages'?: Record<string, PipRequirement>;
requires?: Record<string, string>;
}
interface PipRequirement {
index?: string;
version?: string;
path?: string;
file?: string;
git?: string;
}
function extractFromSection(
pipfile: PipFile,
section: 'packages' | 'dev-packages'
): PackageDependency[] {
if (!(section in pipfile)) {
return [];
}
const specifierRegex = new RegExp(`^${specifierPattern}$`);
const pipfileSection = pipfile[section];
const deps = Object.entries(pipfileSection)
.map((x) => {
const [depName, requirements] = x;
let currentValue: string;
let nestedVersion: boolean;
let skipReason: SkipReason;
if (requirements.git) {
skipReason = SkipReason.GitDependency;
} else if (requirements.file) {
skipReason = SkipReason.FileDependency;
} else if (requirements.path) {
skipReason = SkipReason.LocalDependency;
} else if (requirements.version) {
currentValue = requirements.version;
nestedVersion = true;
} else if (is.object(requirements)) {
skipReason = SkipReason.AnyVersion;
} else {
currentValue = requirements;
}
if (currentValue === '*') {
skipReason = SkipReason.AnyVersion;
}
if (!skipReason) {
const packageMatches = packageRegex.exec(depName);
if (!packageMatches) {
logger.debug(
`Skipping dependency with malformed package name "${depName}".`
);
skipReason = SkipReason.InvalidName;
}
const specifierMatches = specifierRegex.exec(currentValue);
if (!specifierMatches) {
logger.debug(
`Skipping dependency with malformed version specifier "${currentValue}".`
);
skipReason = SkipReason.InvalidVersion;
}
}
const dep: PackageDependency = {
depType: section,
depName,
managerData: {},
};
if (currentValue) {
dep.currentValue = currentValue;
}
if (skipReason) {
dep.skipReason = skipReason;
} else {
dep.datasource = datasourcePypi.id;
}
if (nestedVersion) {
dep.managerData.nestedVersion = nestedVersion;
}
if (requirements.index) {
if (is.array(pipfile.source)) {
const source = pipfile.source.find(
(item) => item.name === requirements.index
);
if (source) {
dep.registryUrls = [source.url];
}
}
}
return dep;
})
.filter(Boolean);
return deps;
}
export async function extractPackageFile(
content: string,
fileName: string
): Promise<PackageFile | null> {
logger.debug('pipenv.extractPackageFile()');
let pipfile: PipFile;
try {
// TODO: fix type (#9610)
pipfile = toml.parse(content) as any;
} catch (err) {
logger.debug({ err }, 'Error parsing Pipfile');
return null;
}
const res: PackageFile = { deps: [] };
if (pipfile.source) {
res.registryUrls = pipfile.source.map((source) => source.url);
}
res.deps = [
...extractFromSection(pipfile, 'packages'),
...extractFromSection(pipfile, 'dev-packages'),
];
if (!res.deps.length) {
return null;
}
const constraints: Record<string, any> = {};
if (is.nonEmptyString(pipfile.requires?.python_version)) {
constraints.python = `== ${pipfile.requires.python_version}.*`;
} else if (is.nonEmptyString(pipfile.requires?.python_full_version)) {
constraints.python = `== ${pipfile.requires.python_full_version}`;
}
if (is.nonEmptyString(pipfile.packages?.pipenv)) {
constraints.pipenv = pipfile.packages.pipenv;
} else if (is.nonEmptyString(pipfile['dev-packages']?.pipenv)) {
constraints.pipenv = pipfile['dev-packages'].pipenv;
}
const lockFileName = fileName + '.lock';
if (await localPathExists(lockFileName)) {
res.lockFiles = [lockFileName];
}
res.constraints = constraints;
return res;
}