@trustify-da/trustify-da-javascript-client 0.3.0-ea.d71f957 → 0.3.0-ea.d84439a

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 CHANGED
@@ -224,7 +224,8 @@ $ trustify-da-javascript-client license /path/to/package.json
224
224
  <li><a href="https://www.javascript.com/">JavaScript</a> - <a href="https://pnpm.io/">pnpm</a></li>
225
225
  <li><a href="https://www.javascript.com/">JavaScript</a> - <a href="https://classic.yarnpkg.com/">Yarn Classic</a> / <a href="https://yarnpkg.com/">Yarn Berry</a></li>
226
226
  <li><a href="https://go.dev/">Golang</a> - <a href="https://go.dev/blog/using-go-modules/">Go Modules</a></li>
227
- <li><a href="https://www.python.org/">Python</a> - <a href="https://pypi.org/project/pip/">pip Installer</a></li>
227
+ <li><a href="https://www.python.org/">Python</a> - <a href="https://pypi.org/project/pip/">pip Installer</a> (<code>requirements.txt</code>)</li>
228
+ <li><a href="https://www.python.org/">Python</a> - <a href="https://python-poetry.org/">Poetry</a> / <a href="https://docs.astral.sh/uv/">uv</a> (<code>pyproject.toml</code>)</li>
228
229
  <li><a href="https://gradle.org/">Gradle (Groovy and Kotlin DSL)</a> - <a href="https://gradle.org/install/">Gradle Installation</a></li>
229
230
  <li><a href="https://www.rust-lang.org/">Rust</a> - <a href="https://doc.rust-lang.org/cargo/">Cargo</a></li>
230
231
  </ul>
@@ -391,7 +392,30 @@ version = "1.10"
391
392
  log = "0.4" # trustify-da-ignore
392
393
  ```
393
394
 
394
- All of the 6 above examples are valid for marking a package to be ignored
395
+
396
+ <em>Python pyproject.toml</em> users can add a comment with <code>#exhortignore</code> (or <code># trustify-da-ignore</code>) next to the dependency in <code>pyproject.toml</code>.
397
+
398
+ PEP 621 style (<code>[project]</code> dependencies):
399
+ ```toml
400
+ [project]
401
+ dependencies = [
402
+ "flask>=2.0.3",
403
+ "requests>=2.25.1",
404
+ "uvicorn>=0.17.0", #exhortignore
405
+ "click>=8.0.4", # trustify-da-ignore
406
+ ]
407
+ ```
408
+
409
+ Poetry style (<code>[tool.poetry.dependencies]</code>):
410
+ ```toml
411
+ [tool.poetry.dependencies]
412
+ flask = "^2.0.3"
413
+ requests = "^2.25.1"
414
+ uvicorn = "^0.17.0" #exhortignore
415
+ click = "^8.0.4" # trustify-da-ignore
416
+ ```
417
+
418
+ All of the above examples are valid for marking a package to be ignored
395
419
  </li>
396
420
  </ul>
397
421
 
@@ -421,6 +445,8 @@ let options = {
421
445
  'TRUSTIFY_DA_PIP3_PATH' : '/path/to/pip3',
422
446
  'TRUSTIFY_DA_PYTHON_PATH' : '/path/to/python',
423
447
  'TRUSTIFY_DA_PIP_PATH' : '/path/to/pip',
448
+ 'TRUSTIFY_DA_UV_PATH' : '/path/to/uv',
449
+ 'TRUSTIFY_DA_POETRY_PATH' : '/path/to/poetry',
424
450
  'TRUSTIFY_DA_GRADLE_PATH' : '/path/to/gradle',
425
451
  'TRUSTIFY_DA_CARGO_PATH' : '/path/to/cargo',
426
452
  // Workspace root for monorepos (Cargo, npm/pnpm/yarn); lock file expected here
@@ -567,6 +593,16 @@ following keys for setting custom paths for the said executables.
567
593
  <td>TRUSTIFY_DA_CARGO_PATH</td>
568
594
  </tr>
569
595
  <tr>
596
+ <td><a href="https://docs.astral.sh/uv/">uv</a></td>
597
+ <td><em>uv</em></td>
598
+ <td>TRUSTIFY_DA_UV_PATH</td>
599
+ </tr>
600
+ <tr>
601
+ <td><a href="https://python-poetry.org/">Poetry</a></td>
602
+ <td><em>poetry</em></td>
603
+ <td>TRUSTIFY_DA_POETRY_PATH</td>
604
+ </tr>
605
+ <tr>
570
606
  <td>Workspace root (monorepos)</td>
571
607
  <td>—</td>
572
608
  <td>workspaceDir / TRUSTIFY_DA_WORKSPACE_DIR</td>
@@ -619,6 +655,24 @@ TRUSTIFY_DA_GO_MVS_LOGIC_ENABLED=false
619
655
 
620
656
  #### Python Support
621
657
 
658
+ The client supports two Python manifest formats:
659
+
660
+ - **`requirements.txt`** — uses pip/pip3 to resolve dependencies
661
+ - **`pyproject.toml`** — uses [uv](https://docs.astral.sh/uv/) or [Poetry](https://python-poetry.org/) to resolve dependencies
662
+
663
+ ##### pyproject.toml
664
+
665
+ For `pyproject.toml` projects, the client detects which tool manages the project by checking for lock files:
666
+ - If `poetry.lock` is present and `[tool.poetry]` is defined, **Poetry** is used (`poetry show --tree` and `poetry show --all`)
667
+ - If `uv.lock` is present, **uv** is used (`uv export --format requirements.txt --frozen --no-hashes`)
668
+ - If neither lock file is found, an error is thrown
669
+
670
+ Both PEP 621 (`[project]` dependencies) and Poetry-style (`[tool.poetry.dependencies]`) are supported.
671
+
672
+ Custom executable paths can be set via `TRUSTIFY_DA_UV_PATH` and `TRUSTIFY_DA_POETRY_PATH`.
673
+
674
+ ##### requirements.txt
675
+
622
676
  By default, For python support, the api assumes that the package is installed using the pip/pip3 binary on the system PATH, or using the customized
623
677
  Binaries passed to environment variables. In any case, If the package is not installed , then an error will be thrown.
624
678
 
package/dist/package.json CHANGED
@@ -38,7 +38,7 @@
38
38
  "lint": "eslint src test --ext js",
39
39
  "lint:fix": "eslint src test --ext js --fix",
40
40
  "test": "c8 npm run tests",
41
- "tests": "mocha --config .mocharc.json --grep \".*analysis module.*\" --invert",
41
+ "tests": "mocha --config .mocharc.json",
42
42
  "tests:rep": "mocha --reporter-option maxDiffSize=0 --reporter json > unit-tests-result.json",
43
43
  "pretest": "cp node_modules/tree-sitter-requirements/tree-sitter-requirements.wasm src/providers/tree-sitter-requirements.wasm && cp node_modules/tree-sitter-gomod/tree-sitter-gomod.wasm src/providers/tree-sitter-gomod.wasm",
44
44
  "precompile": "rm -rf dist",
@@ -189,7 +189,7 @@ async function requestImages(imageRefs, url, html = false, opts = {}) {
189
189
  if (opts['TRUSTIFY_DA_RECOMMENDATIONS_ENABLED'] === 'false') {
190
190
  finalUrl.searchParams.append('recommend', 'false');
191
191
  }
192
- const resp = await fetch(finalUrl, {
192
+ const fetchOptions = addProxyAgent({
193
193
  method: 'POST',
194
194
  headers: {
195
195
  'Accept': html ? 'text/html' : 'application/json',
@@ -197,7 +197,8 @@ async function requestImages(imageRefs, url, html = false, opts = {}) {
197
197
  ...getTokenHeaders(opts)
198
198
  },
199
199
  body: JSON.stringify(imageSboms),
200
- });
200
+ }, opts);
201
+ const resp = await fetch(finalUrl, fetchOptions);
201
202
  if (resp.status === 200) {
202
203
  let result;
203
204
  if (!html) {
package/dist/src/cli.js CHANGED
@@ -1,9 +1,10 @@
1
1
  #!/usr/bin/env node
2
+ import fs from 'node:fs';
2
3
  import * as path from "path";
3
4
  import yargs from 'yargs';
4
5
  import { hideBin } from 'yargs/helpers';
5
6
  import { getProjectLicense, getLicenseDetails } from './license/index.js';
6
- import client, { selectTrustifyDABackend } from './index.js';
7
+ import client, { selectTrustifyDABackend, generateSbom } from './index.js';
7
8
  // command for component analysis take manifest type and content
8
9
  const component = {
9
10
  command: 'component </path/to/manifest>',
@@ -325,15 +326,63 @@ const license = {
325
326
  console.log(JSON.stringify(output, null, 2));
326
327
  }
327
328
  };
329
+ const sbom = {
330
+ command: 'sbom </path/to/manifest> [--output]',
331
+ desc: 'generate a CycloneDX SBOM from a manifest file',
332
+ builder: yargs => yargs.positional('/path/to/manifest', {
333
+ desc: 'manifest path for SBOM generation',
334
+ type: 'string',
335
+ normalize: true,
336
+ }).options({
337
+ output: {
338
+ alias: 'o',
339
+ desc: 'Write SBOM JSON to a file instead of stdout',
340
+ type: 'string',
341
+ normalize: true,
342
+ },
343
+ workspaceDir: {
344
+ alias: 'w',
345
+ desc: 'Workspace root directory (for monorepos; lock file is expected here)',
346
+ type: 'string',
347
+ normalize: true,
348
+ }
349
+ }),
350
+ handler: async (args) => {
351
+ let manifest = args['/path/to/manifest'];
352
+ const opts = args.workspaceDir ? { TRUSTIFY_DA_WORKSPACE_DIR: args.workspaceDir } : {};
353
+ let result;
354
+ try {
355
+ result = await generateSbom(manifest, opts);
356
+ }
357
+ catch (err) {
358
+ console.error(JSON.stringify({ error: `Failed to generate SBOM: ${err.message}` }, null, 2));
359
+ process.exit(1);
360
+ }
361
+ const json = JSON.stringify(result, null, 2);
362
+ if (args.output) {
363
+ try {
364
+ fs.writeFileSync(args.output, json);
365
+ }
366
+ catch (err) {
367
+ console.error(JSON.stringify({ error: `Failed to write output file: ${err.message}` }, null, 2));
368
+ process.exit(1);
369
+ }
370
+ }
371
+ else {
372
+ console.log(json);
373
+ }
374
+ }
375
+ };
328
376
  // parse and invoke the command
329
377
  yargs(hideBin(process.argv))
330
- .usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|stack-batch|image|validate-token|license}`)
378
+ .usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|stack-batch|image|validate-token|license|sbom}`)
331
379
  .command(stack)
332
380
  .command(stackBatch)
333
381
  .command(component)
334
382
  .command(image)
335
383
  .command(validateToken)
336
384
  .command(license)
385
+ .command(sbom)
337
386
  .scriptName('')
338
387
  .version(false)
339
388
  .demandCommand(1)
@@ -13,6 +13,15 @@ export function selectTrustifyDABackend(opts?: {
13
13
  TRUSTIFY_DA_DEBUG?: string | undefined;
14
14
  TRUSTIFY_DA_BACKEND_URL?: string | undefined;
15
15
  }): string;
16
+ /**
17
+ * Generate a CycloneDX SBOM from a manifest file. No backend HTTP request is made.
18
+ *
19
+ * @param {string} manifestPath - path to the manifest file (e.g. pom.xml, package.json)
20
+ * @param {Options} [opts={}] - optional options (e.g. workspace dir, tool paths)
21
+ * @returns {Promise<object>} parsed CycloneDX SBOM JSON object
22
+ * @throws {Error} if the manifest is unsupported or SBOM generation fails
23
+ */
24
+ export function generateSbom(manifestPath: string, opts?: Options): Promise<object>;
16
25
  export { parseImageRef } from "./oci_image/utils.js";
17
26
  export { ImageRef } from "./oci_image/images.js";
18
27
  declare namespace _default {
@@ -21,6 +30,7 @@ declare namespace _default {
21
30
  export { stackAnalysisBatch };
22
31
  export { imageAnalysis };
23
32
  export { validateToken };
33
+ export { generateSbom };
24
34
  }
25
35
  export default _default;
26
36
  export type Options = {
@@ -64,6 +74,8 @@ export type Options = {
64
74
  TRUSTIFY_DA_CONTINUE_ON_ERROR?: string | undefined;
65
75
  batchMetadata?: boolean | undefined;
66
76
  TRUSTIFY_DA_BATCH_METADATA?: string | undefined;
77
+ TRUSTIFY_DA_UV_PATH?: string | undefined;
78
+ TRUSTIFY_DA_POETRY_PATH?: string | undefined;
67
79
  [key: string]: string | number | boolean | string[] | undefined;
68
80
  };
69
81
  export type BatchAnalysisMetadata = {
package/dist/src/index.js CHANGED
@@ -12,7 +12,7 @@ import * as url from 'url';
12
12
  export { parseImageRef } from "./oci_image/utils.js";
13
13
  export { ImageRef } from "./oci_image/images.js";
14
14
  export { getProjectLicense, findLicenseFilePath, identifyLicense, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
15
- export default { componentAnalysis, stackAnalysis, stackAnalysisBatch, imageAnalysis, validateToken };
15
+ export default { componentAnalysis, stackAnalysis, stackAnalysisBatch, imageAnalysis, validateToken, generateSbom };
16
16
  export { discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson, resolveWorkspaceDiscoveryIgnore, filterManifestPathsByDiscoveryIgnore, resolveContinueOnError, resolveBatchMetadata, };
17
17
  /**
18
18
  * @typedef {{
@@ -56,6 +56,8 @@ export { discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson
56
56
  * TRUSTIFY_DA_CONTINUE_ON_ERROR?: string | undefined,
57
57
  * batchMetadata?: boolean | undefined,
58
58
  * TRUSTIFY_DA_BATCH_METADATA?: string | undefined,
59
+ * TRUSTIFY_DA_UV_PATH?: string | undefined,
60
+ * TRUSTIFY_DA_POETRY_PATH?: string | undefined,
59
61
  * [key: string]: string | number | boolean | string[] | undefined,
60
62
  * }} Options
61
63
  */
@@ -235,6 +237,22 @@ function buildBatchAnalysisMetadata(root, ecosystem, totalSbomAttempts, successf
235
237
  errors: [...errors],
236
238
  };
237
239
  }
240
+ /**
241
+ * Generate a CycloneDX SBOM from a manifest file. No backend HTTP request is made.
242
+ *
243
+ * @param {string} manifestPath - path to the manifest file (e.g. pom.xml, package.json)
244
+ * @param {Options} [opts={}] - optional options (e.g. workspace dir, tool paths)
245
+ * @returns {Promise<object>} parsed CycloneDX SBOM JSON object
246
+ * @throws {Error} if the manifest is unsupported or SBOM generation fails
247
+ */
248
+ export async function generateSbom(manifestPath, opts = {}) {
249
+ fs.accessSync(manifestPath, fs.constants.R_OK);
250
+ const result = await generateOneSbom(manifestPath, opts);
251
+ if (!result.ok) {
252
+ throw new Error(`Failed to generate SBOM for ${result.manifestPath}: ${result.reason}`);
253
+ }
254
+ return result.sbom;
255
+ }
238
256
  /**
239
257
  * @typedef {{ ok: true, purl: string, sbom: object } | { ok: false, manifestPath: string, reason: string }} SbomResult
240
258
  */
@@ -135,7 +135,7 @@ function execSyft(imageRef, opts = {}) {
135
135
  function getSyftEnvs(dockerPath, podmanPath) {
136
136
  let path = null;
137
137
  if (dockerPath && podmanPath) {
138
- path = `${dockerPath}${File.pathSeparator}${podmanPath}`;
138
+ path = `${dockerPath}${delimiter}${podmanPath}`;
139
139
  }
140
140
  else if (dockerPath) {
141
141
  path = dockerPath;
@@ -276,7 +276,16 @@ function podmanGetVariant(opts = {}) {
276
276
  * @returns {string} - The information
277
277
  */
278
278
  function dockerPodmanInfo(dockerSupplier, podmanSupplier, opts = {}) {
279
- return dockerSupplier(opts) || podmanSupplier(opts);
279
+ try {
280
+ const result = dockerSupplier(opts);
281
+ if (result) {
282
+ return result;
283
+ }
284
+ }
285
+ catch (_) {
286
+ // docker not available, fall through to podman
287
+ }
288
+ return podmanSupplier(opts);
280
289
  }
281
290
  /**
282
291
  * Gets the digests for an image
@@ -7,6 +7,8 @@ import Javascript_npm from './providers/javascript_npm.js';
7
7
  import Javascript_pnpm from './providers/javascript_pnpm.js';
8
8
  import Javascript_yarn from './providers/javascript_yarn.js';
9
9
  import pythonPipProvider from './providers/python_pip.js';
10
+ import Python_poetry from './providers/python_poetry.js';
11
+ import Python_uv from './providers/python_uv.js';
10
12
  import rustCargoProvider from './providers/rust_cargo.js';
11
13
  /** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
12
14
  /** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string, Object): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
@@ -23,6 +25,8 @@ export const availableProviders = [
23
25
  new Javascript_npm(),
24
26
  golangGomodulesProvider,
25
27
  pythonPipProvider,
28
+ new Python_poetry(),
29
+ new Python_uv(),
26
30
  rustCargoProvider
27
31
  ];
28
32
  /**
@@ -0,0 +1,170 @@
1
+ /** @typedef {{name: string, version: string, children: string[]}} GraphEntry */
2
+ /** @typedef {{name: string, version: string, dependencies: DepTreeEntry[]}} DepTreeEntry */
3
+ /** @typedef {{directDeps: string[], graph: Map<string, GraphEntry>}} DependencyData */
4
+ /** @typedef {{ecosystem: string, content: string, contentType: string}} Provided */
5
+ export default class Base_pyproject {
6
+ /**
7
+ * @param {string} manifestName
8
+ * @returns {boolean}
9
+ */
10
+ isSupported(manifestName: string): boolean;
11
+ /**
12
+ * @param {string} manifestDir
13
+ * @param {Object} [opts={}]
14
+ * @returns {boolean}
15
+ */
16
+ validateLockFile(manifestDir: string, opts?: any): boolean;
17
+ /**
18
+ * Walk up from manifestDir to find the directory containing the lock file.
19
+ * Follows the same pattern as Base_javascript._findLockFileDir().
20
+ * @param {string} manifestDir
21
+ * @param {Object} [opts={}]
22
+ * @returns {string|null}
23
+ * @protected
24
+ */
25
+ protected _findLockFileDir(manifestDir: string, opts?: any): string | null;
26
+ /**
27
+ * Detect workspace root boundaries.
28
+ * Currently only uv has native workspace support ([tool.uv.workspace] in pyproject.toml).
29
+ * Poetry has no workspace/monorepo support (python-poetry/poetry#2270), so each
30
+ * poetry project is treated independently — see Python_poetry._findLockFileDir().
31
+ * @param {string} dir
32
+ * @returns {boolean}
33
+ * @protected
34
+ */
35
+ protected _isWorkspaceRoot(dir: string): boolean;
36
+ /**
37
+ * Read project license from pyproject.toml, with fallback to LICENSE file.
38
+ * @param {string} manifestPath
39
+ * @returns {string|null}
40
+ */
41
+ readLicenseFromManifest(manifestPath: string): string | null;
42
+ /**
43
+ * @param {string} manifest - path to pyproject.toml
44
+ * @param {Object} [opts={}]
45
+ * @returns {Promise<Provided>}
46
+ */
47
+ provideStack(manifest: string, opts?: any): Promise<Provided>;
48
+ /**
49
+ * @param {string} manifest - path to pyproject.toml
50
+ * @param {Object} [opts={}]
51
+ * @returns {Promise<Provided>}
52
+ */
53
+ provideComponent(manifest: string, opts?: any): Promise<Provided>;
54
+ /**
55
+ * @returns {string}
56
+ * @protected
57
+ */
58
+ protected _lockFileName(): string;
59
+ /**
60
+ * @returns {string}
61
+ * @protected
62
+ */
63
+ protected _cmdName(): string;
64
+ /**
65
+ * Resolve dependencies using the tool-specific command and parser.
66
+ *
67
+ * @param {string} manifestDir - directory containing the target pyproject.toml
68
+ * @param {string} workspaceDir - workspace root (where the lock file lives);
69
+ * only used by providers that need workspace-level resolution (e.g. uv)
70
+ * @param {object} parsed - parsed pyproject.toml
71
+ * @param {Object} opts
72
+ * @returns {Promise<DependencyData>}
73
+ * @protected
74
+ */
75
+ protected _getDependencyData(manifestDir: string, workspaceDir: string, parsed: object, opts: any): Promise<DependencyData>;
76
+ /**
77
+ * Canonicalize a Python package name per PEP 503.
78
+ * @param {string} name
79
+ * @returns {string}
80
+ * @protected
81
+ */
82
+ protected _canonicalize(name: string): string;
83
+ /**
84
+ * Get the project name from pyproject.toml.
85
+ * @param {object} parsed
86
+ * @returns {string|null}
87
+ * @protected
88
+ */
89
+ protected _getProjectName(parsed: object): string | null;
90
+ /**
91
+ * Get the project version from pyproject.toml.
92
+ * @param {object} parsed
93
+ * @returns {string|null}
94
+ * @protected
95
+ */
96
+ protected _getProjectVersion(parsed: object): string | null;
97
+ /**
98
+ * Scan raw pyproject.toml text for dependencies with ignore markers.
99
+ * @param {string} manifestPath
100
+ * @returns {Set<string>}
101
+ * @protected
102
+ */
103
+ protected _getIgnoredDeps(manifestPath: string): Set<string>;
104
+ /**
105
+ * Build dependency tree from graph, starting from direct deps.
106
+ * @param {Map<string, GraphEntry>} graph
107
+ * @param {string[]} directDeps - canonical names of direct deps
108
+ * @param {Set<string>} ignoredDeps
109
+ * @param {boolean} includeTransitive
110
+ * @returns {DepTreeEntry[]}
111
+ * @protected
112
+ */
113
+ protected _buildDependencyTree(graph: Map<string, GraphEntry>, directDeps: string[], ignoredDeps: Set<string>, includeTransitive: boolean): DepTreeEntry[];
114
+ /**
115
+ * Recursively collect transitive dependencies.
116
+ * @param {Map<string, GraphEntry>} graph
117
+ * @param {string[]} childKeys
118
+ * @param {DepTreeEntry[]} result - mutated in place
119
+ * @param {Set<string>} ignoredDeps
120
+ * @param {Set<string>} visited
121
+ * @returns {void}
122
+ * @protected
123
+ */
124
+ protected _collectTransitive(graph: Map<string, GraphEntry>, childKeys: string[], result: DepTreeEntry[], ignoredDeps: Set<string>, visited: Set<string>): void;
125
+ /**
126
+ * @param {string} name
127
+ * @param {string} version
128
+ * @returns {PackageURL}
129
+ * @protected
130
+ */
131
+ protected _toPurl(name: string, version: string): PackageURL;
132
+ /**
133
+ * Recursively add a dependency and its transitive deps to the SBOM.
134
+ * @param {PackageURL} source
135
+ * @param {DepTreeEntry} dep
136
+ * @param {Sbom} sbom
137
+ * @returns {void}
138
+ * @private
139
+ */
140
+ private _addAllDependencies;
141
+ /**
142
+ * Create SBOM json string for a pyproject.toml project.
143
+ * @param {string} manifest - path to pyproject.toml
144
+ * @param {Object} opts
145
+ * @param {boolean} includeTransitive
146
+ * @returns {Promise<string>}
147
+ * @private
148
+ */
149
+ private _createSbom;
150
+ }
151
+ export type GraphEntry = {
152
+ name: string;
153
+ version: string;
154
+ children: string[];
155
+ };
156
+ export type DepTreeEntry = {
157
+ name: string;
158
+ version: string;
159
+ dependencies: DepTreeEntry[];
160
+ };
161
+ export type DependencyData = {
162
+ directDeps: string[];
163
+ graph: Map<string, GraphEntry>;
164
+ };
165
+ export type Provided = {
166
+ ecosystem: string;
167
+ content: string;
168
+ contentType: string;
169
+ };
170
+ import { PackageURL } from 'packageurl-js';
@@ -0,0 +1,338 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { PackageURL } from 'packageurl-js';
4
+ import { parse as parseToml } from 'smol-toml';
5
+ import { getLicense } from '../license/license_utils.js';
6
+ import Sbom from '../sbom.js';
7
+ import { getCustom } from '../tools.js';
8
+ const ecosystem = 'pip';
9
+ const IGNORE_MARKERS = ['exhortignore', 'trustify-da-ignore'];
10
+ const DEFAULT_ROOT_NAME = 'default-pip-root';
11
+ const DEFAULT_ROOT_VERSION = '0.0.0';
12
+ /** @typedef {{name: string, version: string, children: string[]}} GraphEntry */
13
+ /** @typedef {{name: string, version: string, dependencies: DepTreeEntry[]}} DepTreeEntry */
14
+ /** @typedef {{directDeps: string[], graph: Map<string, GraphEntry>}} DependencyData */
15
+ /** @typedef {{ecosystem: string, content: string, contentType: string}} Provided */
16
+ export default class Base_pyproject {
17
+ /**
18
+ * @param {string} manifestName
19
+ * @returns {boolean}
20
+ */
21
+ isSupported(manifestName) {
22
+ return 'pyproject.toml' === manifestName;
23
+ }
24
+ /**
25
+ * @param {string} manifestDir
26
+ * @param {Object} [opts={}]
27
+ * @returns {boolean}
28
+ */
29
+ validateLockFile(manifestDir, opts = {}) {
30
+ return this._findLockFileDir(manifestDir, opts) != null;
31
+ }
32
+ /**
33
+ * Walk up from manifestDir to find the directory containing the lock file.
34
+ * Follows the same pattern as Base_javascript._findLockFileDir().
35
+ * @param {string} manifestDir
36
+ * @param {Object} [opts={}]
37
+ * @returns {string|null}
38
+ * @protected
39
+ */
40
+ _findLockFileDir(manifestDir, opts = {}) {
41
+ const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
42
+ if (workspaceDir) {
43
+ const dir = path.resolve(workspaceDir);
44
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
45
+ }
46
+ let dir = path.resolve(manifestDir);
47
+ let parent = dir;
48
+ do {
49
+ dir = parent;
50
+ if (fs.existsSync(path.join(dir, this._lockFileName()))) {
51
+ return dir;
52
+ }
53
+ if (this._isWorkspaceRoot(dir)) {
54
+ return null;
55
+ }
56
+ parent = path.dirname(dir);
57
+ } while (parent !== dir);
58
+ return null;
59
+ }
60
+ /**
61
+ * Detect workspace root boundaries.
62
+ * Currently only uv has native workspace support ([tool.uv.workspace] in pyproject.toml).
63
+ * Poetry has no workspace/monorepo support (python-poetry/poetry#2270), so each
64
+ * poetry project is treated independently — see Python_poetry._findLockFileDir().
65
+ * @param {string} dir
66
+ * @returns {boolean}
67
+ * @protected
68
+ */
69
+ _isWorkspaceRoot(dir) {
70
+ const pyprojectPath = path.join(dir, 'pyproject.toml');
71
+ if (!fs.existsSync(pyprojectPath)) {
72
+ return false;
73
+ }
74
+ try {
75
+ const content = parseToml(fs.readFileSync(pyprojectPath, 'utf-8'));
76
+ if (content.tool?.uv?.workspace) {
77
+ return true;
78
+ }
79
+ }
80
+ catch (_) {
81
+ // ignore parse errors
82
+ }
83
+ return false;
84
+ }
85
+ /**
86
+ * Read project license from pyproject.toml, with fallback to LICENSE file.
87
+ * @param {string} manifestPath
88
+ * @returns {string|null}
89
+ */
90
+ readLicenseFromManifest(manifestPath) {
91
+ let fromManifest = null;
92
+ try {
93
+ let content = fs.readFileSync(manifestPath, 'utf-8');
94
+ let parsed = parseToml(content);
95
+ fromManifest = parsed.project?.license;
96
+ if (typeof fromManifest === 'object' && fromManifest != null) {
97
+ fromManifest = fromManifest.text || null;
98
+ }
99
+ if (!fromManifest) {
100
+ fromManifest = parsed.tool?.poetry?.license || null;
101
+ }
102
+ }
103
+ catch (_) {
104
+ // leave fromManifest as null
105
+ }
106
+ return getLicense(fromManifest, manifestPath);
107
+ }
108
+ /**
109
+ * @param {string} manifest - path to pyproject.toml
110
+ * @param {Object} [opts={}]
111
+ * @returns {Promise<Provided>}
112
+ */
113
+ async provideStack(manifest, opts = {}) {
114
+ return {
115
+ ecosystem,
116
+ content: await this._createSbom(manifest, opts, true),
117
+ contentType: 'application/vnd.cyclonedx+json'
118
+ };
119
+ }
120
+ /**
121
+ * @param {string} manifest - path to pyproject.toml
122
+ * @param {Object} [opts={}]
123
+ * @returns {Promise<Provided>}
124
+ */
125
+ async provideComponent(manifest, opts = {}) {
126
+ return {
127
+ ecosystem,
128
+ content: await this._createSbom(manifest, opts, false),
129
+ contentType: 'application/vnd.cyclonedx+json'
130
+ };
131
+ }
132
+ // --- abstract methods (subclasses must override) ---
133
+ /**
134
+ * @returns {string}
135
+ * @protected
136
+ */
137
+ _lockFileName() {
138
+ throw new TypeError('_lockFileName must be implemented');
139
+ }
140
+ /**
141
+ * @returns {string}
142
+ * @protected
143
+ */
144
+ _cmdName() {
145
+ throw new TypeError('_cmdName must be implemented');
146
+ }
147
+ /**
148
+ * Resolve dependencies using the tool-specific command and parser.
149
+ *
150
+ * @param {string} manifestDir - directory containing the target pyproject.toml
151
+ * @param {string} workspaceDir - workspace root (where the lock file lives);
152
+ * only used by providers that need workspace-level resolution (e.g. uv)
153
+ * @param {object} parsed - parsed pyproject.toml
154
+ * @param {Object} opts
155
+ * @returns {Promise<DependencyData>}
156
+ * @protected
157
+ */
158
+ // eslint-disable-next-line no-unused-vars
159
+ async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
160
+ throw new TypeError('_getDependencyData must be implemented');
161
+ }
162
+ // --- shared helpers ---
163
+ /**
164
+ * Canonicalize a Python package name per PEP 503.
165
+ * @param {string} name
166
+ * @returns {string}
167
+ * @protected
168
+ */
169
+ _canonicalize(name) {
170
+ return name.toLowerCase().replace(/[-_.]+/g, '-');
171
+ }
172
+ /**
173
+ * Get the project name from pyproject.toml.
174
+ * @param {object} parsed
175
+ * @returns {string|null}
176
+ * @protected
177
+ */
178
+ _getProjectName(parsed) {
179
+ return parsed.project?.name || parsed.tool?.poetry?.name || null;
180
+ }
181
+ /**
182
+ * Get the project version from pyproject.toml.
183
+ * @param {object} parsed
184
+ * @returns {string|null}
185
+ * @protected
186
+ */
187
+ _getProjectVersion(parsed) {
188
+ return parsed.project?.version || parsed.tool?.poetry?.version || null;
189
+ }
190
+ /**
191
+ * Scan raw pyproject.toml text for dependencies with ignore markers.
192
+ * @param {string} manifestPath
193
+ * @returns {Set<string>}
194
+ * @protected
195
+ */
196
+ _getIgnoredDeps(manifestPath) {
197
+ let ignored = new Set();
198
+ let content = fs.readFileSync(manifestPath, 'utf-8');
199
+ let lines = content.split(/\r?\n/);
200
+ for (let line of lines) {
201
+ if (!IGNORE_MARKERS.some(m => line.includes(m))) {
202
+ continue;
203
+ }
204
+ // PEP 621 style: "requests>=2.25" #exhortignore
205
+ let pep621Match = line.match(/^\s*"([^"]+)"/);
206
+ if (pep621Match) {
207
+ let reqStr = pep621Match[1];
208
+ let nameMatch = reqStr.match(/^([A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?)/);
209
+ if (nameMatch) {
210
+ ignored.add(this._canonicalize(nameMatch[1]));
211
+ }
212
+ continue;
213
+ }
214
+ // Poetry style: requests = "^2.25" #exhortignore
215
+ let poetryMatch = line.match(/^\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*=/);
216
+ if (poetryMatch) {
217
+ ignored.add(this._canonicalize(poetryMatch[1]));
218
+ }
219
+ }
220
+ return ignored;
221
+ }
222
+ /**
223
+ * Build dependency tree from graph, starting from direct deps.
224
+ * @param {Map<string, GraphEntry>} graph
225
+ * @param {string[]} directDeps - canonical names of direct deps
226
+ * @param {Set<string>} ignoredDeps
227
+ * @param {boolean} includeTransitive
228
+ * @returns {DepTreeEntry[]}
229
+ * @protected
230
+ */
231
+ _buildDependencyTree(graph, directDeps, ignoredDeps, includeTransitive) {
232
+ let result = [];
233
+ for (let key of directDeps) {
234
+ if (ignoredDeps.has(key)) {
235
+ continue;
236
+ }
237
+ let entry = graph.get(key);
238
+ if (!entry) {
239
+ continue;
240
+ }
241
+ let depTree = [];
242
+ if (includeTransitive) {
243
+ let visited = new Set();
244
+ visited.add(key);
245
+ this._collectTransitive(graph, entry.children, depTree, ignoredDeps, visited);
246
+ }
247
+ result.push({ name: entry.name, version: entry.version, dependencies: depTree });
248
+ }
249
+ result.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
250
+ return result;
251
+ }
252
+ /**
253
+ * Recursively collect transitive dependencies.
254
+ * @param {Map<string, GraphEntry>} graph
255
+ * @param {string[]} childKeys
256
+ * @param {DepTreeEntry[]} result - mutated in place
257
+ * @param {Set<string>} ignoredDeps
258
+ * @param {Set<string>} visited
259
+ * @returns {void}
260
+ * @protected
261
+ */
262
+ _collectTransitive(graph, childKeys, result, ignoredDeps, visited) {
263
+ for (let childKey of childKeys) {
264
+ let canonKey = this._canonicalize(childKey);
265
+ if (ignoredDeps.has(canonKey)) {
266
+ continue;
267
+ }
268
+ if (visited.has(canonKey)) {
269
+ continue;
270
+ }
271
+ visited.add(canonKey);
272
+ let entry = graph.get(canonKey);
273
+ if (!entry) {
274
+ continue;
275
+ }
276
+ let childDeps = [];
277
+ this._collectTransitive(graph, entry.children, childDeps, ignoredDeps, visited);
278
+ result.push({ name: entry.name, version: entry.version, dependencies: childDeps });
279
+ }
280
+ result.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
281
+ }
282
+ /**
283
+ * @param {string} name
284
+ * @param {string} version
285
+ * @returns {PackageURL}
286
+ * @protected
287
+ */
288
+ _toPurl(name, version) {
289
+ return new PackageURL('pypi', undefined, name, version, undefined, undefined);
290
+ }
291
+ /**
292
+ * Recursively add a dependency and its transitive deps to the SBOM.
293
+ * @param {PackageURL} source
294
+ * @param {DepTreeEntry} dep
295
+ * @param {Sbom} sbom
296
+ * @returns {void}
297
+ * @private
298
+ */
299
+ _addAllDependencies(source, dep, sbom) {
300
+ let targetPurl = this._toPurl(dep.name, dep.version);
301
+ sbom.addDependency(source, targetPurl);
302
+ if (dep.dependencies && dep.dependencies.length > 0) {
303
+ dep.dependencies.forEach(child => this._addAllDependencies(this._toPurl(dep.name, dep.version), child, sbom));
304
+ }
305
+ }
306
+ /**
307
+ * Create SBOM json string for a pyproject.toml project.
308
+ * @param {string} manifest - path to pyproject.toml
309
+ * @param {Object} opts
310
+ * @param {boolean} includeTransitive
311
+ * @returns {Promise<string>}
312
+ * @private
313
+ */
314
+ async _createSbom(manifest, opts, includeTransitive) {
315
+ let manifestDir = path.dirname(manifest);
316
+ let content = fs.readFileSync(manifest, 'utf-8');
317
+ let parsed = parseToml(content);
318
+ let workspaceDir = this._findLockFileDir(manifestDir, opts) || manifestDir;
319
+ let { directDeps, graph } = await this._getDependencyData(manifestDir, workspaceDir, parsed, opts);
320
+ let ignoredDeps = this._getIgnoredDeps(manifest);
321
+ let dependencies = this._buildDependencyTree(graph, directDeps, ignoredDeps, includeTransitive);
322
+ let sbom = new Sbom();
323
+ let rootName = this._getProjectName(parsed) || DEFAULT_ROOT_NAME;
324
+ let rootVersion = this._getProjectVersion(parsed) || DEFAULT_ROOT_VERSION;
325
+ let rootPurl = this._toPurl(rootName, rootVersion);
326
+ let license = this.readLicenseFromManifest(manifest);
327
+ sbom.addRoot(rootPurl, license);
328
+ dependencies.forEach(dep => {
329
+ if (includeTransitive) {
330
+ this._addAllDependencies(rootPurl, dep, sbom);
331
+ }
332
+ else {
333
+ sbom.addDependency(rootPurl, this._toPurl(dep.name, dep.version));
334
+ }
335
+ });
336
+ return sbom.getAsJsonString(opts);
337
+ }
338
+ }
@@ -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,169 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from '../tools.js';
4
+ import Base_pyproject from './base_pyproject.js';
5
+ export default class Python_poetry extends Base_pyproject {
6
+ /**
7
+ * Poetry has no native workspace/monorepo support (python-poetry/poetry#2270).
8
+ * Each poetry project is treated independently — no lock file walk-up.
9
+ * Running `poetry show` from a parent directory returns the parent's deps, not
10
+ * the sub-package's, so walk-up would produce incorrect SBOMs.
11
+ * @param {string} manifestDir
12
+ * @param {Object} [opts={}]
13
+ * @returns {string|null}
14
+ * @protected
15
+ */
16
+ _findLockFileDir(manifestDir, opts = {}) {
17
+ const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
18
+ if (workspaceDir) {
19
+ const dir = path.resolve(workspaceDir);
20
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
21
+ }
22
+ const dir = path.resolve(manifestDir);
23
+ return fs.existsSync(path.join(dir, this._lockFileName())) ? dir : null;
24
+ }
25
+ /** @returns {string} */
26
+ _lockFileName() {
27
+ return 'poetry.lock';
28
+ }
29
+ /** @returns {string} */
30
+ _cmdName() {
31
+ return 'poetry';
32
+ }
33
+ /**
34
+ * @param {string} manifestDir
35
+ * @param {string} _workspaceDir - unused (poetry has no workspace support)
36
+ * @param {object} parsed - parsed pyproject.toml
37
+ * @param {Object} opts
38
+ * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
39
+ */
40
+ // eslint-disable-next-line no-unused-vars
41
+ async _getDependencyData(manifestDir, _workspaceDir, parsed, opts) {
42
+ let treeOutput = this._getPoetryShowTreeOutput(manifestDir, opts);
43
+ let showAllOutput = this._getPoetryShowAllOutput(manifestDir, opts);
44
+ let versionMap = this._parsePoetryShowAll(showAllOutput);
45
+ return this._parsePoetryTree(treeOutput, versionMap);
46
+ }
47
+ /**
48
+ * Get poetry show --tree output.
49
+ * @param {string} manifestDir
50
+ * @param {Object} opts
51
+ * @returns {string}
52
+ */
53
+ _getPoetryShowTreeOutput(manifestDir, opts) {
54
+ if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_TREE')) {
55
+ return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_TREE'], 'base64').toString('utf-8');
56
+ }
57
+ let poetryBin = getCustomPath('poetry', opts);
58
+ return invokeCommand(poetryBin, ['show', '--tree', '--no-ansi'], { cwd: manifestDir }).toString();
59
+ }
60
+ /**
61
+ * Get poetry show --all output (flat list with resolved versions).
62
+ * @param {string} manifestDir
63
+ * @param {Object} opts
64
+ * @returns {string}
65
+ */
66
+ _getPoetryShowAllOutput(manifestDir, opts) {
67
+ if (environmentVariableIsPopulated('TRUSTIFY_DA_POETRY_SHOW_ALL')) {
68
+ return Buffer.from(process.env['TRUSTIFY_DA_POETRY_SHOW_ALL'], 'base64').toString('utf-8');
69
+ }
70
+ let poetryBin = getCustomPath('poetry', opts);
71
+ return invokeCommand(poetryBin, ['show', '--no-ansi', '--all'], { cwd: manifestDir }).toString();
72
+ }
73
+ /**
74
+ * Parse poetry show --all output into a version map.
75
+ * Lines look like: "name (!) 1.2.3 Description text..."
76
+ * or: "name 1.2.3 Description text..."
77
+ * @param {string} output
78
+ * @returns {Map<string, string>} canonical name -> version
79
+ */
80
+ _parsePoetryShowAll(output) {
81
+ let versions = new Map();
82
+ let lines = output.split(/\r?\n/);
83
+ for (let line of lines) {
84
+ let trimmed = line.trim();
85
+ if (!trimmed) {
86
+ continue;
87
+ }
88
+ let match = trimmed.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(?:\(!\)\s+)?(\S+)/);
89
+ if (match) {
90
+ versions.set(this._canonicalize(match[1]), match[2]);
91
+ }
92
+ }
93
+ return versions;
94
+ }
95
+ /**
96
+ * Parse poetry show --tree output into a dependency graph structure.
97
+ * Top-level lines (no indentation/tree chars) are direct deps: "name version description"
98
+ * Indented lines are transitive deps with tree chars: "├── name >=constraint"
99
+ *
100
+ * @param {string} treeOutput
101
+ * @param {Map<string, string>} versionMap - canonical name -> resolved version
102
+ * @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
103
+ */
104
+ _parsePoetryTree(treeOutput, versionMap) {
105
+ let lines = treeOutput.split(/\r?\n/);
106
+ let graph = new Map();
107
+ let directDeps = [];
108
+ let stack = []; // [{key, depth}]
109
+ let currentDirectDep = null;
110
+ for (let line of lines) {
111
+ if (!line.trim()) {
112
+ continue;
113
+ }
114
+ // top-level line: "name version description..."
115
+ let topMatch = line.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s+(\S+)(?:\s|$)/);
116
+ if (topMatch) {
117
+ let name = topMatch[1];
118
+ let version = topMatch[2];
119
+ let key = this._canonicalize(name);
120
+ directDeps.push(key);
121
+ if (!graph.has(key)) {
122
+ graph.set(key, { name, version, children: [] });
123
+ }
124
+ currentDirectDep = key;
125
+ stack = [{ key, depth: -1 }];
126
+ continue;
127
+ }
128
+ if (!currentDirectDep) {
129
+ continue;
130
+ }
131
+ // indented line with tree chars (UTF-8 box-drawing: ├── └── │)
132
+ let nameStart = line.search(/[A-Za-z0-9]/);
133
+ if (nameStart < 0) {
134
+ continue;
135
+ }
136
+ let rest = line.substring(nameStart);
137
+ let depMatch = rest.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)/);
138
+ if (!depMatch) {
139
+ continue;
140
+ }
141
+ let depName = depMatch[1];
142
+ let depKey = this._canonicalize(depName);
143
+ // determine depth by counting tree-drawing groups in the prefix
144
+ let prefix = line.substring(0, nameStart);
145
+ let depth = (prefix.match(/(?:[├└│ ][\s─]{2} ?)/g) || []).length;
146
+ // resolve version from the version map
147
+ let version = versionMap.get(depKey) || null;
148
+ if (!version) {
149
+ throw new Error(`poetry: package '${depName}' has no resolved version`);
150
+ }
151
+ if (!graph.has(depKey)) {
152
+ graph.set(depKey, { name: depName, version, children: [] });
153
+ }
154
+ // pop stack back to find the parent at depth-1
155
+ while (stack.length > 0 && stack[stack.length - 1].depth >= depth) {
156
+ stack.pop();
157
+ }
158
+ if (stack.length > 0) {
159
+ let parentKey = stack[stack.length - 1].key;
160
+ let parentEntry = graph.get(parentKey);
161
+ if (parentEntry && !parentEntry.children.includes(depKey)) {
162
+ parentEntry.children.push(depKey);
163
+ }
164
+ }
165
+ stack.push({ key: depKey, depth });
166
+ }
167
+ return { directDeps, graph };
168
+ }
169
+ }
@@ -0,0 +1,27 @@
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
+ * @param {string} workspaceDir - workspace root (for resolving editable install paths)
16
+ * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
17
+ */
18
+ _parseUvExport(output: string, projectName: string, workspaceDir: string): Promise<{
19
+ directDeps: string[];
20
+ graph: Map<string, {
21
+ name: string;
22
+ version: string;
23
+ children: string[];
24
+ }>;
25
+ }>;
26
+ }
27
+ 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 { parse as parseToml } from 'smol-toml';
4
+ import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js';
5
+ import Base_pyproject from './base_pyproject.js';
6
+ import { getParser, getPinnedVersionQuery } from './requirements_parser.js';
7
+ export default class Python_uv extends Base_pyproject {
8
+ /** @returns {string} */
9
+ _lockFileName() {
10
+ return 'uv.lock';
11
+ }
12
+ /** @returns {string} */
13
+ _cmdName() {
14
+ return 'uv';
15
+ }
16
+ /**
17
+ * @param {string} manifestDir - directory containing the target pyproject.toml
18
+ * @param {string} workspaceDir - workspace root (for resolving editable install paths)
19
+ * @param {object} parsed - parsed pyproject.toml
20
+ * @param {Object} opts
21
+ * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
22
+ */
23
+ async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
24
+ let projectName = this._getProjectName(parsed);
25
+ let uvOutput = this._getUvExportOutput(manifestDir, opts);
26
+ return this._parseUvExport(uvOutput, projectName, workspaceDir);
27
+ }
28
+ /**
29
+ * Get the uv export output, either from env var or by running the command.
30
+ * @param {string} manifestDir
31
+ * @param {Object} opts
32
+ * @returns {string}
33
+ */
34
+ _getUvExportOutput(manifestDir, opts) {
35
+ if (environmentVariableIsPopulated('TRUSTIFY_DA_UV_EXPORT')) {
36
+ return Buffer.from(process.env['TRUSTIFY_DA_UV_EXPORT'], 'base64').toString('ascii');
37
+ }
38
+ let uvBin = getCustomPath('uv', opts);
39
+ return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes'], { cwd: manifestDir }).toString();
40
+ }
41
+ /**
42
+ * Parse uv export output into a dependency graph using tree-sitter-requirements
43
+ * for package/version extraction and string parsing for "# via" comments.
44
+ *
45
+ * @param {string} output
46
+ * @param {string} projectName - canonical project name to identify direct deps
47
+ * @param {string} workspaceDir - workspace root (for resolving editable install paths)
48
+ * @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
49
+ */
50
+ async _parseUvExport(output, projectName, workspaceDir) {
51
+ let [parser, pinnedVersionQuery] = await Promise.all([
52
+ getParser(), getPinnedVersionQuery()
53
+ ]);
54
+ let tree = parser.parse(output);
55
+ let root = tree.rootNode;
56
+ let canonProjectName = this._canonicalize(projectName);
57
+ let packages = new Map(); // canonical name -> {name, version, parents: Set}
58
+ let currentPkg = null;
59
+ let collectingVia = false;
60
+ for (let child of root.children) {
61
+ if (child.type === 'global_opt') {
62
+ let optNode = child.children.find(c => c.type === 'option');
63
+ let pathNode = child.children.find(c => c.type === 'path');
64
+ if (optNode?.text === '-e' && pathNode && workspaceDir) {
65
+ let memberDir = path.resolve(workspaceDir, pathNode.text);
66
+ let memberManifest = path.join(memberDir, 'pyproject.toml');
67
+ if (fs.existsSync(memberManifest)) {
68
+ let memberParsed = parseToml(fs.readFileSync(memberManifest, 'utf-8'));
69
+ let name = memberParsed.project?.name || memberParsed.tool?.poetry?.name;
70
+ let version = memberParsed.project?.version || memberParsed.tool?.poetry?.version;
71
+ if (name && version) {
72
+ let key = this._canonicalize(name);
73
+ currentPkg = { name, version, parents: new Set() };
74
+ packages.set(key, currentPkg);
75
+ collectingVia = false;
76
+ continue;
77
+ }
78
+ }
79
+ }
80
+ currentPkg = null;
81
+ collectingVia = false;
82
+ continue;
83
+ }
84
+ if (child.type === 'requirement') {
85
+ let nameNode = child.children.find(c => c.type === 'package');
86
+ if (!nameNode) {
87
+ continue;
88
+ }
89
+ let name = nameNode.text;
90
+ let version = null;
91
+ let versionMatches = pinnedVersionQuery.matches(child);
92
+ if (versionMatches.length > 0) {
93
+ version = versionMatches[0].captures.find(c => c.name === 'version').node.text;
94
+ }
95
+ if (!version) {
96
+ throw new Error(`uv export: package '${name}' has no pinned version`);
97
+ }
98
+ let key = this._canonicalize(name);
99
+ currentPkg = { name, version, parents: new Set() };
100
+ packages.set(key, currentPkg);
101
+ collectingVia = false;
102
+ continue;
103
+ }
104
+ if (child.type === 'comment' && currentPkg) {
105
+ let text = child.text.trim();
106
+ let viaSingle = text.match(/^# via ([A-Za-z0-9][A-Za-z0-9._-]*)$/);
107
+ if (viaSingle) {
108
+ currentPkg.parents.add(this._canonicalize(viaSingle[1]));
109
+ collectingVia = false;
110
+ continue;
111
+ }
112
+ if (text === '# via') {
113
+ collectingVia = true;
114
+ continue;
115
+ }
116
+ if (collectingVia) {
117
+ let parentMatch = text.match(/^#\s+([A-Za-z0-9][A-Za-z0-9._-]*)$/);
118
+ if (parentMatch) {
119
+ currentPkg.parents.add(this._canonicalize(parentMatch[1]));
120
+ continue;
121
+ }
122
+ collectingVia = false;
123
+ }
124
+ }
125
+ }
126
+ // Build forward dependency map and extract direct deps in one pass
127
+ let graph = new Map();
128
+ let directDeps = [];
129
+ for (let [key, pkg] of packages) {
130
+ graph.set(key, { name: pkg.name, version: pkg.version, children: [] });
131
+ }
132
+ for (let [childKey, pkg] of packages) {
133
+ for (let parentKey of pkg.parents) {
134
+ if (parentKey === canonProjectName) {
135
+ directDeps.push(childKey);
136
+ continue;
137
+ }
138
+ let parentEntry = graph.get(parentKey);
139
+ if (parentEntry) {
140
+ parentEntry.children.push(childKey);
141
+ }
142
+ }
143
+ }
144
+ return { directDeps, graph };
145
+ }
146
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trustify-da/trustify-da-javascript-client",
3
- "version": "0.3.0-ea.d71f957",
3
+ "version": "0.3.0-ea.d84439a",
4
4
  "description": "Code-Ready Dependency Analytics JavaScript API.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/guacsec/trustify-da-javascript-client#README.md",
@@ -38,7 +38,7 @@
38
38
  "lint": "eslint src test --ext js",
39
39
  "lint:fix": "eslint src test --ext js --fix",
40
40
  "test": "c8 npm run tests",
41
- "tests": "mocha --config .mocharc.json --grep \".*analysis module.*\" --invert",
41
+ "tests": "mocha --config .mocharc.json",
42
42
  "tests:rep": "mocha --reporter-option maxDiffSize=0 --reporter json > unit-tests-result.json",
43
43
  "pretest": "cp node_modules/tree-sitter-requirements/tree-sitter-requirements.wasm src/providers/tree-sitter-requirements.wasm && cp node_modules/tree-sitter-gomod/tree-sitter-gomod.wasm src/providers/tree-sitter-gomod.wasm",
44
44
  "precompile": "rm -rf dist",