|
| 1 | +#! /usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright (c) 2015-2016, Exa Analytics Development Team |
| 4 | +# Distributed under the terms of the Apache License 2.0 |
| 5 | +""" |
| 6 | +Build/Publish |
| 7 | +################# |
| 8 | +""" |
| 9 | +import os |
| 10 | +import shutil |
| 11 | +import argparse |
| 12 | +import subprocess |
| 13 | + |
| 14 | + |
| 15 | +builds = ["win-32", "win-64", "osx-64", "linux-32", "linux-64"] |
| 16 | + |
| 17 | + |
| 18 | +def get_args(): |
| 19 | + """ |
| 20 | + Description of the deployment script. |
| 21 | + """ |
| 22 | + parser = argparse.ArgumentParser(description="Deployment script for Exa Analytics") |
| 23 | + parser.add_argument("pkg", nargs="?") |
| 24 | + parser.add_argument("version", nargs="?") |
| 25 | + parser.add_argument("which_pypi", nargs="?", default="pypi") |
| 26 | + return parser.parse_args() |
| 27 | + |
| 28 | + |
| 29 | +def get_package_name(): |
| 30 | + """ |
| 31 | + Automatically determine the package name. |
| 32 | + """ |
| 33 | + for item in os.listdir("."): |
| 34 | + if item.startswith("ex") and "." not in item: |
| 35 | + return item |
| 36 | + raise Exception("Package name unknown") |
| 37 | + |
| 38 | + |
| 39 | +def get_package_version(pkg): |
| 40 | + """ |
| 41 | + Determine the package version from the primary __init__.py file. |
| 42 | + """ |
| 43 | + search = "__{}_version__".format(pkg) |
| 44 | + versions = [] |
| 45 | + with open(os.sep.join((pkg, "__init__.py"))) as f: |
| 46 | + for line in f: |
| 47 | + if search in line: |
| 48 | + v = line.split('=')[-1] |
| 49 | + v = v.replace("(", "").replace(")", "").replace(",", " ") |
| 50 | + versions.append(tuple((int(i) for i in v.split()))) |
| 51 | + break |
| 52 | + with open("meta.yaml") as f: |
| 53 | + for line in f: |
| 54 | + if any(x in line for x in ["version:", "git_rev:"]): |
| 55 | + v = line.split()[-1].replace('"', '').replace('v', '').split('.') |
| 56 | + versions.append(tuple(int(i) for i in v)) |
| 57 | + if not all(version == versions[0] for version in versions): |
| 58 | + raise Exception("Version mismatch between init {} and meta.yaml {}!".format(versions[0], versions[1])) |
| 59 | + else: |
| 60 | + return versions[0] |
| 61 | + |
| 62 | + |
| 63 | +def get_python_versions(): |
| 64 | + """ |
| 65 | + Get versions of python to build for. |
| 66 | + """ |
| 67 | + versions = [] |
| 68 | + read = False |
| 69 | + with open(".travis.yml") as f: |
| 70 | + for line in f: |
| 71 | + if "python:" in line: |
| 72 | + read = True |
| 73 | + elif "-" not in line and read: |
| 74 | + read = False |
| 75 | + elif read: |
| 76 | + versions.append(line.split()[-1].replace('"', '')) |
| 77 | + return versions |
| 78 | + |
| 79 | + |
| 80 | +def check(pkg, version, pys): |
| 81 | + """ |
| 82 | + Human check for publication. |
| 83 | + """ |
| 84 | + v = ".".join([str(i) for i in version]) |
| 85 | + py = ", ".join([py.replace("'", "") for py in pys]) |
| 86 | + print("Before deploying, make sure '{}' version {} has been tagged for release.".format(pkg, v)) |
| 87 | + inp = "Deploy '{}' {} for python {} on all platforms (y/N): ".format(pkg, v, py) |
| 88 | + chk = input(inp) |
| 89 | + if chk.lower() == "y": |
| 90 | + return True |
| 91 | + return False |
| 92 | + |
| 93 | + |
| 94 | +def conda_build(pys): |
| 95 | + """ |
| 96 | + Deploy to anaconda. |
| 97 | + """ |
| 98 | + build_fmt = "conda build --python={py} ." |
| 99 | + conv_fmt = "conda convert {} -p all" |
| 100 | + conup_fmt = "anaconda upload {}/{}" |
| 101 | + for py in pys: |
| 102 | + string = build_fmt.format(py=py) |
| 103 | + build = subprocess.run([string], shell=True, check=True, stdout=subprocess.PIPE) |
| 104 | + if build.returncode != 0: |
| 105 | + raise Exception("Build failed with command {}".format(string)) |
| 106 | + for line in build.stdout.decode("utf-8").split('\n'): |
| 107 | + if "anaconda upload" in line: |
| 108 | + path = line.split()[-1] |
| 109 | + string = conv_fmt.format(path) |
| 110 | + conv = subprocess.run([string], shell=True, check=True, stdout=subprocess.PIPE) |
| 111 | + if conv.returncode != 0: |
| 112 | + raise Exception("Convert failed with command {}".format(string)) |
| 113 | + name = path.split(os.sep)[-1] |
| 114 | + for pltfrm in builds: |
| 115 | + string = conup_fmt.format(pltfrm, name) |
| 116 | + upload = subprocess.run([string], shell=True, check=True, |
| 117 | + stdout=subprocess.PIPE, stdin=subprocess.PIPE) |
| 118 | + if upload.returncode != 0: |
| 119 | + raise Exception("Upload failed with command {}".format(string)) |
| 120 | + |
| 121 | + |
| 122 | +def pypi_build(pypi): |
| 123 | + """ |
| 124 | + Deploy to pypi/pypitest. |
| 125 | + """ |
| 126 | + string = "python setup.py sdist upload {}".format(pypi) |
| 127 | + ret = subprocess.run([string], shell=True, check=True, |
| 128 | + stdout=subprocess.PIPE, stdin=subprocess.PIPE) |
| 129 | + |
| 130 | + |
| 131 | +def cleanup(): |
| 132 | + """ |
| 133 | + Cleanup the build directories. |
| 134 | + """ |
| 135 | + for build in builds: |
| 136 | + if os.path.isdir(build): |
| 137 | + shutil.rmtree(build) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + args = get_args() |
| 142 | + if args.pkg is None: |
| 143 | + args.pkg = get_package_name() |
| 144 | + if args.version is None: |
| 145 | + args.version = get_package_version(args.pkg) |
| 146 | + pys = get_python_versions() |
| 147 | + chk = check(args.pkg, args.version, pys) |
| 148 | + if chk: |
| 149 | + conda_build(pys) |
| 150 | + pypi_build(args.which_pypi) |
| 151 | + cleanup() |
0 commit comments