Advertisement
foadsf

parseJSONpython

Mar 21st, 2024
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Define the path to your JSON file
  2. $jsonFilePath = 'setuptools.json'
  3.  
  4. # Read the JSON content from the file
  5. $jsonContent = Get-Content $jsonFilePath | Out-String | ConvertFrom-Json
  6.  
  7. # The Python version to match or find a slightly lower version for
  8. $targetPythonVersion = [version]"3.1.*"
  9.  
  10. # Function to check if a version satisfies the requirement
  11. function Test-VersionCompatibility {
  12.     param (
  13.         [version]$requiredVersion,
  14.         [string]$requiresPython
  15.     )
  16.  
  17.     # Split the requirement string into versions, assuming format is ">=2.6,!=3.0.*,!=3.1.*,!=3.2.*"
  18.     $versionConditions = $requiresPython -split ','
  19.  
  20.     foreach ($condition in $versionConditions) {
  21.         if ($condition -match '>=?(\d+\.\d+)(\.(\d+))?') {
  22.             $minVersion = [version]$Matches[1]
  23.             if ($requiredVersion -lt $minVersion) {
  24.                 return $false
  25.             }
  26.         }
  27.         elseif ($condition -match '<=?(\d+\.\d+)(\.(\d+))?') {
  28.             $maxVersion = [version]$Matches[1]
  29.             if ($requiredVersion -gt $maxVersion) {
  30.                 return $false
  31.             }
  32.         }
  33.         elseif ($condition -match '!=?(\d+\.\d+)(\.(\d+))?') {
  34.             $notVersion = [version]$Matches[1]
  35.             if ($requiredVersion -eq $notVersion) {
  36.                 return $false
  37.             }
  38.         }
  39.     }
  40.  
  41.     return $true
  42. }
  43.  
  44. # Iterate through releases in reverse chronological order
  45. $latestCompatibleRelease = $null
  46. foreach ($release in ($jsonContent.releases.PSObject.Properties | Sort-Object Name -Descending)) {
  47.     foreach ($package in $release.Value) {
  48.         if ($null -ne $package.requires_python -and (Test-VersionCompatibility -requiredVersion $targetPythonVersion -requiresPython $package.requires_python)) {
  49.             $latestCompatibleRelease = $release
  50.             break
  51.         }
  52.     }
  53.     if ($latestCompatibleRelease) {
  54.         break
  55.     }
  56. }
  57.  
  58. # Output the found release
  59. if ($latestCompatibleRelease) {
  60.     $latestCompatibleRelease.Name
  61.     $latestCompatibleRelease.Value | ConvertTo-Json
  62. }
  63. else {
  64.     Write-Output "No compatible release found for Python $targetPythonVersion"
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement