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