|
| 1 | +# Usage: migrate-python-packages [apps] [options] |
| 2 | +# Summary: Migrate packages installed from the python bucket to newer versions of Python |
| 3 | +# Help: For example, to migrate grip-py and httpie-py globally: |
| 4 | +# migrate-python-packages grip-py httpie-py --global |
| 5 | +# |
| 6 | +# To migrate all packages installed locally from the python bucket: |
| 7 | +# migrate-python-packages |
| 8 | +# |
| 9 | +# Packages installed globally from the python bucket will need to be migrated for any Python update. |
| 10 | +# Locally installed packages will need to be migrated for any minor or major Python update. |
| 11 | +# |
| 12 | +# Options: |
| 13 | +# -h, --help Show this help message |
| 14 | +# -g, --global Migrate globally installed packages |
| 15 | +# -q, --quiet Do not write package names to console |
| 16 | +# -v, --verbose Display all output |
| 17 | + |
| 18 | +$scoop_lib = "$(Split-Path (Split-Path (scoop which scoop)))\lib" |
| 19 | +. "$scoop_lib\buckets.ps1" |
| 20 | +. "$scoop_lib\getopt.ps1" |
| 21 | +. "$scoop_lib\help.ps1" |
| 22 | + |
| 23 | +$opt, $apps, $err = getopt $args 'hgqv' 'help', 'global', 'quiet', 'verbose' |
| 24 | + |
| 25 | +if ($err) { |
| 26 | + Write-Host "$err`n$(my_usage)`nmigrate-python-packages --help for for more information" -ForegroundColor Red |
| 27 | + exit 1 |
| 28 | +} |
| 29 | + |
| 30 | +if ($opt.h -or $opt.help) { |
| 31 | + $content = Get-Content $PSCommandPath -Raw |
| 32 | + $usage = usage $content |
| 33 | + $help = scoop_help $content |
| 34 | + return "$usage`n`n$help" |
| 35 | +} |
| 36 | + |
| 37 | +$global = $opt.g -or $opt.global |
| 38 | +$quiet = $opt.q -or $opt.quiet |
| 39 | +$verbose = $opt.v -or $opt.verbose |
| 40 | + |
| 41 | +function migrate_global($app, $directory) { |
| 42 | + if (-not $quiet) { Write-Host "Migrating $app (global)" -ForegroundColor Cyan } |
| 43 | + |
| 44 | + if (-not (Test-Path "$directory\setup.py")) { Push-Location "$directory\$app" } |
| 45 | + else { Push-Location $directory } |
| 46 | + |
| 47 | + if (Test-Path 'bootstrap.py') { python bootstrap.py } |
| 48 | + if ($verbose) { python setup.py install --record "$directory\installed_files.txt" } |
| 49 | + else { python setup.py --quiet install --record "$directory\installed_files.txt" } |
| 50 | + Pop-Location |
| 51 | +} |
| 52 | + |
| 53 | +function migrate_global_if_necessary($app, $directory, $lib) { |
| 54 | + $installed_version = Split-Path (Split-Path $lib) -Leaf |
| 55 | + $current_version = (python --version) -replace 'Python ', '' |
| 56 | + if ($installed_version -ne $current_version) { migrate_global $app $directory } |
| 57 | + elseif (-not $quiet) { Write-Host "$app (global) does not need to be migrated" -ForegroundColor Magenta } |
| 58 | +} |
| 59 | + |
| 60 | +function migrate_local($app, $directory) { |
| 61 | + if (-not $quiet) { Write-Host "Migrating $app" -ForegroundColor Cyan } |
| 62 | + |
| 63 | + if (-not (Test-Path "$directory\setup.py")) { Push-Location "$directory\$app" } |
| 64 | + else { Push-Location $directory } |
| 65 | + |
| 66 | + if (Test-Path 'bootstrap.py') { python bootstrap.py } |
| 67 | + if ($verbose) { python setup.py install --user --record "$directory\installed_files.txt" } |
| 68 | + else { python setup.py --quiet install --user --record "$directory\installed_files.txt" } |
| 69 | + Pop-Location |
| 70 | +} |
| 71 | + |
| 72 | +function migrate_local_if_necessary($app, $directory, $lib) { |
| 73 | + if ($lib -ne (Split-Path (python -m site --user-site))) { migrate_local $app $directory } |
| 74 | + elseif (-not $quiet) { Write-Host "$app does not need to be migrated" -ForegroundColor Magenta } |
| 75 | +} |
| 76 | + |
| 77 | +function migrate_if_necessary($app, $directory) { |
| 78 | + $site_packages = Get-Content "$directory\installed_files.txt" -First 1 |
| 79 | + |
| 80 | + if ($site_packages -and (Test-Path $site_packages)) { |
| 81 | + $lib = $site_packages -replace '\\site-packages.*', '' |
| 82 | + if ($global) { migrate_global_if_necessary $app $directory $lib } |
| 83 | + else { migrate_local_if_necessary $app $directory $lib } |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + if ($global) { migrate_global $app $directory } |
| 88 | + else { migrate_local $app $directory } |
| 89 | +} |
| 90 | + |
| 91 | +if (-not $apps) { $apps = apps_in_bucket (Find-BucketDirectory python) } |
| 92 | + |
| 93 | +foreach ($app in $apps) { |
| 94 | + $directory = appdir $app $true |
| 95 | + if ((Test-Path "$directory\current\setup.py") -or (Test-Path "$directory\current\$app\setup.py")) { migrate_if_necessary $app "$directory\current" } |
| 96 | +} |
0 commit comments