summaryrefslogtreecommitdiff
path: root/buildstream/plugins/sources/_downloadablefilesource.py
diff options
context:
space:
mode:
Diffstat (limited to 'buildstream/plugins/sources/_downloadablefilesource.py')
-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()):