19296f092771a0261404f51bcbc1b62ba20654947Doug Zongker#!/usr/bin/env python
29296f092771a0261404f51bcbc1b62ba20654947Doug Zongker#
39296f092771a0261404f51bcbc1b62ba20654947Doug Zongker# Copyright (C) 2012 The Android Open Source Project
49296f092771a0261404f51bcbc1b62ba20654947Doug Zongker#
59296f092771a0261404f51bcbc1b62ba20654947Doug Zongker# Licensed under the Apache License, Version 2.0 (the "License");
69296f092771a0261404f51bcbc1b62ba20654947Doug Zongker# you may not use this file except in compliance with the License.
79296f092771a0261404f51bcbc1b62ba20654947Doug Zongker# You may obtain a copy of the License at
89296f092771a0261404f51bcbc1b62ba20654947Doug Zongker#
99296f092771a0261404f51bcbc1b62ba20654947Doug Zongker#      http://www.apache.org/licenses/LICENSE-2.0
109296f092771a0261404f51bcbc1b62ba20654947Doug Zongker#
119296f092771a0261404f51bcbc1b62ba20654947Doug Zongker# Unless required by applicable law or agreed to in writing, software
129296f092771a0261404f51bcbc1b62ba20654947Doug Zongker# distributed under the License is distributed on an "AS IS" BASIS,
139296f092771a0261404f51bcbc1b62ba20654947Doug Zongker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149296f092771a0261404f51bcbc1b62ba20654947Doug Zongker# See the License for the specific language governing permissions and
159296f092771a0261404f51bcbc1b62ba20654947Doug Zongker# limitations under the License.
169296f092771a0261404f51bcbc1b62ba20654947Doug Zongker
179296f092771a0261404f51bcbc1b62ba20654947Doug Zongkerimport sys
189296f092771a0261404f51bcbc1b62ba20654947Doug Zongkerimport os
199296f092771a0261404f51bcbc1b62ba20654947Doug Zongker
209296f092771a0261404f51bcbc1b62ba20654947Doug Zongkertry:
219296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  from hashlib import sha1
229296f092771a0261404f51bcbc1b62ba20654947Doug Zongkerexcept ImportError:
239296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  from sha import sha as sha1
249296f092771a0261404f51bcbc1b62ba20654947Doug Zongker
258f3670bbc5c1bd88f8ab1ef481cd6344ac551933Doug Zongkerif len(sys.argv) < 2:
268f3670bbc5c1bd88f8ab1ef481cd6344ac551933Doug Zongker  sys.exit(0)
278f3670bbc5c1bd88f8ab1ef481cd6344ac551933Doug Zongker
289296f092771a0261404f51bcbc1b62ba20654947Doug Zongkerbuild_info = {}
299296f092771a0261404f51bcbc1b62ba20654947Doug Zongkerf = open(sys.argv[1])
309296f092771a0261404f51bcbc1b62ba20654947Doug Zongkerfor line in f:
319296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  line = line.strip()
329296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  if line.startswith("require"):
339296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    key, value = line.split()[1].split("=", 1)
349296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    build_info[key] = value
359296f092771a0261404f51bcbc1b62ba20654947Doug Zongkerf.close()
369296f092771a0261404f51bcbc1b62ba20654947Doug Zongker
379296f092771a0261404f51bcbc1b62ba20654947Doug Zongkerbad = False
389296f092771a0261404f51bcbc1b62ba20654947Doug Zongker
399296f092771a0261404f51bcbc1b62ba20654947Doug Zongkerfor item in sys.argv[2:]:
409296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  key, fn = item.split(":", 1)
419296f092771a0261404f51bcbc1b62ba20654947Doug Zongker
429296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  values = build_info.get(key, None)
439296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  if not values:
449296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    continue
459296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  values = values.split("|")
469296f092771a0261404f51bcbc1b62ba20654947Doug Zongker
479296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  f = open(fn, "rb")
489296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  digest = sha1(f.read()).hexdigest()
499296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  f.close()
509296f092771a0261404f51bcbc1b62ba20654947Doug Zongker
519296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  versions = {}
529296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  try:
539296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    f = open(fn + ".sha1")
549296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  except IOError:
559296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    if not bad: print
569296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    print "*** Error opening \"%s.sha1\"; can't verify %s" % (fn, key)
579296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    bad = True
589296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    continue
599296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  for line in f:
609296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    line = line.strip()
619296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    if not line or line.startswith("#"): continue
629296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    h, v = line.split()
639296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    versions[h] = v
649296f092771a0261404f51bcbc1b62ba20654947Doug Zongker
659296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  if digest not in versions:
669296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    if not bad: print
679296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    print "*** SHA-1 hash of \"%s\" doesn't appear in \"%s.sha1\"" % (fn, fn)
689296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    bad = True
699296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    continue
709296f092771a0261404f51bcbc1b62ba20654947Doug Zongker
719296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  if versions[digest] not in values:
729296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    if not bad: print
739296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    print "*** \"%s\" is version %s; not any %s allowed by \"%s\"." % (
749296f092771a0261404f51bcbc1b62ba20654947Doug Zongker        fn, versions[digest], key, sys.argv[1])
759296f092771a0261404f51bcbc1b62ba20654947Doug Zongker    bad = True
769296f092771a0261404f51bcbc1b62ba20654947Doug Zongker
779296f092771a0261404f51bcbc1b62ba20654947Doug Zongkerif bad:
789296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  print
799296f092771a0261404f51bcbc1b62ba20654947Doug Zongker  sys.exit(1)
80