-
Notifications
You must be signed in to change notification settings - Fork 11
fix(python): filter packages by PEP 508 environment markers #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6fb728c
feat(python): filter PEP 508 environment markers in uv and poetry pro…
ruromero 74fe05b
fix(python): use PEP 508 string containment for in/not in operators
ruromero 31416b3
fix(python): reverse directional operators in PEP 508 reversed marker…
ruromero 11917a6
fix(python): differentiate python_version (X.Y) from python_full_vers…
ruromero bb1682e
fix: use invokeCommand for Python version detection
ruromero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| current = '' | ||
| i += sep.length | ||
| continue | ||
| } | ||
| current += ch | ||
| i++ | ||
| } | ||
| parts.push(current) | ||
| return parts.filter(p => p.trim()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.