@trustify-da/trustify-da-javascript-client 0.3.0-ea.e12bc82 → 0.3.0-ea.f2d5d72
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 +191 -11
- package/dist/package.json +22 -9
- package/dist/src/analysis.d.ts +21 -5
- package/dist/src/analysis.js +71 -78
- package/dist/src/batch_opts.d.ts +24 -0
- package/dist/src/batch_opts.js +35 -0
- package/dist/src/cli.js +192 -8
- package/dist/src/cyclone_dx_sbom.d.ts +10 -2
- package/dist/src/cyclone_dx_sbom.js +32 -5
- package/dist/src/index.d.ts +128 -11
- package/dist/src/index.js +272 -7
- package/dist/src/license/index.d.ts +28 -0
- package/dist/src/license/index.js +100 -0
- package/dist/src/license/license_utils.d.ts +40 -0
- package/dist/src/license/license_utils.js +134 -0
- package/dist/src/license/licenses_api.d.ts +34 -0
- package/dist/src/license/licenses_api.js +98 -0
- package/dist/src/license/project_license.d.ts +20 -0
- package/dist/src/license/project_license.js +62 -0
- package/dist/src/oci_image/images.d.ts +4 -5
- package/dist/src/oci_image/utils.d.ts +4 -4
- package/dist/src/provider.d.ts +17 -5
- package/dist/src/provider.js +27 -5
- package/dist/src/providers/base_java.d.ts +3 -5
- package/dist/src/providers/base_javascript.d.ts +29 -7
- package/dist/src/providers/base_javascript.js +129 -22
- package/dist/src/providers/base_pyproject.d.ts +147 -0
- package/dist/src/providers/base_pyproject.js +279 -0
- package/dist/src/providers/golang_gomodules.d.ts +20 -13
- package/dist/src/providers/golang_gomodules.js +112 -114
- 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 +9 -3
- package/dist/src/providers/java_gradle.js +12 -2
- package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
- package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
- package/dist/src/providers/java_maven.d.ts +12 -5
- package/dist/src/providers/java_maven.js +33 -5
- package/dist/src/providers/javascript_pnpm.d.ts +1 -1
- package/dist/src/providers/javascript_pnpm.js +2 -2
- package/dist/src/providers/manifest.d.ts +2 -0
- package/dist/src/providers/manifest.js +22 -4
- package/dist/src/providers/processors/yarn_berry_processor.js +82 -3
- package/dist/src/providers/python_controller.d.ts +5 -2
- package/dist/src/providers/python_controller.js +56 -58
- package/dist/src/providers/python_pip.d.ts +11 -4
- package/dist/src/providers/python_pip.js +47 -54
- package/dist/src/providers/python_poetry.d.ts +42 -0
- package/dist/src/providers/python_poetry.js +146 -0
- package/dist/src/providers/python_uv.d.ts +26 -0
- package/dist/src/providers/python_uv.js +118 -0
- package/dist/src/providers/requirements_parser.d.ts +6 -0
- package/dist/src/providers/requirements_parser.js +24 -0
- package/dist/src/providers/rust_cargo.d.ts +52 -0
- package/dist/src/providers/rust_cargo.js +614 -0
- package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
- package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
- package/dist/src/sbom.d.ts +10 -1
- package/dist/src/sbom.js +12 -2
- package/dist/src/tools.d.ts +22 -6
- package/dist/src/tools.js +56 -1
- package/dist/src/workspace.d.ts +61 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +23 -10
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
|
-
import { EOL } from 'os';
|
|
3
2
|
import { PackageURL } from 'packageurl-js';
|
|
3
|
+
import { readLicenseFile } from '../license/license_utils.js';
|
|
4
4
|
import Sbom from '../sbom.js';
|
|
5
5
|
import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from "../tools.js";
|
|
6
6
|
import Python_controller from './python_controller.js';
|
|
7
|
-
|
|
7
|
+
import { getParser, getIgnoreQuery, getPinnedVersionQuery } from './requirements_parser.js';
|
|
8
|
+
export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
|
|
8
9
|
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
|
|
9
10
|
/**
|
|
10
11
|
* @type {string} ecosystem for python-pip is 'pip'
|
|
@@ -18,6 +19,13 @@ const ecosystem = 'pip';
|
|
|
18
19
|
function isSupported(manifestName) {
|
|
19
20
|
return 'requirements.txt' === manifestName;
|
|
20
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Python requirements.txt has no standard license field
|
|
24
|
+
* @param {string} manifestPath - path to requirements.txt
|
|
25
|
+
* @returns {string|null}
|
|
26
|
+
*/
|
|
27
|
+
// eslint-disable-next-line no-unused-vars
|
|
28
|
+
function readLicenseFromManifest(manifestPath) { return readLicenseFile(manifestPath); }
|
|
21
29
|
/**
|
|
22
30
|
* @param {string} manifestDir - the directory where the manifest lies
|
|
23
31
|
*/
|
|
@@ -26,12 +34,12 @@ function validateLockFile() { return true; }
|
|
|
26
34
|
* Provide content and content type for python-pip stack analysis.
|
|
27
35
|
* @param {string} manifest - the manifest path or name
|
|
28
36
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
29
|
-
* @returns {Provided}
|
|
37
|
+
* @returns {Promise<Provided>}
|
|
30
38
|
*/
|
|
31
|
-
function provideStack(manifest, opts = {}) {
|
|
39
|
+
async function provideStack(manifest, opts = {}) {
|
|
32
40
|
return {
|
|
33
41
|
ecosystem,
|
|
34
|
-
content: createSbomStackAnalysis(manifest, opts),
|
|
42
|
+
content: await createSbomStackAnalysis(manifest, opts),
|
|
35
43
|
contentType: 'application/vnd.cyclonedx+json'
|
|
36
44
|
};
|
|
37
45
|
}
|
|
@@ -39,12 +47,12 @@ function provideStack(manifest, opts = {}) {
|
|
|
39
47
|
* Provide content and content type for python-pip component analysis.
|
|
40
48
|
* @param {string} manifest - path to requirements.txt for component report
|
|
41
49
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
42
|
-
* @returns {Provided}
|
|
50
|
+
* @returns {Promise<Provided>}
|
|
43
51
|
*/
|
|
44
|
-
function provideComponent(manifest, opts = {}) {
|
|
52
|
+
async function provideComponent(manifest, opts = {}) {
|
|
45
53
|
return {
|
|
46
54
|
ecosystem,
|
|
47
|
-
content: getSbomForComponentAnalysis(manifest, opts),
|
|
55
|
+
content: await getSbomForComponentAnalysis(manifest, opts),
|
|
48
56
|
contentType: 'application/vnd.cyclonedx+json'
|
|
49
57
|
};
|
|
50
58
|
}
|
|
@@ -66,49 +74,34 @@ function addAllDependencies(source, dep, sbom) {
|
|
|
66
74
|
}
|
|
67
75
|
/**
|
|
68
76
|
*
|
|
69
|
-
* @param
|
|
70
|
-
* @return {
|
|
71
|
-
*/
|
|
72
|
-
function splitToNameVersion(nameVersion) {
|
|
73
|
-
let result = [];
|
|
74
|
-
if (nameVersion.includes("==")) {
|
|
75
|
-
return nameVersion.split("==");
|
|
76
|
-
}
|
|
77
|
-
const regex = /[^\w\s-_]/g;
|
|
78
|
-
let endIndex = nameVersion.search(regex);
|
|
79
|
-
if (endIndex === -1) {
|
|
80
|
-
return [nameVersion.trim()];
|
|
81
|
-
}
|
|
82
|
-
result.push(nameVersion.substring(0, endIndex).trim());
|
|
83
|
-
return result;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
*
|
|
87
|
-
* @param {string} requirementTxtContent
|
|
88
|
-
* @return {PackageURL []}
|
|
77
|
+
* @param {string} manifest - path to requirements.txt
|
|
78
|
+
* @return {Promise<PackageURL[]>}
|
|
89
79
|
*/
|
|
90
|
-
function getIgnoredDependencies(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
80
|
+
async function getIgnoredDependencies(manifest) {
|
|
81
|
+
const [parser, ignoreQuery, pinnedVersionQuery] = await Promise.all([
|
|
82
|
+
getParser(), getIgnoreQuery(), getPinnedVersionQuery()
|
|
83
|
+
]);
|
|
84
|
+
const content = fs.readFileSync(manifest).toString();
|
|
85
|
+
const tree = parser.parse(content);
|
|
86
|
+
return ignoreQuery.matches(tree.rootNode).map(match => {
|
|
87
|
+
const reqNode = match.captures.find(c => c.name === 'req').node;
|
|
88
|
+
const name = match.captures.find(c => c.name === 'name').node.text;
|
|
89
|
+
const versionMatches = pinnedVersionQuery.matches(reqNode);
|
|
90
|
+
const version = versionMatches.length > 0
|
|
91
|
+
? versionMatches[0].captures.find(c => c.name === 'version').node.text
|
|
92
|
+
: undefined;
|
|
93
|
+
return toPurl(name, version);
|
|
101
94
|
});
|
|
102
95
|
}
|
|
103
96
|
/**
|
|
104
97
|
*
|
|
105
|
-
* @param {string}
|
|
98
|
+
* @param {string} manifest - path to requirements.txt
|
|
106
99
|
* @param {Sbom} sbom object to filter out from it exhortignore dependencies.
|
|
107
100
|
* @param {{Object}} opts - various options and settings for the application
|
|
108
101
|
* @private
|
|
109
102
|
*/
|
|
110
|
-
function handleIgnoredDependencies(
|
|
111
|
-
let ignoredDeps = getIgnoredDependencies(
|
|
103
|
+
async function handleIgnoredDependencies(manifest, sbom, opts = {}) {
|
|
104
|
+
let ignoredDeps = await getIgnoredDependencies(manifest);
|
|
112
105
|
let matchManifestVersions = getCustom("MATCH_MANIFEST_VERSIONS", "true", opts);
|
|
113
106
|
if (matchManifestVersions === "true") {
|
|
114
107
|
const ignoredDepsVersion = ignoredDeps.filter(dep => dep.version !== undefined);
|
|
@@ -172,22 +165,22 @@ const DEFAULT_PIP_ROOT_COMPONENT_VERSION = "0.0.0";
|
|
|
172
165
|
* Create sbom json string out of a manifest path for stack analysis.
|
|
173
166
|
* @param {string} manifest - path for requirements.txt
|
|
174
167
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
175
|
-
* @returns {string} the sbom json string content
|
|
168
|
+
* @returns {Promise<string>} the sbom json string content
|
|
176
169
|
* @private
|
|
177
170
|
*/
|
|
178
|
-
function createSbomStackAnalysis(manifest, opts = {}) {
|
|
171
|
+
async function createSbomStackAnalysis(manifest, opts = {}) {
|
|
179
172
|
let binaries = {};
|
|
180
173
|
let createVirtualPythonEnv = handlePythonEnvironment(binaries, opts);
|
|
181
174
|
let pythonController = new Python_controller(createVirtualPythonEnv === "false", binaries.pip, binaries.python, manifest, opts);
|
|
182
|
-
let dependencies = pythonController.getDependencies(true);
|
|
175
|
+
let dependencies = await pythonController.getDependencies(true);
|
|
183
176
|
let sbom = new Sbom();
|
|
184
177
|
const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
|
|
185
|
-
|
|
178
|
+
const license = readLicenseFromManifest(manifest);
|
|
179
|
+
sbom.addRoot(rootPurl, license);
|
|
186
180
|
dependencies.forEach(dep => {
|
|
187
181
|
addAllDependencies(rootPurl, dep, sbom);
|
|
188
182
|
});
|
|
189
|
-
|
|
190
|
-
handleIgnoredDependencies(requirementTxtContent, sbom, opts);
|
|
183
|
+
await handleIgnoredDependencies(manifest, sbom, opts);
|
|
191
184
|
// 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
|
|
192
185
|
// sbom.removeRootComponent()
|
|
193
186
|
return sbom.getAsJsonString(opts);
|
|
@@ -196,22 +189,22 @@ function createSbomStackAnalysis(manifest, opts = {}) {
|
|
|
196
189
|
* Create a sbom json string out of a manifest content for component analysis
|
|
197
190
|
* @param {string} manifest - path to requirements.txt
|
|
198
191
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
199
|
-
* @returns {string} the sbom json string content
|
|
192
|
+
* @returns {Promise<string>} the sbom json string content
|
|
200
193
|
* @private
|
|
201
194
|
*/
|
|
202
|
-
function getSbomForComponentAnalysis(manifest, opts = {}) {
|
|
195
|
+
async function getSbomForComponentAnalysis(manifest, opts = {}) {
|
|
203
196
|
let binaries = {};
|
|
204
197
|
let createVirtualPythonEnv = handlePythonEnvironment(binaries, opts);
|
|
205
198
|
let pythonController = new Python_controller(createVirtualPythonEnv === "false", binaries.pip, binaries.python, manifest, opts);
|
|
206
|
-
let dependencies = pythonController.getDependencies(false);
|
|
199
|
+
let dependencies = await pythonController.getDependencies(false);
|
|
207
200
|
let sbom = new Sbom();
|
|
208
201
|
const rootPurl = toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION);
|
|
209
|
-
|
|
202
|
+
const license = readLicenseFromManifest(manifest);
|
|
203
|
+
sbom.addRoot(rootPurl, license);
|
|
210
204
|
dependencies.forEach(dep => {
|
|
211
205
|
sbom.addDependency(rootPurl, toPurl(dep.name, dep.version));
|
|
212
206
|
});
|
|
213
|
-
|
|
214
|
-
handleIgnoredDependencies(requirementTxtContent, sbom, opts);
|
|
207
|
+
await handleIgnoredDependencies(manifest, sbom, opts);
|
|
215
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
|
|
216
209
|
// sbom.removeRootComponent()
|
|
217
210
|
return sbom.getAsJsonString(opts);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export default class Python_poetry extends Base_pyproject {
|
|
2
|
+
/**
|
|
3
|
+
* Get poetry show --tree output.
|
|
4
|
+
* @param {string} manifestDir
|
|
5
|
+
* @param {Object} opts
|
|
6
|
+
* @returns {string}
|
|
7
|
+
*/
|
|
8
|
+
_getPoetryShowTreeOutput(manifestDir: string, opts: any): string;
|
|
9
|
+
/**
|
|
10
|
+
* Get poetry show --all output (flat list with resolved versions).
|
|
11
|
+
* @param {string} manifestDir
|
|
12
|
+
* @param {Object} opts
|
|
13
|
+
* @returns {string}
|
|
14
|
+
*/
|
|
15
|
+
_getPoetryShowAllOutput(manifestDir: string, opts: any): string;
|
|
16
|
+
/**
|
|
17
|
+
* Parse poetry show --all output into a version map.
|
|
18
|
+
* Lines look like: "name (!) 1.2.3 Description text..."
|
|
19
|
+
* or: "name 1.2.3 Description text..."
|
|
20
|
+
* @param {string} output
|
|
21
|
+
* @returns {Map<string, string>} canonical name -> version
|
|
22
|
+
*/
|
|
23
|
+
_parsePoetryShowAll(output: string): Map<string, string>;
|
|
24
|
+
/**
|
|
25
|
+
* Parse poetry show --tree output into a dependency graph structure.
|
|
26
|
+
* Top-level lines (no indentation/tree chars) are direct deps: "name version description"
|
|
27
|
+
* Indented lines are transitive deps with tree chars: "├── name >=constraint"
|
|
28
|
+
*
|
|
29
|
+
* @param {string} treeOutput
|
|
30
|
+
* @param {Map<string, string>} versionMap - canonical name -> resolved version
|
|
31
|
+
* @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
|
|
32
|
+
*/
|
|
33
|
+
_parsePoetryTree(treeOutput: string, versionMap: Map<string, string>): {
|
|
34
|
+
directDeps: string[];
|
|
35
|
+
graph: Map<string, {
|
|
36
|
+
name: string;
|
|
37
|
+
version: string;
|
|
38
|
+
children: string[];
|
|
39
|
+
}>;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
import Base_pyproject from './base_pyproject.js';
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
|
|
2
|
+
import Base_pyproject from './base_pyproject.js';
|
|
3
|
+
export default class Python_poetry extends Base_pyproject {
|
|
4
|
+
/** @returns {string} */
|
|
5
|
+
_lockFileName() {
|
|
6
|
+
return 'poetry.lock';
|
|
7
|
+
}
|
|
8
|
+
/** @returns {string} */
|
|
9
|
+
_cmdName() {
|
|
10
|
+
return 'poetry';
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} manifestDir
|
|
14
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
15
|
+
* @param {Object} opts
|
|
16
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
17
|
+
*/
|
|
18
|
+
async _getDependencyData(manifestDir, parsed, opts) {
|
|
19
|
+
let treeOutput = this._getPoetryShowTreeOutput(manifestDir, opts);
|
|
20
|
+
let showAllOutput = this._getPoetryShowAllOutput(manifestDir, opts);
|
|
21
|
+
let versionMap = this._parsePoetryShowAll(showAllOutput);
|
|
22
|
+
return this._parsePoetryTree(treeOutput, versionMap);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Get poetry show --tree output.
|
|
26
|
+
* @param {string} manifestDir
|
|
27
|
+
* @param {Object} opts
|
|
28
|
+
* @returns {string}
|
|
29
|
+
*/
|
|
30
|
+
_getPoetryShowTreeOutput(manifestDir, opts) {
|
|
31
|
+
if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_TREE')) {
|
|
32
|
+
return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_TREE'], 'base64').toString('utf-8');
|
|
33
|
+
}
|
|
34
|
+
let poetryBin = getCustomPath('poetry', opts);
|
|
35
|
+
return invokeCommand(poetryBin, ['show', '--tree', '--no-ansi'], { cwd: manifestDir }).toString();
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get poetry show --all output (flat list with resolved versions).
|
|
39
|
+
* @param {string} manifestDir
|
|
40
|
+
* @param {Object} opts
|
|
41
|
+
* @returns {string}
|
|
42
|
+
*/
|
|
43
|
+
_getPoetryShowAllOutput(manifestDir, opts) {
|
|
44
|
+
if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_ALL')) {
|
|
45
|
+
return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_ALL'], 'base64').toString('utf-8');
|
|
46
|
+
}
|
|
47
|
+
let poetryBin = getCustomPath('poetry', opts);
|
|
48
|
+
return invokeCommand(poetryBin, ['show', '--no-ansi', '--all'], { cwd: manifestDir }).toString();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Parse poetry show --all output into a version map.
|
|
52
|
+
* Lines look like: "name (!) 1.2.3 Description text..."
|
|
53
|
+
* or: "name 1.2.3 Description text..."
|
|
54
|
+
* @param {string} output
|
|
55
|
+
* @returns {Map<string, string>} canonical name -> version
|
|
56
|
+
*/
|
|
57
|
+
_parsePoetryShowAll(output) {
|
|
58
|
+
let versions = new Map();
|
|
59
|
+
let lines = output.split(/\r?\n/);
|
|
60
|
+
for (let line of lines) {
|
|
61
|
+
let trimmed = line.trim();
|
|
62
|
+
if (!trimmed) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
let match = trimmed.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(?:\(!\)\s+)?(\S+)/);
|
|
66
|
+
if (match) {
|
|
67
|
+
versions.set(this._canonicalize(match[1]), match[2]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return versions;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Parse poetry show --tree output into a dependency graph structure.
|
|
74
|
+
* Top-level lines (no indentation/tree chars) are direct deps: "name version description"
|
|
75
|
+
* Indented lines are transitive deps with tree chars: "├── name >=constraint"
|
|
76
|
+
*
|
|
77
|
+
* @param {string} treeOutput
|
|
78
|
+
* @param {Map<string, string>} versionMap - canonical name -> resolved version
|
|
79
|
+
* @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
|
|
80
|
+
*/
|
|
81
|
+
_parsePoetryTree(treeOutput, versionMap) {
|
|
82
|
+
let lines = treeOutput.split(/\r?\n/);
|
|
83
|
+
let graph = new Map();
|
|
84
|
+
let directDeps = [];
|
|
85
|
+
let stack = []; // [{key, depth}]
|
|
86
|
+
let currentDirectDep = null;
|
|
87
|
+
for (let line of lines) {
|
|
88
|
+
if (!line.trim()) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
// top-level line: "name version description..."
|
|
92
|
+
let topMatch = line.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(\S+)\s/);
|
|
93
|
+
if (topMatch) {
|
|
94
|
+
let name = topMatch[1];
|
|
95
|
+
let version = topMatch[2];
|
|
96
|
+
let key = this._canonicalize(name);
|
|
97
|
+
directDeps.push(key);
|
|
98
|
+
if (!graph.has(key)) {
|
|
99
|
+
graph.set(key, { name, version, children: [] });
|
|
100
|
+
}
|
|
101
|
+
currentDirectDep = key;
|
|
102
|
+
stack = [{ key, depth: -1 }];
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (!currentDirectDep) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
// indented line with tree chars (UTF-8 box-drawing: ├── └── │)
|
|
109
|
+
let nameStart = line.search(/[A-Za-z0-9]/);
|
|
110
|
+
if (nameStart < 0) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
let rest = line.substring(nameStart);
|
|
114
|
+
let depMatch = rest.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)/);
|
|
115
|
+
if (!depMatch) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
let depName = depMatch[1];
|
|
119
|
+
let depKey = this._canonicalize(depName);
|
|
120
|
+
// determine depth by counting tree-drawing groups in the prefix
|
|
121
|
+
let prefix = line.substring(0, nameStart);
|
|
122
|
+
let depth = (prefix.match(/(?:[├└│ ][\s─]{2} ?)/g) || []).length;
|
|
123
|
+
// resolve version from the version map
|
|
124
|
+
let version = versionMap.get(depKey) || null;
|
|
125
|
+
if (!version) {
|
|
126
|
+
throw new Error(`poetry: package '${depName}' has no resolved version`);
|
|
127
|
+
}
|
|
128
|
+
if (!graph.has(depKey)) {
|
|
129
|
+
graph.set(depKey, { name: depName, version, children: [] });
|
|
130
|
+
}
|
|
131
|
+
// pop stack back to find the parent at depth-1
|
|
132
|
+
while (stack.length > 0 && stack[stack.length - 1].depth >= depth) {
|
|
133
|
+
stack.pop();
|
|
134
|
+
}
|
|
135
|
+
if (stack.length > 0) {
|
|
136
|
+
let parentKey = stack[stack.length - 1].key;
|
|
137
|
+
let parentEntry = graph.get(parentKey);
|
|
138
|
+
if (parentEntry && !parentEntry.children.includes(depKey)) {
|
|
139
|
+
parentEntry.children.push(depKey);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
stack.push({ key: depKey, depth });
|
|
143
|
+
}
|
|
144
|
+
return { directDeps, graph };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export default class Python_uv extends Base_pyproject {
|
|
2
|
+
/**
|
|
3
|
+
* Get the uv export output, either from env var or by running the command.
|
|
4
|
+
* @param {string} manifestDir
|
|
5
|
+
* @param {Object} opts
|
|
6
|
+
* @returns {string}
|
|
7
|
+
*/
|
|
8
|
+
_getUvExportOutput(manifestDir: string, opts: any): string;
|
|
9
|
+
/**
|
|
10
|
+
* Parse uv export output into a dependency graph using tree-sitter-requirements
|
|
11
|
+
* for package/version extraction and string parsing for "# via" comments.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} output
|
|
14
|
+
* @param {string} projectName - canonical project name to identify direct deps
|
|
15
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
16
|
+
*/
|
|
17
|
+
_parseUvExport(output: string, projectName: string): Promise<{
|
|
18
|
+
directDeps: string[];
|
|
19
|
+
graph: Map<string, {
|
|
20
|
+
name: string;
|
|
21
|
+
version: string;
|
|
22
|
+
children: string[];
|
|
23
|
+
}>;
|
|
24
|
+
}>;
|
|
25
|
+
}
|
|
26
|
+
import Base_pyproject from './base_pyproject.js';
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
|
|
2
|
+
import Base_pyproject from './base_pyproject.js';
|
|
3
|
+
import { getParser, getPinnedVersionQuery } from './requirements_parser.js';
|
|
4
|
+
export default class Python_uv extends Base_pyproject {
|
|
5
|
+
/** @returns {string} */
|
|
6
|
+
_lockFileName() {
|
|
7
|
+
return 'uv.lock';
|
|
8
|
+
}
|
|
9
|
+
/** @returns {string} */
|
|
10
|
+
_cmdName() {
|
|
11
|
+
return 'uv';
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} manifestDir
|
|
15
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
16
|
+
* @param {Object} opts
|
|
17
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
18
|
+
*/
|
|
19
|
+
async _getDependencyData(manifestDir, parsed, opts) {
|
|
20
|
+
let projectName = this._getProjectName(parsed);
|
|
21
|
+
let uvOutput = this._getUvExportOutput(manifestDir, opts);
|
|
22
|
+
return this._parseUvExport(uvOutput, projectName);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Get the uv export output, either from env var or by running the command.
|
|
26
|
+
* @param {string} manifestDir
|
|
27
|
+
* @param {Object} opts
|
|
28
|
+
* @returns {string}
|
|
29
|
+
*/
|
|
30
|
+
_getUvExportOutput(manifestDir, opts) {
|
|
31
|
+
if (environmentVariableIsPopulated('TRUSTIFY_DA_UV_EXPORT')) {
|
|
32
|
+
return Buffer.from(process.env['TRUSTIFY_DA_UV_EXPORT'], 'base64').toString('ascii');
|
|
33
|
+
}
|
|
34
|
+
let uvBin = getCustomPath('uv', opts);
|
|
35
|
+
return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes'], { cwd: manifestDir }).toString();
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parse uv export output into a dependency graph using tree-sitter-requirements
|
|
39
|
+
* for package/version extraction and string parsing for "# via" comments.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} output
|
|
42
|
+
* @param {string} projectName - canonical project name to identify direct deps
|
|
43
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
44
|
+
*/
|
|
45
|
+
async _parseUvExport(output, projectName) {
|
|
46
|
+
let [parser, pinnedVersionQuery] = await Promise.all([
|
|
47
|
+
getParser(), getPinnedVersionQuery()
|
|
48
|
+
]);
|
|
49
|
+
let tree = parser.parse(output);
|
|
50
|
+
let root = tree.rootNode;
|
|
51
|
+
let canonProjectName = this._canonicalize(projectName);
|
|
52
|
+
let packages = new Map(); // canonical name -> {name, version, parents: Set}
|
|
53
|
+
let currentPkg = null;
|
|
54
|
+
let collectingVia = false;
|
|
55
|
+
for (let child of root.children) {
|
|
56
|
+
if (child.type === 'requirement') {
|
|
57
|
+
let nameNode = child.children.find(c => c.type === 'package');
|
|
58
|
+
if (!nameNode) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
let name = nameNode.text;
|
|
62
|
+
let version = null;
|
|
63
|
+
let versionMatches = pinnedVersionQuery.matches(child);
|
|
64
|
+
if (versionMatches.length > 0) {
|
|
65
|
+
version = versionMatches[0].captures.find(c => c.name === 'version').node.text;
|
|
66
|
+
}
|
|
67
|
+
if (!version) {
|
|
68
|
+
throw new Error(`uv export: package '${name}' has no pinned version`);
|
|
69
|
+
}
|
|
70
|
+
let key = this._canonicalize(name);
|
|
71
|
+
currentPkg = { name, version, parents: new Set() };
|
|
72
|
+
packages.set(key, currentPkg);
|
|
73
|
+
collectingVia = false;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (child.type === 'comment' && currentPkg) {
|
|
77
|
+
let text = child.text.trim();
|
|
78
|
+
let viaSingle = text.match(/^# via ([A-Za-z0-9][A-Za-z0-9._-]*)$/);
|
|
79
|
+
if (viaSingle) {
|
|
80
|
+
currentPkg.parents.add(this._canonicalize(viaSingle[1]));
|
|
81
|
+
collectingVia = false;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (text === '# via') {
|
|
85
|
+
collectingVia = true;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (collectingVia) {
|
|
89
|
+
let parentMatch = text.match(/^#\s+([A-Za-z0-9][A-Za-z0-9._-]*)$/);
|
|
90
|
+
if (parentMatch) {
|
|
91
|
+
currentPkg.parents.add(this._canonicalize(parentMatch[1]));
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
collectingVia = false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// Build forward dependency map and extract direct deps in one pass
|
|
99
|
+
let graph = new Map();
|
|
100
|
+
let directDeps = [];
|
|
101
|
+
for (let [key, pkg] of packages) {
|
|
102
|
+
graph.set(key, { name: pkg.name, version: pkg.version, children: [] });
|
|
103
|
+
}
|
|
104
|
+
for (let [childKey, pkg] of packages) {
|
|
105
|
+
for (let parentKey of pkg.parents) {
|
|
106
|
+
if (parentKey === canonProjectName) {
|
|
107
|
+
directDeps.push(childKey);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
let parentEntry = graph.get(parentKey);
|
|
111
|
+
if (parentEntry) {
|
|
112
|
+
parentEntry.children.push(childKey);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return { directDeps, graph };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export function getParser(): Promise<Parser>;
|
|
2
|
+
export function getRequirementQuery(): Promise<Query>;
|
|
3
|
+
export function getIgnoreQuery(): Promise<Query>;
|
|
4
|
+
export function getPinnedVersionQuery(): Promise<Query>;
|
|
5
|
+
import { Parser } from 'web-tree-sitter';
|
|
6
|
+
import { Query } from 'web-tree-sitter';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { Language, Parser, Query } from 'web-tree-sitter';
|
|
3
|
+
const wasmUrl = new URL('./tree-sitter-requirements.wasm', import.meta.url);
|
|
4
|
+
async function init() {
|
|
5
|
+
await Parser.init();
|
|
6
|
+
const wasmBytes = new Uint8Array(await readFile(wasmUrl));
|
|
7
|
+
return await Language.load(wasmBytes);
|
|
8
|
+
}
|
|
9
|
+
export async function getParser() {
|
|
10
|
+
const language = await init();
|
|
11
|
+
return new Parser().setLanguage(language);
|
|
12
|
+
}
|
|
13
|
+
export async function getRequirementQuery() {
|
|
14
|
+
const language = await init();
|
|
15
|
+
return new Query(language, '(requirement (package) @name) @req');
|
|
16
|
+
}
|
|
17
|
+
export async function getIgnoreQuery() {
|
|
18
|
+
const language = await init();
|
|
19
|
+
return new Query(language, '((requirement (package) @name) @req . (comment) @comment (#match? @comment "^#[\\t ]*exhortignore"))');
|
|
20
|
+
}
|
|
21
|
+
export async function getPinnedVersionQuery() {
|
|
22
|
+
const language = await init();
|
|
23
|
+
return new Query(language, '(version_spec (version_cmp) @cmp (version) @version (#eq? @cmp "=="))');
|
|
24
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
declare namespace _default {
|
|
2
|
+
export { isSupported };
|
|
3
|
+
export { validateLockFile };
|
|
4
|
+
export { provideComponent };
|
|
5
|
+
export { provideStack };
|
|
6
|
+
export { readLicenseFromManifest };
|
|
7
|
+
}
|
|
8
|
+
export default _default;
|
|
9
|
+
export type Provided = import("../provider").Provided;
|
|
10
|
+
/**
|
|
11
|
+
* @param {string} manifestName - the subject manifest name-type
|
|
12
|
+
* @returns {boolean} - return true if `Cargo.toml` is the manifest name-type
|
|
13
|
+
*/
|
|
14
|
+
declare function isSupported(manifestName: string): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Validates that Cargo.lock exists in the manifest directory or in a parent
|
|
17
|
+
* workspace root directory. In Cargo workspaces the lock file always lives at
|
|
18
|
+
* the workspace root, so when a member crate's Cargo.toml is provided we walk
|
|
19
|
+
* up the directory tree looking for Cargo.lock (stopping when we find a
|
|
20
|
+
* Cargo.toml that contains a [workspace] section, or when we reach the
|
|
21
|
+
* filesystem root).
|
|
22
|
+
* When TRUSTIFY_DA_WORKSPACE_DIR is provided (via env var or opts),
|
|
23
|
+
* checks only that directory for Cargo.lock — no walk-up.
|
|
24
|
+
* @param {string} manifestDir - the directory where the manifest lies
|
|
25
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
|
|
26
|
+
* @returns {boolean} true if Cargo.lock is found
|
|
27
|
+
*/
|
|
28
|
+
declare function validateLockFile(manifestDir: string, opts?: {
|
|
29
|
+
TRUSTIFY_DA_WORKSPACE_DIR?: string;
|
|
30
|
+
}): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Provide content and content type for Cargo component analysis.
|
|
33
|
+
* @param {string} manifest - path to Cargo.toml for component report
|
|
34
|
+
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
35
|
+
* @returns {Provided}
|
|
36
|
+
*/
|
|
37
|
+
declare function provideComponent(manifest: string, opts?: {}): Provided;
|
|
38
|
+
/**
|
|
39
|
+
* Provide content and content type for Cargo stack analysis.
|
|
40
|
+
* @param {string} manifest - the manifest path
|
|
41
|
+
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
42
|
+
* @returns {Provided}
|
|
43
|
+
*/
|
|
44
|
+
declare function provideStack(manifest: string, opts?: {}): Provided;
|
|
45
|
+
/**
|
|
46
|
+
* Read project license from Cargo.toml, with fallback to LICENSE file.
|
|
47
|
+
* Supports the `license` field under `[package]` (single crate / workspace
|
|
48
|
+
* with root) and under `[workspace.package]` (virtual workspaces).
|
|
49
|
+
* @param {string} manifestPath - path to Cargo.toml
|
|
50
|
+
* @returns {string|null} SPDX identifier or null
|
|
51
|
+
*/
|
|
52
|
+
declare function readLicenseFromManifest(manifestPath: string): string | null;
|