Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

windows_bootstrap

dirk edited this page Jun 20, 2018 · 11 revisions

Bootstrap a Fresh Windows Box for Python

$SaveDir = "C:\Temp"
$PythonUrl = "https://www.python.org/ftp/python/3.6.5/python-3.6.5-amd64.exe"
$GitUrl = "https://github.com/git-for-windows/git/releases/download/v2.17.1.windows.2/Git-2.17.1.2-64-bit.exe"
$Repo = "pyppyn"

# Init
[Net.ServicePointManager]::SecurityProtocol = "Ssl3, Tls, Tls11, Tls12"
New-Item -ItemType "directory" -Path $SaveDir -ErrorAction Continue

# functions
function Install-Exe {
  Param( [String]$Installer, [String[]]$InstallerArgs )
  Write-Verbose "Installing $Installer"
  $ret = Start-Process "${Installer}" -ArgumentList ${InstallerArgs} -NoNewWindow -PassThru -Wait
}

function Download-File {
  Param( [string]$Url, [string]$SavePath )
  # Download a file, if it doesn't already exist.
  if( !(Test-Path ${SavePath} -PathType Leaf) ) {
    [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::SystemDefault
    $SecurityProtocolTypes = @([Net.SecurityProtocolType].GetEnumNames())
    if ("Tls11" -in $SecurityProtocolTypes) {
        [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls11
    }
    if ("Tls12" -in $SecurityProtocolTypes) {
        [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
    }

    (New-Object System.Net.WebClient).DownloadFile(${Url}, ${SavePath})
    Write-Verbose "Downloaded ${Url} to ${SavePath}"
  }
}

function Reset-EnvironmentVariables {
  foreach( $Level in "Machine", "User" ) {
    [Environment]::GetEnvironmentVariables(${Level}).GetEnumerator() | % {
      # For Path variables, append the new values, if they're not already in there.
      if($_.Name -match 'Path$') {
        $_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select -unique) -join ';'
      }
      $_
    } | Set-Content -Path { "Env:$($_.Name)" }
  }
}

function Install-Python {
  $PythonFile = "${SaveDir}\$(${PythonUrl}.split("/")[-1])"

  Download-File -Url ${PythonUrl} -SavePath ${PythonFile}

  if ($PythonFile -match "^.*exe$") {
    $Arguments = @()
    $Arguments += "/quiet"
    $Arguments += "InstallAllUsers=1"
    $Arguments += "PrependPath=1"
    Install-Exe -Installer ${PythonFile} -InstallerArgs ${Arguments}
  }

  Write-Verbose "Installed Python"
}

function Install-Git {
  $GitFile = "${SaveDir}\$(${GitUrl}.split("/")[-1])"

  Download-File -Url ${GitUrl} -SavePath ${GitFile}

  $Arguments = @()
  $Arguments += "/SILENT"
  $Arguments += "/NOCANCEL"
  $Arguments += "/NORESTART"
  $Arguments += "/SAVEINF=${SaveDir}\git_params.txt"
  Install-Exe -Installer ${GitFile} -InstallerArgs ${Arguments}

  Write-Verbose "Installed Git"
}

# Main

# Install Python
Write-Verbose "Python will be installed from ${PythonUrl}"
Install-Python

# Download and install git
Write-Verbose "Git will be installed from ${GitUrl}"
Install-Git

Reset-EnvironmentVariables
Write-Verbose "Reset the PATH environment for this shell"

Write-Verbose "Complete!"


python -m pip install --upgrade pip setuptools virtualenv
git clone -q --branch=dev https://github.com/YakDriver/${Repo}.git C:\projects\${Repo}

$venv = "C:\venv"
mkdir $venv
virtualenv $venv
cd $venv\Scripts
.\activate

pip install pytest

cd C:\projects\${Repo}
pip install --editable .
Clone this wiki locally