예제 #1
0
    def start(self):
        label = "Downloading"
        itercontent = self._request.iter_content(chunk_size=self.CHUNK_SIZE)
        f = open(self._destination, "wb")
        try:
            if app.is_disabled_progressbar() or self.get_size() == -1:
                click.echo("%s..." % label)
                for chunk in itercontent:
                    if chunk:
                        f.write(chunk)
            else:
                chunks = int(ceil(self.get_size() / float(self.CHUNK_SIZE)))
                with click.progressbar(length=chunks, label=label) as pb:
                    for _ in pb:
                        f.write(next(itercontent))
        except IOError as e:
            click.secho(
                "Error: Please read http://bit.ly/package-manager-ioerror",
                fg="red",
                err=True)
            raise e
        finally:
            f.close()
            self._request.close()

        if self.get_lmtime():
            self._preserve_filemtime(self.get_lmtime())
예제 #2
0
    def start(self):
        label = "Downloading"
        itercontent = self._request.iter_content(chunk_size=self.CHUNK_SIZE)
        f = open(self._destination, "wb")
        try:
            if app.is_disabled_progressbar() or self.get_size() == -1:
                click.echo("%s..." % label)
                for chunk in itercontent:
                    if chunk:
                        f.write(chunk)
            else:
                chunks = int(ceil(self.get_size() / float(self.CHUNK_SIZE)))
                with click.progressbar(length=chunks, label=label) as pb:
                    for _ in pb:
                        f.write(next(itercontent))
        except IOError as e:
            click.secho(
                "Error: Please read http://bit.ly/package-manager-ioerror",
                fg="red",
                err=True)
            raise e
        finally:
            f.close()
            self._request.close()

        if self.get_lmtime():
            self._preserve_filemtime(self.get_lmtime())
예제 #3
0
 def unpack(source_path, dest_dir):
     with_progress = not app.is_disabled_progressbar()
     try:
         with FileUnpacker(source_path) as fu:
             return fu.unpack(dest_dir, with_progress=with_progress)
     except IOError as e:
         if not with_progress:
             raise e
         with FileUnpacker(source_path) as fu:
             return fu.unpack(dest_dir, with_progress=False)
예제 #4
0
 def unpack(src, dst):
     with_progress = not app.is_disabled_progressbar()
     try:
         with FileUnpacker(src) as fu:
             return fu.unpack(dst, with_progress=with_progress)
     except IOError as e:
         if not with_progress:
             raise e
         with FileUnpacker(src) as fu:
             return fu.unpack(dst, with_progress=False)
예제 #5
0
 def unpack(source_path, dest_dir):
     with_progress = not app.is_disabled_progressbar()
     try:
         with FileUnpacker(source_path) as fu:
             return fu.unpack(dest_dir, with_progress=with_progress)
     except IOError as e:
         if not with_progress:
             raise e
         with FileUnpacker(source_path) as fu:
             return fu.unpack(dest_dir, with_progress=False)
예제 #6
0
 def start(self):
     if app.is_disabled_progressbar():
         click.echo("Unpacking...")
         for item in self._unpacker.get_items():
             self._unpacker.extract_item(item, self._dest_dir)
     else:
         with click.progressbar(self._unpacker.get_items(),
                                label="Unpacking") as pb:
             for item in pb:
                 self._unpacker.extract_item(item, self._dest_dir)
     return True
예제 #7
0
 def start(self):
     if app.is_disabled_progressbar():
         click.echo("Unpacking...")
         for item in self._unpacker.get_items():
             self._unpacker.extract_item(item, self._dest_dir)
     else:
         items = self._unpacker.get_items()
         with click.progressbar(items, label="Unpacking") as pb:
             for item in pb:
                 self._unpacker.extract_item(item, self._dest_dir)
     return True
예제 #8
0
    def download(self, url, dest_dir, sha1=None):
        cache_key_fname = app.ContentCache.key_from_args(url, "fname")
        cache_key_data = app.ContentCache.key_from_args(url, "data")
        if self.FILE_CACHE_VALID:
            with app.ContentCache() as cc:
                fname = str(cc.get(cache_key_fname))
                cache_path = cc.get_cache_path(cache_key_data)
                if fname and isfile(cache_path):
                    dst_path = join(dest_dir, fname)
                    shutil.copy(cache_path, dst_path)
                    click.echo("Using cache: %s" % cache_path)
                    return dst_path

        with_progress = not app.is_disabled_progressbar()
        try:
            fd = FileDownloader(url, dest_dir)
            fd.start(with_progress=with_progress)
        except IOError as e:
            raise_error = not with_progress
            if with_progress:
                try:
                    fd = FileDownloader(url, dest_dir)
                    fd.start(with_progress=False)
                except IOError:
                    raise_error = True
            if raise_error:
                click.secho(
                    "Error: Please read http://bit.ly/package-manager-ioerror",
                    fg="red",
                    err=True,
                )
                raise e

        if sha1:
            fd.verify(sha1)
        dst_path = fd.get_filepath()
        if (
            not self.FILE_CACHE_VALID
            or getsize(dst_path) > PkgInstallerMixin.FILE_CACHE_MAX_SIZE
        ):
            return dst_path

        with app.ContentCache() as cc:
            cc.set(cache_key_fname, basename(dst_path), self.FILE_CACHE_VALID)
            cc.set(cache_key_data, "DUMMY", self.FILE_CACHE_VALID)
            shutil.copy(dst_path, cc.get_cache_path(cache_key_data))
        return dst_path
예제 #9
0
    def start(self):
        itercontent = self._request.iter_content(chunk_size=self.CHUNK_SIZE)
        f = open(self._destination, "wb")
        chunks = int(ceil(self.get_size() / float(self.CHUNK_SIZE)))

        if app.is_disabled_progressbar():
            click.echo("Downloading...")
            for _ in range(0, chunks):
                f.write(next(itercontent))
        else:
            with click.progressbar(length=chunks, label="Downloading") as pb:
                for _ in pb:
                    f.write(next(itercontent))
        f.close()
        self._request.close()

        self._preserve_filemtime(self.get_lmtime())
예제 #10
0
    def download(self, url, dest_dir, sha1=None):
        cache_key_fname = app.ContentCache.key_from_args(url, "fname")
        cache_key_data = app.ContentCache.key_from_args(url, "data")
        if self.FILE_CACHE_VALID:
            with app.ContentCache() as cc:
                fname = cc.get(cache_key_fname)
                cache_path = cc.get_cache_path(cache_key_data)
                if fname and isfile(cache_path):
                    dst_path = join(dest_dir, fname)
                    shutil.copy(cache_path, dst_path)
                    return dst_path

        with_progress = not app.is_disabled_progressbar()
        try:
            fd = FileDownloader(url, dest_dir)
            fd.start(with_progress=with_progress)
        except IOError as e:
            raise_error = not with_progress
            if with_progress:
                try:
                    fd = FileDownloader(url, dest_dir)
                    fd.start(with_progress=False)
                except IOError:
                    raise_error = True
            if raise_error:
                click.secho(
                    "Error: Please read http://bit.ly/package-manager-ioerror",
                    fg="red",
                    err=True)
                raise e

        if sha1:
            fd.verify(sha1)
        dst_path = fd.get_filepath()
        if not self.FILE_CACHE_VALID or getsize(
                dst_path) > PkgInstallerMixin.FILE_CACHE_MAX_SIZE:
            return dst_path

        with app.ContentCache() as cc:
            cc.set(cache_key_fname, basename(dst_path), self.FILE_CACHE_VALID)
            cc.set(cache_key_data, "DUMMY", self.FILE_CACHE_VALID)
            shutil.copy(dst_path, cc.get_cache_path(cache_key_data))
        return dst_path
예제 #11
0
    def download(self, url, checksum=None, silent=False):
        dl_path = self.compute_download_path(url, checksum or "")
        if os.path.isfile(dl_path):
            self.set_download_utime(dl_path)
            return dl_path

        with_progress = not silent and not app.is_disabled_progressbar()
        tmp_fd, tmp_path = tempfile.mkstemp(dir=self.get_download_dir())
        try:
            with LockFile(dl_path):
                try:
                    fd = FileDownloader(url)
                    fd.set_destination(tmp_path)
                    fd.start(with_progress=with_progress, silent=silent)
                except IOError as e:
                    raise_error = not with_progress
                    if with_progress:
                        try:
                            fd = FileDownloader(url)
                            fd.set_destination(tmp_path)
                            fd.start(with_progress=False, silent=silent)
                        except IOError:
                            raise_error = True
                    if raise_error:
                        self.print_message(
                            "Error: Please read http://bit.ly/package-manager-ioerror",
                            fg="red",
                            err=True,
                        )
                        raise e
            if checksum:
                fd.verify(checksum)
            os.close(tmp_fd)
            os.rename(tmp_path, dl_path)
        finally:
            if os.path.isfile(tmp_path):
                os.close(tmp_fd)
                os.remove(tmp_path)

        assert os.path.isfile(dl_path)
        self.set_download_utime(dl_path)
        return dl_path