示例#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
示例#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
示例#3
0
文件: editor.py 项目: Tayyib/uludag
 def tools_fetch(self):
     p = re.compile("<Archive(.*)>(.*)</Archive>")
     data = unicode(self.spec_ed.text())
     m = p.search(data)
     if not m or m.groups()[1] == "":
         QMessageBox.warning(self, "Fetch error", "Archive URI is not specified")
         return
     uri = pisi.uri.URI(m.groups()[1])
     fname = os.path.join(pisi.context.config.archives_dir(), uri.filename())
     if not os.access(fname, os.R_OK):
         try:
             fetch_url(uri, pisi.context.config.archives_dir())
         except:
             QMessageBox.warning(self, "Fetch error", "Cannot fetch URI")
             return
     f = file(fname)
     s = sha.new(f.read())
     digest = s.hexdigest()
     f.close()
     p2 = re.compile("sha1sum=\"(.*)\"")
     p3 = re.compile("sha1sum='(.*)'")
     m2 = p2.search(data, m.start(1), m.end(1))
     m3 = p3.search(data, m.start(1), m.end(1))
     if m2:
         data = data[:m2.start(1)] + digest + data[m2.end(1):]
     elif m3:
         data = data[:m3.start(1)] + digest + data[m3.end(1):]
     else:
         data = data[:m.end(1)] + " sha1sum='" + digest + "'" + data[m.end(1):]
     self.spec_ed.setText(data)
示例#4
0
文件: file.py 项目: aligulle1/inary
    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
示例#5
0
文件: file.py 项目: Pardus-Linux/pisi
    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