• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 The TensorFlow Authors. 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"""Generates a Python module containing information about the build."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19import argparse
20
21
22def write_build_info(filename, build_config, key_value_list):
23  """Writes a Python that describes the build.
24
25  Args:
26    filename: filename to write to.
27    build_config: A string that represents the config used in this build (e.g.
28      "cuda").
29    key_value_list: A list of "key=value" strings that will be added to the
30      module as additional fields.
31
32  Raises:
33    ValueError: If `key_value_list` includes the key "is_cuda_build", which
34      would clash with one of the default fields.
35  """
36  module_docstring = "\"\"\"Generates a Python module containing information "
37  module_docstring += "about the build.\"\"\""
38  if build_config == "cuda":
39    build_config_bool = "True"
40  else:
41    build_config_bool = "False"
42
43  key_value_pair_stmts = []
44  if key_value_list:
45    for arg in key_value_list:
46      key, value = arg.split("=")
47      if key == "is_cuda_build":
48        raise ValueError("The key \"is_cuda_build\" cannot be passed as one of "
49                         "the --key_value arguments.")
50      key_value_pair_stmts.append("%s = %r" % (key, value))
51  key_value_pair_content = "\n".join(key_value_pair_stmts)
52
53  contents = """
54# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
55#
56# Licensed under the Apache License, Version 2.0 (the "License");
57# you may not use this file except in compliance with the License.
58# You may obtain a copy of the License at
59#
60#     http://www.apache.org/licenses/LICENSE-2.0
61#
62# Unless required by applicable law or agreed to in writing, software
63# distributed under the License is distributed on an "AS IS" BASIS,
64# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
65# See the License for the specific language governing permissions and
66# limitations under the License.
67# ==============================================================================
68%s
69from __future__ import absolute_import
70from __future__ import division
71from __future__ import print_function
72
73is_cuda_build = %s
74
75%s
76""" % (module_docstring, build_config_bool, key_value_pair_content)
77  open(filename, "w").write(contents)
78
79
80parser = argparse.ArgumentParser(
81    description="""Build info injection into the PIP package.""")
82
83parser.add_argument(
84    "--build_config",
85    type=str,
86    help="Either 'cuda' for GPU builds or 'cpu' for CPU builds.")
87
88parser.add_argument("--raw_generate", type=str, help="Generate build_info.py")
89
90parser.add_argument("--key_value", type=str, nargs="*",
91                    help="List of key=value pairs.")
92
93args = parser.parse_args()
94
95if args.raw_generate is not None and args.build_config is not None:
96  write_build_info(args.raw_generate, args.build_config, args.key_value)
97else:
98  raise RuntimeError("--raw_generate and --build_config must be used")
99