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