Jack Neus | c474c9c | 2021-07-26 23:08:54 +0000 | [diff] [blame] | 1 | # Copyright (C) 2021 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 | """This module contains functions used to fetch files from various sources.""" |
| 16 | |
| 17 | import subprocess |
| 18 | import sys |
| 19 | from urllib.parse import urlparse |
Matt Story | 11b30b9 | 2021-10-26 10:56:13 -0400 | [diff] [blame] | 20 | from urllib.request import urlopen |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame^] | 21 | |
Jason Chang | f9aacd4 | 2023-08-03 14:38:00 -0700 | [diff] [blame] | 22 | from error import RepoExitError |
| 23 | |
| 24 | |
| 25 | class FetchFileError(RepoExitError): |
| 26 | """Exit error when fetch_file fails.""" |
Matt Story | 11b30b9 | 2021-10-26 10:56:13 -0400 | [diff] [blame] | 27 | |
Jack Neus | c474c9c | 2021-07-26 23:08:54 +0000 | [diff] [blame] | 28 | |
Jack Neus | 1988385 | 2021-10-25 22:38:44 +0000 | [diff] [blame] | 29 | def fetch_file(url, verbose=False): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 30 | """Fetch a file from the specified source using the appropriate protocol. |
Jack Neus | c474c9c | 2021-07-26 23:08:54 +0000 | [diff] [blame] | 31 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 32 | Returns: |
| 33 | The contents of the file as bytes. |
| 34 | """ |
| 35 | scheme = urlparse(url).scheme |
| 36 | if scheme == "gs": |
| 37 | cmd = ["gsutil", "cat", url] |
Jason Chang | f9aacd4 | 2023-08-03 14:38:00 -0700 | [diff] [blame] | 38 | errors = [] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 39 | try: |
| 40 | result = subprocess.run( |
| 41 | cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True |
| 42 | ) |
| 43 | if result.stderr and verbose: |
| 44 | print( |
| 45 | 'warning: non-fatal error running "gsutil": %s' |
| 46 | % result.stderr, |
| 47 | file=sys.stderr, |
| 48 | ) |
| 49 | return result.stdout |
| 50 | except subprocess.CalledProcessError as e: |
Jason Chang | f9aacd4 | 2023-08-03 14:38:00 -0700 | [diff] [blame] | 51 | errors.append(e) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 52 | print( |
| 53 | 'fatal: error running "gsutil": %s' % e.stderr, file=sys.stderr |
| 54 | ) |
Jason Chang | f9aacd4 | 2023-08-03 14:38:00 -0700 | [diff] [blame] | 55 | raise FetchFileError(aggregate_errors=errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 56 | with urlopen(url) as f: |
| 57 | return f.read() |