Skip to content

Commit 888a1f3

Browse files
committed
Add migrate-python-packages
1 parent a581b71 commit 888a1f3

File tree

7 files changed

+119
-16
lines changed

7 files changed

+119
-16
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!-- markdownlint-disable MD010 -->
22
<!-- markdownlint-disable MD014 -->
3+
<!-- markdownlint-disable MD046 -->
34

45
# Scoop-Python
56

@@ -10,11 +11,18 @@ standalone executables.
1011

1112
This bucket is an alternative to using pip to install these packages.
1213

13-
Note that for the purpose of avoiding conflicts, uninstalling a package in this bucket will not
14-
uninstall its dependencies.
14+
## Drawbacks
1515

16-
For local installs, binaries may not be added to PATH due to
16+
* For the purpose of avoiding conflicts, uninstalling a package in this bucket will not
17+
uninstall its dependencies.
18+
* For local installs, binaries may not be added to PATH due to
1719
[ScoopInstaller/Main#772](https://github.com/ScoopInstaller/Main/issues/772).
20+
* Packages installed globally from this bucket will need to be migrated for any Python update.
21+
* Packages installed locally from this bucket will need to be migrated for any minor or major
22+
Python update.
23+
* A script to migrate packages in this bucket between Python versions is provided in the `scripts`
24+
directory of this bucket:
1825

19-
Global installs will be reset after Python is updated due to
20-
[lukesampson/scoop#2180](https://github.com/lukesampson/scoop/issues/2180).
26+
```powershell
27+
$ & "$(Split-Path (Split-Path (Get-Command scoop).Source))\buckets\python\scripts\migrate-python-packages.ps1" --help
28+
```

bucket/keyring-py.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
],
1010
"url": "https://raw.githubusercontent.com/TheRandomLabs/Scoop-Python/master/placeholder.txt",
1111
"hash": "8f5572e508c2cb95197e550f54c6bc03cfd4668ef3c9f1011ec3950037cb4b11",
12-
"extract_dir": "keyring-21.2.1",
1312
"installer": {
1413
"script": [
15-
"git clone --quiet -c advice.detachedHead=false --branch \"v$version\" https://github.com/jaraco/keyring.git \"$dir\\keyring\"",
16-
"Push-Location \"$dir\\keyring\"",
14+
"git clone --quiet -c advice.detachedHead=false --branch \"v$version\" https://github.com/jaraco/keyring.git \"$dir\\$app\"",
15+
"Push-Location \"$dir\\$app\"",
1716
"if ($global) { python setup.py --quiet install --record \"$dir\\installed_files.txt\" }",
1817
"else { python setup.py --quiet install --user --record \"$dir\\installed_files.txt\" }",
1918
"Pop-Location"

bucket/pytest-py.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"hash": "8f5572e508c2cb95197e550f54c6bc03cfd4668ef3c9f1011ec3950037cb4b11",
1212
"installer": {
1313
"script": [
14-
"git clone --quiet -c advice.detachedHead=false --branch $version https://github.com/pytest-dev/pytest.git \"$dir\\pytest\"",
15-
"Push-Location \"$dir\\pytest\"",
14+
"git clone --quiet -c advice.detachedHead=false --branch $version https://github.com/pytest-dev/pytest.git \"$dir\\$app\"",
15+
"Push-Location \"$dir\\$app\"",
1616
"if ($global) { python setup.py --quiet install --record \"$dir\\installed_files.txt\" }",
1717
"else { python setup.py --quiet install --user --record \"$dir\\installed_files.txt\" }",
1818
"Pop-Location"

bucket/setuptools-py.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
"url": "https://github.com/pypa/setuptools/archive/v$version.zip",
3333
"extract_dir": "setuptools-$version"
3434
}
35-
}
35+
}

bucket/twine-py.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"hash": "8f5572e508c2cb95197e550f54c6bc03cfd4668ef3c9f1011ec3950037cb4b11",
1212
"installer": {
1313
"script": [
14-
"git clone --quiet -c advice.detachedHead=false --branch $version https://github.com/pypa/twine.git \"$dir\\twine\"",
15-
"Push-Location \"$dir\\twine\"",
14+
"git clone --quiet -c advice.detachedHead=false --branch $version https://github.com/pypa/twine.git \"$dir\\$app\"",
15+
"Push-Location \"$dir\\$app\"",
1616
"if ($global) { python setup.py --quiet install --record \"$dir\\installed_files.txt\" }",
1717
"else { python setup.py --quiet install --user --record \"$dir\\installed_files.txt\" }",
1818
"Pop-Location"

bucket/virtualenv-py.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"hash": "8f5572e508c2cb95197e550f54c6bc03cfd4668ef3c9f1011ec3950037cb4b11",
1212
"installer": {
1313
"script": [
14-
"git clone --quiet -c advice.detachedHead=false --branch $version https://github.com/pypa/virtualenv.git \"$dir\\virtualenv\"",
15-
"Push-Location \"$dir\\virtualenv\"",
14+
"git clone --quiet -c advice.detachedHead=false --branch $version https://github.com/pypa/virtualenv.git \"$dir\\$app\"",
15+
"Push-Location \"$dir\\$app\"",
1616
"if ($global) { python setup.py --quiet install --record \"$dir\\installed_files.txt\" }",
1717
"else { python setup.py --quiet install --user --record \"$dir\\installed_files.txt\" }",
1818
"Pop-Location"
@@ -33,4 +33,4 @@
3333
"autoupdate": {
3434
"url": "https://raw.githubusercontent.com/TheRandomLabs/Scoop-Python/master/placeholder.txt"
3535
}
36-
}
36+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)