Skip to content

Commit 588836d

Browse files
authored
bpo-30450: Adds alternate download approach for nuget.exe (python#2737)
* bpo-30450: Adds alternate download approach for nuget.exe * Whitespace fix.
1 parent 2c8a5e4 commit 588836d

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

PCbuild/find_python.bat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
@echo Downloading nuget...
4343
@rem NB: Must use single quotes around NUGET here, NOT double!
4444
@rem Otherwise, a space in the path would break things
45-
@powershell.exe -Command Invoke-WebRequest %_Py_NUGET_URL% -OutFile '%_Py_NUGET%'
45+
@rem If it fails, retry with any available copy of Python
46+
@powershell.exe -Command Invoke-WebRequest %_Py_NUGET_URL% -OutFile '%_Py_NUGET%' || @py -c "%~dp0\urlretrieve.py" "%_Py_NUGET_URL%" "%_Py_NUGET%"
4647
)
4748
@echo Installing Python via nuget...
4849
@"%_Py_NUGET%" install pythonx86 -ExcludeVersion -OutputDirectory "%_Py_EXTERNALS_DIR%"

PCbuild/urlretrieve.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Simple Python script to download a file. Used as a fallback
2+
# when other more reliable methods fail.
3+
#
4+
from __future__ import print_function
5+
import sys
6+
7+
try:
8+
from requests import get
9+
except ImportError:
10+
try:
11+
from urllib.request import urlretrieve
12+
USING = "urllib.request.urlretrieve"
13+
except ImportError:
14+
try:
15+
from urllib import urlretrieve
16+
USING = "urllib.retrieve"
17+
except ImportError:
18+
print("Python at", sys.executable, "is not suitable",
19+
"for downloading files.", file=sys.stderr)
20+
sys.exit(2)
21+
else:
22+
USING = "requests.get"
23+
24+
def urlretrieve(url, filename):
25+
r = get(url, stream=True)
26+
r.raise_for_status()
27+
with open(filename, 'wb') as f:
28+
for chunk in r.iter_content(chunk_size=1024):
29+
f.write(chunk)
30+
return filename
31+
32+
if __name__ == '__main__':
33+
if len(sys.argv) != 3:
34+
print("Usage: urlretrieve.py [url] [filename]", file=sys.stderr)
35+
sys.exit(1)
36+
URL = sys.argv[1]
37+
FILENAME = sys.argv[2]
38+
print("Downloading from", URL, "to", FILENAME, "using", USING)
39+
urlretrieve(URL, FILENAME)

0 commit comments

Comments
 (0)