Functions/ConfigGettersAndSetters.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
. "$PSScriptRoot\Get-InstalledPythonVersions.ps1"
. "$PSScriptRoot\Uninstall-Python.ps1"

$script:registryConfigPath = "HKLM:\SOFTWARE\PythonPowershellUtilities"

function Get-PythonUtilitiesConfigValue([string]$Key){
$Null = @(
    # Add registry keys if they haven't been added already, otherwise just return them
    if (!(Test-Path $script:registryConfigPath)){
        New-Item -Path $script:registryConfigPath
        Set-PythonUtilitiesConfigValue -Key "PythonInstallRoot" -Value "C:\PythonInstallations"
        Set-PythonUtilitiesConfigValue -Key "VirtualEnvironmentRoot" -Value "C:\PythonVirtualEnvironments"
    }
    $entry = Get-ItemProperty $script:registryConfigPath -Name $Key
)
    return $entry.$Key
}

function Set-PythonUtilitiesConfigValue([string]$Key, [string]$Value){
$Null = @(
    Set-ItemProperty -Path $script:registryConfigPath -Name $Key -Value $Value > $null
)
}

function Get-PythonInstallerCache(){
$Null = @(
    $installRoot = Get-PythonInstallRoot
    $installerCache = "$installRoot\Installers"
    if (!(Test-Path $installerCache)){
        New-Item -ItemType Directory -Path $installerCache -Force > $null
    }
)
    return $installerCache
}

function Get-PythonInstallRoot(){
    return Get-PythonUtilitiesConfigValue -Key "PythonInstallRoot"
}

function Set-PythonInstallRoot([string]$Path, [switch]$Force=$false){
$Null = @(
    $installRoot = Get-PythonInstallRoot
    $installedVersions = Get-InstalledPythonVersions
    if ($installedVersions.Count -gt 0){
        if (!$Force){
            $response = Read-Host "WARNING: One or more versions of python have already been installed into the current PythonInstallRoot located at $installRoot. Changing the root will uninstall those versions and orphan any virtual environments created from those versions. To continue and uninstall the current python versions enter 'yes'. To cancel, enter 'no' or press enter/return"
            if (!($response -eq "yes")){
                return
            }
        }
        
        foreach ($version in $(Get-InstalledPythonVersions)){
            Uninstall-Python -Version $version
        }
        
        $installerCache = Get-PythonInstallerCache
        if (Test-Path -Path $installerCache){
            # Delete the installer cache and the installation directory if it's empty
            Remove-Item -Recurse $installerCache
            $installerDirInfo = Get-ChildItem $installRoot | Measure-Object
            if ($installerDirInfo.Count -eq 0){
                Remove-Item -Recurse $installRoot
            }
        }
    }
    New-Item -ItemType directory -Path "$Path\Installers" -Force
    Set-PythonUtilitiesConfigValue -Key "PythonInstallRoot" -Value $Path
)
}

function Get-VirtualEnvironmentRoot(){
    return Get-PythonUtilitiesConfigValue -Key "VirtualEnvironmentRoot"
}

function Set-VirtualEnvironmentRoot([string]$Path, [switch]$Force){
$Null = @(
    if (!$Force){
        $venvRootPath = Get-VirtualEnvironmentRoot
        if (Test-Path $venvRootPath) {
            $venvRootInfo = Get-ChildItem $venvRootPath | Measure-Object
            if ($venvRootInfo.Count -gt 0){
                $response = Read-Host "WARNING: One or more virtual environments have already been created in the current VirtualEnvironmentRoot, located at $venvRootPath. Changing the root will leave the environments in place, but they will not be accessible through calls to `Enter-PythonVirtualEnvironment`. To continue enter 'yes'. To cancel, enter 'no' or press enter/return"
                if (!($response -eq "yes")){
                    return
                }
            }
        }
    }
    Set-PythonUtilitiesConfigValue -Key "VirtualEnvironmentRoot" -Value $Path
)
}