@trustify-da/trustify-da-javascript-client 0.3.0-ea.a783c26 → 0.3.0-ea.a8c3942
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package.json +0 -1
- package/dist/src/cyclone_dx_sbom.d.ts +7 -1
- package/dist/src/cyclone_dx_sbom.js +18 -5
- package/dist/src/index.d.ts +62 -3
- package/dist/src/index.js +68 -4
- package/dist/src/providers/base_java.d.ts +0 -9
- package/dist/src/providers/base_java.js +2 -38
- package/dist/src/providers/base_pyproject.d.ts +5 -1
- package/dist/src/providers/base_pyproject.js +5 -4
- package/dist/src/providers/golang_gomodules.d.ts +9 -0
- package/dist/src/providers/golang_gomodules.js +64 -7
- package/dist/src/providers/java_gradle.d.ts +19 -0
- package/dist/src/providers/java_gradle.js +116 -2
- package/dist/src/providers/java_maven.d.ts +8 -0
- package/dist/src/providers/java_maven.js +93 -1
- package/dist/src/providers/javascript_pnpm.js +6 -2
- package/dist/src/providers/marker_evaluator.d.ts +14 -0
- package/dist/src/providers/marker_evaluator.js +191 -0
- package/dist/src/providers/python_controller.d.ts +5 -1
- package/dist/src/providers/python_controller.js +1 -1
- package/dist/src/providers/python_pip.d.ts +4 -0
- package/dist/src/providers/python_pip.js +4 -4
- package/dist/src/providers/python_pip_pyproject.js +3 -1
- package/dist/src/providers/python_poetry.d.ts +35 -3
- package/dist/src/providers/python_poetry.js +73 -10
- package/dist/src/providers/python_uv.d.ts +28 -0
- package/dist/src/providers/python_uv.js +78 -0
- package/dist/src/sbom.d.ts +7 -1
- package/dist/src/sbom.js +4 -2
- package/dist/src/tools.d.ts +26 -0
- package/dist/src/tools.js +58 -0
- package/dist/src/workspace.d.ts +9 -0
- package/dist/src/workspace.js +1 -1
- package/package.json +1 -2
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
1
2
|
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
2
4
|
import path from 'node:path';
|
|
3
5
|
import { EOL } from 'os';
|
|
4
|
-
import
|
|
6
|
+
import { parse as parseToml } from 'smol-toml';
|
|
5
7
|
import { readLicenseFile } from '../license/license_utils.js';
|
|
6
8
|
import Sbom from '../sbom.js';
|
|
9
|
+
import { invokeCommand } from '../tools.js';
|
|
10
|
+
import { filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore } from '../workspace.js';
|
|
7
11
|
import Base_java, { ecosystem_gradle } from "./base_java.js";
|
|
8
12
|
/** @typedef {import('../provider.js').Provider} */
|
|
9
13
|
/** @typedef {import('../provider.js').Provided} Provided */
|
|
@@ -362,7 +366,7 @@ export default class Java_gradle extends Base_java {
|
|
|
362
366
|
// Read and parse the TOML file
|
|
363
367
|
let pathOfToml = path.join(path.dirname(manifestPath), "gradle", "libs.versions.toml");
|
|
364
368
|
const tomlString = fs.readFileSync(pathOfToml).toString();
|
|
365
|
-
let tomlObject =
|
|
369
|
+
let tomlObject = parseToml(tomlString);
|
|
366
370
|
let groupPlusArtifactObject = tomlObject.libraries[alias];
|
|
367
371
|
let parts = groupPlusArtifactObject.module.split(":");
|
|
368
372
|
let groupId = parts[0];
|
|
@@ -407,3 +411,113 @@ export default class Java_gradle extends Base_java {
|
|
|
407
411
|
return undefined;
|
|
408
412
|
}
|
|
409
413
|
}
|
|
414
|
+
const DEFAULT_GRADLE_DISCOVERY_IGNORE = [
|
|
415
|
+
'**/build/**',
|
|
416
|
+
'**/.gradle/**',
|
|
417
|
+
];
|
|
418
|
+
/** Gradle init script that emits structured project listing. */
|
|
419
|
+
const GRADLE_INIT_SCRIPT = `allprojects {
|
|
420
|
+
task daListProjects {
|
|
421
|
+
doLast {
|
|
422
|
+
println "::DA_PROJECT::\${project.path}::\${project.projectDir}"
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
`;
|
|
427
|
+
/**
|
|
428
|
+
* Discover all build.gradle[.kts] manifest paths in a Gradle multi-project build.
|
|
429
|
+
* Uses a custom init script to get structured project listing.
|
|
430
|
+
*
|
|
431
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain settings.gradle[.kts])
|
|
432
|
+
* @param {import('../index.js').Options} [opts={}]
|
|
433
|
+
* @returns {Promise<string[]>} Paths to build.gradle[.kts] files (absolute)
|
|
434
|
+
*/
|
|
435
|
+
export async function discoverGradleSubprojects(workspaceRoot, opts = {}) {
|
|
436
|
+
const root = path.resolve(workspaceRoot);
|
|
437
|
+
const hasSettings = fs.existsSync(path.join(root, 'settings.gradle'))
|
|
438
|
+
|| fs.existsSync(path.join(root, 'settings.gradle.kts'));
|
|
439
|
+
if (!hasSettings) {
|
|
440
|
+
return [];
|
|
441
|
+
}
|
|
442
|
+
const manifestPaths = [];
|
|
443
|
+
const rootBuildKts = path.join(root, 'build.gradle.kts');
|
|
444
|
+
const rootBuild = path.join(root, 'build.gradle');
|
|
445
|
+
const rootManifest = fs.existsSync(rootBuildKts) ? rootBuildKts : fs.existsSync(rootBuild) ? rootBuild : null;
|
|
446
|
+
if (rootManifest) {
|
|
447
|
+
manifestPaths.push(rootManifest);
|
|
448
|
+
}
|
|
449
|
+
let gradleBin;
|
|
450
|
+
try {
|
|
451
|
+
gradleBin = new Java_gradle().selectToolBinary(rootManifest || rootBuild, opts);
|
|
452
|
+
}
|
|
453
|
+
catch {
|
|
454
|
+
const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_GRADLE_DISCOVERY_IGNORE];
|
|
455
|
+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
|
|
456
|
+
}
|
|
457
|
+
const initScriptPath = path.join(os.tmpdir(), `da-list-projects-${crypto.randomUUID()}.gradle`);
|
|
458
|
+
try {
|
|
459
|
+
fs.writeFileSync(initScriptPath, GRADLE_INIT_SCRIPT);
|
|
460
|
+
let output;
|
|
461
|
+
try {
|
|
462
|
+
output = invokeCommand(gradleBin, [
|
|
463
|
+
'-q', '--no-daemon',
|
|
464
|
+
'--init-script', initScriptPath,
|
|
465
|
+
'daListProjects',
|
|
466
|
+
], { cwd: root });
|
|
467
|
+
}
|
|
468
|
+
catch {
|
|
469
|
+
const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_GRADLE_DISCOVERY_IGNORE];
|
|
470
|
+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
|
|
471
|
+
}
|
|
472
|
+
const projects = parseGradleInitScriptOutput(output.toString());
|
|
473
|
+
for (const proj of projects) {
|
|
474
|
+
if (proj.path === ':') {
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
const projDir = path.resolve(proj.dir);
|
|
478
|
+
const buildKts = path.join(projDir, 'build.gradle.kts');
|
|
479
|
+
const buildGroovy = path.join(projDir, 'build.gradle');
|
|
480
|
+
if (fs.existsSync(buildKts)) {
|
|
481
|
+
manifestPaths.push(buildKts);
|
|
482
|
+
}
|
|
483
|
+
else if (fs.existsSync(buildGroovy)) {
|
|
484
|
+
manifestPaths.push(buildGroovy);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
finally {
|
|
489
|
+
try {
|
|
490
|
+
fs.unlinkSync(initScriptPath);
|
|
491
|
+
}
|
|
492
|
+
catch { /* ignore */ }
|
|
493
|
+
}
|
|
494
|
+
const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_GRADLE_DISCOVERY_IGNORE];
|
|
495
|
+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Parse the structured output from the Gradle init script.
|
|
499
|
+
*
|
|
500
|
+
* @param {string} raw - Raw stdout from gradle
|
|
501
|
+
* @returns {{ path: string, dir: string }[]}
|
|
502
|
+
*/
|
|
503
|
+
export function parseGradleInitScriptOutput(raw) {
|
|
504
|
+
const projects = [];
|
|
505
|
+
for (const rawLine of raw.split('\n')) {
|
|
506
|
+
const line = rawLine.trimEnd();
|
|
507
|
+
if (!line.startsWith('::DA_PROJECT::')) {
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
const prefix = '::DA_PROJECT::';
|
|
511
|
+
const remainder = line.substring(prefix.length);
|
|
512
|
+
const lastSep = remainder.lastIndexOf('::');
|
|
513
|
+
if (lastSep < 0) {
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
const projPath = remainder.substring(0, lastSep);
|
|
517
|
+
const dir = remainder.substring(lastSep + 2);
|
|
518
|
+
if (projPath && dir) {
|
|
519
|
+
projects.push({ path: projPath, dir });
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return projects;
|
|
523
|
+
}
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discover all pom.xml manifest paths in a Maven multi-module project.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain pom.xml)
|
|
5
|
+
* @param {object} [opts={}]
|
|
6
|
+
* @returns {Promise<string[]>} Paths to pom.xml files (absolute)
|
|
7
|
+
*/
|
|
8
|
+
export function discoverMavenModules(workspaceRoot: string, opts?: object): Promise<string[]>;
|
|
1
9
|
/** @typedef {import('../provider').Provider} */
|
|
2
10
|
/** @typedef {import('../provider').Provided} Provided */
|
|
3
11
|
/** @typedef {{name: string, version: string}} Package */
|
|
@@ -5,7 +5,8 @@ import { EOL } from 'os';
|
|
|
5
5
|
import { XMLParser } from 'fast-xml-parser';
|
|
6
6
|
import { getLicense } from '../license/license_utils.js';
|
|
7
7
|
import Sbom from '../sbom.js';
|
|
8
|
-
import { getCustom } from '../tools.js';
|
|
8
|
+
import { getCustom, invokeCommand } from '../tools.js';
|
|
9
|
+
import { filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore } from '../workspace.js';
|
|
9
10
|
import Base_java, { ecosystem_maven } from "./base_java.js";
|
|
10
11
|
/** @typedef {import('../provider').Provider} */
|
|
11
12
|
/** @typedef {import('../provider').Provided} Provided */
|
|
@@ -289,3 +290,94 @@ export default class Java_maven extends Base_java {
|
|
|
289
290
|
return deps.filter(d => dep.artifactId === d.artifactId && dep.groupId === d.groupId && dep.scope === d.scope).length > 0;
|
|
290
291
|
}
|
|
291
292
|
}
|
|
293
|
+
const DEFAULT_MAVEN_DISCOVERY_IGNORE = [
|
|
294
|
+
'**/target/**',
|
|
295
|
+
];
|
|
296
|
+
/**
|
|
297
|
+
* Discover all pom.xml manifest paths in a Maven multi-module project.
|
|
298
|
+
*
|
|
299
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain pom.xml)
|
|
300
|
+
* @param {object} [opts={}]
|
|
301
|
+
* @returns {Promise<string[]>} Paths to pom.xml files (absolute)
|
|
302
|
+
*/
|
|
303
|
+
export async function discoverMavenModules(workspaceRoot, opts = {}) {
|
|
304
|
+
const root = path.resolve(workspaceRoot);
|
|
305
|
+
const rootPom = path.join(root, 'pom.xml');
|
|
306
|
+
if (!fs.existsSync(rootPom)) {
|
|
307
|
+
return [];
|
|
308
|
+
}
|
|
309
|
+
let mvnBin;
|
|
310
|
+
try {
|
|
311
|
+
mvnBin = new Java_maven().selectToolBinary(rootPom, opts);
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
return [rootPom];
|
|
315
|
+
}
|
|
316
|
+
const visited = new Set();
|
|
317
|
+
const manifestPaths = [rootPom];
|
|
318
|
+
collectMavenModules(root, mvnBin, visited, manifestPaths);
|
|
319
|
+
const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_MAVEN_DISCOVERY_IGNORE];
|
|
320
|
+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* @param {string} dir - Absolute path to directory containing pom.xml
|
|
324
|
+
* @param {string} mvnBin - Maven binary path
|
|
325
|
+
* @param {Set<string>} visited - Already-visited directories (cycle guard)
|
|
326
|
+
* @param {string[]} manifestPaths - Accumulator for discovered pom.xml paths
|
|
327
|
+
*/
|
|
328
|
+
function collectMavenModules(dir, mvnBin, visited, manifestPaths) {
|
|
329
|
+
const resolvedDir = path.resolve(dir);
|
|
330
|
+
if (visited.has(resolvedDir)) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
visited.add(resolvedDir);
|
|
334
|
+
const modules = listMavenModules(resolvedDir, mvnBin);
|
|
335
|
+
for (const mod of modules) {
|
|
336
|
+
const moduleDir = path.resolve(resolvedDir, mod);
|
|
337
|
+
const modulePom = path.join(moduleDir, 'pom.xml');
|
|
338
|
+
if (fs.existsSync(modulePom)) {
|
|
339
|
+
manifestPaths.push(modulePom);
|
|
340
|
+
collectMavenModules(moduleDir, mvnBin, visited, manifestPaths);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* @param {string} dir - Directory containing pom.xml
|
|
346
|
+
* @param {string} mvnBin - Maven binary path
|
|
347
|
+
* @returns {string[]} Module directory names (relative to `dir`)
|
|
348
|
+
*/
|
|
349
|
+
function listMavenModules(dir, mvnBin) {
|
|
350
|
+
let output;
|
|
351
|
+
try {
|
|
352
|
+
output = invokeCommand(mvnBin, [
|
|
353
|
+
'help:evaluate',
|
|
354
|
+
'-Dexpression=project.modules',
|
|
355
|
+
'-q',
|
|
356
|
+
'-DforceStdout',
|
|
357
|
+
'-f', path.join(dir, 'pom.xml'),
|
|
358
|
+
'--batch-mode',
|
|
359
|
+
], { cwd: dir });
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
return [];
|
|
363
|
+
}
|
|
364
|
+
const raw = output.toString().trim();
|
|
365
|
+
if (!raw || raw.startsWith('<modules')) {
|
|
366
|
+
return [];
|
|
367
|
+
}
|
|
368
|
+
return parseMavenModuleList(raw);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* @param {string} raw - Raw stdout from mvn help:evaluate -DforceStdout
|
|
372
|
+
* @returns {string[]}
|
|
373
|
+
*/
|
|
374
|
+
function parseMavenModuleList(raw) {
|
|
375
|
+
const parser = new XMLParser();
|
|
376
|
+
const parsed = parser.parse(raw);
|
|
377
|
+
const entries = parsed?.strings?.string;
|
|
378
|
+
if (!entries) {
|
|
379
|
+
return [];
|
|
380
|
+
}
|
|
381
|
+
const list = Array.isArray(entries) ? entries : [entries];
|
|
382
|
+
return list.map(s => String(s).trim()).filter(Boolean);
|
|
383
|
+
}
|
|
@@ -7,15 +7,19 @@ export default class Javascript_pnpm extends Base_javascript {
|
|
|
7
7
|
return "pnpm";
|
|
8
8
|
}
|
|
9
9
|
_listCmdArgs(includeTransitive) {
|
|
10
|
-
return ['ls', includeTransitive ? '--depth=Infinity' : '--depth=0', '--prod', '--json'];
|
|
10
|
+
return ['ls', includeTransitive ? '--depth=Infinity' : '--depth=0', '--prod', '--json', '-r'];
|
|
11
11
|
}
|
|
12
12
|
_updateLockFileCmdArgs() {
|
|
13
13
|
return ['install', '--frozen-lockfile'];
|
|
14
14
|
}
|
|
15
15
|
_buildDependencyTree(includeTransitive, opts = {}) {
|
|
16
|
+
// pnpm ls --json returns an array with one entry per workspace package.
|
|
17
|
+
// When analyzing a workspace member, find its entry by name instead of
|
|
18
|
+
// blindly taking the first element (which is the workspace root).
|
|
16
19
|
const tree = super._buildDependencyTree(includeTransitive, opts);
|
|
17
20
|
if (Array.isArray(tree) && tree.length > 0) {
|
|
18
|
-
|
|
21
|
+
const memberName = this._getManifest().name;
|
|
22
|
+
return tree.find(pkg => pkg.name === memberName) || tree[0];
|
|
19
23
|
}
|
|
20
24
|
return {};
|
|
21
25
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps Node.js/OS values to PEP 508 marker variables.
|
|
3
|
+
* Example: on Linux, sys_platform='linux', platform_system='Linux', os_name='posix'
|
|
4
|
+
* @returns {Record<string, string>}
|
|
5
|
+
*/
|
|
6
|
+
export function getEnvironmentMarkers(): Record<string, string>;
|
|
7
|
+
/**
|
|
8
|
+
* Evaluates a full PEP 508 marker expression against the current platform.
|
|
9
|
+
* Example: "sys_platform == 'win32' and python_version >= '3.8'" → false on Linux
|
|
10
|
+
* Empty/missing markers return true (unconditional dependency).
|
|
11
|
+
* @param {string} markerExpr
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
14
|
+
export function evaluateMarker(markerExpr: string): boolean;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// PEP 508 environment marker evaluator.
|
|
2
|
+
// Filters Python dependencies by platform/version markers so that e.g.
|
|
3
|
+
// "pywin32 ; sys_platform == 'win32'" is excluded on Linux/macOS.
|
|
4
|
+
// See https://peps.python.org/pep-0508/#environment-markers
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import { getCustomPath, invokeCommand } from '../tools.js';
|
|
7
|
+
let cachedPythonVersions = undefined;
|
|
8
|
+
function getPythonVersions() {
|
|
9
|
+
if (cachedPythonVersions !== undefined) {
|
|
10
|
+
return cachedPythonVersions;
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
let python = getCustomPath('python3');
|
|
14
|
+
let out = invokeCommand(python, ['-c', "import sys; v=sys.version_info; print(f'{v.major}.{v.minor} {v.major}.{v.minor}.{v.micro}')"], { timeout: 5000 }).toString().trim();
|
|
15
|
+
let [short, full] = out.split(' ');
|
|
16
|
+
cachedPythonVersions = { short, full };
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
cachedPythonVersions = null;
|
|
20
|
+
}
|
|
21
|
+
return cachedPythonVersions;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Maps Node.js/OS values to PEP 508 marker variables.
|
|
25
|
+
* Example: on Linux, sys_platform='linux', platform_system='Linux', os_name='posix'
|
|
26
|
+
* @returns {Record<string, string>}
|
|
27
|
+
*/
|
|
28
|
+
export function getEnvironmentMarkers() {
|
|
29
|
+
let platform = process.platform;
|
|
30
|
+
let systemMap = { win32: 'Windows', linux: 'Linux', darwin: 'Darwin' };
|
|
31
|
+
let machine = typeof os.machine === 'function' ? os.machine() : process.arch;
|
|
32
|
+
let pyVer = getPythonVersions();
|
|
33
|
+
return {
|
|
34
|
+
sys_platform: platform,
|
|
35
|
+
platform_system: systemMap[platform] || platform,
|
|
36
|
+
os_name: platform === 'win32' ? 'nt' : 'posix',
|
|
37
|
+
platform_machine: machine,
|
|
38
|
+
platform_release: os.release(),
|
|
39
|
+
platform_version: os.version?.() || '',
|
|
40
|
+
python_version: pyVer?.short || '',
|
|
41
|
+
python_full_version: pyVer?.full || '',
|
|
42
|
+
implementation_name: 'cpython',
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function compareVersions(left, right) {
|
|
46
|
+
let lParts = left.split('.').map(Number);
|
|
47
|
+
let rParts = right.split('.').map(Number);
|
|
48
|
+
let len = Math.max(lParts.length, rParts.length);
|
|
49
|
+
for (let i = 0; i < len; i++) {
|
|
50
|
+
let l = lParts[i] || 0;
|
|
51
|
+
let r = rParts[i] || 0;
|
|
52
|
+
if (l < r) {
|
|
53
|
+
return -1;
|
|
54
|
+
}
|
|
55
|
+
if (l > r) {
|
|
56
|
+
return 1;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
// Evaluates a single comparison like sys_platform == 'win32' or python_version >= '3.8'.
|
|
62
|
+
// Version-bearing variables (python_version, python_full_version) use numeric comparison;
|
|
63
|
+
// all others use string equality. Returns false when the env value is missing.
|
|
64
|
+
function evaluateComparison(variable, op, value, env) {
|
|
65
|
+
let envVal = env[variable];
|
|
66
|
+
if (envVal === undefined || envVal === '') {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
let isVersion = variable.includes('version');
|
|
70
|
+
if (isVersion) {
|
|
71
|
+
let cmp = compareVersions(envVal, value);
|
|
72
|
+
switch (op) {
|
|
73
|
+
case '==': return cmp === 0;
|
|
74
|
+
case '!=': return cmp !== 0;
|
|
75
|
+
case '>=': return cmp >= 0;
|
|
76
|
+
case '<=': return cmp <= 0;
|
|
77
|
+
case '>': return cmp > 0;
|
|
78
|
+
case '<': return cmp < 0;
|
|
79
|
+
case '~=': {
|
|
80
|
+
let parts = value.split('.');
|
|
81
|
+
parts.pop();
|
|
82
|
+
let prefix = parts.join('.');
|
|
83
|
+
return envVal.startsWith(prefix) && cmp >= 0;
|
|
84
|
+
}
|
|
85
|
+
default: return true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
switch (op) {
|
|
89
|
+
case '==': return envVal === value;
|
|
90
|
+
case '!=': return envVal !== value;
|
|
91
|
+
case 'in': return value.includes(envVal);
|
|
92
|
+
case 'not in': return !value.includes(envVal);
|
|
93
|
+
default: return envVal === value;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Parses a single marker comparison into {variable, op, value}.
|
|
97
|
+
// Handles both normal and reversed forms:
|
|
98
|
+
// "sys_platform == 'linux'" → { variable: 'sys_platform', op: '==', value: 'linux' }
|
|
99
|
+
// "'linux' == sys_platform" → { variable: 'sys_platform', op: '==', value: 'linux' }
|
|
100
|
+
function parseAtom(expr) {
|
|
101
|
+
// Normal form: variable op 'value'
|
|
102
|
+
let m = expr.match(/^\s*([\w.]+)\s*(~=|!=|==|>=|<=|>|<|not\s+in|in)\s*["']([^"']*)["']\s*$/);
|
|
103
|
+
if (m) {
|
|
104
|
+
return { variable: m[1], op: m[2].replace(/\s+/g, ' '), value: m[3] };
|
|
105
|
+
}
|
|
106
|
+
// Reversed form: 'value' op variable — reverse directional operators
|
|
107
|
+
let mReverse = expr.match(/^\s*["']([^"']*)['"]\s*(~=|!=|==|>=|<=|>|<|not\s+in|in)\s*([\w.]+)\s*$/);
|
|
108
|
+
if (mReverse) {
|
|
109
|
+
let reverseOp = { '<': '>', '>': '<', '<=': '>=', '>=': '<=' };
|
|
110
|
+
let op = mReverse[2].replace(/\s+/g, ' ');
|
|
111
|
+
return { variable: mReverse[3], op: reverseOp[op] || op, value: mReverse[1] };
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Evaluates a full PEP 508 marker expression against the current platform.
|
|
117
|
+
* Example: "sys_platform == 'win32' and python_version >= '3.8'" → false on Linux
|
|
118
|
+
* Empty/missing markers return true (unconditional dependency).
|
|
119
|
+
* @param {string} markerExpr
|
|
120
|
+
* @returns {boolean}
|
|
121
|
+
*/
|
|
122
|
+
export function evaluateMarker(markerExpr) {
|
|
123
|
+
if (!markerExpr || !markerExpr.trim()) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
let env = getEnvironmentMarkers();
|
|
127
|
+
return evaluateExpr(markerExpr.trim(), env);
|
|
128
|
+
}
|
|
129
|
+
function evaluateExpr(expr, env) {
|
|
130
|
+
let orParts = splitLogical(expr, ' or ');
|
|
131
|
+
if (orParts.length > 1) {
|
|
132
|
+
return orParts.some(part => evaluateExpr(part, env));
|
|
133
|
+
}
|
|
134
|
+
let andParts = splitLogical(expr, ' and ');
|
|
135
|
+
if (andParts.length > 1) {
|
|
136
|
+
return andParts.every(part => evaluateExpr(part, env));
|
|
137
|
+
}
|
|
138
|
+
let trimmed = expr.trim();
|
|
139
|
+
if (trimmed.startsWith('(') && trimmed.endsWith(')')) {
|
|
140
|
+
return evaluateExpr(trimmed.slice(1, -1), env);
|
|
141
|
+
}
|
|
142
|
+
let atom = parseAtom(trimmed);
|
|
143
|
+
if (!atom) {
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
return evaluateComparison(atom.variable, atom.op, atom.value, env);
|
|
147
|
+
}
|
|
148
|
+
// Splits an expression by " and " or " or " at the top level, skipping
|
|
149
|
+
// separators inside parentheses or quoted strings.
|
|
150
|
+
// Example: splitLogical("a == 'x' and (b == 'y' or c == 'z')", " and ")
|
|
151
|
+
// → ["a == 'x'", "(b == 'y' or c == 'z')"]
|
|
152
|
+
function splitLogical(expr, sep) {
|
|
153
|
+
let parts = [];
|
|
154
|
+
let depth = 0;
|
|
155
|
+
let current = '';
|
|
156
|
+
let i = 0;
|
|
157
|
+
let quoteChar = null;
|
|
158
|
+
while (i < expr.length) {
|
|
159
|
+
let ch = expr[i];
|
|
160
|
+
if (quoteChar) {
|
|
161
|
+
if (ch === quoteChar) {
|
|
162
|
+
quoteChar = null;
|
|
163
|
+
}
|
|
164
|
+
current += ch;
|
|
165
|
+
i++;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (ch === '"' || ch === "'") {
|
|
169
|
+
quoteChar = ch;
|
|
170
|
+
current += ch;
|
|
171
|
+
i++;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (ch === '(') {
|
|
175
|
+
depth++;
|
|
176
|
+
}
|
|
177
|
+
if (ch === ')') {
|
|
178
|
+
depth--;
|
|
179
|
+
}
|
|
180
|
+
if (depth === 0 && expr.substring(i, i + sep.length) === sep) {
|
|
181
|
+
parts.push(current);
|
|
182
|
+
current = '';
|
|
183
|
+
i += sep.length;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
current += ch;
|
|
187
|
+
i++;
|
|
188
|
+
}
|
|
189
|
+
parts.push(current);
|
|
190
|
+
return parts.filter(p => p.trim());
|
|
191
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
|
|
1
|
+
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[], hashes?: Array<{alg: string, content: string}>}} DependencyEntry */
|
|
2
2
|
export default class Python_controller {
|
|
3
3
|
/**
|
|
4
4
|
* Constructor to create new python controller instance to interact with pip package manager
|
|
@@ -31,4 +31,8 @@ export type DependencyEntry = {
|
|
|
31
31
|
name: string;
|
|
32
32
|
version: string;
|
|
33
33
|
dependencies: DependencyEntry[];
|
|
34
|
+
hashes?: Array<{
|
|
35
|
+
alg: string;
|
|
36
|
+
content: string;
|
|
37
|
+
}>;
|
|
34
38
|
};
|
|
@@ -19,7 +19,7 @@ function getPipShowOutput(depNames) {
|
|
|
19
19
|
throw new Error('fail invoking \'pip show\' to fetch metadata for all installed packages in environment', { cause: error });
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
-
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
|
|
22
|
+
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[], hashes?: Array<{alg: string, content: string}>}} DependencyEntry */
|
|
23
23
|
export default class Python_controller {
|
|
24
24
|
pythonEnvDir;
|
|
25
25
|
pathToPipBin;
|
|
@@ -6,12 +6,13 @@ import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand
|
|
|
6
6
|
import Python_controller from './python_controller.js';
|
|
7
7
|
import { getParser, getIgnoreQuery, getPinnedVersionQuery } from './requirements_parser.js';
|
|
8
8
|
export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
|
|
9
|
-
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
|
|
9
|
+
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[], hashes?: Array<{alg: string, content: string}>}} DependencyEntry */
|
|
10
10
|
/**
|
|
11
11
|
* @type {string} ecosystem for python-pip is 'pip'
|
|
12
12
|
* @private
|
|
13
13
|
*/
|
|
14
14
|
const ecosystem = 'pip';
|
|
15
|
+
const NO_SCOPE = undefined;
|
|
15
16
|
/**
|
|
16
17
|
* @param {string} manifestName - the subject manifest name-type
|
|
17
18
|
* @returns {boolean} - return true if `requirements.txt` is the manifest name-type
|
|
@@ -56,7 +57,6 @@ async function provideComponent(manifest, opts = {}) {
|
|
|
56
57
|
contentType: 'application/vnd.cyclonedx+json'
|
|
57
58
|
};
|
|
58
59
|
}
|
|
59
|
-
/** @typedef {{name: string, , version: string, dependencies: DependencyEntry[]}} DependencyEntry */
|
|
60
60
|
/**
|
|
61
61
|
*
|
|
62
62
|
* @param {PackageURL}source
|
|
@@ -66,7 +66,7 @@ async function provideComponent(manifest, opts = {}) {
|
|
|
66
66
|
*/
|
|
67
67
|
function addAllDependencies(source, dep, sbom) {
|
|
68
68
|
let targetPurl = toPurl(dep["name"], dep["version"]);
|
|
69
|
-
sbom.addDependency(source, targetPurl);
|
|
69
|
+
sbom.addDependency(source, targetPurl, NO_SCOPE, dep["hashes"]);
|
|
70
70
|
let directDeps = dep["dependencies"];
|
|
71
71
|
if (directDeps !== undefined && directDeps.length > 0) {
|
|
72
72
|
directDeps.forEach((dependency) => { addAllDependencies(toPurl(dep["name"], dep["version"]), dependency, sbom); });
|
|
@@ -202,7 +202,7 @@ async function getSbomForComponentAnalysis(manifest, opts = {}) {
|
|
|
202
202
|
const license = readLicenseFromManifest(manifest);
|
|
203
203
|
sbom.addRoot(rootPurl, license);
|
|
204
204
|
dependencies.forEach(dep => {
|
|
205
|
-
sbom.addDependency(rootPurl, toPurl(dep.name, dep.version));
|
|
205
|
+
sbom.addDependency(rootPurl, toPurl(dep.name, dep.version), NO_SCOPE, dep.hashes);
|
|
206
206
|
});
|
|
207
207
|
await handleIgnoredDependencies(manifest, sbom, opts);
|
|
208
208
|
// In python there is no root component, then we must remove the dummy root we added, so the sbom json will be accepted by the DA backend
|
|
@@ -76,7 +76,9 @@ export default class Python_pip_pyproject extends Base_pyproject {
|
|
|
76
76
|
let name = pkg.metadata.name;
|
|
77
77
|
let version = pkg.metadata.version;
|
|
78
78
|
let key = this._canonicalize(name);
|
|
79
|
-
|
|
79
|
+
let sha256 = pkg.download_info?.archive_info?.hashes?.sha256;
|
|
80
|
+
let hashes = sha256 ? [{ alg: "SHA-256", content: sha256 }] : undefined;
|
|
81
|
+
graph.set(key, { name, version, children: [], hashes });
|
|
80
82
|
}
|
|
81
83
|
for (let pkg of nonRootPackages) {
|
|
82
84
|
let key = this._canonicalize(pkg.metadata.name);
|
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
export default class Python_poetry extends Base_pyproject {
|
|
2
|
+
/**
|
|
3
|
+
* @param {string} manifestDir
|
|
4
|
+
* @param {string} _workspaceDir - unused (poetry has no workspace support)
|
|
5
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
6
|
+
* @param {Object} opts
|
|
7
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
8
|
+
*/
|
|
9
|
+
_getDependencyData(manifestDir: string, _workspaceDir: string, parsed: object, opts: any): Promise<{
|
|
10
|
+
directDeps: string[];
|
|
11
|
+
graph: Map<string, {
|
|
12
|
+
name: string;
|
|
13
|
+
version: string;
|
|
14
|
+
children: string[];
|
|
15
|
+
}>;
|
|
16
|
+
}>;
|
|
2
17
|
/**
|
|
3
18
|
* Get poetry show --tree output.
|
|
4
19
|
* @param {string} manifestDir
|
|
@@ -22,16 +37,33 @@ export default class Python_poetry extends Base_pyproject {
|
|
|
22
37
|
* @returns {Map<string, string>} canonical name -> version
|
|
23
38
|
*/
|
|
24
39
|
_parsePoetryShowAll(output: string): Map<string, string>;
|
|
40
|
+
/**
|
|
41
|
+
* Collects PEP 508 marker expressions for direct and transitive deps.
|
|
42
|
+
* Direct markers come from pyproject.toml dependency strings, e.g.:
|
|
43
|
+
* "pywin32>=311 ; sys_platform == 'win32'" → directMarkers['pywin32'] = "sys_platform == 'win32'"
|
|
44
|
+
* Transitive markers come from poetry.lock [package.dependencies] entries, e.g.:
|
|
45
|
+
* colorama = {version = "*", markers = "sys_platform == 'win32'"}
|
|
46
|
+
* → transitiveMarkers['click']['colorama'] = "sys_platform == 'win32'"
|
|
47
|
+
* @param {string|null} lockDir
|
|
48
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
49
|
+
* @returns {{directMarkers: Map<string, string>, transitiveMarkers: Map<string, Map<string, string>>}}
|
|
50
|
+
*/
|
|
51
|
+
_extractMarkerData(lockDir: string | null, parsed: object): {
|
|
52
|
+
directMarkers: Map<string, string>;
|
|
53
|
+
transitiveMarkers: Map<string, Map<string, string>>;
|
|
54
|
+
};
|
|
25
55
|
/**
|
|
26
56
|
* Parse poetry show --tree output into a dependency graph structure.
|
|
27
|
-
* Top-level lines (no indentation/tree chars) are direct deps: "name version description"
|
|
28
|
-
* Indented lines are transitive deps with tree chars: "├── name >=constraint"
|
|
29
57
|
*
|
|
30
58
|
* @param {string} treeOutput
|
|
31
59
|
* @param {Map<string, string>} versionMap - canonical name -> resolved version
|
|
60
|
+
* @param {{directMarkers: Map<string, string>, transitiveMarkers: Map<string, Map<string, string>>}} markerData
|
|
32
61
|
* @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
|
|
33
62
|
*/
|
|
34
|
-
_parsePoetryTree(treeOutput: string, versionMap: Map<string, string
|
|
63
|
+
_parsePoetryTree(treeOutput: string, versionMap: Map<string, string>, markerData: {
|
|
64
|
+
directMarkers: Map<string, string>;
|
|
65
|
+
transitiveMarkers: Map<string, Map<string, string>>;
|
|
66
|
+
}): {
|
|
35
67
|
directDeps: string[];
|
|
36
68
|
graph: Map<string, {
|
|
37
69
|
name: string;
|