forked from guacsec/trustify-da-javascript-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_uv.js
More file actions
179 lines (156 loc) · 5.75 KB
/
Copy pathpython_uv.js
File metadata and controls
179 lines (156 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import fs from 'node:fs'
import path from 'node:path'
import { parse as parseToml } from 'smol-toml'
import { environmentVariableIsPopulated, getCustomPath, invokeCommand } from '../tools.js'
import Base_pyproject from './base_pyproject.js'
import { evaluateMarker } from './marker_evaluator.js'
import { getParser, getPinnedVersionQuery } from './requirements_parser.js'
export default class Python_uv extends Base_pyproject {
/** @returns {string} */
_lockFileName() {
return 'uv.lock'
}
/** @returns {string} */
_cmdName() {
return 'uv'
}
/**
* @param {string} manifestDir - directory containing the target pyproject.toml
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
* @param {object} parsed - parsed pyproject.toml
* @param {Object} opts
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
*/
async _getDependencyData(manifestDir, workspaceDir, parsed, opts) {
let projectName = this._getProjectName(parsed)
let uvOutput = this._getUvExportOutput(manifestDir, opts)
return this._parseUvExport(uvOutput, projectName, workspaceDir)
}
/**
* Get the uv export output, either from env var or by running the command.
* @param {string} manifestDir
* @param {Object} opts
* @returns {string}
*/
_getUvExportOutput(manifestDir, opts) {
if (environmentVariableIsPopulated('TRUSTIFY_DA_UV_EXPORT')) {
return Buffer.from(process.env['TRUSTIFY_DA_UV_EXPORT'], 'base64').toString('ascii')
}
let uvBin = getCustomPath('uv', opts)
return invokeCommand(uvBin, ['export', '--format', 'requirements.txt', '--frozen', '--no-hashes', '--no-dev', '--no-emit-project'], { cwd: manifestDir }).toString()
}
/**
* Parse uv export output into a dependency graph using tree-sitter-requirements
* for package/version extraction and string parsing for "# via" comments.
*
* @param {string} output
* @param {string} projectName - canonical project name to identify direct deps
* @param {string} workspaceDir - workspace root (for resolving editable install paths)
* @returns {Promise<{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}>}
*/
async _parseUvExport(output, projectName, workspaceDir) {
let [parser, pinnedVersionQuery] = await Promise.all([
getParser(), getPinnedVersionQuery()
])
let tree = parser.parse(output)
let root = tree.rootNode
let canonProjectName = this._canonicalize(projectName)
let packages = new Map() // canonical name -> {name, version, parents: Set}
let currentPkg = null
let collectingVia = false
for (let child of root.children) {
if (child.type === 'global_opt') {
let optNode = child.children.find(c => c.type === 'option')
let pathNode = child.children.find(c => c.type === 'path')
if (optNode?.text === '-e' && pathNode && workspaceDir) {
let memberDir = path.resolve(workspaceDir, pathNode.text)
let memberManifest = path.join(memberDir, 'pyproject.toml')
if (fs.existsSync(memberManifest)) {
let memberParsed = parseToml(fs.readFileSync(memberManifest, 'utf-8'))
let name = memberParsed.project?.name || memberParsed.tool?.poetry?.name
let version = memberParsed.project?.version || memberParsed.tool?.poetry?.version
if (name && version) {
let key = this._canonicalize(name)
if (key === canonProjectName) { continue }
currentPkg = { name, version, parents: new Set() }
packages.set(key, currentPkg)
collectingVia = false
continue
}
}
}
currentPkg = null
collectingVia = false
continue
}
if (child.type === 'requirement') {
let nameNode = child.children.find(c => c.type === 'package')
if (!nameNode) { continue }
// Skip packages with non-matching PEP 508 markers, e.g. "pywin32==311 ; sys_platform == 'win32'"
let markerNode = child.children.find(c => c.type === 'marker_spec')
if (markerNode) {
let markerText = markerNode.text.replace(/^\s*;\s*/, '')
if (!evaluateMarker(markerText)) {
currentPkg = null
collectingVia = false
continue
}
}
let name = nameNode.text
let version = null
let versionMatches = pinnedVersionQuery.matches(child)
if (versionMatches.length > 0) {
version = versionMatches[0].captures.find(c => c.name === 'version').node.text
}
if (!version) {
throw new Error(`uv export: package '${name}' has no pinned version`)
}
let key = this._canonicalize(name)
currentPkg = { name, version, parents: new Set() }
packages.set(key, currentPkg)
collectingVia = false
continue
}
if (child.type === 'comment' && currentPkg) {
let text = child.text.trim()
let viaSingle = text.match(/^# via ([A-Za-z0-9][A-Za-z0-9._-]*)$/)
if (viaSingle) {
currentPkg.parents.add(this._canonicalize(viaSingle[1]))
collectingVia = false
continue
}
if (text === '# via') {
collectingVia = true
continue
}
if (collectingVia) {
let parentMatch = text.match(/^#\s+([A-Za-z0-9][A-Za-z0-9._-]*)$/)
if (parentMatch) {
currentPkg.parents.add(this._canonicalize(parentMatch[1]))
continue
}
collectingVia = false
}
}
}
// Build forward dependency map and extract direct deps in one pass
let graph = new Map()
let directDeps = []
for (let [key, pkg] of packages) {
graph.set(key, { name: pkg.name, version: pkg.version, children: [] })
}
for (let [childKey, pkg] of packages) {
for (let parentKey of pkg.parents) {
if (parentKey === canonProjectName) {
directDeps.push(childKey)
continue
}
let parentEntry = graph.get(parentKey)
if (parentEntry) {
parentEntry.children.push(childKey)
}
}
}
return { directDeps, graph }
}
}