-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathuninstall-python-package.ps1
52 lines (42 loc) · 2.14 KB
/
uninstall-python-package.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Usage: uninstall-python-package <name> [options]
# Summary: Uninstalls a Python package installed using install-python-package
# Help: For example, to uninstall a Python package installed globally using install-python-package:
# uninstall-python-package --global --dir keyring
#
# Options:
# -h, --help Show this help message
# -g, --global Uninstall a global package
# -d, --dir <dir> The setup directory
# -m, --match <pattern> Pattern that specifies which files to remove
$scoop_lib = "$(Split-Path (Split-Path (scoop which scoop)))\lib"
. "$scoop_lib\getopt.ps1"
. "$scoop_lib\help.ps1"
$opt, $name, $err = getopt $args 'hgd:r:b:c:m:' 'help', 'global', 'dir=', 'match='
if ($err) {
Write-Host "$err`n$(my_usage)`nuninstall-python-package --help for for more information" -ForegroundColor Red
exit 1
}
if ($opt.h -or $opt.help) {
# scoop_help removes the '#' character at the start of each line along with the whitespace
# character that follows, but if that whitespace character is a newline because it's just
# an empty line, then that empty line is completely removed.
# To combat that, we add a space to all lines that are just a '#'.
$content = (Get-Content $PSCommandPath -Raw) -replace '(?ms)^#$', '# '
$usage = usage $content
$help = scoop_help $content
return "$usage`n`n$help"
}
$global = $opt.g -or $opt.global
if ($opt.d) { $directory = $opt.d }
elseif ($opt.dir) { $directory = $opt.dir }
else { $directory = '.' }
$name = $name.TrimEnd('-py')
if ($opt.m) { $match = $opt.m }
elseif ($opt.match) { $match = $opt.match }
else { $match = ".*$name.*" }
(Get-Content -ErrorAction Ignore "$directory\installed_files.txt") -match $match | Remove-Item -ErrorAction Ignore -Force
if (Get-Command -ErrorAction Ignore python) {
if ($global) { $site_packages = "$(Split-Path (scoop which python))\lib\site-packages" }
else { $site_packages = python -m site --user-site }
Get-ChildItem -ErrorAction Ignore "$site_packages\$name-*-py*.egg" | ForEach-Object { Remove-Item -ErrorAction Ignore -Force -Recurse $_ }
}