#!/usr/bin/env python
# Install into Python.org python
# vim ft:python
import sys
import os
from os.path import exists, dirname
from subprocess import check_call

# Find disk image files
package_path = os.environ.get('PACKAGE_PATH')
if package_path is None:
    sys.exit(10)
package_dir = dirname(package_path)
wheelhouse = package_dir + '/{{ info.wheel_sdir }}'
# Find Python.org Python
python_bin = '{{ info.py_org_base }}/{{ info.pyv_m_m }}/bin'
python_path = python_bin + '/python{{ info.pyv_m_m }}'
if not exists(python_path):
    sys.exit(20)
# Install pip
check_call([python_path,
            wheelhouse + '/get-pip.py',
            '-f', wheelhouse,
            '--no-index'])
# Find pip
expected_pip = python_bin + '/pip{{ info.pyv_m_m }}'
if not exists(expected_pip):
    sys.exit(30)
check_call([expected_pip, 'install',
            '--no-index', '--upgrade',
            '--find-links', wheelhouse,
            '-r', wheelhouse + '/{{ info.pkg_name_version }}.txt'])
