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