@trustify-da/trustify-da-javascript-client 0.3.0-ea.b8af0f8 → 0.3.0-ea.bbe2094
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/README.md +73 -8
- package/dist/package.json +6 -5
- package/dist/src/analysis.js +3 -2
- package/dist/src/cli.js +51 -2
- package/dist/src/cyclone_dx_sbom.d.ts +14 -1
- package/dist/src/cyclone_dx_sbom.js +32 -5
- package/dist/src/index.d.ts +74 -3
- package/dist/src/index.js +89 -6
- package/dist/src/oci_image/utils.js +11 -2
- package/dist/src/provider.js +8 -0
- package/dist/src/providers/base_java.d.ts +0 -9
- package/dist/src/providers/base_java.js +2 -38
- package/dist/src/providers/base_javascript.d.ts +6 -0
- package/dist/src/providers/base_javascript.js +37 -6
- package/dist/src/providers/base_pyproject.d.ts +153 -0
- package/dist/src/providers/base_pyproject.js +315 -0
- package/dist/src/providers/golang_gomodules.d.ts +21 -12
- package/dist/src/providers/golang_gomodules.js +164 -118
- package/dist/src/providers/gomod_parser.d.ts +4 -0
- package/dist/src/providers/gomod_parser.js +16 -0
- 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_bun.d.ts +10 -0
- package/dist/src/providers/javascript_bun.js +100 -0
- package/dist/src/providers/javascript_npm.d.ts +1 -0
- package/dist/src/providers/javascript_npm.js +21 -0
- package/dist/src/providers/javascript_pnpm.js +6 -2
- package/dist/src/providers/manifest.d.ts +2 -0
- package/dist/src/providers/manifest.js +22 -4
- package/dist/src/providers/marker_evaluator.d.ts +14 -0
- package/dist/src/providers/marker_evaluator.js +191 -0
- package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
- package/dist/src/providers/python_controller.d.ts +5 -1
- package/dist/src/providers/python_controller.js +8 -4
- package/dist/src/providers/python_pip.d.ts +4 -0
- package/dist/src/providers/python_pip.js +5 -5
- package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
- package/dist/src/providers/python_pip_pyproject.js +146 -0
- package/dist/src/providers/python_poetry.d.ts +75 -0
- package/dist/src/providers/python_poetry.js +238 -0
- package/dist/src/providers/python_uv.d.ts +55 -0
- package/dist/src/providers/python_uv.js +227 -0
- package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
- package/dist/src/sbom.d.ts +14 -1
- package/dist/src/sbom.js +13 -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 +7 -6
|
@@ -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;
|
|
@@ -95,7 +95,7 @@ export default class Python_controller {
|
|
|
95
95
|
}
|
|
96
96
|
/**
|
|
97
97
|
* Parse the requirements.txt file using tree-sitter and return structured requirement data.
|
|
98
|
-
* @return {Promise<{name: string, version: string|null}[]>}
|
|
98
|
+
* @return {Promise<{name: string, version: string|null, hasMarker: boolean}[]>}
|
|
99
99
|
*/
|
|
100
100
|
async #parseRequirements() {
|
|
101
101
|
const content = fs.readFileSync(this.pathToRequirements).toString();
|
|
@@ -107,7 +107,8 @@ export default class Python_controller {
|
|
|
107
107
|
const version = versionMatches.length > 0
|
|
108
108
|
? versionMatches[0].captures.find(c => c.name === 'version').node.text
|
|
109
109
|
: null;
|
|
110
|
-
|
|
110
|
+
const hasMarker = reqNode.children.some(c => c.type === 'marker_spec');
|
|
111
|
+
return { name, version, hasMarker };
|
|
111
112
|
}));
|
|
112
113
|
}
|
|
113
114
|
#decideIfWindowsOrLinuxPath(fileName) {
|
|
@@ -224,7 +225,10 @@ export default class Python_controller {
|
|
|
224
225
|
CachedEnvironmentDeps[packageName.replace("_", "-")] = pipDepTreeEntryForCache;
|
|
225
226
|
});
|
|
226
227
|
}
|
|
227
|
-
parsedRequirements.forEach(({ name: depName, version: manifestVersion }) => {
|
|
228
|
+
parsedRequirements.forEach(({ name: depName, version: manifestVersion, hasMarker }) => {
|
|
229
|
+
if (hasMarker && CachedEnvironmentDeps[depName.toLowerCase()] === undefined) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
228
232
|
if (matchManifestVersions === "true" && manifestVersion != null) {
|
|
229
233
|
let installedVersion;
|
|
230
234
|
if (CachedEnvironmentDeps[depName.toLowerCase()] !== undefined) {
|
|
@@ -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); });
|
|
@@ -75,7 +75,7 @@ function addAllDependencies(source, dep, sbom) {
|
|
|
75
75
|
/**
|
|
76
76
|
*
|
|
77
77
|
* @param {string} manifest - path to requirements.txt
|
|
78
|
-
* @return {PackageURL
|
|
78
|
+
* @return {Promise<PackageURL[]>}
|
|
79
79
|
*/
|
|
80
80
|
async function getIgnoredDependencies(manifest) {
|
|
81
81
|
const [parser, ignoreQuery, pinnedVersionQuery] = await Promise.all([
|
|
@@ -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
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Python provider for pyproject.toml files using PEP 621 format without a lock file.
|
|
3
|
+
* Uses `pip install --dry-run --ignore-installed --report` to resolve the full dependency tree.
|
|
4
|
+
* Acts as the fallback provider when no lock file (uv.lock/poetry.lock) is found.
|
|
5
|
+
*/
|
|
6
|
+
export default class Python_pip_pyproject extends Base_pyproject {
|
|
7
|
+
/**
|
|
8
|
+
* Always returns true — pip provider is the fallback when no lock file is found.
|
|
9
|
+
* @param {string} manifestDir
|
|
10
|
+
* @param {{}} [opts={}]
|
|
11
|
+
* @returns {boolean}
|
|
12
|
+
*/
|
|
13
|
+
validateLockFile(manifestDir: string, opts?: {}): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Get pip report output from env var override or by running pip.
|
|
16
|
+
* @param {string} manifestDir - directory containing pyproject.toml
|
|
17
|
+
* @param {{}} [opts={}]
|
|
18
|
+
* @returns {string} pip report JSON string
|
|
19
|
+
*/
|
|
20
|
+
_getPipReportOutput(manifestDir: string, opts?: {}): string;
|
|
21
|
+
/**
|
|
22
|
+
* Parse pip report JSON and build dependency graph.
|
|
23
|
+
* @param {string} reportJson - pip report JSON string
|
|
24
|
+
* @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
|
|
25
|
+
*/
|
|
26
|
+
_parsePipReport(reportJson: string): {
|
|
27
|
+
directDeps: string[];
|
|
28
|
+
graph: Map<string, {
|
|
29
|
+
name: string;
|
|
30
|
+
version: string;
|
|
31
|
+
children: string[];
|
|
32
|
+
}>;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Check if a requires_dist entry is an extras-only dependency.
|
|
36
|
+
* @param {string} req - e.g. "PySocks!=1.5.7,>=1.5.6; extra == \"socks\""
|
|
37
|
+
* @returns {boolean}
|
|
38
|
+
*/
|
|
39
|
+
_hasExtraMarker(req: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Extract package name from a requires_dist entry.
|
|
42
|
+
* @param {string} req - e.g. "charset_normalizer<4,>=2"
|
|
43
|
+
* @returns {string|null}
|
|
44
|
+
*/
|
|
45
|
+
_extractDepName(req: string): string | null;
|
|
46
|
+
/**
|
|
47
|
+
* Resolve dependencies using pip install --dry-run --report.
|
|
48
|
+
* @param {string} manifestDir
|
|
49
|
+
* @param {string} _workspaceDir - unused (pip resolves from manifest directory)
|
|
50
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
51
|
+
* @param {{}} [opts={}]
|
|
52
|
+
* @returns {Promise<{directDeps: string[], graph: Map}>}
|
|
53
|
+
*/
|
|
54
|
+
_getDependencyData(manifestDir: string, _workspaceDir: string, parsed: object, opts?: {}): Promise<{
|
|
55
|
+
directDeps: string[];
|
|
56
|
+
graph: Map<any, any>;
|
|
57
|
+
}>;
|
|
58
|
+
_findEggInfoDirs(dir: any): string[];
|
|
59
|
+
_cleanupEggInfo(dir: any, existing: any): void;
|
|
60
|
+
}
|
|
61
|
+
import Base_pyproject from './base_pyproject.js';
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
|
|
4
|
+
import Base_pyproject from './base_pyproject.js';
|
|
5
|
+
/**
|
|
6
|
+
* Python provider for pyproject.toml files using PEP 621 format without a lock file.
|
|
7
|
+
* Uses `pip install --dry-run --ignore-installed --report` to resolve the full dependency tree.
|
|
8
|
+
* Acts as the fallback provider when no lock file (uv.lock/poetry.lock) is found.
|
|
9
|
+
*/
|
|
10
|
+
export default class Python_pip_pyproject extends Base_pyproject {
|
|
11
|
+
/** @returns {string} */
|
|
12
|
+
_lockFileName() {
|
|
13
|
+
return '.pip-lock-nonexistent';
|
|
14
|
+
}
|
|
15
|
+
/** @returns {string} */
|
|
16
|
+
_cmdName() {
|
|
17
|
+
return 'pip';
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Always returns true — pip provider is the fallback when no lock file is found.
|
|
21
|
+
* @param {string} manifestDir
|
|
22
|
+
* @param {{}} [opts={}]
|
|
23
|
+
* @returns {boolean}
|
|
24
|
+
*/
|
|
25
|
+
// eslint-disable-next-line no-unused-vars
|
|
26
|
+
validateLockFile(manifestDir, opts = {}) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get pip report output from env var override or by running pip.
|
|
31
|
+
* @param {string} manifestDir - directory containing pyproject.toml
|
|
32
|
+
* @param {{}} [opts={}]
|
|
33
|
+
* @returns {string} pip report JSON string
|
|
34
|
+
*/
|
|
35
|
+
_getPipReportOutput(manifestDir, opts) {
|
|
36
|
+
if (environmentVariableIsPopulated('TRUSTIFY_DA_PIP_REPORT')) {
|
|
37
|
+
return Buffer.from(process.env['TRUSTIFY_DA_PIP_REPORT'], 'base64').toString('ascii');
|
|
38
|
+
}
|
|
39
|
+
let pipBin = getCustomPath('pip3', opts);
|
|
40
|
+
try {
|
|
41
|
+
invokeCommand(pipBin, ['--version']);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
pipBin = getCustomPath('pip', opts);
|
|
45
|
+
}
|
|
46
|
+
let eggInfoDirs = this._findEggInfoDirs(manifestDir);
|
|
47
|
+
let result = invokeCommand(pipBin, [
|
|
48
|
+
'install', '--dry-run', '--ignore-installed', '--quiet', '--report', '-', '.'
|
|
49
|
+
], { cwd: manifestDir }).toString();
|
|
50
|
+
this._cleanupEggInfo(manifestDir, eggInfoDirs);
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Parse pip report JSON and build dependency graph.
|
|
55
|
+
* @param {string} reportJson - pip report JSON string
|
|
56
|
+
* @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
|
|
57
|
+
*/
|
|
58
|
+
_parsePipReport(reportJson) {
|
|
59
|
+
let report = JSON.parse(reportJson);
|
|
60
|
+
let packages = report.install || [];
|
|
61
|
+
let rootEntry = packages.find(p => p.download_info?.dir_info !== undefined);
|
|
62
|
+
let rootRequires = rootEntry?.metadata?.requires_dist || [];
|
|
63
|
+
let directDepNames = new Set();
|
|
64
|
+
for (let req of rootRequires) {
|
|
65
|
+
if (this._hasExtraMarker(req)) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
let name = this._extractDepName(req);
|
|
69
|
+
if (name) {
|
|
70
|
+
directDepNames.add(this._canonicalize(name));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
let graph = new Map();
|
|
74
|
+
let nonRootPackages = packages.filter(p => p !== rootEntry);
|
|
75
|
+
for (let pkg of nonRootPackages) {
|
|
76
|
+
let name = pkg.metadata.name;
|
|
77
|
+
let version = pkg.metadata.version;
|
|
78
|
+
let key = this._canonicalize(name);
|
|
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 });
|
|
82
|
+
}
|
|
83
|
+
for (let pkg of nonRootPackages) {
|
|
84
|
+
let key = this._canonicalize(pkg.metadata.name);
|
|
85
|
+
let entry = graph.get(key);
|
|
86
|
+
let requires = pkg.metadata.requires_dist || [];
|
|
87
|
+
for (let req of requires) {
|
|
88
|
+
let depName = this._extractDepName(req);
|
|
89
|
+
if (!depName) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
let depKey = this._canonicalize(depName);
|
|
93
|
+
if (graph.has(depKey)) {
|
|
94
|
+
entry.children.push(depKey);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
let directDeps = [...directDepNames].filter(key => graph.has(key));
|
|
99
|
+
return { directDeps, graph };
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Check if a requires_dist entry is an extras-only dependency.
|
|
103
|
+
* @param {string} req - e.g. "PySocks!=1.5.7,>=1.5.6; extra == \"socks\""
|
|
104
|
+
* @returns {boolean}
|
|
105
|
+
*/
|
|
106
|
+
_hasExtraMarker(req) {
|
|
107
|
+
return /;\s*.*extra\s*==/.test(req);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Extract package name from a requires_dist entry.
|
|
111
|
+
* @param {string} req - e.g. "charset_normalizer<4,>=2"
|
|
112
|
+
* @returns {string|null}
|
|
113
|
+
*/
|
|
114
|
+
_extractDepName(req) {
|
|
115
|
+
let match = req.match(/^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)/);
|
|
116
|
+
return match ? match[1] : null;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Resolve dependencies using pip install --dry-run --report.
|
|
120
|
+
* @param {string} manifestDir
|
|
121
|
+
* @param {string} _workspaceDir - unused (pip resolves from manifest directory)
|
|
122
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
123
|
+
* @param {{}} [opts={}]
|
|
124
|
+
* @returns {Promise<{directDeps: string[], graph: Map}>}
|
|
125
|
+
*/
|
|
126
|
+
// eslint-disable-next-line no-unused-vars
|
|
127
|
+
async _getDependencyData(manifestDir, _workspaceDir, parsed, opts) {
|
|
128
|
+
let reportOutput = this._getPipReportOutput(manifestDir, opts);
|
|
129
|
+
return this._parsePipReport(reportOutput);
|
|
130
|
+
}
|
|
131
|
+
_findEggInfoDirs(dir) {
|
|
132
|
+
try {
|
|
133
|
+
return fs.readdirSync(dir).filter(f => f.endsWith('.egg-info'));
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
return [];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
_cleanupEggInfo(dir, existing) {
|
|
140
|
+
for (let entry of this._findEggInfoDirs(dir)) {
|
|
141
|
+
if (!existing.includes(entry)) {
|
|
142
|
+
fs.rmSync(path.join(dir, entry), { recursive: true, force: true });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
}>;
|
|
17
|
+
/**
|
|
18
|
+
* Get poetry show --tree output.
|
|
19
|
+
* @param {string} manifestDir
|
|
20
|
+
* @param {boolean} hasDevGroup
|
|
21
|
+
* @param {Object} opts
|
|
22
|
+
* @returns {string}
|
|
23
|
+
*/
|
|
24
|
+
_getPoetryShowTreeOutput(manifestDir: string, hasDevGroup: boolean, opts: any): string;
|
|
25
|
+
/**
|
|
26
|
+
* Get poetry show --all output (flat list with resolved versions).
|
|
27
|
+
* @param {string} manifestDir
|
|
28
|
+
* @param {Object} opts
|
|
29
|
+
* @returns {string}
|
|
30
|
+
*/
|
|
31
|
+
_getPoetryShowAllOutput(manifestDir: string, opts: any): string;
|
|
32
|
+
/**
|
|
33
|
+
* Parse poetry show --all output into a version map.
|
|
34
|
+
* Lines look like: "name (!) 1.2.3 Description text..."
|
|
35
|
+
* or: "name 1.2.3 Description text..."
|
|
36
|
+
* @param {string} output
|
|
37
|
+
* @returns {Map<string, string>} canonical name -> version
|
|
38
|
+
*/
|
|
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
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Parse poetry show --tree output into a dependency graph structure.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} treeOutput
|
|
59
|
+
* @param {Map<string, string>} versionMap - canonical name -> resolved version
|
|
60
|
+
* @param {{directMarkers: Map<string, string>, transitiveMarkers: Map<string, Map<string, string>>}} markerData
|
|
61
|
+
* @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
|
|
62
|
+
*/
|
|
63
|
+
_parsePoetryTree(treeOutput: string, versionMap: Map<string, string>, markerData: {
|
|
64
|
+
directMarkers: Map<string, string>;
|
|
65
|
+
transitiveMarkers: Map<string, Map<string, string>>;
|
|
66
|
+
}): {
|
|
67
|
+
directDeps: string[];
|
|
68
|
+
graph: Map<string, {
|
|
69
|
+
name: string;
|
|
70
|
+
version: string;
|
|
71
|
+
children: string[];
|
|
72
|
+
}>;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
import Base_pyproject from './base_pyproject.js';
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parse as parseToml } from 'smol-toml';
|
|
4
|
+
import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from '../tools.js';
|
|
5
|
+
import Base_pyproject from './base_pyproject.js';
|
|
6
|
+
import { evaluateMarker } from './marker_evaluator.js';
|
|
7
|
+
export default class Python_poetry extends Base_pyproject {
|
|
8
|
+
/**
|
|
9
|
+
* Poetry has no native workspace/monorepo support (python-poetry/poetry#2270).
|
|
10
|
+
* Each poetry project is treated independently — no lock file walk-up.
|
|
11
|
+
* Running `poetry show` from a parent directory returns the parent's deps, not
|
|
12
|
+
* the sub-package's, so walk-up would produce incorrect SBOMs.
|
|
13
|
+
* @param {string} manifestDir
|
|
14
|
+
* @param {Object} [opts={}]
|
|
15
|
+
* @returns {string|null}
|
|
16
|
+
* @protected
|
|
17
|
+
*/
|
|
18
|
+
_findLockFileDir(manifestDir, opts = {}) {
|
|
19
|
+
const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
|
|
20
|
+
if (workspaceDir) {
|
|
21
|
+
const dir = path.resolve(workspaceDir);
|
|
22
|
+
return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
|
|
23
|
+
}
|
|
24
|
+
const dir = path.resolve(manifestDir);
|
|
25
|
+
return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
|
|
26
|
+
}
|
|
27
|
+
/** @returns {string} */
|
|
28
|
+
_lockFileName() {
|
|
29
|
+
return 'poetry.lock';
|
|
30
|
+
}
|
|
31
|
+
/** @returns {string} */
|
|
32
|
+
_cmdName() {
|
|
33
|
+
return 'poetry';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @param {string} manifestDir
|
|
37
|
+
* @param {string} _workspaceDir - unused (poetry has no workspace support)
|
|
38
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
39
|
+
* @param {Object} opts
|
|
40
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
41
|
+
*/
|
|
42
|
+
// eslint-disable-next-line no-unused-vars
|
|
43
|
+
async _getDependencyData(manifestDir, _workspaceDir, parsed, opts) {
|
|
44
|
+
let hasDevGroup = !!(parsed.tool?.poetry?.group?.dev || parsed.tool?.poetry?.['dev-dependencies']);
|
|
45
|
+
let treeOutput = this._getPoetryShowTreeOutput(manifestDir, hasDevGroup, opts);
|
|
46
|
+
let showAllOutput = this._getPoetryShowAllOutput(manifestDir, opts);
|
|
47
|
+
let versionMap = this._parsePoetryShowAll(showAllOutput);
|
|
48
|
+
let lockDir = this._findLockFileDir(manifestDir, opts);
|
|
49
|
+
let markerData = this._extractMarkerData(lockDir, parsed);
|
|
50
|
+
return this._parsePoetryTree(treeOutput, versionMap, markerData);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Get poetry show --tree output.
|
|
54
|
+
* @param {string} manifestDir
|
|
55
|
+
* @param {boolean} hasDevGroup
|
|
56
|
+
* @param {Object} opts
|
|
57
|
+
* @returns {string}
|
|
58
|
+
*/
|
|
59
|
+
_getPoetryShowTreeOutput(manifestDir, hasDevGroup, opts) {
|
|
60
|
+
if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_TREE')) {
|
|
61
|
+
return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_TREE'], 'base64').toString('utf-8');
|
|
62
|
+
}
|
|
63
|
+
let poetryBin = getCustomPath('poetry', opts);
|
|
64
|
+
let args = ['show', '--tree', '--no-ansi'];
|
|
65
|
+
if (hasDevGroup) {
|
|
66
|
+
args.push('--without', 'dev');
|
|
67
|
+
}
|
|
68
|
+
return invokeCommand(poetryBin, args, { cwd: manifestDir }).toString();
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get poetry show --all output (flat list with resolved versions).
|
|
72
|
+
* @param {string} manifestDir
|
|
73
|
+
* @param {Object} opts
|
|
74
|
+
* @returns {string}
|
|
75
|
+
*/
|
|
76
|
+
_getPoetryShowAllOutput(manifestDir, opts) {
|
|
77
|
+
if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_ALL')) {
|
|
78
|
+
return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_ALL'], 'base64').toString('utf-8');
|
|
79
|
+
}
|
|
80
|
+
let poetryBin = getCustomPath('poetry', opts);
|
|
81
|
+
return invokeCommand(poetryBin, ['show', '--no-ansi', '--all'], { cwd: manifestDir }).toString();
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Parse poetry show --all output into a version map.
|
|
85
|
+
* Lines look like: "name (!) 1.2.3 Description text..."
|
|
86
|
+
* or: "name 1.2.3 Description text..."
|
|
87
|
+
* @param {string} output
|
|
88
|
+
* @returns {Map<string, string>} canonical name -> version
|
|
89
|
+
*/
|
|
90
|
+
_parsePoetryShowAll(output) {
|
|
91
|
+
let versions = new Map();
|
|
92
|
+
let lines = output.split(/\r?\n/);
|
|
93
|
+
for (let line of lines) {
|
|
94
|
+
let trimmed = line.trim();
|
|
95
|
+
if (!trimmed) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
let match = trimmed.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(?:\(!\)\s+)?(\S+)/);
|
|
99
|
+
if (match) {
|
|
100
|
+
versions.set(this._canonicalize(match[1]), match[2]);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return versions;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Collects PEP 508 marker expressions for direct and transitive deps.
|
|
107
|
+
* Direct markers come from pyproject.toml dependency strings, e.g.:
|
|
108
|
+
* "pywin32>=311 ; sys_platform == 'win32'" → directMarkers['pywin32'] = "sys_platform == 'win32'"
|
|
109
|
+
* Transitive markers come from poetry.lock [package.dependencies] entries, e.g.:
|
|
110
|
+
* colorama = {version = "*", markers = "sys_platform == 'win32'"}
|
|
111
|
+
* → transitiveMarkers['click']['colorama'] = "sys_platform == 'win32'"
|
|
112
|
+
* @param {string|null} lockDir
|
|
113
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
114
|
+
* @returns {{directMarkers: Map<string, string>, transitiveMarkers: Map<string, Map<string, string>>}}
|
|
115
|
+
*/
|
|
116
|
+
_extractMarkerData(lockDir, parsed) {
|
|
117
|
+
let directMarkers = new Map();
|
|
118
|
+
let transitiveMarkers = new Map();
|
|
119
|
+
// Extract markers from PEP 621 dependency strings: "name[extras]>=ver ; marker"
|
|
120
|
+
let deps = parsed.project?.dependencies || [];
|
|
121
|
+
for (let dep of deps) {
|
|
122
|
+
let m = dep.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s*[^;]*;\s*(.+)$/);
|
|
123
|
+
if (m) {
|
|
124
|
+
directMarkers.set(this._canonicalize(m[1]), m[2].trim());
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (lockDir) {
|
|
128
|
+
let lockPath = path.join(lockDir, this._lockFileName());
|
|
129
|
+
if (fs.existsSync(lockPath)) {
|
|
130
|
+
let lockContent = fs.readFileSync(lockPath, 'utf-8');
|
|
131
|
+
let lock = parseToml(lockContent);
|
|
132
|
+
let packages = lock.package || [];
|
|
133
|
+
for (let pkg of packages) {
|
|
134
|
+
let pkgKey = this._canonicalize(pkg.name);
|
|
135
|
+
let pkgDeps = pkg.dependencies || {};
|
|
136
|
+
for (let [depName, depSpec] of Object.entries(pkgDeps)) {
|
|
137
|
+
let markers = typeof depSpec === 'object' && depSpec != null ? depSpec.markers : null;
|
|
138
|
+
if (markers) {
|
|
139
|
+
if (!transitiveMarkers.has(pkgKey)) {
|
|
140
|
+
transitiveMarkers.set(pkgKey, new Map());
|
|
141
|
+
}
|
|
142
|
+
transitiveMarkers.get(pkgKey).set(this._canonicalize(depName), markers);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return { directMarkers, transitiveMarkers };
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Parse poetry show --tree output into a dependency graph structure.
|
|
152
|
+
*
|
|
153
|
+
* @param {string} treeOutput
|
|
154
|
+
* @param {Map<string, string>} versionMap - canonical name -> resolved version
|
|
155
|
+
* @param {{directMarkers: Map<string, string>, transitiveMarkers: Map<string, Map<string, string>>}} markerData
|
|
156
|
+
* @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
|
|
157
|
+
*/
|
|
158
|
+
_parsePoetryTree(treeOutput, versionMap, markerData) {
|
|
159
|
+
let lines = treeOutput.split(/\r?\n/);
|
|
160
|
+
let graph = new Map();
|
|
161
|
+
let directDeps = [];
|
|
162
|
+
let stack = []; // [{key, depth}]
|
|
163
|
+
let currentDirectDep = null;
|
|
164
|
+
for (let line of lines) {
|
|
165
|
+
if (!line.trim()) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
// top-level line: "name version description..."
|
|
169
|
+
let topMatch = line.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(\S+)(?:\s|$)/);
|
|
170
|
+
if (topMatch) {
|
|
171
|
+
let name = topMatch[1];
|
|
172
|
+
let version = topMatch[2];
|
|
173
|
+
let key = this._canonicalize(name);
|
|
174
|
+
let marker = markerData.directMarkers.get(key);
|
|
175
|
+
if (marker && !evaluateMarker(marker)) {
|
|
176
|
+
currentDirectDep = null;
|
|
177
|
+
stack = [];
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
directDeps.push(key);
|
|
181
|
+
if (!graph.has(key)) {
|
|
182
|
+
graph.set(key, { name, version, children: [] });
|
|
183
|
+
}
|
|
184
|
+
currentDirectDep = key;
|
|
185
|
+
stack = [{ key, depth: -1 }];
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (!currentDirectDep) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
// indented line with tree chars (UTF-8 box-drawing: ├── └── │)
|
|
192
|
+
let nameStart = line.search(/[A-Za-z0-9]/);
|
|
193
|
+
if (nameStart < 0) {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
let rest = line.substring(nameStart);
|
|
197
|
+
let depMatch = rest.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)/);
|
|
198
|
+
if (!depMatch) {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
let depName = depMatch[1];
|
|
202
|
+
let depKey = this._canonicalize(depName);
|
|
203
|
+
// determine depth by counting tree-drawing groups in the prefix
|
|
204
|
+
let prefix = line.substring(0, nameStart);
|
|
205
|
+
let depth = (prefix.match(/(?:[├└│ ][\s─]{2} ?)/g) || []).length;
|
|
206
|
+
// pop stack back to find the parent at depth-1
|
|
207
|
+
while (stack.length > 0 && stack[stack.length - 1].depth >= depth) {
|
|
208
|
+
stack.pop();
|
|
209
|
+
}
|
|
210
|
+
let parentKey = stack.length > 0 ? stack[stack.length - 1].key : null;
|
|
211
|
+
if (parentKey) {
|
|
212
|
+
let parentMarkers = markerData.transitiveMarkers.get(parentKey);
|
|
213
|
+
if (parentMarkers) {
|
|
214
|
+
let marker = parentMarkers.get(depKey);
|
|
215
|
+
if (marker && !evaluateMarker(marker)) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// resolve version from the version map
|
|
221
|
+
let version = versionMap.get(depKey) || null;
|
|
222
|
+
if (!version) {
|
|
223
|
+
throw new Error(`poetry: package '${depName}' has no resolved version`);
|
|
224
|
+
}
|
|
225
|
+
if (!graph.has(depKey)) {
|
|
226
|
+
graph.set(depKey, { name: depName, version, children: [] });
|
|
227
|
+
}
|
|
228
|
+
if (parentKey) {
|
|
229
|
+
let parentEntry = graph.get(parentKey);
|
|
230
|
+
if (parentEntry && !parentEntry.children.includes(depKey)) {
|
|
231
|
+
parentEntry.children.push(depKey);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
stack.push({ key: depKey, depth });
|
|
235
|
+
}
|
|
236
|
+
return { directDeps, graph };
|
|
237
|
+
}
|
|
238
|
+
}
|