diff options
author | Andrew Selle <aselle@google.com> | 2016-09-23 11:11:26 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2016-09-23 12:19:03 -0700 |
commit | 1380cbd6c2310b0f1db53828303b5643914b7fe0 (patch) | |
tree | 3daa74e1fcd52e6bfae3ea91d926825d03b153d0 /tensorflow/tools/git | |
parent | b86a515e3bf892cad74da3cc61a3ce16022083b1 (diff) |
Fix various issues with the git version tracking
- Robust to .git not being present. Was regression from Windows fix.
- Refactor the python code to only generate the file in one function
- Properly include __VERSION__ unquoted
- Make a shell script version for use in Make
Change: 134102340
Diffstat (limited to 'tensorflow/tools/git')
-rwxr-xr-x | tensorflow/tools/git/gen_git_source.py | 63 | ||||
-rwxr-xr-x | tensorflow/tools/git/gen_git_source.sh | 32 |
2 files changed, 80 insertions, 15 deletions
diff --git a/tensorflow/tools/git/gen_git_source.py b/tensorflow/tools/git/gen_git_source.py index ffd228ed7e..94cba7184c 100755 --- a/tensorflow/tools/git/gen_git_source.py +++ b/tensorflow/tools/git/gen_git_source.py @@ -124,6 +124,48 @@ def configure(src_base_path, debug=False): print("gen_git_source.py: spec is %r" % spec) +def get_git_version(git_base_path): + """Get the git version from the repository. + + This function runs `git describe ...` in the path given as `git_base_path`. + This will return a string of the form: + <base-tag>-<number of commits since tag>-<shortened sha hash> + + For example, 'v0.10.0-1585-gbb717a6' means v0.10.0 was the last tag when + compiled. 1585 commits are after that commit tag, and we can get back to this + version by running `git checkout gbb717a6`. + + Args: + git_base_path: where the .git directory is located + Returns: + A string representing the git version + """ + + unknown_label = "unknown" + try: + val = subprocess.check_output(["git", "-C", git_base_path, "describe", + "--long", "--dirty", "--tags"]).strip() + return val if val else unknown_label + except subprocess.CalledProcessError: + return unknown_label + + +def write_version_info(filename, git_version): + """Write a c file that defines the version functions. + + Args: + filename: filename to write to. + git_version: the result of a git describe. + """ + if "\"" in git_version or "\\" in git_version: + git_version = "git_version_is_invalid" # do not cause build to fail! + contents = """/* Generated by gen_git_source.py */ +const char* tf_git_version() {return "%s";} +const char* tf_compiler_version() {return __VERSION__;} +""" % git_version + open(filename, "w").write(contents) + + def generate(arglist): """Generate version_info.cc as given `destination_file`. @@ -151,9 +193,9 @@ def generate(arglist): # unused ref_symlink arg spec, head_symlink, _, dest_file = arglist data = json.load(open(spec)) - strs = {"tf_compiler_version": "__VERSION__"} + git_version = None if not data["git"]: - strs["tf_git_version"] = "internal" + git_version = "unknown" else: old_branch = data["branch"] new_branch = parse_branch_ref(head_symlink) @@ -161,12 +203,8 @@ def generate(arglist): raise RuntimeError( "Run ./configure again, branch was '%s' but is now '%s'" % (old_branch, new_branch)) - strs["tf_git_version"] = subprocess.check_output( - ["git", "-C", data["path"], "describe", "--long", "--dirty", "--tags"]).strip() - # TODO(aselle): Check for escaping - cpp_file = "\n".join("const char* %s() {return \"%s\";}" % (x, y) - for x, y in strs.items()) - open(dest_file, "w").write(cpp_file + "\n") + git_version = get_git_version(data["path"]) + write_version_info(dest_file, git_version) def raw_generate(output_file): @@ -179,13 +217,8 @@ def raw_generate(output_file): output_file: Output filename for the version info cc """ - strs = {"tf_compiler_version": "__VERSION__"} - version = subprocess.check_output(["git", "describe", "--long", "--dirty", "--tags"]).strip() - version = version if version else "unknown" - strs["tf_git_version"] = version - cpp_file = "\n".join("const char* %s() {return \"%s\";}" % (x, y) - for x, y in strs.items()) - open(output_file, "w").write(cpp_file + "\n") + git_version = get_git_version(".") + write_version_info(output_file, git_version) parser = argparse.ArgumentParser(description="""Git hash injection into bazel. diff --git a/tensorflow/tools/git/gen_git_source.sh b/tensorflow/tools/git/gen_git_source.sh new file mode 100755 index 0000000000..ce128822d7 --- /dev/null +++ b/tensorflow/tools/git/gen_git_source.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# Copyright 2016 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== + +OUTPUT_FILENAME=$1 +if [[ -z "${OUTPUT_FILENAME}" ]]; then + echo "Usage: $0 <filename>" + exit 1 +fi + +GIT_VERSION=`git describe --long --dirty --tags` +if [[ $? != 0 ]]; then + GIT_VERSION=unknown; +fi + +cat <<EOF > ${OUTPUT_FILENAME} +const char* tf_git_version() {return "${GIT_VERSION}";} +const char* tf_compiler_version() {return __VERSION__;} +EOF + |