Skip to content

Commit f44da2e

Browse files
jbower-fbmeta-codesync[bot]
authored andcommitted
Automatically attempt downloading using fwdproxy
Summary: Somehow I didn't need any special setup from my devserver but I do when using an On Demand host. Also manual wget configuration with args from `$(fwdproxy-config wget)` doesn't seem to work. Let's make things easier for the next person. Reviewed By: alexmalyshev Differential Revision: D91706453 fbshipit-source-id: 7f855afbe1e01c6c65283a7f5803139c1038513f
1 parent 715388e commit f44da2e

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

build/fbcode_builder/getdeps/fetcher.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import os
1212
import random
1313
import re
14+
import shlex
1415
import shutil
1516
import stat
1617
import subprocess
@@ -873,20 +874,39 @@ def progress_pycurl(self, total, amount, _uploadtotal, _uploadamount):
873874
c.close()
874875
headers = None
875876
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:
889886
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
890910
except (OSError, IOError) as exc: # noqa: B014
891911
raise TransientFailure(
892912
"Failed to download %s to %s: %s" % (url, file_name, str(exc))

0 commit comments

Comments
 (0)