| 1 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | AppVeyor will at least have few Pythons around so there's no point of implementing a bootstrapper in PowerShell. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | This is a port of https://github.com/pypa/python-packaging-user-guide/blob/master/source/code/install.ps1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | with various fixes and improvements that just weren't feasible to implement in PowerShell. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from __future__ import print_function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | from os import environ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | from os.path import exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from subprocess import check_call | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     from urllib.request import urlretrieve | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | except ImportError: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     from urllib import urlretrieve | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | BASE_URL = "https://www.python.org/ftp/python/" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | GET_PIP_PATH = "C:\get-pip.py" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | URLS = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     ("2.7", "64"): BASE_URL + "2.7.13/python-2.7.13.amd64.msi", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     ("2.7", "32"): BASE_URL + "2.7.13/python-2.7.13.msi", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     ("3.4", "64"): BASE_URL + "3.4.4/python-3.4.4.amd64.msi", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     ("3.4", "32"): BASE_URL + "3.4.4/python-3.4.4.msi", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     ("3.5", "64"): BASE_URL + "3.5.4/python-3.5.4-amd64.exe", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     ("3.5", "32"): BASE_URL + "3.5.4/python-3.5.4.exe", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     ("3.6", "64"): BASE_URL + "3.6.2/python-3.6.2-amd64.exe", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     ("3.6", "32"): BASE_URL + "3.6.2/python-3.6.2.exe", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | INSTALL_CMD = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     # Commands are allowed to fail only if they are not the last command.  Eg: uninstall (/x) allowed to fail. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     "2.7": [["msiexec.exe", "/L*+!", "install.log", "/qn", "/x", "{path}"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ["msiexec.exe", "/L*+!", "install.log", "/qn", "/i", "{path}", "TARGETDIR={home}"]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     "3.4": [["msiexec.exe", "/L*+!", "install.log", "/qn", "/x", "{path}"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ["msiexec.exe", "/L*+!", "install.log", "/qn", "/i", "{path}", "TARGETDIR={home}"]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     "3.5": [["{path}", "/quiet", "TargetDir={home}"]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     "3.6": [["{path}", "/quiet", "TargetDir={home}"]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | def download_file(url, path): | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     print("Downloading: {} (into {})".format(url, path)) | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     progress = [0, 0] | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     def report(count, size, total): | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         progress[0] = count * size | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         if progress[0] - progress[1] > 1000000: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             progress[1] = progress[0] | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             print("Downloaded {:,}/{:,} ...".format(progress[1], total)) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     dest, _ = urlretrieve(url, path, reporthook=report) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     return dest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | def install_python(version, arch, home): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     print("Installing Python", version, "for", arch, "bit architecture to", home) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     if exists(home): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     path = download_python(version, arch) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     print("Installing", path, "to", home) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     success = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     for cmd in INSTALL_CMD[version]: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         cmd = [part.format(home=home, path=path) for part in cmd] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         print("Running:", " ".join(cmd)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             check_call(cmd) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         except Exception as exc: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             print("Failed command", cmd, "with:", exc) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             if exists("install.log"): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 with open("install.log") as fh: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     print(fh.read()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             success = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     if success: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         print("Installation complete!") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         print("Installation failed") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | def download_python(version, arch): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     for _ in range(3): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return download_file(URLS[version, arch], "installer.exe") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         except Exception as exc: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             print("Failed to download:", exc) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         print("Retrying ...") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | def install_pip(home): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     pip_path = home + "/Scripts/pip.exe" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     python_path = home + "/python.exe" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     if exists(pip_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         print("pip already installed.") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         print("Installing pip...") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         download_file(GET_PIP_URL, GET_PIP_PATH) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         print("Executing:", python_path, GET_PIP_PATH) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         check_call([python_path, GET_PIP_PATH]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | def upgrade_pip(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         check_call(['python', '-m', 'pip', 'install', '--upgrade', 'pip']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     except Exception as e: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         print("Failed to upgade pip: {}".format(e)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | def install_packages(home, *packages): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     cmd = [home + "/Scripts/pip.exe", "install"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     cmd.extend(packages) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     check_call(cmd) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | if __name__ == "__main__": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     install_python(environ['PYTHON_VERSION'], environ['PYTHON_ARCH'], environ['PYTHON_HOME']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     install_pip(environ['PYTHON_HOME']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     upgrade_pip() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  |     install_packages(environ['PYTHON_HOME'], "setuptools>=18.0.1", "wheel", "tox", "virtualenv>=14.0.0") | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |  |