Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Define the path to your JSON file
- $jsonFilePath = 'setuptools.json'
- # Read the JSON content from the file
- $jsonContent = Get-Content $jsonFilePath | Out-String | ConvertFrom-Json
- # The Python version to match or find a slightly lower version for
- $targetPythonVersion = [version]"3.1.*"
- # Function to check if a version satisfies the requirement
- function Test-VersionCompatibility {
- param (
- [version]$requiredVersion,
- [string]$requiresPython
- )
- # Split the requirement string into versions, assuming format is ">=2.6,!=3.0.*,!=3.1.*,!=3.2.*"
- $versionConditions = $requiresPython -split ','
- foreach ($condition in $versionConditions) {
- if ($condition -match '>=?(\d+\.\d+)(\.(\d+))?') {
- $minVersion = [version]$Matches[1]
- if ($requiredVersion -lt $minVersion) {
- return $false
- }
- }
- elseif ($condition -match '<=?(\d+\.\d+)(\.(\d+))?') {
- $maxVersion = [version]$Matches[1]
- if ($requiredVersion -gt $maxVersion) {
- return $false
- }
- }
- elseif ($condition -match '!=?(\d+\.\d+)(\.(\d+))?') {
- $notVersion = [version]$Matches[1]
- if ($requiredVersion -eq $notVersion) {
- return $false
- }
- }
- }
- return $true
- }
- # Iterate through releases in reverse chronological order
- $latestCompatibleRelease = $null
- foreach ($release in ($jsonContent.releases.PSObject.Properties | Sort-Object Name -Descending)) {
- foreach ($package in $release.Value) {
- if ($null -ne $package.requires_python -and (Test-VersionCompatibility -requiredVersion $targetPythonVersion -requiresPython $package.requires_python)) {
- $latestCompatibleRelease = $release
- break
- }
- }
- if ($latestCompatibleRelease) {
- break
- }
- }
- # Output the found release
- if ($latestCompatibleRelease) {
- $latestCompatibleRelease.Name
- $latestCompatibleRelease.Value | ConvertTo-Json
- }
- else {
- Write-Output "No compatible release found for Python $targetPythonVersion"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement