38"""A download of a single resource to a temporary file. 39 @ivar url: the URL of the resource being fetched 40 @type url: str 41 @ivar tempfile: the file storing the downloaded data 42 @type tempfile: file 43 @ivar status: the status of the download 44 @type status: (download_fetching | download_failed | download_complete) 45 @ivar expected_size: the expected final size of the file 46 @type expected_size: int | None 47 @ivar downloaded: triggered when the download ends (on success or failure) 48 @type downloaded: L{tasks.Blocker} 49 @ivar hint: hint passed by and for caller 50 @type hint: object 51 @ivar aborted_by_user: whether anyone has called L{abort} 52 @type aborted_by_user: bool 53 @ivar unmodified: whether the resource was not modified since the modification_time given at construction 54 @type unmodified: bool 55 @ivar mirror: an alternative URL to try if this download fails 56 @type mirror: str | None 57 """ 58__slots__=['url','tempfile','status','expected_size','downloaded', 59'hint','_final_total_size','aborted_by_user','mirror', 60'modification_time','unmodified','_aborted'] 61
63"""Create a new download object. 64 @param url: the resource to download 65 @type url: str 66 @param hint: object with which this download is associated (an optional hint for the GUI) 67 @param modification_time: string with HTTP date that indicates last modification time. The resource will not be downloaded if it was not modified since that date. 68 @type modification_time: str | None 69 @type auto_delete: bool 70 @postcondition: L{status} == L{download_fetching}.""" 71assertauto_deletein(True,False)# XXX 72self.url=url 73self.hint=hint 74self.aborted_by_user=False# replace with _aborted? 75self.modification_time=modification_time 76self.unmodified=False 77 78self.tempfile=None# Stream for result 79self.downloaded=None 80self.mirror=None 81 82self.expected_size=expected_size# Final size (excluding skipped bytes) 83self._final_total_size=None# Set when download is finished 84 85self.status=download_fetching 86ifauto_delete: 87self.tempfile=tempfile.TemporaryFile(prefix='injector-dl-data-',mode='w+b') 88else: 89self.tempfile=tempfile.NamedTemporaryFile(prefix='injector-dl-data-',mode='w+b',delete=False) 90 91self._aborted=tasks.Blocker("abort "+url)
94"""@type status: int""" 95assertself.statusisdownload_fetching 96assertself.tempfileisnotNone 97assertnotself.aborted_by_user 98 99ifstatus==RESULT_NOT_MODIFIED:100logger.debug("%s not modified",self.url)101self.tempfile=None102self.unmodified=True103self.status=download_complete104self._final_total_size=0105return106107self._final_total_size=self.get_bytes_downloaded_so_far()108109self.tempfile=None110111try:112assertstatus==RESULT_OK113114# Check that the download has the correct size, if we know what it should be.115ifself.expected_sizeisnotNone:116ifself._final_total_size!=self.expected_size:117raiseSafeException(_('Downloaded archive has incorrect size.\n'118'URL: %(url)s\n'119'Expected: %(expected_size)d bytes\n'120'Received: %(size)d bytes')%{'url':self.url,'expected_size':self.expected_size,'size':self._final_total_size})121except:122self.status=download_failed123raise124else:125self.status=download_complete
128"""Signal the current download to stop.129 @postcondition: L{aborted_by_user}"""130self.status=download_failed131132ifself.tempfileisnotNone:133logger.info(_("Aborting download of %s"),self.url)134# TODO: we currently just close the output file; the thread will end when it tries to135# write to it. We should try harder to stop the thread immediately (e.g. by closing its136# socket when known), although we can never cover all cases (e.g. a stuck DNS lookup).137# In any case, we don't wait for the child to exit before notifying tasks that are waiting138# on us.139self.aborted_by_user=True140self.tempfile.close()141ifhasattr(self.tempfile,'delete')andnotself.tempfile.delete:142os.remove(self.tempfile.name)143self.tempfile=None144self._aborted.trigger()
147"""Returns the current fraction of this download that has been fetched (from 0 to 1),148 or None if the total size isn't known.149 @return: fraction downloaded150 @rtype: int | None"""151ifself.tempfileisNone:152return1153ifself.expected_sizeisNone:154returnNone# Unknown155current_size=self.get_bytes_downloaded_so_far()156returnfloat(current_size)/self.expected_size
159"""Get the download progress. Will be zero if the download has not yet started.160 @rtype: int"""161ifself.statusisdownload_fetching:162ifself.tempfile.closed:163return1164else:165returnos.fstat(self.tempfile.fileno()).st_size166else:167returnself._final_total_sizeor0
170"""Return an alternative download URL to try, or None if we're out of options.171 @rtype: str"""172mirror=self.mirror173self.mirror=None174returnmirror