blob: 9fb82afeffcf93a6413eff57dfa1292d694a4d10 [file] [log] [blame]
Han-Wen Nienhuys28e7a6d2016-09-21 15:03:54 +02001# Copyright (C) 2013 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
Han-Wen Nienhuys28e7a6d2016-09-21 15:03:54 +020016
17
18def hash_bower_component(hash_obj, path):
Chad Horohoedd224702018-05-16 22:33:06 -040019 """Hash the contents of a bower component directory.
Han-Wen Nienhuys28e7a6d2016-09-21 15:03:54 +020020
Chad Horohoedd224702018-05-16 22:33:06 -040021 This is a stable hash of a directory downloaded with `bower install`, minus
Chad Horohoe69142322018-05-17 10:19:22 -070022 the .bower.json file, which is autogenerated each time by bower. Used in
23 lieu of hashing a zipfile of the contents, since zipfiles are difficult to
24 hash in a stable manner.
Han-Wen Nienhuys28e7a6d2016-09-21 15:03:54 +020025
Chad Horohoedd224702018-05-16 22:33:06 -040026 Args:
27 hash_obj: an open hash object, e.g. hashlib.sha1().
28 path: path to the directory to hash.
Han-Wen Nienhuys28e7a6d2016-09-21 15:03:54 +020029
Chad Horohoedd224702018-05-16 22:33:06 -040030 Returns:
31 The passed-in hash_obj.
32 """
33 if not os.path.isdir(path):
34 raise ValueError('Not a directory: %s' % path)
Han-Wen Nienhuys28e7a6d2016-09-21 15:03:54 +020035
Chad Horohoedd224702018-05-16 22:33:06 -040036 path = os.path.abspath(path)
37 for root, dirs, files in os.walk(path):
38 dirs.sort()
39 for f in sorted(files):
40 if f == '.bower.json':
41 continue
42 p = os.path.join(root, f)
43 hash_obj.update(p[len(path)+1:].encode("utf-8"))
44 hash_obj.update(open(p, "rb").read())
Han-Wen Nienhuys28e7a6d2016-09-21 15:03:54 +020045
Chad Horohoedd224702018-05-16 22:33:06 -040046 return hash_obj