Ray Milkey | f80bbb2 | 2016-03-11 10:16:22 -0800 | [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 |
| 16 | from os import path |
| 17 | |
| 18 | REPO_ROOTS = { |
| 19 | 'GERRIT': 'http://gerrit-maven.storage.googleapis.com', |
| 20 | 'GERRIT_API': 'https://gerrit-api.commondatastorage.googleapis.com/release', |
| 21 | 'MAVEN_CENTRAL': 'http://repo1.maven.org/maven2', |
| 22 | 'MAVEN_LOCAL': 'file://' + path.expanduser('~/.m2/repository'), |
| 23 | } |
| 24 | |
| 25 | |
| 26 | def resolve_url(url, redirects): |
| 27 | """ Resolve URL of a Maven artifact. |
| 28 | |
| 29 | prefix:path is passed as URL. prefix identifies known or custom |
| 30 | repositories that can be rewritten in redirects set, passed as |
| 31 | second arguments. |
| 32 | |
| 33 | A special case is supported, when prefix neither exists in |
| 34 | REPO_ROOTS, no in redirects set: the url is returned as is. |
| 35 | This enables plugins to pass custom maven_repository URL as is |
| 36 | directly to maven_jar(). |
| 37 | |
| 38 | Returns a resolved path for Maven artifact. |
| 39 | """ |
| 40 | s = url.find(':') |
| 41 | if s < 0: |
| 42 | return url |
| 43 | scheme, rest = url[:s], url[s+1:] |
| 44 | if scheme in redirects: |
| 45 | root = redirects[scheme] |
| 46 | elif scheme in REPO_ROOTS: |
| 47 | root = REPO_ROOTS[scheme] |
| 48 | else: |
| 49 | return url |
| 50 | root = root.rstrip('/') |
| 51 | rest = rest.lstrip('/') |
| 52 | return '/'.join([root, rest]) |
| 53 | |
| 54 | |
| 55 | def hash_file(hash_obj, path): |
| 56 | """Hash the contents of a file. |
| 57 | |
| 58 | Args: |
| 59 | hash_obj: an open hash object, e.g. hashlib.sha1(). |
| 60 | path: path to the file to hash. |
| 61 | |
| 62 | Returns: |
| 63 | The passed-in hash_obj. |
| 64 | """ |
| 65 | with open(path, 'rb') as f: |
| 66 | while True: |
| 67 | b = f.read(8192) |
| 68 | if not b: |
| 69 | break |
| 70 | hash_obj.update(b) |
| 71 | return hash_obj |
| 72 | |
| 73 | |
| 74 | def hash_bower_component(hash_obj, path): |
| 75 | """Hash the contents of a bower component directory. |
| 76 | |
| 77 | This is a stable hash of a directory downloaded with `bower install`, minus |
| 78 | the .bower.json file, which is autogenerated each time by bower. Used in lieu |
| 79 | of hashing a zipfile of the contents, since zipfiles are difficult to hash in |
| 80 | a stable manner. |
| 81 | |
| 82 | Args: |
| 83 | hash_obj: an open hash object, e.g. hashlib.sha1(). |
| 84 | path: path to the directory to hash. |
| 85 | |
| 86 | Returns: |
| 87 | The passed-in hash_obj. |
| 88 | """ |
| 89 | if not os.path.isdir(path): |
| 90 | raise ValueError('Not a directory: %s' % path) |
| 91 | |
| 92 | path = os.path.abspath(path) |
| 93 | for root, dirs, files in os.walk(path): |
| 94 | dirs.sort() |
| 95 | for f in sorted(files): |
| 96 | if f == '.bower.json': |
| 97 | continue |
| 98 | p = os.path.join(root, f) |
| 99 | hash_obj.update(p[len(path)+1:]) |
| 100 | hash_file(hash_obj, p) |
| 101 | |
| 102 | return hash_obj |