blob: 947e2c0a0f1b8f62b1fd01adea239b35b2018cd0 [file] [log] [blame]
David Ostrovsky72623a12013-05-30 01:05:56 +02001# 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 Ostrovsky2536d062013-11-14 00:35:07 +010015from os import path
16
David Ostrovsky2536d062013-11-14 00:35:07 +010017REPO_ROOTS = {
David Ostrovskya9c45652018-12-28 09:23:12 +010018 'ECLIPSE': 'https://repo.eclipse.org/content/groups/releases',
Paladox noneb85ef812019-04-19 15:15:06 +000019 'GERRIT': 'https://gerrit-maven.storage.googleapis.com',
David Ostrovskya2707972014-01-17 09:04:35 +010020 'GERRIT_API': 'https://gerrit-api.commondatastorage.googleapis.com/release',
Paladox noneb85ef812019-04-19 15:15:06 +000021 'MAVEN_CENTRAL': 'https://repo1.maven.org/maven2',
David Ostrovsky2536d062013-11-14 00:35:07 +010022 'MAVEN_LOCAL': 'file://' + path.expanduser('~/.m2/repository'),
David Pursehouse777cd1a2016-04-08 13:57:38 +090023 'MAVEN_SNAPSHOT': 'https://oss.sonatype.org/content/repositories/snapshots',
David Ostrovsky2536d062013-11-14 00:35:07 +010024}
25
David Pursehouse15a9f532014-06-23 10:55:20 +090026
David Ostrovsky2536d062013-11-14 00:35:07 +010027def resolve_url(url, redirects):
Chad Horohoedd224702018-05-16 22:33:06 -040028 """ Resolve URL of a Maven artifact.
David Ostrovsky2536d062013-11-14 00:35:07 +010029
Chad Horohoedd224702018-05-16 22:33:06 -040030 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 Ostrovsky2536d062013-11-14 00:35:07 +010033
Chad Horohoedd224702018-05-16 22:33:06 -040034 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 Ostrovsky2536d062013-11-14 00:35:07 +010038
Chad Horohoedd224702018-05-16 22:33:06 -040039 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 Borowitz0cb928c2015-11-18 10:19:52 -050054
55
56def hash_file(hash_obj, path):
Chad Horohoedd224702018-05-16 22:33:06 -040057 """Hash the contents of a file.
Dave Borowitz0cb928c2015-11-18 10:19:52 -050058
Chad Horohoedd224702018-05-16 22:33:06 -040059 Args:
60 hash_obj: an open hash object, e.g. hashlib.sha1().
61 path: path to the file to hash.
Dave Borowitz0cb928c2015-11-18 10:19:52 -050062
Chad Horohoedd224702018-05-16 22:33:06 -040063 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