#!/usr/bin/env python3
#
# Install Python modules in the install_python_packages.txt requirements file.

import sys
import subprocess
import distutils.spawn


def emulate_run(args, env=None):
    p = subprocess.Popen(args, env=env)
    if p.wait() == 0:
        return True
    return False


def install_python_packages():
    python = distutils.spawn.find_executable('python3')
    if not python:
        python = distutils.spawn.find_executable('python')

    if not python:
        print('error: Python 3 not installed, not installing Python 3 packages.')
        return False

    if not emulate_run([python, '-m', 'pip', 'install', '--upgrade', 'pip']):
        return False

    return emulate_run([python, '-m', 'pip', 'install', '--user', '--upgrade', '-r', 'install_python_packages.txt'])


if __name__ == '__main__':
    ok = install_python_packages()

    sys.exit(1 if not ok else 0)
