blob: 2617424d42f07f42a29218d2a76ba4a966f5301c [file] [log] [blame]
Doug Zongker9296f092012-03-20 16:42:22 -07001#!/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
Anthony Kingc713d762015-11-03 00:23:11 +000017from __future__ import print_function
18
Doug Zongker9296f092012-03-20 16:42:22 -070019import sys
Doug Zongker9296f092012-03-20 16:42:22 -070020
21try:
22 from hashlib import sha1
23except ImportError:
24 from sha import sha as sha1
25
Doug Zongker8f3670b2012-03-21 10:01:01 -070026if len(sys.argv) < 2:
27 sys.exit(0)
28
Doug Zongker9296f092012-03-20 16:42:22 -070029build_info = {}
30f = open(sys.argv[1])
31for line in f:
32 line = line.strip()
33 if line.startswith("require"):
34 key, value = line.split()[1].split("=", 1)
35 build_info[key] = value
36f.close()
37
38bad = False
39
40for item in sys.argv[2:]:
41 key, fn = item.split(":", 1)
42
43 values = build_info.get(key, None)
44 if not values:
45 continue
46 values = values.split("|")
47
48 f = open(fn, "rb")
49 digest = sha1(f.read()).hexdigest()
50 f.close()
51
52 versions = {}
53 try:
54 f = open(fn + ".sha1")
55 except IOError:
Anthony Kingc713d762015-11-03 00:23:11 +000056 if not bad:
57 print()
58 print("*** Error opening \"%s.sha1\"; can't verify %s" % (fn, key))
Doug Zongker9296f092012-03-20 16:42:22 -070059 bad = True
60 continue
61 for line in f:
62 line = line.strip()
63 if not line or line.startswith("#"): continue
64 h, v = line.split()
65 versions[h] = v
66
67 if digest not in versions:
Anthony Kingc713d762015-11-03 00:23:11 +000068 if not bad:
69 print()
70 print("*** SHA-1 hash of \"%s\" doesn't appear in \"%s.sha1\"" % (fn, fn))
Doug Zongker9296f092012-03-20 16:42:22 -070071 bad = True
72 continue
73
74 if versions[digest] not in values:
Anthony Kingc713d762015-11-03 00:23:11 +000075 if not bad:
76 print()
77 print("*** \"%s\" is version %s; not any %s allowed by \"%s\"." % (
78 fn, versions[digest], key, sys.argv[1]))
Doug Zongker9296f092012-03-20 16:42:22 -070079 bad = True
80
81if bad:
Anthony Kingc713d762015-11-03 00:23:11 +000082 print()
Doug Zongker9296f092012-03-20 16:42:22 -070083 sys.exit(1)