예제 #1
0
    def testAbort(self):
        dl = download.Download("http://localhost/test.tgz", auto_delete=True)
        dl.abort()
        assert dl._aborted.happened
        assert dl.tempfile is None

        dl = download.Download("http://localhost/test.tgz", auto_delete=False)
        path = dl.tempfile.name
        dl.abort()
        assert not os.path.exists(path)
        assert dl._aborted.happened
        assert dl.tempfile is None
예제 #2
0
    def __init__(self, server, fingerprint):
        self.fingerprint = fingerprint
        self.info = []
        self.blocker = None

        if server is None: return

        self.status = _('Fetching key information from %s...') % server

        dl = download.Download(server + '/key/' + fingerprint)
        dl.start()

        from xml.dom import minidom

        @tasks. async
        def fetch_key_info():
            try:
                tempfile = dl.tempfile
                yield dl.downloaded
                self.blocker = None
                tasks.check(dl.downloaded)
                tempfile.seek(0)
                doc = minidom.parse(tempfile)
                if doc.documentElement.localName != 'key-lookup':
                    raise SafeException(
                        _('Expected <key-lookup>, not <%s>') %
                        doc.documentElement.localName)
                self.info += doc.documentElement.childNodes
            except Exception, ex:
                doc = minidom.parseString('<item vote="bad"/>')
                root = doc.documentElement
                root.appendChild(
                    doc.createTextNode(
                        _('Error getting key information: %s') % ex))
                self.info.append(root)
예제 #3
0
    def download_url(self,
                     url,
                     hint=None,
                     modification_time=None,
                     expected_size=None,
                     mirror_url=None):
        """The most low-level method here; just download a raw URL.
		It is the caller's responsibility to ensure that dl.stream is closed.
		@param url: the location to download from
		@param hint: user-defined data to store on the Download (e.g. used by the GUI)
		@param modification_time: don't download unless newer than this
		@param mirror_url: an altertive URL to try if this one fails
		@type mirror_url: str
		@rtype: L{download.Download}
		@since: 1.5
		"""
        if self.handler.dry_run:
            raise NeedDownload(url)

        dl = download.Download(url,
                               hint=hint,
                               modification_time=modification_time,
                               expected_size=expected_size,
                               auto_delete=not self.external_store)
        dl.mirror = mirror_url
        self.handler.monitor_download(dl)
        dl.downloaded = self.scheduler.download(dl)
        return dl
예제 #4
0
파일: fetch.py 프로젝트: rammstein/0install
	def download_url(self, url, hint = None, modification_time = None, expected_size = None, mirror_url = None, timeout = None):
		"""The most low-level method here; just download a raw URL.
		It is the caller's responsibility to ensure that dl.stream is closed.
		@param url: the location to download from
		@type url: str
		@param hint: user-defined data to store on the Download (e.g. used by the GUI)
		@param modification_time: don't download unless newer than this
		@param mirror_url: an altertive URL to try if this one fails
		@type mirror_url: str
		@param timeout: create a blocker which triggers if a download hangs for this long
		@type timeout: float | None
		@rtype: L{download.Download}
		@since: 1.5"""
		if not (url.startswith('http:') or url.startswith('https:') or url.startswith('ftp:')):
			raise SafeException(_("Unknown scheme in download URL '%s'") % url)
		dl = download.Download(url, hint = hint, modification_time = modification_time, expected_size = expected_size, auto_delete = not self.external_store)
		dl.mirror = mirror_url
		self.handler.monitor_download(dl)

		if timeout is not None:
			dl.timeout = tasks.Blocker('Download timeout')

		dl.downloaded = self.scheduler.download(dl, timeout = timeout)

		return dl
예제 #5
0
    def download_icon(self, interface, force=False, modification_time=None):
        """Download an icon for this interface and add it to the
		icon cache. If the interface has no icon or we are offline, do nothing.
		@return: the task doing the import, or None
		@rtype: L{tasks.Task}"""
        debug(_("download_icon %(interface)s (force = %(force)d)"), {
            'interface': interface,
            'force': force
        })

        # Find a suitable icon to download
        for icon in interface.get_metadata(XMLNS_IFACE, 'icon'):
            type = icon.getAttribute('type')
            source = icon.getAttribute('href')
            if source:
                break
            warn(_('Missing "href" attribute on <icon> in %s'), interface)
        else:
            info(_('No PNG icons found in %s'), interface)
            return

        try:
            dl = self.handler.monitored_downloads[source]
            if dl and force:
                dl.abort()
                raise KeyError
        except KeyError:
            dl = download.Download(source,
                                   hint=interface,
                                   modification_time=modification_time)
            self.handler.monitor_download(dl)

        @tasks. async
        def download_and_add_icon():
            stream = dl.tempfile
            yield dl.downloaded
            try:
                tasks.check(dl.downloaded)
                if dl.unmodified: return
                stream.seek(0)

                import shutil
                icons_cache = basedir.save_cache_path(config_site,
                                                      'interface_icons')
                icon_file = file(
                    os.path.join(icons_cache, escape(interface.uri)), 'w')
                shutil.copyfileobj(stream, icon_file)
            except Exception, ex:
                self.handler.report_error(ex)
예제 #6
0
    def download_url(self,
                     url,
                     hint=None,
                     modification_time=None,
                     expected_size=None):
        """The most low-level method here; just download a raw URL.
		@param url: the location to download from
		@param hint: user-defined data to store on the Download (e.g. used by the GUI)
		@param modification_time: don't download unless newer than this
		@rtype: L{download.Download}
		@since: 1.5
		"""
        if self.handler.dry_run:
            raise NeedDownload(url)

        dl = download.Download(url,
                               hint=hint,
                               modification_time=modification_time,
                               expected_size=expected_size)
        self.handler.monitor_download(dl)
        dl.downloaded = self.scheduler.download(dl)
        return dl
예제 #7
0
	def get_download(self, url, force = False, hint = None, factory = None):
		"""Return the Download object currently downloading 'url'.
		If no download for this URL has been started, start one now (and
		start monitoring it).
		If the download failed and force is False, return it anyway.
		If force is True, abort any current or failed download and start
		a new one.
		@rtype: L{download.Download}
		"""
		if self.dry_run:
			raise NeedDownload(url)

		try:
			dl = self.monitored_downloads[url]
			if dl and force:
				dl.abort()
				raise KeyError
		except KeyError:
			if factory is None:
				dl = download.Download(url, hint)
			else:
				dl = factory(url, hint)
			self.monitor_download(dl)
		return dl
예제 #8
0
    def download_icon(self, interface, force=False):
        """Download an icon for this interface and add it to the
		icon cache. If the interface has no icon do nothing.
		@return: the task doing the import, or None
		@rtype: L{tasks.Task}"""
        debug("download_icon %(interface)s (force = %(force)d)", {
            'interface': interface,
            'force': force
        })

        modification_time = None
        existing_icon = self.config.iface_cache.get_icon_path(interface)
        if existing_icon:
            file_mtime = os.stat(existing_icon).st_mtime
            from email.utils import formatdate
            modification_time = formatdate(timeval=file_mtime,
                                           localtime=False,
                                           usegmt=True)

        # Find a suitable icon to download
        for icon in interface.get_metadata(XMLNS_IFACE, 'icon'):
            type = icon.getAttribute('type')
            if type != 'image/png':
                debug(_('Skipping non-PNG icon'))
                continue
            source = icon.getAttribute('href')
            if source:
                break
            warn(_('Missing "href" attribute on <icon> in %s'), interface)
        else:
            info(_('No PNG icons found in %s'), interface)
            return

        try:
            dl = self.handler.monitored_downloads[source]
            if dl and force:
                dl.abort()
                raise KeyError
        except KeyError:
            dl = download.Download(source,
                                   hint=interface,
                                   modification_time=modification_time)
            self.handler.monitor_download(dl)

        @tasks. async
        def download_and_add_icon():
            stream = dl.tempfile
            yield dl.downloaded
            try:
                tasks.check(dl.downloaded)
                if dl.unmodified: return
                stream.seek(0)

                import shutil
                icons_cache = basedir.save_cache_path(config_site,
                                                      'interface_icons')
                icon_file = file(
                    os.path.join(icons_cache, escape(interface.uri)), 'w')
                shutil.copyfileobj(stream, icon_file)
            except Exception as ex:
                self.handler.report_error(ex)

        return download_and_add_icon()