예제 #1
0
def do_add(args):
    """add DIGEST (DIRECTORY | (ARCHIVE [EXTRACT]))"""
    from zeroinstall.zerostore import unpack
    if len(args) < 2: raise UsageError(_("Missing arguments"))
    digest = args[0]
    if os.path.isdir(args[1]):
        if len(args) > 2: raise UsageError(_("Too many arguments"))
        stores.add_dir_to_cache(digest, args[1])
    elif os.path.isfile(args[1]):
        if len(args) > 3: raise UsageError(_("Too many arguments"))
        if len(args) > 2:
            extract = args[2]
        else:
            extract = None

        type = unpack.type_from_url(args[1])
        if not type:
            raise SafeException(
                _("Unknown extension in '%s' - can't guess MIME type") %
                args[1])
        unpack.check_type_ok(type)

        with open(args[1], 'rb') as stream:
            stores.add_archive_to_cache(digest,
                                        stream,
                                        args[1],
                                        extract,
                                        type=type)
    else:
        try:
            os.stat(args[1])
        except OSError as ex:
            if ex.errno != errno.ENOENT:  # No such file or directory
                raise UsageError(str(ex))  # E.g. permission denied
        raise UsageError(_("No such file or directory '%s'") % args[1])
예제 #2
0
def do_add(args):
	"""add DIGEST (DIRECTORY | (ARCHIVE [EXTRACT]))"""
	from zeroinstall.zerostore import unpack
	if len(args) < 2: raise UsageError(_("Missing arguments"))
	digest = args[0]
	if os.path.isdir(args[1]):
		if len(args) > 2: raise UsageError(_("Too many arguments"))
		stores.add_dir_to_cache(digest, args[1])
	elif os.path.isfile(args[1]):
		if len(args) > 3: raise UsageError(_("Too many arguments"))
		if len(args) > 2:
			extract = args[2]
		else:
			extract = None

		type = unpack.type_from_url(args[1])
		if not type:
			raise SafeException(_("Unknown extension in '%s' - can't guess MIME type") % args[1])
		unpack.check_type_ok(type)

		with open(args[1], 'rb') as stream:
			stores.add_archive_to_cache(digest, stream, args[1], extract, type = type)
	else:
		try:
			os.stat(args[1])
		except OSError as ex:
			if ex.errno != errno.ENOENT:		# No such file or directory
				raise UsageError(str(ex))	# E.g. permission denied
		raise UsageError(_("No such file or directory '%s'") % args[1])
예제 #3
0
    def download_archive(self, download_source, force=False, impl_hint=None, may_use_mirror=False):
        """Fetch an archive. You should normally call L{download_impl}
		instead, since it handles other kinds of retrieval method too.
		It is the caller's responsibility to ensure that the returned stream is closed.
		"""
        from zeroinstall.zerostore import unpack

        url = download_source.url
        if not (url.startswith("http:") or url.startswith("https:") or url.startswith("ftp:")):
            raise SafeException(_("Unknown scheme in download URL '%s'") % url)

        mime_type = download_source.type
        if not mime_type:
            mime_type = unpack.type_from_url(download_source.url)
        if not mime_type:
            raise SafeException(
                _("No 'type' attribute on archive, and I can't guess from the name (%s)") % download_source.url
            )
        if not self.external_store:
            unpack.check_type_ok(mime_type)

        if may_use_mirror:
            mirror = self._get_archive_mirror(download_source)
        else:
            mirror = None

        dl = self.download_url(download_source.url, hint=impl_hint, mirror_url=mirror)
        if download_source.size is not None:
            dl.expected_size = download_source.size + (download_source.start_offset or 0)
            # (else don't know sizes for mirrored archives)
        return (dl.downloaded, dl.tempfile)
예제 #4
0
	def download_archive(self, download_source, force = False, impl_hint = None, may_use_mirror = False):
		"""Fetch an archive. You should normally call L{download_impl}
		instead, since it handles other kinds of retrieval method too.
		It is the caller's responsibility to ensure that the returned stream is closed.
		If impl_hint is from a local feed and the url is relative, just opens the existing file for reading.
		@type download_source: L{model.DownloadSource}
		@type force: bool
		@type may_use_mirror: bool
		@rtype: (L{Blocker} | None, file)"""
		from zeroinstall.zerostore import unpack

		mime_type = download_source.type
		if not mime_type:
			mime_type = unpack.type_from_url(download_source.url)
		if not mime_type:
			raise SafeException(_("No 'type' attribute on archive, and I can't guess from the name (%s)") % download_source.url)
		if not self.external_store:
			unpack.check_type_ok(mime_type)

		if '://' not in download_source.url:
			return self._download_local_file(download_source, impl_hint)

		if may_use_mirror:
			mirror = self._get_archive_mirror(download_source)
		else:
			mirror = None

		if self.config.handler.dry_run:
			print(_("[dry-run] downloading archive {url}").format(url = download_source.url))
		dl = self.download_url(download_source.url, hint = impl_hint, mirror_url = mirror)
		if download_source.size is not None:
			dl.expected_size = download_source.size + (download_source.start_offset or 0)
		# (else don't know sizes for mirrored archives)
		return (dl.downloaded, dl.tempfile)
예제 #5
0
파일: fetch.py 프로젝트: timdiels/0install
	def download_archive(self, download_source, force = False, impl_hint = None, may_use_mirror = False):
		"""Fetch an archive. You should normally call L{download_impl}
		instead, since it handles other kinds of retrieval method too.
		It is the caller's responsibility to ensure that the returned stream is closed.
		"""
		from zeroinstall.zerostore import unpack

		url = download_source.url
		if not (url.startswith('http:') or url.startswith('https:') or url.startswith('ftp:')):
			raise SafeException(_("Unknown scheme in download URL '%s'") % url)

		mime_type = download_source.type
		if not mime_type:
			mime_type = unpack.type_from_url(download_source.url)
		if not mime_type:
			raise SafeException(_("No 'type' attribute on archive, and I can't guess from the name (%s)") % download_source.url)
		if not self.external_store:
			unpack.check_type_ok(mime_type)

		if may_use_mirror:
			mirror = self._get_archive_mirror(download_source)
		else:
			mirror = None

		dl = self.download_url(download_source.url, hint = impl_hint, mirror_url = mirror)
		if download_source.size is not None:
			dl.expected_size = download_source.size + (download_source.start_offset or 0)
		# (else don't know sizes for mirrored archives)
		return (dl.downloaded, dl.tempfile)
예제 #6
0
파일: fetch.py 프로젝트: rammstein/0install
	def download_archive(self, download_source, force = False, impl_hint = None, may_use_mirror = False):
		"""Fetch an archive. You should normally call L{download_impl}
		instead, since it handles other kinds of retrieval method too.
		It is the caller's responsibility to ensure that the returned stream is closed.
		If impl_hint is from a local feed and the url is relative, just opens the existing file for reading.
		@type download_source: L{model.DownloadSource}
		@type force: bool
		@type may_use_mirror: bool
		@rtype: (L{Blocker} | None, file)"""
		from zeroinstall.zerostore import unpack

		mime_type = download_source.type
		if not mime_type:
			mime_type = unpack.type_from_url(download_source.url)
		if not mime_type:
			raise SafeException(_("No 'type' attribute on archive, and I can't guess from the name (%s)") % download_source.url)
		if not self.external_store:
			unpack.check_type_ok(mime_type)

		if '://' not in download_source.url:
			return self._download_local_file(download_source, impl_hint)

		if may_use_mirror:
			mirror = self._get_archive_mirror(download_source)
		else:
			mirror = None

		if self.config.handler.dry_run:
			print(_("[dry-run] downloading archive {url}").format(url = download_source.url))
		dl = self.download_url(download_source.url, hint = impl_hint, mirror_url = mirror)
		if download_source.size is not None:
			dl.expected_size = download_source.size + (download_source.start_offset or 0)
		# (else don't know sizes for mirrored archives)
		return (dl.downloaded, dl.tempfile)
예제 #7
0
	def download_archive(self, download_source, force = False, impl_hint = None, may_use_mirror = False):
		"""Fetch an archive. You should normally call L{download_impl}
		instead, since it handles other kinds of retrieval method too.
		It is the caller's responsibility to ensure that the returned stream is closed.
		If impl_hint is from a local feed and the url is relative, just opens the existing file for reading.
		@type download_source: L{model.DownloadSource}
		@type force: bool
		@type may_use_mirror: bool
		@rtype: (L{Blocker} | None, file)"""
		from zeroinstall.zerostore import unpack

		mime_type = download_source.type
		if not mime_type:
			mime_type = unpack.type_from_url(download_source.url)
		if not mime_type:
			raise SafeException(_("No 'type' attribute on archive, and I can't guess from the name (%s)") % download_source.url)
		if not self.external_store:
			unpack.check_type_ok(mime_type)

		if '://' not in download_source.url:
			# Relative path
			if impl_hint is None or not impl_hint.feed.local_path:
				raise SafeException(_("Relative URL '{url}' in non-local feed '{feed}'").format(
					url = download_source.url,
					feed = impl_hint.feed))

			archive_file = os.path.join(os.path.dirname(impl_hint.feed.local_path), download_source.url)
			try:
				size = os.path.getsize(archive_file)
				if size != download_source.size:
					raise SafeException(_("Wrong size for {path}: feed says {expected}, but actually {actual} bytes").format(
						path = archive_file,
						expected = download_source.size,
						actual = size))
				return (None, open(archive_file, 'rb'))
			except OSError as ex:
				raise SafeException(str(ex))	# (error already includes path)

		if may_use_mirror:
			mirror = self._get_archive_mirror(download_source)
		else:
			mirror = None

		if self.config.handler.dry_run:
			print(_("[dry-run] downloading archive {url}").format(url = download_source.url))
		dl = self.download_url(download_source.url, hint = impl_hint, mirror_url = mirror)
		if download_source.size is not None:
			dl.expected_size = download_source.size + (download_source.start_offset or 0)
		# (else don't know sizes for mirrored archives)
		return (dl.downloaded, dl.tempfile)
예제 #8
0
	def download_archive(self, download_source, force = False, impl_hint = None):
		"""Fetch an archive. You should normally call L{download_impl}
		instead, since it handles other kinds of retrieval method too."""
		from zeroinstall.zerostore import unpack

		url = download_source.url
		if not (url.startswith('http:') or url.startswith('https:') or url.startswith('ftp:')):
			raise SafeException(_("Unknown scheme in download URL '%s'") % url)

		mime_type = download_source.type
		if not mime_type:
			mime_type = unpack.type_from_url(download_source.url)
		if not mime_type:
			raise SafeException(_("No 'type' attribute on archive, and I can't guess from the name (%s)") % download_source.url)
		unpack.check_type_ok(mime_type)
		dl = self.download_url(download_source.url, hint = impl_hint)
		dl.expected_size = download_source.size + (download_source.start_offset or 0)
		return (dl.downloaded, dl.tempfile)
예제 #9
0
    def download_archive(self, download_source, force=False, impl_hint=None):
        """Fetch an archive. You should normally call L{download_impl}
		instead, since it handles other kinds of retrieval method too."""
        from zeroinstall.zerostore import unpack

        url = download_source.url
        if not (url.startswith('http:') or url.startswith('https:')
                or url.startswith('ftp:')):
            raise SafeException(_("Unknown scheme in download URL '%s'") % url)

        mime_type = download_source.type
        if not mime_type:
            mime_type = unpack.type_from_url(download_source.url)
        if not mime_type:
            raise SafeException(
                _("No 'type' attribute on archive, and I can't guess from the name (%s)"
                  ) % download_source.url)
        unpack.check_type_ok(mime_type)
        dl = self.download_url(download_source.url, hint=impl_hint)
        dl.expected_size = download_source.size + (download_source.start_offset
                                                   or 0)
        return (dl.downloaded, dl.tempfile)
예제 #10
0
    def download_archive(self,
                         download_source,
                         force=False,
                         impl_hint=None,
                         may_use_mirror=False):
        """Fetch an archive. You should normally call L{download_impl}
		instead, since it handles other kinds of retrieval method too.
		It is the caller's responsibility to ensure that the returned stream is closed.
		If impl_hint is from a local feed and the url is relative, just opens the existing file for reading.
		@type download_source: L{model.DownloadSource}
		@type force: bool
		@type may_use_mirror: bool
		@rtype: (L{Blocker} | None, file)"""
        from zeroinstall.zerostore import unpack

        mime_type = download_source.type
        if not mime_type:
            mime_type = unpack.type_from_url(download_source.url)
        if not mime_type:
            raise SafeException(
                _("No 'type' attribute on archive, and I can't guess from the name (%s)"
                  ) % download_source.url)
        if not self.external_store:
            unpack.check_type_ok(mime_type)

        if '://' not in download_source.url:
            # Relative path
            if impl_hint is None or not impl_hint.feed.local_path:
                raise SafeException(
                    _("Relative URL '{url}' in non-local feed '{feed}'").
                    format(url=download_source.url, feed=impl_hint.feed))

            archive_file = os.path.join(
                os.path.dirname(impl_hint.feed.local_path),
                download_source.url)
            try:
                size = os.path.getsize(archive_file)
                if size != download_source.size:
                    raise SafeException(
                        _("Wrong size for {path}: feed says {expected}, but actually {actual} bytes"
                          ).format(path=archive_file,
                                   expected=download_source.size,
                                   actual=size))
                return (None, open(archive_file, 'rb'))
            except OSError as ex:
                raise SafeException(str(ex))  # (error already includes path)

        if may_use_mirror:
            mirror = self._get_archive_mirror(download_source)
        else:
            mirror = None

        if self.config.handler.dry_run:
            print(
                _("[dry-run] downloading archive {url}").format(
                    url=download_source.url))
        dl = self.download_url(download_source.url,
                               hint=impl_hint,
                               mirror_url=mirror)
        if download_source.size is not None:
            dl.expected_size = download_source.size + (
                download_source.start_offset or 0)
        # (else don't know sizes for mirrored archives)
        return (dl.downloaded, dl.tempfile)