Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions src/providers/marker_evaluator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// PEP 508 environment marker evaluator.
// Filters Python dependencies by platform/version markers so that e.g.
// "pywin32 ; sys_platform == 'win32'" is excluded on Linux/macOS.
// See https://peps.python.org/pep-0508/#environment-markers

import os from 'node:os'

import { getCustomPath, invokeCommand } from '../tools.js'

let cachedPythonVersions = undefined

function getPythonVersions() {
if (cachedPythonVersions !== undefined) { return cachedPythonVersions }
try {
let python = getCustomPath('python3')
let out = invokeCommand(python, ['-c', "import sys; v=sys.version_info; print(f'{v.major}.{v.minor} {v.major}.{v.minor}.{v.micro}')"],
{ timeout: 5000 }).toString().trim()
let [short, full] = out.split(' ')
cachedPythonVersions = { short, full }
} catch {
cachedPythonVersions = null
}
return cachedPythonVersions
}

/**
* Maps Node.js/OS values to PEP 508 marker variables.
* Example: on Linux, sys_platform='linux', platform_system='Linux', os_name='posix'
* @returns {Record<string, string>}
*/
export function getEnvironmentMarkers() {
let platform = process.platform
let systemMap = { win32: 'Windows', linux: 'Linux', darwin: 'Darwin' }
let machine = typeof os.machine === 'function' ? os.machine() : process.arch
let pyVer = getPythonVersions()
return {
sys_platform: platform,
platform_system: systemMap[platform] || platform,
os_name: platform === 'win32' ? 'nt' : 'posix',
platform_machine: machine,
platform_release: os.release(),
platform_version: os.version?.() || '',
python_version: pyVer?.short || '',
python_full_version: pyVer?.full || '',
implementation_name: 'cpython',
}
}

function compareVersions(left, right) {
let lParts = left.split('.').map(Number)
let rParts = right.split('.').map(Number)
let len = Math.max(lParts.length, rParts.length)
for (let i = 0; i < len; i++) {
let l = lParts[i] || 0
let r = rParts[i] || 0
if (l < r) { return -1 }
if (l > r) { return 1 }
}
return 0
}

// Evaluates a single comparison like sys_platform == 'win32' or python_version >= '3.8'.
// Version-bearing variables (python_version, python_full_version) use numeric comparison;
// all others use string equality. Returns false when the env value is missing.
function evaluateComparison(variable, op, value, env) {
let envVal = env[variable]
if (envVal === undefined || envVal === '') {
return false
}

let isVersion = variable.includes('version')
if (isVersion) {
let cmp = compareVersions(envVal, value)
switch (op) {
case '==': return cmp === 0
case '!=': return cmp !== 0
case '>=': return cmp >= 0
case '<=': return cmp <= 0
case '>': return cmp > 0
case '<': return cmp < 0
case '~=': {
let parts = value.split('.')
parts.pop()
let prefix = parts.join('.')
return envVal.startsWith(prefix) && cmp >= 0
}
default: return true
}
}

switch (op) {
case '==': return envVal === value
case '!=': return envVal !== value
case 'in': return value.includes(envVal)
case 'not in': return !value.includes(envVal)
default: return envVal === value
}
}

// Parses a single marker comparison into {variable, op, value}.
// Handles both normal and reversed forms:
// "sys_platform == 'linux'" → { variable: 'sys_platform', op: '==', value: 'linux' }
// "'linux' == sys_platform" → { variable: 'sys_platform', op: '==', value: 'linux' }
function parseAtom(expr) {
// Normal form: variable op 'value'
let m = expr.match(/^\s*([\w.]+)\s*(~=|!=|==|>=|<=|>|<|not\s+in|in)\s*["']([^"']*)["']\s*$/)
if (m) { return { variable: m[1], op: m[2].replace(/\s+/g, ' '), value: m[3] } }

// Reversed form: 'value' op variable — reverse directional operators
let mReverse = expr.match(/^\s*["']([^"']*)['"]\s*(~=|!=|==|>=|<=|>|<|not\s+in|in)\s*([\w.]+)\s*$/)
if (mReverse) {
let reverseOp = { '<': '>', '>': '<', '<=': '>=', '>=': '<=' }
let op = mReverse[2].replace(/\s+/g, ' ')
return { variable: mReverse[3], op: reverseOp[op] || op, value: mReverse[1] }
}

return null
}

/**
* Evaluates a full PEP 508 marker expression against the current platform.
* Example: "sys_platform == 'win32' and python_version >= '3.8'" → false on Linux
* Empty/missing markers return true (unconditional dependency).
* @param {string} markerExpr
* @returns {boolean}
*/
export function evaluateMarker(markerExpr) {
if (!markerExpr || !markerExpr.trim()) { return true }
let env = getEnvironmentMarkers()
return evaluateExpr(markerExpr.trim(), env)
}

function evaluateExpr(expr, env) {
let orParts = splitLogical(expr, ' or ')
if (orParts.length > 1) {
return orParts.some(part => evaluateExpr(part, env))
}

let andParts = splitLogical(expr, ' and ')
if (andParts.length > 1) {
return andParts.every(part => evaluateExpr(part, env))
}

let trimmed = expr.trim()
if (trimmed.startsWith('(') && trimmed.endsWith(')')) {
return evaluateExpr(trimmed.slice(1, -1), env)
}

let atom = parseAtom(trimmed)
if (!atom) { return true }

return evaluateComparison(atom.variable, atom.op, atom.value, env)
}

// Splits an expression by " and " or " or " at the top level, skipping
// separators inside parentheses or quoted strings.
// Example: splitLogical("a == 'x' and (b == 'y' or c == 'z')", " and ")
// → ["a == 'x'", "(b == 'y' or c == 'z')"]
function splitLogical(expr, sep) {
let parts = []
let depth = 0
let current = ''
let i = 0
let quoteChar = null
while (i < expr.length) {
let ch = expr[i]
if (quoteChar) {
if (ch === quoteChar) { quoteChar = null }
current += ch
i++
continue
}
if (ch === '"' || ch === "'") {
quoteChar = ch
current += ch
i++
continue
}
if (ch === '(') { depth++ }
if (ch === ')') { depth-- }
if (depth === 0 && expr.substring(i, i + sep.length) === sep) {
parts.push(current)
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
current = ''
i += sep.length
continue
}
current += ch
i++
}
parts.push(current)
return parts.filter(p => p.trim())
}
93 changes: 82 additions & 11 deletions src/providers/python_poetry.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import fs from 'node:fs'
import path from 'node:path'

import { parse as parseToml } from 'smol-toml'

import { environmentVariableIsPopulated, getCustom, getCustomPath, invokeCommand } from '../tools.js'

import Base_pyproject from './base_pyproject.js'
import { evaluateMarker } from './marker_evaluator.js'

export default class Python_poetry extends Base_pyproject {

Expand Down Expand Up @@ -50,7 +53,9 @@ export default class Python_poetry extends Base_pyproject {
let treeOutput = this._getPoetryShowTreeOutput(manifestDir, hasDevGroup, opts)
let showAllOutput = this._getPoetryShowAllOutput(manifestDir, opts)
let versionMap = this._parsePoetryShowAll(showAllOutput)
return this._parsePoetryTree(treeOutput, versionMap)
let lockDir = this._findLockFileDir(manifestDir, opts)
let markerData = this._extractMarkerData(lockDir, parsed)
return this._parsePoetryTree(treeOutput, versionMap, markerData)
}

/**
Expand Down Expand Up @@ -107,16 +112,64 @@ export default class Python_poetry extends Base_pyproject {
return versions
}

/**
* Collects PEP 508 marker expressions for direct and transitive deps.
* Direct markers come from pyproject.toml dependency strings, e.g.:
* "pywin32>=311 ; sys_platform == 'win32'" → directMarkers['pywin32'] = "sys_platform == 'win32'"
* Transitive markers come from poetry.lock [package.dependencies] entries, e.g.:
* colorama = {version = "*", markers = "sys_platform == 'win32'"}
* → transitiveMarkers['click']['colorama'] = "sys_platform == 'win32'"
* @param {string|null} lockDir
* @param {object} parsed - parsed pyproject.toml
* @returns {{directMarkers: Map<string, string>, transitiveMarkers: Map<string, Map<string, string>>}}
*/
_extractMarkerData(lockDir, parsed) {
let directMarkers = new Map()
let transitiveMarkers = new Map()

// Extract markers from PEP 621 dependency strings: "name[extras]>=ver ; marker"
let deps = parsed.project?.dependencies || []
for (let dep of deps) {
let m = dep.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)\s*[^;]*;\s*(.+)$/)
Comment thread
ruromero marked this conversation as resolved.
if (m) {
directMarkers.set(this._canonicalize(m[1]), m[2].trim())
}
}

if (lockDir) {
let lockPath = path.join(lockDir, this._lockFileName())
if (fs.existsSync(lockPath)) {
let lockContent = fs.readFileSync(lockPath, 'utf-8')
let lock = parseToml(lockContent)
let packages = lock.package || []
for (let pkg of packages) {
let pkgKey = this._canonicalize(pkg.name)
let pkgDeps = pkg.dependencies || {}
for (let [depName, depSpec] of Object.entries(pkgDeps)) {
let markers = typeof depSpec === 'object' && depSpec != null ? depSpec.markers : null
if (markers) {
if (!transitiveMarkers.has(pkgKey)) {
transitiveMarkers.set(pkgKey, new Map())
}
transitiveMarkers.get(pkgKey).set(this._canonicalize(depName), markers)
}
}
}
}
}

return { directMarkers, transitiveMarkers }
}

/**
* Parse poetry show --tree output into a dependency graph structure.
* Top-level lines (no indentation/tree chars) are direct deps: "name version description"
* Indented lines are transitive deps with tree chars: "├── name >=constraint"
*
* @param {string} treeOutput
* @param {Map<string, string>} versionMap - canonical name -> resolved version
* @param {{directMarkers: Map<string, string>, transitiveMarkers: Map<string, Map<string, string>>}} markerData
* @returns {{directDeps: string[], graph: Map<string, {name: string, version: string, children: string[]}>}}
*/
_parsePoetryTree(treeOutput, versionMap) {
_parsePoetryTree(treeOutput, versionMap, markerData) {
let lines = treeOutput.split(/\r?\n/)
let graph = new Map()
let directDeps = []
Expand All @@ -133,6 +186,14 @@ export default class Python_poetry extends Base_pyproject {
let name = topMatch[1]
let version = topMatch[2]
let key = this._canonicalize(name)

let marker = markerData.directMarkers.get(key)
if (marker && !evaluateMarker(marker)) {
currentDirectDep = null
stack = []
continue
}

directDeps.push(key)
if (!graph.has(key)) {
graph.set(key, { name, version, children: [] })
Expand All @@ -159,6 +220,22 @@ export default class Python_poetry extends Base_pyproject {
let prefix = line.substring(0, nameStart)
let depth = (prefix.match(/(?:[├└│ ][\s─]{2} ?)/g) || []).length

// pop stack back to find the parent at depth-1
while (stack.length > 0 && stack[stack.length - 1].depth >= depth) {
stack.pop()
}

let parentKey = stack.length > 0 ? stack[stack.length - 1].key : null
if (parentKey) {
let parentMarkers = markerData.transitiveMarkers.get(parentKey)
if (parentMarkers) {
let marker = parentMarkers.get(depKey)
if (marker && !evaluateMarker(marker)) {
continue
}
}
}

// resolve version from the version map
let version = versionMap.get(depKey) || null
if (!version) {
Expand All @@ -169,13 +246,7 @@ export default class Python_poetry extends Base_pyproject {
graph.set(depKey, { name: depName, version, children: [] })
}

// pop stack back to find the parent at depth-1
while (stack.length > 0 && stack[stack.length - 1].depth >= depth) {
stack.pop()
}

if (stack.length > 0) {
let parentKey = stack[stack.length - 1].key
if (parentKey) {
let parentEntry = graph.get(parentKey)
if (parentEntry && !parentEntry.children.includes(depKey)) {
parentEntry.children.push(depKey)
Expand Down
12 changes: 12 additions & 0 deletions src/providers/python_uv.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 {
Expand Down Expand Up @@ -98,6 +99,17 @@ export default class Python_uv extends Base_pyproject {
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)
Expand Down
Loading
Loading