#!/usr/bin/env python3
import os
import subprocess
import sys

# Clean out this directory, as it otherwise may
# accumuluate multiple versions of the same library
# and pip will complain.
subprocess.run(['rm', '-rf', '.env/jsii-calc'], check=True)

venv = os.path.join(os.getcwd(), '.env')

subprocess.run(
    [
        "jsii-pacmak",
        "-t",
        "python",
        "-o",
        ".env/jsii-calc",
        "--recurse",
        "../../jsii-calc",
    ],
    check=True,
    env={
        **os.environ,
        # Make sure we run within the virtual environment provisioned here...
        'PATH': "%s:%s" % (os.path.join(venv, 'bin'), os.environ['PATH']),
        'VIRTUAL_ENV': venv,
    },
)
subprocess.run(
    [
        sys.executable,
        "-m",
        "pip",
        "install",
        "--upgrade",
    ]
    +
    [x for x in os.listdir(".") if x.endswith(".whl")]
    +
    [os.path.join('.env/jsii-calc/python', x) for x in os.listdir(".env/jsii-calc/python") if x.endswith(".whl")],
    check=True,
)
