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 | 72623a1 | 2013-05-30 01:05:56 +0200 | [diff] [blame] | 17 | try: |
| 18 | from subprocess import check_output |
| 19 | except ImportError: |
| 20 | from subprocess import Popen, PIPE |
| 21 | def check_output(*cmd): |
| 22 | return Popen(*cmd, stdout=PIPE).communicate()[0] |
David Ostrovsky | 2536d06 | 2013-11-14 00:35:07 +0100 | [diff] [blame] | 23 | |
| 24 | REPO_ROOTS = { |
| 25 | 'GERRIT': 'http://gerrit-maven.storage.googleapis.com', |
David Ostrovsky | a270797 | 2014-01-17 09:04:35 +0100 | [diff] [blame] | 26 | 'GERRIT_API': 'https://gerrit-api.commondatastorage.googleapis.com/release', |
David Ostrovsky | 2536d06 | 2013-11-14 00:35:07 +0100 | [diff] [blame] | 27 | 'ECLIPSE': 'https://repo.eclipse.org/content/groups/releases', |
| 28 | 'MAVEN_CENTRAL': 'http://repo1.maven.org/maven2', |
| 29 | 'MAVEN_LOCAL': 'file://' + path.expanduser('~/.m2/repository'), |
| 30 | } |
| 31 | |
| 32 | def resolve_url(url, redirects): |
| 33 | """ Resolve URL of a Maven artifact. |
| 34 | |
| 35 | prefix:path is passed as URL. prefix identifies known or custom |
| 36 | repositories that can be rewritten in redirects set, passed as |
| 37 | second arguments. |
| 38 | |
| 39 | A special case is supported, when prefix neither exists in |
| 40 | REPO_ROOTS, no in redirects set: the url is returned as is. |
| 41 | This enables plugins to pass custom maven_repository URL as is |
| 42 | directly to maven_jar(). |
| 43 | |
| 44 | Returns a resolved path for Maven artifact. |
| 45 | """ |
| 46 | s = url.find(':') |
| 47 | if s < 0: |
| 48 | return url |
| 49 | scheme, rest = url[:s], url[s+1:] |
| 50 | if scheme in redirects: |
| 51 | root = redirects[scheme] |
| 52 | elif scheme in REPO_ROOTS: |
| 53 | root = REPO_ROOTS[scheme] |
| 54 | else: |
| 55 | return url |
| 56 | root = root.rstrip('/') |
| 57 | rest = rest.lstrip('/') |
| 58 | return '/'.join([root, rest]) |