Han-Wen Nienhuys | 28e7a6d | 2016-09-21 15:03:54 +0200 | [diff] [blame] | 1 | # 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 | |
| 15 | import os |
Han-Wen Nienhuys | 28e7a6d | 2016-09-21 15:03:54 +0200 | [diff] [blame] | 16 | |
| 17 | |
| 18 | def hash_bower_component(hash_obj, path): |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 19 | """Hash the contents of a bower component directory. |
Han-Wen Nienhuys | 28e7a6d | 2016-09-21 15:03:54 +0200 | [diff] [blame] | 20 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 21 | This is a stable hash of a directory downloaded with `bower install`, minus |
Chad Horohoe | 6914232 | 2018-05-17 10:19:22 -0700 | [diff] [blame] | 22 | 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 Nienhuys | 28e7a6d | 2016-09-21 15:03:54 +0200 | [diff] [blame] | 25 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 26 | Args: |
| 27 | hash_obj: an open hash object, e.g. hashlib.sha1(). |
| 28 | path: path to the directory to hash. |
Han-Wen Nienhuys | 28e7a6d | 2016-09-21 15:03:54 +0200 | [diff] [blame] | 29 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 30 | 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 Nienhuys | 28e7a6d | 2016-09-21 15:03:54 +0200 | [diff] [blame] | 35 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 36 | 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 Nienhuys | 28e7a6d | 2016-09-21 15:03:54 +0200 | [diff] [blame] | 45 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 46 | return hash_obj |