Example #1
0
    def download(uri,
                 transfer_dir="/tmp",
                 sha1sum=False,
                 compress=None,
                 sign=None,
                 copylocal=False):

        assert isinstance(uri, pisi.uri.URI)

        if sha1sum:
            sha1filename = File.download(
                pisi.uri.URI(uri.get_uri() + '.sha1sum'), transfer_dir)
            sha1f = file(sha1filename)
            newsha1 = sha1f.read().split("\n")[0]

        if uri.is_remote_file() or copylocal:
            localfile = pisi.util.join_path(transfer_dir, uri.filename())

            # TODO: code to use old .sha1sum file, is this a necessary optimization?
            #oldsha1fn = localfile + '.sha1sum'
            #if os.exists(oldsha1fn):
            #oldsha1 = file(oldsha1fn).readlines()[0]
            if sha1sum and os.path.exists(localfile):
                oldsha1 = pisi.util.sha1_file(localfile)
                if (newsha1 == oldsha1):
                    # early terminate, we already got it ;)
                    raise AlreadyHaveException(uri, localfile)

            if uri.is_remote_file():
                ctx.ui.info(_("Fetching %s") % uri.get_uri(), verbose=True)
                pisi.fetcher.fetch_url(uri, transfer_dir, ctx.ui.Progress)
            else:
                # copy to transfer dir,
                localfile = pisi.util.join_path(transfer_dir, uri.filename())
                ctx.ui.info(_("Copying %s to transfer dir") % uri.get_uri(),
                            verbose=True)
                shutil.copy(uri.get_uri(), transfer_dir)
        else:
            localfile = uri.get_uri()  #TODO: use a special function here?
            if not os.path.exists(localfile):
                raise IOError(_("File '%s' not found.") % localfile)
            if not os.access(localfile, os.W_OK):
                oldfn = localfile
                localfile = pisi.util.join_path(transfer_dir,
                                                os.path.basename(localfile))
                shutil.copy(oldfn, localfile)

        if sha1sum:
            if (pisi.util.sha1_file(localfile) != newsha1):
                raise Error(_("File integrity of %s compromised.") % uri)

        localfile = File.decompress(localfile, compress)

        return localfile
Example #2
0
    def download(uri, transfer_dir = "/tmp", sha1sum = False,
                 compress = None, sign = None, copylocal = False):

        assert isinstance(uri, pisi.uri.URI)

        pisi.util.ensure_dirs(transfer_dir)

        if sha1sum:
            sha1filename = File.download(pisi.uri.URI(uri.get_uri() + '.sha1sum'), transfer_dir)
            sha1f = file(sha1filename)
            newsha1 = sha1f.read().split("\n")[0]

        if uri.is_remote_file() or copylocal:
            localfile = pisi.util.join_path(transfer_dir, uri.filename())

            # TODO: code to use old .sha1sum file, is this a necessary optimization?
            #oldsha1fn = localfile + '.sha1sum'
            #if os.exists(oldsha1fn):
                #oldsha1 = file(oldsha1fn).readlines()[0]
            if sha1sum and os.path.exists(localfile):
                oldsha1 = pisi.util.sha1_file(localfile)
                if (newsha1 == oldsha1):
                    # early terminate, we already got it ;)
                    raise AlreadyHaveException(uri, localfile)

            if uri.is_remote_file():
                ctx.ui.info(_("Fetching %s") % uri.get_uri(), verbose=True)
                pisi.fetcher.fetch_url(uri, transfer_dir, ctx.ui.Progress)
            else:
                # copy to transfer dir,
                localfile = pisi.util.join_path(transfer_dir, uri.filename())
                ctx.ui.info(_("Copying %s to transfer dir") % uri.get_uri(), verbose=True)
                shutil.copy(uri.get_uri(), transfer_dir)
        else:
            localfile = uri.get_uri() #TODO: use a special function here?
            if not os.path.exists(localfile):
                raise IOError(_("File '%s' not found.") % localfile)
            if not os.access(localfile, os.W_OK):
                oldfn = localfile
                localfile = pisi.util.join_path(transfer_dir, os.path.basename(localfile))
                shutil.copy(oldfn, localfile)

        if sha1sum:
            if (pisi.util.sha1_file(localfile) != newsha1):
                raise Error(_("File integrity of %s compromised.") % uri)

        localfile = File.decompress(localfile, compress)

        return localfile
Example #3
0
    def __init__(self, uri, mode, transfer_dir = "/tmp",
                 sha1sum = False, compress = None, sign = None):
        "it is pointless to open a file without a URI and a mode"

        self.transfer_dir = transfer_dir
        self.sha1sum = sha1sum
        self.compress = compress
        self.sign = sign

        uri = File.make_uri(uri)
        if mode==File.read or mode==File.write:
            self.mode = mode
        else:
            raise Error(_("File mode must be either File.read or File.write"))
        if uri.is_remote_file():
            if self.mode == File.read:
                localfile = File.download(uri, transfer_dir, sha1sum, compress, sign)
            else:
                raise Error(_("Remote write not implemented"))
        else:
            localfile = uri.get_uri()
            if self.mode == File.read:
                localfile = File.decompress(localfile, self.compress)

        if self.mode == File.read:
            access = 'r'
        else:
            access = 'w'
        self.__file__ = file(localfile, access)
        self.localfile = localfile
Example #4
0
    def __init__(self,
                 uri,
                 mode,
                 transfer_dir="/tmp",
                 sha1sum=False,
                 compress=None,
                 sign=None):
        "it is pointless to open a file without a URI and a mode"

        self.transfer_dir = transfer_dir
        self.sha1sum = sha1sum
        self.compress = compress
        self.sign = sign

        uri = File.make_uri(uri)
        if mode == File.read or mode == File.write:
            self.mode = mode
        else:
            raise Error(_("File mode must be either File.read or File.write"))
        if uri.is_remote_file():
            if self.mode == File.read:
                localfile = File.download(uri, transfer_dir, sha1sum, compress,
                                          sign)
            else:
                raise Error(_("Remote write not implemented"))
        else:
            localfile = uri.get_uri()
            if self.mode == File.read:
                localfile = File.decompress(localfile, self.compress)

        if self.mode == File.read:
            access = 'r'
        else:
            access = 'w'
        self.__file__ = file(localfile, access)
        self.localfile = localfile
Example #5
0
    def download(uri,
                 transfer_dir="/tmp",
                 sha1sum=False,
                 compress=None,
                 sign=None,
                 copylocal=False):

        assert isinstance(uri, pisi.uri.URI)

        pisi.util.ensure_dirs(transfer_dir)

        # Check file integrity before saving?
        check_integrity = sha1sum or sign

        origfile = pisi.util.join_path(transfer_dir, uri.filename())

        if sha1sum:
            sha1filename = File.download(
                pisi.uri.URI(uri.get_uri() + '.sha1sum'), transfer_dir)
            sha1f = file(sha1filename)
            newsha1 = sha1f.read().split("\n")[0]

        if uri.is_remote_file() or copylocal:
            tmpfile = check_integrity and uri.filename(
            ) + ctx.const.temporary_suffix
            localfile = pisi.util.join_path(transfer_dir, tmpfile
                                            or uri.filename())

            # TODO: code to use old .sha1sum file, is this a necessary optimization?
            #oldsha1fn = localfile + '.sha1sum'
            #if os.exists(oldsha1fn):
            #oldsha1 = file(oldsha1fn).readlines()[0]
            if sha1sum and os.path.exists(origfile):
                oldsha1 = pisi.util.sha1_file(origfile)
                if (newsha1 == oldsha1):
                    # early terminate, we already got it ;)
                    raise AlreadyHaveException(uri, origfile)

            if uri.is_remote_file():
                ctx.ui.info(_("Fetching %s") % uri.get_uri(), verbose=True)
                pisi.fetcher.fetch_url(uri, transfer_dir, ctx.ui.Progress,
                                       tmpfile)
            else:
                # copy to transfer dir
                ctx.ui.info(_("Copying %s to transfer dir") % uri.get_uri(),
                            verbose=True)
                shutil.copy(uri.get_uri(), localfile)
        else:
            localfile = uri.get_uri()  #TODO: use a special function here?
            if not os.path.exists(localfile):
                raise IOError(_("File '%s' not found.") % localfile)
            if not os.access(localfile, os.W_OK):
                oldfn = localfile
                localfile = pisi.util.join_path(transfer_dir,
                                                os.path.basename(localfile))
                shutil.copy(oldfn, localfile)

        def clean_temporary():
            temp_files = []
            if sha1sum:
                temp_files.append(sha1filename)
            if check_integrity:
                temp_files.append(localfile)
            for filename in temp_files:
                try:
                    os.unlink(filename)
                except OSError:
                    pass

        if sha1sum:
            if (pisi.util.sha1_file(localfile) != newsha1):
                clean_temporary()
                raise Error(_("File integrity of %s compromised.") % uri)

        if check_integrity:
            shutil.move(localfile, origfile)
            localfile = origfile

        localfile = File.decompress(localfile, compress)

        return localfile
Example #6
0
    def download(uri, transfer_dir = "/tmp", sha1sum = False,
                 compress = None, sign = None, copylocal = False):

        assert isinstance(uri, pisi.uri.URI)

        pisi.util.ensure_dirs(transfer_dir)

        # Check file integrity before saving?
        check_integrity = sha1sum or sign

        origfile = pisi.util.join_path(transfer_dir, uri.filename())

        if sha1sum:
            sha1filename = File.download(pisi.uri.URI(uri.get_uri() + '.sha1sum'), transfer_dir)
            sha1f = file(sha1filename)
            newsha1 = sha1f.read().split("\n")[0]

        if uri.is_remote_file() or copylocal:
            tmpfile = check_integrity and uri.filename() + ctx.const.temporary_suffix
            localfile = pisi.util.join_path(transfer_dir, tmpfile or uri.filename())

            # TODO: code to use old .sha1sum file, is this a necessary optimization?
            #oldsha1fn = localfile + '.sha1sum'
            #if os.exists(oldsha1fn):
                #oldsha1 = file(oldsha1fn).readlines()[0]
            if sha1sum and os.path.exists(origfile):
                oldsha1 = pisi.util.sha1_file(origfile)
                if (newsha1 == oldsha1):
                    # early terminate, we already got it ;)
                    raise AlreadyHaveException(uri, origfile)

            if uri.is_remote_file():
                ctx.ui.info(_("Fetching %s") % uri.get_uri(), verbose=True)
                pisi.fetcher.fetch_url(uri, transfer_dir, ctx.ui.Progress, tmpfile)
            else:
                # copy to transfer dir
                ctx.ui.info(_("Copying %s to transfer dir") % uri.get_uri(), verbose=True)
                shutil.copy(uri.get_uri(), localfile)
        else:
            localfile = uri.get_uri() #TODO: use a special function here?
            if not os.path.exists(localfile):
                raise IOError(_("File '%s' not found.") % localfile)
            if not os.access(localfile, os.W_OK):
                oldfn = localfile
                localfile = pisi.util.join_path(transfer_dir, os.path.basename(localfile))
                shutil.copy(oldfn, localfile)

        def clean_temporary():
            temp_files = []
            if sha1sum:
                temp_files.append(sha1filename)
            if check_integrity:
                temp_files.append(localfile)
            for filename in temp_files:
                try:
                    os.unlink(filename)
                except OSError:
                    pass

        if sha1sum:
            if (pisi.util.sha1_file(localfile) != newsha1):
                clean_temporary()
                raise Error(_("File integrity of %s compromised.") % uri)

        if check_integrity:
            shutil.move(localfile, origfile)
            localfile = origfile

        localfile = File.decompress(localfile, compress)

        return localfile