-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbase_pyproject.js
More file actions
348 lines (305 loc) · 9.45 KB
/
Copy pathbase_pyproject.js
File metadata and controls
348 lines (305 loc) · 9.45 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import fs from 'node:fs'
import path from 'node:path'
import { PackageURL } from 'packageurl-js'
import { parse as parseToml } from 'smol-toml'
import { getLicense } from '../license/license_utils.js'
import Sbom from '../sbom.js'
import { getCustom } from '../tools.js'
const ecosystem = 'pip'
const IGNORE_MARKERS = ['exhortignore', 'trustify-da-ignore']
const NO_SCOPE = undefined
const DEFAULT_ROOT_NAME = 'default-pip-root'
const DEFAULT_ROOT_VERSION = '0.0.0'
/** @typedef {{name: string, version: string, children: string[], hashes?: Array<{alg: string, content: string}>}} GraphEntry */
/** @typedef {{name: string, version: string, dependencies: DepTreeEntry[]}} DepTreeEntry */
/** @typedef {{directDeps: string[], graph: Map<string, GraphEntry>}} DependencyData */
/** @typedef {{ecosystem: string, content: string, contentType: string}} Provided */
export default class Base_pyproject {
/**
* @param {string} manifestName
* @returns {boolean}
*/
isSupported(manifestName) {
return 'pyproject.toml' === manifestName
}
/**
* @param {string} manifestDir
* @param {Object} [opts={}]
* @returns {boolean}
*/
validateLockFile(manifestDir, opts = {}) {
return this._findLockFileDir(manifestDir, opts) != null
}
/**
* Walk up from manifestDir to find the directory containing the lock file.
* Follows the same pattern as Base_javascript._findLockFileDir().
* @param {string} manifestDir
* @param {Object} [opts={}]
* @returns {string|null}
* @protected
*/
_findLockFileDir(manifestDir, opts = {}) {
const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts)
if (workspaceDir) {
const dir = path.resolve(workspaceDir)
return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null
}
let dir = path.resolve(manifestDir)
let parent = dir
do {
dir = parent
if (fs.existsSync(path.join(dir, this._lockFileName()))) {
return dir
}
if (this._isWorkspaceRoot(dir)) {
return null
}
parent = path.dirname(dir)
} while (parent !== dir)
return null
}
/**
* Detect workspace root boundaries.
* Currently only uv has native workspace support ([tool.uv.workspace] in pyproject.toml).
* Poetry has no workspace/monorepo support (python-poetry/poetry#2270), so each
* poetry project is treated independently — see Python_poetry._findLockFileDir().
* @param {string} dir
* @returns {boolean}
* @protected
*/
_isWorkspaceRoot(dir) {
const pyprojectPath = path.join(dir, 'pyproject.toml')
if (!fs.existsSync(pyprojectPath)) {
return false
}
try {
const content = parseToml(fs.readFileSync(pyprojectPath, 'utf-8'))
if (content.tool?.uv?.workspace) {
return true
}
} catch (_) {
// ignore parse errors
}
return false
}
/**
* Read project license from pyproject.toml, with fallback to LICENSE file.
* @param {string} manifestPath
* @returns {string|null}
*/
readLicenseFromManifest(manifestPath) {
let fromManifest = null
try {
let content = fs.readFileSync(manifestPath, 'utf-8')
let parsed = parseToml(content)
fromManifest = parsed.project?.license
if (typeof fromManifest === 'object' && fromManifest != null) {
fromManifest = fromManifest.text || null
}
if (!fromManifest) {
fromManifest = parsed.tool?.poetry?.license || null
}
} catch (_) {
// leave fromManifest as null
}
return getLicense(fromManifest, manifestPath)
}
/**
* @param {string} manifest - path to pyproject.toml
* @param {Object} [opts={}]
* @returns {Promise<Provided>}
*/
async provideStack(manifest, opts = {}) {
return {
ecosystem,
content: await this._createSbom(manifest, opts, true),
contentType: 'application/vnd.cyclonedx+json'
}
}
/**
* @param {string} manifest - path to pyproject.toml
* @param {Object} [opts={}]
* @returns {Promise<Provided>}
*/
async provideComponent(manifest, opts = {}) {
return {
ecosystem,
content: await this._createSbom(manifest, opts, false),
contentType: 'application/vnd.cyclonedx+json'
}
}
// --- abstract methods (subclasses must override) ---
/**
* @returns {string}
* @protected
*/
_lockFileName() {
throw new TypeError('_lockFileName must be implemented')
}
/**
* @returns {string}
* @protected
*/
_cmdName() {
throw new TypeError('_cmdName must be implemented')
}
/**
* Returns the package manager name (e.g. pip, poetry, uv)
* @returns {string}
*/
packageManagerName() {
return this._cmdName()
}
/**
* Resolve dependencies using the tool-specific command and parser.
*
* @param {string} manifestDir - directory containing the target pyproject.toml
* @param {string} workspaceDir - workspace root (where the lock file lives);
* only used by providers that need workspace-level resolution (e.g. uv)
* @param {object} parsed - parsed pyproject.toml
* @param {Object} opts
* @returns {Promise<DependencyData>}
* @protected
*/
// eslint-disable-next-line no-unused-vars
async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
throw new TypeError('_getDependencyData must be implemented')
}
// --- shared helpers ---
/**
* Canonicalize a Python package name per PEP 503.
* @param {string} name
* @returns {string}
* @protected
*/
_canonicalize(name) {
return name.toLowerCase().replace(/[-_.]+/g, '-')
}
/**
* Get the project name from pyproject.toml.
* @param {object} parsed
* @returns {string|null}
* @protected
*/
_getProjectName(parsed) {
return parsed.project?.name || parsed.tool?.poetry?.name || null
}
/**
* Get the project version from pyproject.toml.
* @param {object} parsed
* @returns {string|null}
* @protected
*/
_getProjectVersion(parsed) {
return parsed.project?.version || parsed.tool?.poetry?.version || null
}
/**
* Scan raw pyproject.toml text for dependencies with ignore markers.
* @param {string} manifestPath
* @returns {Set<string>}
* @protected
*/
_getIgnoredDeps(manifestPath) {
let ignored = new Set()
let content = fs.readFileSync(manifestPath, 'utf-8')
let lines = content.split(/\r?\n/)
for (let line of lines) {
if (!IGNORE_MARKERS.some(m => line.includes(m))) { continue }
// PEP 621 style: "requests>=2.25" #exhortignore
let pep621Match = line.match(/^\s*"([^"]+)"/)
if (pep621Match) {
let reqStr = pep621Match[1]
let nameMatch = reqStr.match(/^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)/)
if (nameMatch) {
ignored.add(this._canonicalize(nameMatch[1]))
}
continue
}
// Poetry style: requests = "^2.25" #exhortignore
let poetryMatch = line.match(/^\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*=/)
if (poetryMatch) {
ignored.add(this._canonicalize(poetryMatch[1]))
}
}
return ignored
}
/**
* Compute the set of graph nodes reachable from direct deps, excluding ignored.
* @param {Map<string, GraphEntry>} graph
* @param {string[]} directDeps
* @param {Set<string>} ignoredDeps
* @returns {Set<string>}
* @protected
*/
_reachableNodes(graph, directDeps, ignoredDeps) {
let reachable = new Set()
let queue = directDeps.filter(k => !ignoredDeps.has(k) && graph.has(k))
while (queue.length > 0) {
let key = queue.shift()
if (reachable.has(key)) { continue }
reachable.add(key)
for (let child of graph.get(key).children) {
if (!ignoredDeps.has(child) && graph.has(child) && !reachable.has(child)) {
queue.push(child)
}
}
}
return reachable
}
/**
* @param {string} name
* @param {string} version
* @returns {PackageURL}
* @protected
*/
_toPurl(name, version) {
return new PackageURL('pypi', undefined, name, version, undefined, undefined)
}
/**
* Create SBOM json string for a pyproject.toml project.
* @param {string} manifest - path to pyproject.toml
* @param {Object} opts
* @param {boolean} includeTransitive
* @returns {Promise<string>}
* @private
*/
async _createSbom(manifest, opts, includeTransitive) {
let manifestDir = path.dirname(manifest)
let content = fs.readFileSync(manifest, 'utf-8')
let parsed = parseToml(content)
let workspaceDir = this._findLockFileDir(manifestDir, opts) || manifestDir
let { directDeps, graph } = await this._getDependencyData(manifestDir, workspaceDir, parsed, opts)
let ignoredDeps = this._getIgnoredDeps(manifest)
let sbom = new Sbom()
let rootName = this._getProjectName(parsed) || DEFAULT_ROOT_NAME
let rootVersion = this._getProjectVersion(parsed) || DEFAULT_ROOT_VERSION
let rootPurl = this._toPurl(rootName, rootVersion)
let license = this.readLicenseFromManifest(manifest)
sbom.addRoot(rootPurl, license)
if (includeTransitive) {
let reachable = this._reachableNodes(graph, directDeps, ignoredDeps)
for (let key of directDeps) {
if (!reachable.has(key)) { continue }
let entry = graph.get(key)
sbom.addDependency(rootPurl, this._toPurl(entry.name, entry.version), NO_SCOPE, entry.hashes)
}
for (let [key, entry] of graph) {
if (!reachable.has(key)) { continue }
let parentPurl = this._toPurl(entry.name, entry.version)
for (let child of entry.children) {
if (!reachable.has(child)) { continue }
let childEntry = graph.get(child)
sbom.addDependency(parentPurl, this._toPurl(childEntry.name, childEntry.version), NO_SCOPE, childEntry.hashes)
}
}
} else {
for (let key of directDeps) {
if (ignoredDeps.has(key)) { continue }
let entry = graph.get(key)
if (!entry) { continue }
sbom.addDependency(rootPurl, this._toPurl(entry.name, entry.version), NO_SCOPE, entry.hashes)
}
}
return sbom.getAsJsonString(opts)
}
}