1#!/usr/bin/env python 2# 3# Copyright (C) 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 17import sys 18import os 19 20try: 21 from hashlib import sha1 22except ImportError: 23 from sha import sha as sha1 24 25import argparse 26 27parser = argparse.ArgumentParser() 28parser.add_argument("--board_info_txt", nargs="?", required=True) 29parser.add_argument("--board_info_check", nargs="*", required=True) 30args = parser.parse_args() 31 32if not args.board_info_txt: 33 sys.exit(0) 34 35build_info = {} 36f = open(args.board_info_txt) 37for line in f: 38 line = line.strip() 39 if line.startswith("require"): 40 key, value = line.split()[1].split("=", 1) 41 build_info[key] = value 42f.close() 43 44bad = False 45 46for item in args.board_info_check: 47 key, fn = item.split(":", 1) 48 49 values = build_info.get(key, None) 50 if not values: 51 continue 52 values = values.split("|") 53 54 f = open(fn, "rb") 55 digest = sha1(f.read()).hexdigest() 56 f.close() 57 58 versions = {} 59 try: 60 f = open(fn + ".sha1") 61 except IOError: 62 if not bad: print() 63 print("*** Error opening \"%s.sha1\"; can't verify %s" % (fn, key)) 64 bad = True 65 continue 66 for line in f: 67 line = line.strip() 68 if not line or line.startswith("#"): continue 69 h, v = line.split() 70 versions[h] = v 71 72 if digest not in versions: 73 if not bad: print() 74 print("*** SHA-1 hash of \"%s\" doesn't appear in \"%s.sha1\"" % (fn, fn)) 75 bad = True 76 continue 77 78 if versions[digest] not in values: 79 if not bad: print() 80 print("*** \"%s\" is version %s; not any %s allowed by \"%s\"." % ( 81 fn, versions[digest], key, args.board_info_txt)) 82 bad = True 83 84if bad: 85 print() 86 sys.exit(1) 87