@@ -220,29 +220,57 @@ function Get-AllPackageInfoFromRepo ($serviceDirectory)
220220}
221221
222222# Returns the pypi publish status of a package id and version.
223+ # Uses pip download against the configured package index (PIP_INDEX_URL) rather than
224+ # calling pypi.org directly, so this works in network-isolated (CFS) environments.
225+ # The Azure Artifacts feed has upstream to PyPI, so packages on PyPI are still found.
223226function IsPythonPackageVersionPublished ($pkgId , $pkgVersion )
224227{
228+ $tmpDir = Join-Path ([System.IO.Path ]::GetTempPath()) " pkg-verify-$ ( [System.Guid ]::NewGuid()) "
229+ New-Item - ItemType Directory - Force - Path $tmpDir | Out-Null
230+
225231 try
226232 {
227- $existingVersion = (Invoke-RestMethod - MaximumRetryCount 3 - RetryIntervalSec 10 - Method " Get" - uri " https://pypi.org/pypi/$pkgId /$pkgVersion /json" ).info.version
228- # if existingVersion exists, then it's already been published
229- return $True
230- }
231- catch
232- {
233- $statusCode = $_.Exception.Response.StatusCode.value__
234- $statusDescription = $_.Exception.Response.StatusDescription
233+ Write-Host " Checking whether $pkgId ==$pkgVersion is already published (using pip download)"
234+
235+ $pipArgs = @ (" download" , " --no-deps" , " --no-cache-dir" , " --dest" , $tmpDir , " $pkgId ==$pkgVersion " )
236+
237+ if ($env: PIP_INDEX_URL ) {
238+ Write-Host " Using index from PIP_INDEX_URL"
239+ }
240+ else {
241+ Write-Host " PIP_INDEX_URL is not set; pip will fall back to public PyPI."
242+ }
243+
244+ $pipOutput = pip $pipArgs 2>&1
245+ $pipExitCode = $LASTEXITCODE
246+ # Reset $LASTEXITCODE so a non-zero pip exit code doesn't leak out and cause
247+ # the PowerShell ADO task to report failure after the script finishes.
248+ $global :LASTEXITCODE = 0
235249
236- # if this is 404ing, then this pkg has never been published before
237- if ($statusCode -eq 404 )
250+ if ($pipExitCode -eq 0 )
238251 {
252+ Write-Host " Package $pkgId ==$pkgVersion was found on the package index."
253+ return $True
254+ }
255+
256+ $outputStr = $pipOutput -join " `n "
257+
258+ # pip reports "No matching distribution found" when the version doesn't exist.
259+ if ($outputStr -match " No matching distribution found" -or $outputStr -match " Could not find a version that satisfies" )
260+ {
261+ Write-Host " Package $pkgId ==$pkgVersion was not found on the package index (not yet published)."
239262 return $False
240263 }
241- Write-Host " PyPI Invocation failed:"
242- Write-Host " StatusCode:" $statusCode
243- Write-Host " StatusDescription:" $statusDescription
264+
265+ # Any other failure is unexpected — fail hard to avoid accidentally re-publishing.
266+ Write-Host " Package version check failed unexpectedly:"
267+ Write-Host $outputStr
244268 exit (1 )
245269 }
270+ finally
271+ {
272+ Remove-Item $tmpDir - Recurse - Force - ErrorAction SilentlyContinue
273+ }
246274}
247275
248276# Parse out package publishing information given a python sdist of tar.gz format.
0 commit comments