File: python-install.py

package info (click to toggle)
oscrypto 1.3.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,164 kB
  • sloc: python: 22,115; makefile: 7
file content (77 lines) | stat: -rw-r--r-- 2,054 bytes parent folder | download | duplicates (2)
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
# coding: utf-8
from __future__ import unicode_literals, division, absolute_import, print_function

import os
import shutil
import subprocess
import sys
from urllib.parse import urlparse
from urllib.request import urlopen


run_args = [
    {
        'name': 'version',
        'kwarg': 'version',
    },
    {
        'name': 'arch',
        'kwarg': 'arch',
    },
]


def run(version=None, arch=None):
    """
    Installs a version of Python on Windows

    :return:
        A bool - if Python was installed successfully
    """

    if sys.platform != 'win32':
        raise ValueError('python-install is only designed for Windows')

    if version not in set(['2.6', '3.3']):
        raise ValueError('Invalid version: %r' % version)

    if arch not in set(['x86', 'x64']):
        raise ValueError('Invalid arch: %r' % arch)

    if version == '2.6':
        if arch == 'x64':
            url = 'https://www.python.org/ftp/python/2.6.6/python-2.6.6.amd64.msi'
        else:
            url = 'https://www.python.org/ftp/python/2.6.6/python-2.6.6.msi'
    else:
        if arch == 'x64':
            url = 'https://www.python.org/ftp/python/3.3.5/python-3.3.5.amd64.msi'
        else:
            url = 'https://www.python.org/ftp/python/3.3.5/python-3.3.5.msi'

    home = os.environ.get('USERPROFILE')
    msi_filename = os.path.basename(urlparse(url).path)
    msi_path = os.path.join(home, msi_filename)
    install_path = os.path.join(os.environ.get('LOCALAPPDATA'), 'Python%s-%s' % (version, arch))

    if os.path.exists(os.path.join(install_path, 'python.exe')):
        print(install_path)
        return True

    try:
        with urlopen(url) as r, open(msi_path, 'wb') as f:
            shutil.copyfileobj(r, f)

        proc = subprocess.Popen(
            'msiexec /passive /a %s TARGETDIR=%s' % (msi_filename, install_path),
            shell=True,
            cwd=home
        )
        proc.communicate()

    finally:
        if os.path.exists(msi_path):
            os.unlink(msi_path)

    print(install_path)
    return True