Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Zonr Chang | fa52e20 | 2012-04-12 15:38:42 +0800 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2011-2012 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 17 | |
| 18 | import datetime |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 19 | import os |
| 20 | import re |
| 21 | import sys |
| 22 | import subprocess |
| 23 | |
Daniel Sandler | 1c13147 | 2011-07-18 10:59:17 -0400 | [diff] [blame] | 24 | try: |
| 25 | import hashlib |
| 26 | sha1 = hashlib.sha1 |
| 27 | except ImportError, e: |
| 28 | import sha |
| 29 | sha1 = sha.sha |
| 30 | |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 31 | def get_repo_revision(repo_dir): |
| 32 | if not os.path.exists(os.path.join(repo_dir, '.git')): |
| 33 | return 'Unknown (not git)' |
| 34 | |
Logan Chien | 3bfcd86 | 2011-07-13 20:11:30 +0800 | [diff] [blame] | 35 | # Get the HEAD revision |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 36 | proc = subprocess.Popen(['git', 'log', '-1', '--format=%H'], |
| 37 | stdout=subprocess.PIPE, |
| 38 | stderr=subprocess.PIPE, |
| 39 | cwd=repo_dir) |
| 40 | out, err = proc.communicate() |
| 41 | proc.wait() |
| 42 | |
Logan Chien | 3bfcd86 | 2011-07-13 20:11:30 +0800 | [diff] [blame] | 43 | rev_sha1 = out.strip() |
| 44 | |
| 45 | # Working Directory Modified |
| 46 | proc = subprocess.Popen(['git', 'status'], |
| 47 | stdout=subprocess.PIPE, |
| 48 | stderr=subprocess.PIPE, |
| 49 | cwd=repo_dir) |
| 50 | out, err = proc.communicate() |
| 51 | proc.wait() |
| 52 | |
Logan Chien | 76bb588 | 2011-07-14 21:28:56 +0800 | [diff] [blame] | 53 | if out.find('(working directory clean)') == -1: |
Daniel Sandler | 1c13147 | 2011-07-18 10:59:17 -0400 | [diff] [blame] | 54 | mod = ' modified' |
| 55 | else: |
| 56 | mod = '' |
Logan Chien | 3bfcd86 | 2011-07-13 20:11:30 +0800 | [diff] [blame] | 57 | |
| 58 | return rev_sha1 + mod + ' (git)' |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 59 | |
| 60 | def compute_sha1(path, global_hasher = None): |
| 61 | f = open(path, 'rb') |
Daniel Sandler | 1c13147 | 2011-07-18 10:59:17 -0400 | [diff] [blame] | 62 | hasher = sha1() |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 63 | while True: |
| 64 | buf = f.read(512) |
| 65 | hasher.update(buf) |
| 66 | if global_hasher: |
| 67 | global_hasher.update(buf) |
| 68 | if len(buf) < 512: |
| 69 | break |
| 70 | f.close() |
| 71 | return hasher.hexdigest() |
| 72 | |
| 73 | def compute_sha1_list(paths): |
Daniel Sandler | 1c13147 | 2011-07-18 10:59:17 -0400 | [diff] [blame] | 74 | hasher = sha1() |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 75 | sha1sums = [] |
| 76 | for path in paths: |
| 77 | sha1sums.append(compute_sha1(path, hasher)) |
| 78 | return (hasher.hexdigest(), sha1sums) |
| 79 | |
| 80 | def quote_str(s): |
| 81 | result = '"' |
| 82 | for c in s: |
| 83 | if c == '\\': |
| 84 | result += '\\\\' |
| 85 | elif c == '\r': |
| 86 | result += '\\r' |
| 87 | elif c == '\n': |
| 88 | result += '\\n' |
| 89 | elif c == '\t': |
| 90 | result += '\\t' |
| 91 | elif c == '\"': |
| 92 | result += '\\"' |
| 93 | elif c == '\'': |
| 94 | result += '\\\'' |
| 95 | else: |
| 96 | result += c |
| 97 | result += '"' |
| 98 | return result |
| 99 | |
| 100 | def main(): |
| 101 | # Check Argument |
| 102 | if len(sys.argv) < 2: |
| 103 | print >> sys.stderr, 'USAGE:', sys.argv[0], '[REPO] [LIBs]' |
| 104 | sys.exit(1) |
| 105 | |
| 106 | # Record Build Time |
| 107 | build_time = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S') |
| 108 | |
| 109 | # Repository Directory (For build revision) |
| 110 | repo_dir = sys.argv[1] |
| 111 | build_rev = get_repo_revision(repo_dir) |
| 112 | |
| 113 | # Compute SHA1 |
| 114 | lib_list = list(set(sys.argv[2:])) |
| 115 | lib_list.sort() |
| 116 | build_sha1, sha1sum_list = compute_sha1_list(lib_list) |
| 117 | |
| 118 | # Build file list string |
| 119 | lib_list_str = '' |
| 120 | for i, path in enumerate(lib_list): |
| 121 | lib_list_str += ' %s %s\n' % (sha1sum_list[i], path) |
| 122 | |
| 123 | # Print the automatically generated code |
| 124 | print """/* Automatically generated file (DON'T MODIFY) */ |
| 125 | |
| 126 | /* Repository directory: %s */ |
| 127 | |
| 128 | /* File list: |
| 129 | %s*/ |
| 130 | |
Stephen Hines | aa72a21 | 2012-05-03 12:27:04 -0700 | [diff] [blame] | 131 | #ifdef __cplusplus |
| 132 | extern "C" { |
| 133 | #endif |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 134 | |
Stephen Hines | aa72a21 | 2012-05-03 12:27:04 -0700 | [diff] [blame] | 135 | char const *bccGetBuildTime() { |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 136 | return %s; |
| 137 | } |
| 138 | |
Stephen Hines | aa72a21 | 2012-05-03 12:27:04 -0700 | [diff] [blame] | 139 | char const *bccGetBuildRev() { |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 140 | return %s; |
| 141 | } |
| 142 | |
Stephen Hines | aa72a21 | 2012-05-03 12:27:04 -0700 | [diff] [blame] | 143 | char const *bccGetBuildSHA1() { |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 144 | return %s; |
| 145 | } |
| 146 | |
Stephen Hines | aa72a21 | 2012-05-03 12:27:04 -0700 | [diff] [blame] | 147 | #ifdef __cplusplus |
| 148 | } |
| 149 | #endif |
| 150 | |
Logan Chien | 48dc01d | 2011-07-13 18:12:06 +0800 | [diff] [blame] | 151 | """ % (os.path.abspath(repo_dir), |
| 152 | lib_list_str, |
| 153 | quote_str(build_time), |
| 154 | quote_str(build_rev), |
| 155 | quote_str(build_sha1)) |
| 156 | |
| 157 | if __name__ == '__main__': |
| 158 | main() |