.pcode_python/.pcode/helpers/build_env.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
[CmdletBinding()]
Param(
    # Specify the specific Python Version to use
    [Parameter(Mandatory=$False,Position=1)]
    [string]$desiredVersion
)


# Returns Python Cores that are present in the Windows Registry
function Get-PythonFromRegistry {
    $install_paths = @(
        "hklm:\software\python\pythoncore\",
        "hkcu:\software\python\pythoncore\",
        "hklm:\software\wow6432node\python\pythoncore\")
    $python_cores = @()
    foreach ($path in $install_paths) {
        if (test-path -path $path) {
            foreach ($core in Get-ChildItem -path $path) {
                $exe_regpath = Join-Path $core.PSPath -ChildPath "InstallPath"
                if (test-path -path $exe_regpath) {
                    $py_core = Get-ItemProperty -Path $exe_regpath
                    if ($py_core.PSObject.properties.name -contains "ExecutablePath") {
                        if (Test-Path -path $py_core.ExecutablePath) {
                            # Python 3 provides ExecutablePath value that points to Python exe
                            $python_cores += $py_core.ExecutablePath
                        }
                    } else {
                        if (Test-Path -path (Join-Path $py_core."(default)" -ChildPath "python.exe")) {
                            # Python 2 default value contains the Path that the Python exe resides
                            $py_exe_path = Join-Path $py_core."(default)" -ChildPath "python.exe"
                            $python_cores += $py_exe_path
                        }
                    }
                }
            }
        }
    }
    return $python_cores
}

# Returns Python Cores that are Windows AppX Packages
function Get-PythonFromAppx {
    $python_cores = @()
    foreach ($python_appx in (Get-AppxPackage | Where-Object -Property "Name" -Like "*Python*")) {
        # Local AppData for Windows Appx should contain valid links
        $py_folder = [IO.Path]::Combine(
            $env:LOCALAPPDATA, "Microsoft", "WindowsApps", $python_appx.PackageFamilyName)
        if (Test-Path -path (Join-Path $py_folder -ChildPath "python.exe")) {
            $py_exe_path = Join-Path $py_folder -ChildPath "python.exe"
            $python_cores += $py_exe_path
        }
    }
    return $python_cores
}

# TODO: Add one more to just go through the paths var and look for python.exe

# Runs python.exe with info.py script to extract information
function Get-PythonInfo($python_paths) {
    $py_info = @()
    foreach ($python in $python_paths) {
        try {
            $info = & $python (Join-Path (Split-Path -Parent $PSCommandPath) -ChildPath info.py) | ConvertFrom-Json
            $info | Add-Member NoteProperty "FullPath" $python
            $py_info += $info
        } catch {

        }
    }
    return $py_info
}

function Get-PythonInstalls() {
    Param(
        # Specify the specific Python Version to use
        [Parameter(Mandatory=$False,Position=1)]
        [string]$desiredVersion
    )

    $python_cores = @()
    $python_cores += Get-PythonFromRegistry
    $python_cores += Get-PythonFromAppx
    $py_installs = @(PythonInfo($python_cores) | Sort-Object -Property versionInfo)

    if ($desiredVersion) {
        $py_installs = @($py_installs | Where-Object { $_.versionInfo[0..2] -Join "." -Match $desiredVersion})
    }

    if ($py_installs.Count -lt 1) {
        if ($desiredVersion) {
            throw "Could not find a Python Installation matching version {0}" -f $desiredVersion
        } else {
            throw "Could not find a Python Installation!"
        }
        exit 1
    }

    return $py_installs
}

function Get-PythonUsersChoice() {
    Param(
        # Specify the specific Python Version to use
        [Parameter(Mandatory=$False,Position=1)]
        [string]$desiredVersion
    )

    [array]$py_installs = Get-PythonInstalls $desiredVersion

    if ($py_installs.count -gt 1) {
        $python = (dispMenu($py_installs))
    } else {
        $python = $py_installs[0]
    }

    return $python
}

function dispMenu {
    param($py_installs)
    Write-Output "========== Choose Python Version to use ==========" | Out-Host
    $entry = @()
    foreach ($install in $py_installs) {
        $entry += $install
        $is64 = If ($install.is64Bit -eq "True") {"64-bit"} else {"32-bit"}
        Write-Output (
            "$($entry.count): Python {0}.{1}.{2} {3} {4}" -f  $($install.versionInfo + $is64)) | Out-Host
    }
    while ($True) {
        $selection = Read-Host "Make a selection"
        if ($selection -match "\d+") {
            $selection = [int]$selection - 1
            if ($selection -lt $py_installs.Count -and $selection -ge 0) {
                return $entry[$selection]
            }
        }
    }
}

function main {
    $python = Get-PythonUsersChoice $desiredVersion
    # Start building the env
    Write-Output "Building Virtual Environment..."
    if (Test-Path env:PWD) {
        Set-Location $env:PWD
    } else {
        if ((Split-Path (Get-Location) -Leaf) -eq ".pcode") {
            # Stepping out of .pcode dir
        Set-Location ..
        }
    }
    try {
        if ($python.versionInfo[0] -eq "2") {
            # Python 2.7 virtualenv
            & $python.FullPath -m virtualenv venv --no-download
        } else {
            # Python 3 comes with venv, but desire to use virtualenv if available
            & $python.FullPath -m virtualenv venv --no-download
            if (-not $?) {
                Write-Output "Using Built-in venv instead..."
                & $python.FullPath -m venv venv
            }
        }

        # Run the activation script
        & .\venv\Scripts\activate.ps1
    } catch {
        throw
        Write-Output "Failed to Create the Virtual Environment!"
        exit 1
    }
    # Install requirements
    if (Test-Path "requirements.txt") {
        Write-Output "Installing PIP packages listed in requirements"
        & python -m pip install -r requirements.txt
    }
    Write-Output "Virtual Env up"
}

if ($MyInvocation.InvocationName -eq "&") {
    main
}