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 | |
David Ostrovsky | 2536d06 | 2013-11-14 00:35:07 +0100 | [diff] [blame] | 15 | from os import path |
| 16 | |
David Ostrovsky | 2536d06 | 2013-11-14 00:35:07 +0100 | [diff] [blame] | 17 | REPO_ROOTS = { |
David Ostrovsky | a9c4565 | 2018-12-28 09:23:12 +0100 | [diff] [blame] | 18 | 'ECLIPSE': 'https://repo.eclipse.org/content/groups/releases', |
Paladox none | b85ef81 | 2019-04-19 15:15:06 +0000 | [diff] [blame] | 19 | 'GERRIT': 'https://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', |
Paladox none | b85ef81 | 2019-04-19 15:15:06 +0000 | [diff] [blame] | 21 | 'MAVEN_CENTRAL': 'https://repo1.maven.org/maven2', |
David Ostrovsky | 2536d06 | 2013-11-14 00:35:07 +0100 | [diff] [blame] | 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): |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 28 | """ Resolve URL of a Maven artifact. |
David Ostrovsky | 2536d06 | 2013-11-14 00:35:07 +0100 | [diff] [blame] | 29 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 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. |
David Ostrovsky | 2536d06 | 2013-11-14 00:35:07 +0100 | [diff] [blame] | 33 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 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(). |
David Ostrovsky | 2536d06 | 2013-11-14 00:35:07 +0100 | [diff] [blame] | 38 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 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): |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 57 | """Hash the contents of a file. |
Dave Borowitz | 0cb928c | 2015-11-18 10:19:52 -0500 | [diff] [blame] | 58 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 59 | Args: |
| 60 | hash_obj: an open hash object, e.g. hashlib.sha1(). |
| 61 | path: path to the file to hash. |
Dave Borowitz | 0cb928c | 2015-11-18 10:19:52 -0500 | [diff] [blame] | 62 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 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 |