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