summaryrefslogtreecommitdiffstats
path: root/PCbuild/urlretrieve.py
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@microsoft.com>2017-07-17 12:55:28 (GMT)
committerGitHub <noreply@github.com>2017-07-17 12:55:28 (GMT)
commit588836d3e646c2bcb3473cda7c5f6a1e0ff2c2e9 (patch)
tree7cc11bcc8f53d5c8da207036602514bbe0ad30aa /PCbuild/urlretrieve.py
parent2c8a5e4c968217f9672340e520942c4ed788d8de (diff)
downloadcpython-588836d3e646c2bcb3473cda7c5f6a1e0ff2c2e9.zip
cpython-588836d3e646c2bcb3473cda7c5f6a1e0ff2c2e9.tar.gz
cpython-588836d3e646c2bcb3473cda7c5f6a1e0ff2c2e9.tar.bz2
bpo-30450: Adds alternate download approach for nuget.exe (#2737)
* bpo-30450: Adds alternate download approach for nuget.exe * Whitespace fix.
Diffstat (limited to 'PCbuild/urlretrieve.py')
-rw-r--r--PCbuild/urlretrieve.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/PCbuild/urlretrieve.py b/PCbuild/urlretrieve.py
new file mode 100644
index 0000000..9df773c
--- /dev/null
+++ b/PCbuild/urlretrieve.py
@@ -0,0 +1,39 @@
+# Simple Python script to download a file. Used as a fallback
+# when other more reliable methods fail.
+#
+from __future__ import print_function
+import sys
+
+try:
+ from requests import get
+except ImportError:
+ try:
+ from urllib.request import urlretrieve
+ USING = "urllib.request.urlretrieve"
+ except ImportError:
+ try:
+ from urllib import urlretrieve
+ USING = "urllib.retrieve"
+ except ImportError:
+ print("Python at", sys.executable, "is not suitable",
+ "for downloading files.", file=sys.stderr)
+ sys.exit(2)
+else:
+ USING = "requests.get"
+
+ def urlretrieve(url, filename):
+ r = get(url, stream=True)
+ r.raise_for_status()
+ with open(filename, 'wb') as f:
+ for chunk in r.iter_content(chunk_size=1024):
+ f.write(chunk)
+ return filename
+
+if __name__ == '__main__':
+ if len(sys.argv) != 3:
+ print("Usage: urlretrieve.py [url] [filename]", file=sys.stderr)
+ sys.exit(1)
+ URL = sys.argv[1]
+ FILENAME = sys.argv[2]
+ print("Downloading from", URL, "to", FILENAME, "using", USING)
+ urlretrieve(URL, FILENAME)