|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions |
| 6 | +# are met: |
| 7 | +# * Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# * Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# * Neither the name of NVIDIA CORPORATION nor the names of its |
| 13 | +# contributors may be used to endorse or promote products derived |
| 14 | +# from this software without specific prior written permission. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 17 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 19 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 20 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 | +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | +import argparse |
| 29 | +import os |
| 30 | +import pathlib |
| 31 | +import re |
| 32 | +import shutil |
| 33 | +import subprocess |
| 34 | +import sys |
| 35 | +from distutils.dir_util import copy_tree |
| 36 | +from tempfile import mkstemp |
| 37 | + |
| 38 | + |
| 39 | +def fail_if(p, msg): |
| 40 | + if p: |
| 41 | + print("error: {}".format(msg), file=sys.stderr) |
| 42 | + sys.exit(1) |
| 43 | + |
| 44 | + |
| 45 | +def mkdir(path): |
| 46 | + pathlib.Path(path).mkdir(parents=True, exist_ok=True) |
| 47 | + |
| 48 | + |
| 49 | +def touch(path): |
| 50 | + pathlib.Path(path).touch() |
| 51 | + |
| 52 | + |
| 53 | +def cpdir(src, dest): |
| 54 | + copy_tree(src, dest, preserve_symlinks=1) |
| 55 | + |
| 56 | + |
| 57 | +def sed(pattern, replace, source, dest=None): |
| 58 | + fin = open(source, "r") |
| 59 | + if dest: |
| 60 | + fout = open(dest, "w") |
| 61 | + else: |
| 62 | + fd, name = mkstemp() |
| 63 | + fout = open(name, "w") |
| 64 | + |
| 65 | + for line in fin: |
| 66 | + out = re.sub(pattern, replace, line) |
| 67 | + fout.write(out) |
| 68 | + |
| 69 | + fin.close() |
| 70 | + fout.close() |
| 71 | + if not dest: |
| 72 | + shutil.copyfile(name, source) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + parser = argparse.ArgumentParser() |
| 77 | + |
| 78 | + parser.add_argument( |
| 79 | + "--dest-dir", type=str, required=True, help="Destination directory." |
| 80 | + ) |
| 81 | + parser.add_argument( |
| 82 | + "--binding-path", type=str, required=True, help="Path to Triton Python binding." |
| 83 | + ) |
| 84 | + |
| 85 | + FLAGS = parser.parse_args() |
| 86 | + |
| 87 | + FLAGS.triton_version = None |
| 88 | + with open("TRITON_VERSION", "r") as vfile: |
| 89 | + FLAGS.triton_version = vfile.readline().strip() |
| 90 | + |
| 91 | + FLAGS.whl_dir = os.path.join(FLAGS.dest_dir, "wheel") |
| 92 | + |
| 93 | + print("=== Building in: {}".format(os.getcwd())) |
| 94 | + print("=== Using builddir: {}".format(FLAGS.whl_dir)) |
| 95 | + print("Adding package files") |
| 96 | + |
| 97 | + mkdir(os.path.join(FLAGS.whl_dir, "tritonserver")) |
| 98 | + shutil.copy("tritonserver/__init__.py", os.path.join(FLAGS.whl_dir, "tritonserver")) |
| 99 | + |
| 100 | + cpdir("tritonserver/_c", os.path.join(FLAGS.whl_dir, "tritonserver", "_c")) |
| 101 | + PYBIND_LIB = os.path.basename(FLAGS.binding_path) |
| 102 | + shutil.copyfile( |
| 103 | + FLAGS.binding_path, |
| 104 | + os.path.join(FLAGS.whl_dir, "tritonserver", "_c", PYBIND_LIB), |
| 105 | + ) |
| 106 | + |
| 107 | + shutil.copyfile("LICENSE.txt", os.path.join(FLAGS.whl_dir, "LICENSE.txt")) |
| 108 | + shutil.copyfile("setup.py", os.path.join(FLAGS.whl_dir, "setup.py")) |
| 109 | + |
| 110 | + os.chdir(FLAGS.whl_dir) |
| 111 | + print("=== Building wheel") |
| 112 | + args = ["python3", "setup.py", "bdist_wheel"] |
| 113 | + |
| 114 | + wenv = os.environ.copy() |
| 115 | + wenv["VERSION"] = FLAGS.triton_version |
| 116 | + wenv["TRITON_PYBIND"] = PYBIND_LIB |
| 117 | + p = subprocess.Popen(args, env=wenv) |
| 118 | + p.wait() |
| 119 | + fail_if(p.returncode != 0, "setup.py failed") |
| 120 | + |
| 121 | + cpdir("dist", FLAGS.dest_dir) |
| 122 | + |
| 123 | + print("=== Output wheel file is in: {}".format(FLAGS.dest_dir)) |
| 124 | + touch(os.path.join(FLAGS.dest_dir, "stamp.whl")) |
0 commit comments