1#!/usr/bin/env python3 2# 3# Copyright (c) 2015-2017 The Khronos Group Inc. 4# Copyright (c) 2015-2017 Valve Corporation 5# Copyright (c) 2015-2017 LunarG, Inc. 6# Copyright (c) 2015-2017 Google Inc. 7# 8# Licensed under the Apache License, Version 2.0 (the "License"); 9# you may not use this file except in compliance with the License. 10# You may obtain a copy of the License at 11# 12# http://www.apache.org/licenses/LICENSE-2.0 13# 14# Unless required by applicable law or agreed to in writing, software 15# distributed under the License is distributed on an "AS IS" BASIS, 16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17# See the License for the specific language governing permissions and 18# limitations under the License. 19# 20# Author: Cort Stratton <cort@google.com> 21# Author: Jean-Francois Roy <jfroy@google.com> 22 23import argparse 24import hashlib 25import subprocess 26import uuid 27 28def generate(symbol_name, commit_id, output_header_file): 29 # Write commit ID to output header file 30 with open(output_header_file, "w") as header_file: 31 # File Comment 32 file_comment = '// *** THIS FILE IS GENERATED - DO NOT EDIT ***\n' 33 file_comment += '// See external_revision_generator.py for modifications\n' 34 header_file.write(file_comment) 35 # Copyright Notice 36 copyright = '' 37 copyright += '\n' 38 copyright += '/***************************************************************************\n' 39 copyright += ' *\n' 40 copyright += ' * Copyright (c) 2015-2017 The Khronos Group Inc.\n' 41 copyright += ' * Copyright (c) 2015-2017 Valve Corporation\n' 42 copyright += ' * Copyright (c) 2015-2017 LunarG, Inc.\n' 43 copyright += ' * Copyright (c) 2015-2017 Google Inc.\n' 44 copyright += ' *\n' 45 copyright += ' * Licensed under the Apache License, Version 2.0 (the "License");\n' 46 copyright += ' * you may not use this file except in compliance with the License.\n' 47 copyright += ' * You may obtain a copy of the License at\n' 48 copyright += ' *\n' 49 copyright += ' * http://www.apache.org/licenses/LICENSE-2.0\n' 50 copyright += ' *\n' 51 copyright += ' * Unless required by applicable law or agreed to in writing, software\n' 52 copyright += ' * distributed under the License is distributed on an "AS IS" BASIS,\n' 53 copyright += ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' 54 copyright += ' * See the License for the specific language governing permissions and\n' 55 copyright += ' * limitations under the License.\n' 56 copyright += ' *\n' 57 copyright += ' * Author: Chris Forbes <chrisforbes@google.com>\n' 58 copyright += ' * Author: Cort Stratton <cort@google.com>\n' 59 copyright += ' *\n' 60 copyright += ' ****************************************************************************/\n' 61 header_file.write(copyright) 62 # Contents 63 contents = '#pragma once\n\n' 64 contents += '#define %s "%s"\n' % (symbol_name, commit_id) 65 header_file.write(contents) 66 67def get_commit_id_from_git(git_binary, source_dir): 68 value = subprocess.check_output([git_binary, "rev-parse", "HEAD"], cwd=source_dir).decode('utf-8').strip() 69 return value 70 71def is_sha1(str): 72 try: str_as_int = int(str, 16) 73 except ValueError: return False 74 return len(str) == 40 75 76def get_commit_id_from_file(rev_file): 77 with open(rev_file, 'r') as rev_stream: 78 rev_contents = rev_stream.read() 79 rev_contents_stripped = rev_contents.strip() 80 if is_sha1(rev_contents_stripped): 81 return rev_contents_stripped; 82 # otherwise, SHA1 the entire (unstripped) file contents 83 sha1 = hashlib.sha1(); 84 sha1.update(rev_contents.encode('utf-8')) 85 return sha1.hexdigest() 86 87def get_commit_id_from_uuid(): 88 unique_uuid = str(uuid.uuid4()) 89 sha1 = hashlib.sha1(); 90 sha1.update(unique_uuid.encode()) 91 return sha1.hexdigest() 92 93def main(): 94 parser = argparse.ArgumentParser() 95 parser.add_argument("-s", "--symbol_name", metavar="SYMBOL_NAME", required=True, help="C symbol name") 96 parser.add_argument("-o", "--output_header_file", metavar="OUTPUT_HEADER_FILE", required=True, help="output header file path") 97 rev_method_group = parser.add_mutually_exclusive_group(required=True) 98 rev_method_group.add_argument("--git_dir", metavar="SOURCE_DIR", help="git working copy directory") 99 rev_method_group.add_argument("--rev_file", metavar="REVISION_FILE", help="source revision file path (must contain a SHA1 hash") 100 rev_method_group.add_argument("--from_uuid", action='store_true', help="base SHA1 on a dynamically generated UUID") 101 args = parser.parse_args() 102 103 # We can either parse the latest Git commit ID out of the specified repository (preferred where possible), 104 # or computing the SHA1 hash of the contents of a file passed on the command line and (where necessary -- 105 # e.g. when building the layers outside of a Git environment). 106 if args.git_dir is not None: 107 # Extract commit ID from the specified source directory 108 try: 109 commit_id = get_commit_id_from_git('git', args.git_dir) 110 except WindowsError: 111 # Call git.bat on Windows for compatibility. 112 commit_id = get_commit_id_from_git('git.bat', args.git_dir) 113 elif args.rev_file is not None: 114 # Read the commit ID from a file. 115 commit_id = get_commit_id_from_file(args.rev_file) 116 elif args.from_uuid is not None: 117 commit_id = get_commit_id_from_uuid() 118 119 if not is_sha1(commit_id): 120 raise ValueError("commit ID for " + args.symbol_name + " must be a SHA1 hash.") 121 122 generate(args.symbol_name, commit_id, args.output_header_file) 123 124if __name__ == '__main__': 125 main() 126