|
| 1 | +import os |
| 2 | +import requests |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | + |
| 6 | +if __name__ == '__main__': # noqa |
| 7 | + dest = '.' |
| 8 | + instdest = None |
| 9 | + force = False |
| 10 | + help = False |
| 11 | + is64 = False |
| 12 | + keep = False |
| 13 | + minpoint = 0 |
| 14 | + remove = False |
| 15 | + version = None |
| 16 | + for arg in sys.argv[1:]: |
| 17 | + if arg.startswith('-'): |
| 18 | + if arg in ('-f', '--force'): |
| 19 | + force = True |
| 20 | + elif arg in ('-k', '--keep'): |
| 21 | + keep = True |
| 22 | + elif arg.startswith('--inst='): |
| 23 | + instdest = arg.split('=', 1)[1] |
| 24 | + elif arg.startswith('--min='): |
| 25 | + minpoint = int(arg.split('=', 1)[1]) |
| 26 | + elif arg.startswith('--out='): |
| 27 | + dest = arg.split('=', 1)[1] |
| 28 | + elif arg in ('-r', '--remove'): |
| 29 | + remove = True |
| 30 | + elif arg == '--32': |
| 31 | + is64 = False |
| 32 | + elif arg == '--64': |
| 33 | + is64 = True |
| 34 | + else: |
| 35 | + help = True |
| 36 | + elif not version: |
| 37 | + version = arg |
| 38 | + else: |
| 39 | + help = True |
| 40 | + if help or not version: |
| 41 | + print("""Install the latest revision of a version of python. |
| 42 | +
|
| 43 | +Syntax: install_python.py (version) [--32|--64] [--out=(destination directory)] |
| 44 | + [--inst=(installation directory)] [--force] |
| 45 | + [--min=(minimum point release)] [--keep] [--remove] |
| 46 | +
|
| 47 | +Specify just the major and minor version, such as 2.7 or 3.6. |
| 48 | +--force will overwrite an existing installation file. |
| 49 | +--keep keeps the install file. |
| 50 | +--remove first uninstalls the specified version of python.""") |
| 51 | + sys.exit(0) |
| 52 | + baseurl = 'https://www.python.org/ftp/python/' |
| 53 | + versionList = requests.get(baseurl).text |
| 54 | + versionList = [part.split('"')[0] for part in versionList.split('<a href="')[1:]] |
| 55 | + versionList = sorted([(int(ver[len(version) + 1:].strip('/')), ver) |
| 56 | + for ver in versionList if ver.startswith(version + '.')], |
| 57 | + reverse=True) |
| 58 | + for subver, ver in versionList: |
| 59 | + if subver < minpoint: |
| 60 | + continue |
| 61 | + url = baseurl + ver + 'python-' + version + '.' + str(subver) + ( |
| 62 | + '' if not is64 else ('.amd64' if version.startswith('2') else '-amd64')) + ( |
| 63 | + '.msi' if version.startswith('2') else '.exe') |
| 64 | + try: |
| 65 | + response = requests.get(url).content |
| 66 | + if len(response) < 1000000: |
| 67 | + continue |
| 68 | + break |
| 69 | + except Exception: |
| 70 | + continue |
| 71 | + if len(response) < 1000000: |
| 72 | + raise Exception('Failed to find install file') |
| 73 | + print('Size: %d, url: %s' % (len(response), url)) |
| 74 | + if instdest is None: |
| 75 | + instdest = os.path.split(dest)[0] |
| 76 | + if not os.path.exists(instdest): |
| 77 | + os.makedirs(instdest) |
| 78 | + installer = os.path.join(instdest, url.rsplit('/', 1)[-1]) |
| 79 | + if os.path.exists(installer) and not force: |
| 80 | + raise Exception('Path already exists: %s' % installer) |
| 81 | + open(installer, 'wb').write(response) |
| 82 | + if installer.endswith('.msi'): |
| 83 | + uncmd = ['msiexec', '/uninstall', installer, '/quiet', '/norestart'] |
| 84 | + cmd = ['msiexec', '/i', installer, '/quiet', '/norestart'] |
| 85 | + altcmd = ['msiexec', '/fa', installer, '/quiet', '/norestart'] |
| 86 | + else: |
| 87 | + uncmd = [installer, '/quiet', '/uninstall'] |
| 88 | + cmd = [installer, '/quiet'] |
| 89 | + altcmd = [installer, '/quiet', '/repair'] |
| 90 | + cmdopts = [ |
| 91 | + 'TargetDir=' + dest, 'AssociateFiles=0', 'CompileAll=0', |
| 92 | + 'PrependPath=0', 'Shortcuts=0', 'Include_doc=0', 'Include_dev=0', |
| 93 | + 'Include_debug=0', 'Include_exe=1', 'Include_launcher=0', |
| 94 | + 'InstallLauncherAllUsers=0', 'Include_lib=1', 'Include_symbols=0', |
| 95 | + 'Include_tcltk=0', 'Include_test=0', 'Include_tools=0', |
| 96 | + 'SimpleInstall=1'] |
| 97 | + if remove: |
| 98 | + print('Remove: ' + ' '.join(uncmd)) |
| 99 | + try: |
| 100 | + subprocess.check_call(uncmd) |
| 101 | + except Exception: |
| 102 | + pass |
| 103 | + print('Install: ' + ' '.join(cmd + cmdopts)) |
| 104 | + try: |
| 105 | + subprocess.check_call(cmd + cmdopts) |
| 106 | + except Exception: |
| 107 | + pass |
| 108 | + if not os.path.exists(os.path.join(dest, 'python.exe')): |
| 109 | + print('Update ' + ' '.join(altcmd + cmdopts)) |
| 110 | + subprocess.check_call(altcmd + cmdopts) |
| 111 | + print('Done') |
| 112 | + if not keep: |
| 113 | + os.unlink(installer) |
0 commit comments