build_pip_package.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright 2017 Google Inc. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Builds a pip package suitable for redistribution.
  16. Adapted from tensorflow/tools/pip_package/build_pip_package.sh. This might have
  17. to change if Bazel changes how it modifies paths.
  18. """
  19. from __future__ import absolute_import
  20. from __future__ import division
  21. from __future__ import print_function
  22. import argparse
  23. import glob
  24. import os
  25. import shutil
  26. import subprocess
  27. import sys
  28. import tempfile
  29. import dragnn
  30. import tensorflow
  31. def main():
  32. cmd_args = argparse.ArgumentParser()
  33. cmd_args.add_argument("--include-tensorflow", action="store_true")
  34. cmd_args.add_argument("--output-dir", required=True)
  35. args = cmd_args.parse_args()
  36. if not os.path.isdir(args.output_dir):
  37. raise EnvironmentError(
  38. "Output directory {} doesn't exist".format(args.output_dir))
  39. elif not args.output_dir.startswith("/"):
  40. raise EnvironmentError("Please pass an absolute path to --output-dir.")
  41. tmp_packaging = tempfile.mkdtemp()
  42. runfiles, = (path for path in sys.path
  43. if path.endswith("build_pip_package.runfiles"))
  44. # Use the dragnn and tensorflow modules to resolve specific paths in the
  45. # runfiles directory. Current Bazel puts dragnn in a __main__ subdirectory,
  46. # for example.
  47. lib_path = os.path.abspath(dragnn.__file__)
  48. if runfiles not in lib_path:
  49. raise EnvironmentError("WARNING: Unexpected PYTHONPATH set by Bazel :(")
  50. base_dir = os.path.dirname(os.path.dirname(lib_path))
  51. tensorflow_dir = os.path.dirname(tensorflow.__file__)
  52. if runfiles not in tensorflow_dir:
  53. raise EnvironmentError("WARNING: Unexpected tf PYTHONPATH set by Bazel :(")
  54. # Copy the files.
  55. subprocess.check_call([
  56. "cp", "-r",
  57. "--no-preserve=all", os.path.join(base_dir, "dragnn"), os.path.join(
  58. base_dir, "syntaxnet"), tmp_packaging
  59. ])
  60. if args.include_tensorflow:
  61. subprocess.check_call(
  62. ["cp", "-r", "--no-preserve=all", tensorflow_dir, tmp_packaging])
  63. shutil.copy(
  64. os.path.join(base_dir, "dragnn/tools/oss_setup.py"),
  65. os.path.join(tmp_packaging, "setup.py"))
  66. subprocess.check_output(
  67. ["python", "setup.py", "bdist_wheel"], cwd=tmp_packaging)
  68. wheel, = glob.glob("{}/*.whl".format(os.path.join(tmp_packaging, "dist")))
  69. shutil.move(wheel, args.output_dir)
  70. print(
  71. "Wrote {}".format(os.path.join(args.output_dir, os.path.basename(wheel))))
  72. if __name__ == "__main__":
  73. main()