summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValentin David <valentin.david@gmail.com>2017-11-25 22:13:53 +0100
committerValentin David <valentin.david@gmail.com>2017-11-25 22:13:53 +0100
commit3fd7fe1fd3406fc9c35d0d2719085809c48247c7 (patch)
tree87d015cd5f93b3e56cf78991cd95033f4d2a3fdb
parenta6334521433a8a917b82d826f600d854604e9ed7 (diff)
downloadbuildstream-3fd7fe1fd3406fc9c35d0d2719085809c48247c7.tar.gz
Replace call to legacy urllib.request.urlretrieve by urllib.request.urlopen and add accept header to avoid 406 error on some http servers (e.g alioth.debian.org).
-rw-r--r--buildstream/plugins/sources/_downloadablefilesource.py20
1 files changed, 12 insertions, 8 deletions
diff --git a/buildstream/plugins/sources/_downloadablefilesource.py b/buildstream/plugins/sources/_downloadablefilesource.py
index a2ed366ad..99eab340e 100644
--- a/buildstream/plugins/sources/_downloadablefilesource.py
+++ b/buildstream/plugins/sources/_downloadablefilesource.py
@@ -3,6 +3,8 @@
import os
import urllib.request
import urllib.error
+import contextlib
+import shutil
from buildstream import Source, SourceError, Consistency
from buildstream import utils
@@ -72,14 +74,16 @@ class DownloadableFileSource(Source):
# Downloads from the url and caches it according to its sha256sum.
try:
with self.tempdir() as td:
- # Using basename because there needs to be a filename, and 'foo'
- # would be too silly.
- temp_dest = os.path.join(td, os.path.basename(self.url))
-
- local_file, _ = urllib.request.urlretrieve(self.url, temp_dest)
- if local_file != temp_dest:
- raise SourceError("Expected to download file to '{}', downloaded to '{}' instead!"
- .format(temp_dest, local_file))
+ default_name = os.path.basename(self.url)
+ request = urllib.request.Request(self.url)
+ request.add_header('Accept', '*/*')
+ with contextlib.closing(urllib.request.urlopen(request)) as response:
+ info = response.info()
+ filename = info.get_filename(default_name)
+ filename = os.path.basename(filename)
+ local_file = os.path.join(td, filename)
+ with open(local_file, 'wb') as dest:
+ shutil.copyfileobj(response, dest)
# Make sure url-specific mirror dir exists.
if not os.path.isdir(self._get_mirror_dir()):