|
11 | 11 | import os |
12 | 12 | import random |
13 | 13 | import re |
| 14 | +import shlex |
14 | 15 | import shutil |
15 | 16 | import stat |
16 | 17 | import subprocess |
@@ -873,20 +874,39 @@ def progress_pycurl(self, total, amount, _uploadtotal, _uploadamount): |
873 | 874 | c.close() |
874 | 875 | headers = None |
875 | 876 | else: |
876 | | - req_header = {"Accept": "application/*"} |
877 | | - res = urlopen(Request(url, None, req_header)) |
878 | | - chunk_size = 8192 # urlretrieve uses this value |
879 | | - headers = res.headers |
880 | | - content_length = res.headers.get("Content-Length") |
881 | | - total = int(content_length.strip()) if content_length else -1 |
882 | | - amount = 0 |
883 | | - with open(file_name, "wb") as f: |
884 | | - chunk = res.read(chunk_size) |
885 | | - while chunk: |
886 | | - f.write(chunk) |
887 | | - amount += len(chunk) |
888 | | - progress.write_update(total, amount) |
| 877 | + try: |
| 878 | + req_header = {"Accept": "application/*"} |
| 879 | + res = urlopen(Request(url, None, req_header)) |
| 880 | + chunk_size = 8192 # urlretrieve uses this value |
| 881 | + headers = res.headers |
| 882 | + content_length = res.headers.get("Content-Length") |
| 883 | + total = int(content_length.strip()) if content_length else -1 |
| 884 | + amount = 0 |
| 885 | + with open(file_name, "wb") as f: |
889 | 886 | chunk = res.read(chunk_size) |
| 887 | + while chunk: |
| 888 | + f.write(chunk) |
| 889 | + amount += len(chunk) |
| 890 | + progress.write_update(total, amount) |
| 891 | + chunk = res.read(chunk_size) |
| 892 | + except (OSError, IOError) as exc: # noqa: B014 |
| 893 | + # Downloading from within Meta's network needs to use a proxy. |
| 894 | + if shutil.which("fwdproxy-config") is None: |
| 895 | + print( |
| 896 | + "Note: Could not find Meta-specific fallback 'fwdproxy-config'. " |
| 897 | + "If you are working externally, you can ignore this message." |
| 898 | + ) |
| 899 | + raise |
| 900 | + |
| 901 | + print("Default download failed, retrying with curl and fwdproxy...") |
| 902 | + cmd = f"curl -L $(fwdproxy-config curl) -o {shlex.quote(file_name)} {shlex.quote(url)}" |
| 903 | + print(f"Running command: {cmd}") |
| 904 | + result = subprocess.run(cmd, shell=True, capture_output=True) |
| 905 | + if result.returncode != 0: |
| 906 | + raise TransientFailure( |
| 907 | + f"Failed to download {url} to {file_name}: {exc} (fwdproxy fallback failed: {result.stderr.decode()})" |
| 908 | + ) |
| 909 | + headers = None |
890 | 910 | except (OSError, IOError) as exc: # noqa: B014 |
891 | 911 | raise TransientFailure( |
892 | 912 | "Failed to download %s to %s: %s" % (url, file_name, str(exc)) |
|
0 commit comments