#! /usr/bin/env python

import sys, os, subprocess, struct, re

pyver2dist = {
    "py24": ("bdist_wininst",),
    "py25": ("bdist_wininst", "bdist_msi"),
    "py26": ("bdist_wininst", "bdist_msi"),
    "py27": ("bdist_wininst", "bdist_msi", "bdist_wheel"),
    "py27-x64": ("bdist_wininst", "bdist_msi", "bdist_wheel"),
    "py33": ("bdist_wininst", "bdist_msi", "bdist_wheel"),
    "py33-x64": ("bdist_wininst", "bdist_msi", "bdist_wheel")}


def system(cmd):
    sys.stdout.write("====> Running %s\n" % cmd)

    popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
    nl = True
    while 1:
        char = popen.stdout.read(1)
        if not char:
            break
        if nl:
            sys.stdout.write("    ")
        sys.stdout.write(char)
        sys.stdout.flush()
        nl = char == "\n"

    st = popen.wait()
    if st != 0:
        sys.exit("Error: command %r failed" % cmd)
    sys.stdout.write("\n")


def main():
    here = os.path.dirname(os.path.dirname(sys.executable))
    for pyver, dists in sorted(pyver2dist.items()):
        exe = os.path.join(here, pyver, "python.exe")
        for d in dists:
            cmd = "%s setup.py -q %s" % (exe, d)
            system(cmd)

if __name__ == "__main__":
    main()
