@trustify-da/trustify-da-javascript-client 0.3.0-ea.e12bc82 → 0.3.0-ea.e645720
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 +23 -11
- package/dist/src/analysis.d.ts +21 -5
- package/dist/src/analysis.js +74 -80
- package/dist/src/batch_opts.d.ts +24 -0
- package/dist/src/batch_opts.js +35 -0
- package/dist/src/cli.js +241 -8
- package/dist/src/cyclone_dx_sbom.d.ts +17 -3
- package/dist/src/cyclone_dx_sbom.js +48 -8
- package/dist/src/index.d.ts +197 -11
- package/dist/src/index.js +352 -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/oci_image/utils.js +11 -2
- package/dist/src/provider.d.ts +17 -5
- package/dist/src/provider.js +29 -5
- package/dist/src/providers/base_java.d.ts +3 -14
- package/dist/src/providers/base_java.js +2 -38
- 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 +153 -0
- package/dist/src/providers/base_pyproject.js +315 -0
- package/dist/src/providers/golang_gomodules.d.ts +29 -13
- package/dist/src/providers/golang_gomodules.js +176 -121
- 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 +28 -3
- package/dist/src/providers/java_gradle.js +128 -4
- 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 +20 -5
- package/dist/src/providers/java_maven.js +126 -6
- 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.d.ts +1 -1
- package/dist/src/providers/javascript_pnpm.js +8 -4
- 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 +10 -3
- package/dist/src/providers/python_controller.js +61 -59
- package/dist/src/providers/python_pip.d.ts +15 -4
- package/dist/src/providers/python_pip.js +51 -58
- package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
- package/dist/src/providers/python_pip_pyproject.js +144 -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/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 +17 -2
- package/dist/src/sbom.js +16 -4
- package/dist/src/tools.d.ts +48 -6
- package/dist/src/tools.js +114 -1
- package/dist/src/workspace.d.ts +70 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +24 -12
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discover all pyproject.toml manifest paths in a uv workspace.
|
|
3
|
+
* Parses `[tool.uv.workspace]` from root pyproject.toml and glob-expands member patterns.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain pyproject.toml and uv.lock)
|
|
6
|
+
* @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}]
|
|
7
|
+
* @returns {Promise<string[]>} Paths to pyproject.toml files (absolute)
|
|
8
|
+
*/
|
|
9
|
+
export function discoverUvWorkspaceMembers(workspaceRoot: string, opts?: {
|
|
10
|
+
workspaceDiscoveryIgnore?: string[];
|
|
11
|
+
TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string;
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
}): Promise<string[]>;
|
|
14
|
+
export default class Python_uv extends Base_pyproject {
|
|
15
|
+
/**
|
|
16
|
+
* @param {string} manifestDir - directory containing the target pyproject.toml
|
|
17
|
+
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
|
|
18
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
19
|
+
* @param {Object} opts
|
|
20
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
21
|
+
*/
|
|
22
|
+
_getDependencyData(manifestDir: string, workspaceDir: string, parsed: object, opts: any): Promise<{
|
|
23
|
+
directDeps: string[];
|
|
24
|
+
graph: Map<string, {
|
|
25
|
+
name: string;
|
|
26
|
+
version: string;
|
|
27
|
+
children: string[];
|
|
28
|
+
}>;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* Get the uv export output, either from env var or by running the command.
|
|
32
|
+
* @param {string} manifestDir
|
|
33
|
+
* @param {Object} opts
|
|
34
|
+
* @returns {string}
|
|
35
|
+
*/
|
|
36
|
+
_getUvExportOutput(manifestDir: string, opts: any): string;
|
|
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
|
+
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
|
|
44
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
45
|
+
*/
|
|
46
|
+
_parseUvExport(output: string, projectName: string, workspaceDir: string): Promise<{
|
|
47
|
+
directDeps: string[];
|
|
48
|
+
graph: Map<string, {
|
|
49
|
+
name: string;
|
|
50
|
+
version: string;
|
|
51
|
+
children: string[];
|
|
52
|
+
}>;
|
|
53
|
+
}>;
|
|
54
|
+
}
|
|
55
|
+
import Base_pyproject from './base_pyproject.js';
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import fg from 'fast-glob';
|
|
4
|
+
import { parse as parseToml } from 'smol-toml';
|
|
5
|
+
import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
|
|
6
|
+
import { filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore, toManifestGlobPatterns } from '../workspace.js';
|
|
7
|
+
import Base_pyproject from './base_pyproject.js';
|
|
8
|
+
import { evaluateMarker } from './marker_evaluator.js';
|
|
9
|
+
import { getParser, getPinnedVersionQuery } from './requirements_parser.js';
|
|
10
|
+
export default class Python_uv extends Base_pyproject {
|
|
11
|
+
/** @returns {string} */
|
|
12
|
+
_lockFileName() {
|
|
13
|
+
return 'uv.lock';
|
|
14
|
+
}
|
|
15
|
+
/** @returns {string} */
|
|
16
|
+
_cmdName() {
|
|
17
|
+
return 'uv';
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} manifestDir - directory containing the target pyproject.toml
|
|
21
|
+
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
|
|
22
|
+
* @param {object} parsed - parsed pyproject.toml
|
|
23
|
+
* @param {Object} opts
|
|
24
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
25
|
+
*/
|
|
26
|
+
async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
|
|
27
|
+
let projectName = this._getProjectName(parsed);
|
|
28
|
+
let uvOutput = this._getUvExportOutput(manifestDir, opts);
|
|
29
|
+
return this._parseUvExport(uvOutput, projectName, workspaceDir);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Get the uv export output, either from env var or by running the command.
|
|
33
|
+
* @param {string} manifestDir
|
|
34
|
+
* @param {Object} opts
|
|
35
|
+
* @returns {string}
|
|
36
|
+
*/
|
|
37
|
+
_getUvExportOutput(manifestDir, opts) {
|
|
38
|
+
if (environmentVariableIsPopulated('TRUSTIFY_DA_UV_EXPORT')) {
|
|
39
|
+
return Buffer.from(process.env['TRUSTIFY_DA_UV_EXPORT'], 'base64').toString('ascii');
|
|
40
|
+
}
|
|
41
|
+
let uvBin = getCustomPath('uv', opts);
|
|
42
|
+
return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes', '--no-dev', '--no-emit-project'], { cwd: manifestDir }).toString();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Parse uv export output into a dependency graph using tree-sitter-requirements
|
|
46
|
+
* for package/version extraction and string parsing for "# via" comments.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} output
|
|
49
|
+
* @param {string} projectName - canonical project name to identify direct deps
|
|
50
|
+
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
|
|
51
|
+
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
|
|
52
|
+
*/
|
|
53
|
+
async _parseUvExport(output, projectName, workspaceDir) {
|
|
54
|
+
let [parser, pinnedVersionQuery] = await Promise.all([
|
|
55
|
+
getParser(), getPinnedVersionQuery()
|
|
56
|
+
]);
|
|
57
|
+
let tree = parser.parse(output);
|
|
58
|
+
let root = tree.rootNode;
|
|
59
|
+
let canonProjectName = this._canonicalize(projectName);
|
|
60
|
+
let packages = new Map(); // canonical name -> {name, version, parents: Set}
|
|
61
|
+
let currentPkg = null;
|
|
62
|
+
let collectingVia = false;
|
|
63
|
+
for (let child of root.children) {
|
|
64
|
+
if (child.type === 'global_opt') {
|
|
65
|
+
let optNode = child.children.find(c => c.type === 'option');
|
|
66
|
+
let pathNode = child.children.find(c => c.type === 'path');
|
|
67
|
+
if (optNode?.text === '-e' && pathNode && workspaceDir) {
|
|
68
|
+
let memberDir = path.resolve(workspaceDir, pathNode.text);
|
|
69
|
+
let memberManifest = path.join(memberDir, 'pyproject.toml');
|
|
70
|
+
if (fs.existsSync(memberManifest)) {
|
|
71
|
+
let memberParsed = parseToml(fs.readFileSync(memberManifest, 'utf-8'));
|
|
72
|
+
let name = memberParsed.project?.name || memberParsed.tool?.poetry?.name;
|
|
73
|
+
let version = memberParsed.project?.version || memberParsed.tool?.poetry?.version;
|
|
74
|
+
if (name && version) {
|
|
75
|
+
let key = this._canonicalize(name);
|
|
76
|
+
if (key === canonProjectName) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
currentPkg = { name, version, parents: new Set() };
|
|
80
|
+
packages.set(key, currentPkg);
|
|
81
|
+
collectingVia = false;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
currentPkg = null;
|
|
87
|
+
collectingVia = false;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (child.type === 'requirement') {
|
|
91
|
+
let nameNode = child.children.find(c => c.type === 'package');
|
|
92
|
+
if (!nameNode) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
// Skip packages with non-matching PEP 508 markers, e.g. "pywin32==311 ; sys_platform == 'win32'"
|
|
96
|
+
let markerNode = child.children.find(c => c.type === 'marker_spec');
|
|
97
|
+
if (markerNode) {
|
|
98
|
+
let markerText = markerNode.text.replace(/^\s*;\s*/, '');
|
|
99
|
+
if (!evaluateMarker(markerText)) {
|
|
100
|
+
currentPkg = null;
|
|
101
|
+
collectingVia = false;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
let name = nameNode.text;
|
|
106
|
+
let version = null;
|
|
107
|
+
let versionMatches = pinnedVersionQuery.matches(child);
|
|
108
|
+
if (versionMatches.length > 0) {
|
|
109
|
+
version = versionMatches[0].captures.find(c => c.name === 'version').node.text;
|
|
110
|
+
}
|
|
111
|
+
if (!version) {
|
|
112
|
+
throw new Error(`uv export: package '${name}' has no pinned version`);
|
|
113
|
+
}
|
|
114
|
+
let key = this._canonicalize(name);
|
|
115
|
+
currentPkg = { name, version, parents: new Set() };
|
|
116
|
+
packages.set(key, currentPkg);
|
|
117
|
+
collectingVia = false;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (child.type === 'comment' && currentPkg) {
|
|
121
|
+
let text = child.text.trim();
|
|
122
|
+
let viaSingle = text.match(/^# via ([A-Za-z0-9][A-Za-z0-9._-]*)$/);
|
|
123
|
+
if (viaSingle) {
|
|
124
|
+
currentPkg.parents.add(this._canonicalize(viaSingle[1]));
|
|
125
|
+
collectingVia = false;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (text === '# via') {
|
|
129
|
+
collectingVia = true;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (collectingVia) {
|
|
133
|
+
let parentMatch = text.match(/^#\s+([A-Za-z0-9][A-Za-z0-9._-]*)$/);
|
|
134
|
+
if (parentMatch) {
|
|
135
|
+
currentPkg.parents.add(this._canonicalize(parentMatch[1]));
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
collectingVia = false;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// Build forward dependency map and extract direct deps in one pass
|
|
143
|
+
let graph = new Map();
|
|
144
|
+
let directDeps = [];
|
|
145
|
+
for (let [key, pkg] of packages) {
|
|
146
|
+
graph.set(key, { name: pkg.name, version: pkg.version, children: [] });
|
|
147
|
+
}
|
|
148
|
+
for (let [childKey, pkg] of packages) {
|
|
149
|
+
for (let parentKey of pkg.parents) {
|
|
150
|
+
if (parentKey === canonProjectName) {
|
|
151
|
+
directDeps.push(childKey);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
let parentEntry = graph.get(parentKey);
|
|
155
|
+
if (parentEntry) {
|
|
156
|
+
parentEntry.children.push(childKey);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return { directDeps, graph };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const DEFAULT_UV_DISCOVERY_IGNORE = [
|
|
164
|
+
'**/__pycache__/**',
|
|
165
|
+
'**/.venv/**',
|
|
166
|
+
];
|
|
167
|
+
/**
|
|
168
|
+
* Discover all pyproject.toml manifest paths in a uv workspace.
|
|
169
|
+
* Parses `[tool.uv.workspace]` from root pyproject.toml and glob-expands member patterns.
|
|
170
|
+
*
|
|
171
|
+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain pyproject.toml and uv.lock)
|
|
172
|
+
* @param {{ workspaceDiscoveryIgnore?: string[], TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string, [key: string]: unknown }} [opts={}]
|
|
173
|
+
* @returns {Promise<string[]>} Paths to pyproject.toml files (absolute)
|
|
174
|
+
*/
|
|
175
|
+
export async function discoverUvWorkspaceMembers(workspaceRoot, opts = {}) {
|
|
176
|
+
const root = path.resolve(workspaceRoot);
|
|
177
|
+
const rootPyproject = path.join(root, 'pyproject.toml');
|
|
178
|
+
const uvLock = path.join(root, 'uv.lock');
|
|
179
|
+
if (!fs.existsSync(rootPyproject) || !fs.existsSync(uvLock)) {
|
|
180
|
+
return [];
|
|
181
|
+
}
|
|
182
|
+
let parsed;
|
|
183
|
+
try {
|
|
184
|
+
parsed = parseToml(fs.readFileSync(rootPyproject, 'utf-8'));
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
return [];
|
|
188
|
+
}
|
|
189
|
+
const workspaceConfig = parsed?.tool?.uv?.workspace;
|
|
190
|
+
if (!workspaceConfig) {
|
|
191
|
+
return [];
|
|
192
|
+
}
|
|
193
|
+
const memberPatterns = workspaceConfig.members;
|
|
194
|
+
if (!Array.isArray(memberPatterns) || memberPatterns.length === 0) {
|
|
195
|
+
return [];
|
|
196
|
+
}
|
|
197
|
+
const excludePatterns = Array.isArray(workspaceConfig.exclude) ? workspaceConfig.exclude : [];
|
|
198
|
+
const excludeGlobs = excludePatterns
|
|
199
|
+
.filter(p => typeof p === 'string' && p.trim())
|
|
200
|
+
.map(p => `${p.trim()}/pyproject.toml`);
|
|
201
|
+
const ignorePatterns = [...resolveWorkspaceDiscoveryIgnore(opts), ...DEFAULT_UV_DISCOVERY_IGNORE];
|
|
202
|
+
const globOpts = {
|
|
203
|
+
cwd: root,
|
|
204
|
+
absolute: true,
|
|
205
|
+
onlyFiles: true,
|
|
206
|
+
ignore: [...ignorePatterns, ...excludeGlobs],
|
|
207
|
+
followSymbolicLinks: false,
|
|
208
|
+
};
|
|
209
|
+
const patterns = toManifestGlobPatterns(memberPatterns.filter(p => typeof p === 'string'), 'pyproject.toml');
|
|
210
|
+
const manifestPaths = await fg(patterns, globOpts);
|
|
211
|
+
if (!manifestPaths.includes(rootPyproject) && hasProjectMetadata(parsed)) {
|
|
212
|
+
manifestPaths.unshift(rootPyproject);
|
|
213
|
+
}
|
|
214
|
+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* @param {import('smol-toml').TomlTable} parsedPyProject
|
|
218
|
+
* @returns {boolean}
|
|
219
|
+
*/
|
|
220
|
+
function hasProjectMetadata(parsedPyProject) {
|
|
221
|
+
try {
|
|
222
|
+
return typeof parsedPyProject?.project?.name === 'string' && parsedPyProject.project.name.trim() !== '';
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -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;
|