Esempio n. 1
0
    def download(self, uri, save_path, digest=None, digest_type='SHA256'):
        r_f = uri
        l_f = save_path
        valid = False
        if digest is not None:
            self.addDigest(l_f, digest, digest_type)

        if os.path.exists(l_f):
            #logger.w(_('Exists: %s') % l_f)
            # Checksum
            if digest is not None:
                if self.checksum(l_f, digest_type, silent=True):
                    valid = True
                else:
                    os.remove(l_f)
            else:
                valid = True

        if not valid:
            # Fetch
            logger.i(_('Start: download %s') % r_f)
            dl_dir, dl_fn = os.path.split(l_f)
            self._dl.fetch(r_f, dl_dir, dl_fn)
            if os.path.exists(l_f):
                logger.success(_('Sucess: download %s') % l_f)
                valid = True
            else:
                logger.failed(_('Failed: download %s') % l_f)

        return valid
Esempio n. 2
0
    def checksum(self, _file, _type='SHA256', silent=False):
        if _type not in self.digest_types:
            if not silent:
                logger.w(_('Digest(%s) can only be %s') % (_type, ' '.join(self.digest_types)))
            return False

        file_digests = self.digest_map.get(_file)
        if file_digests is None:
            if not silent:
                logger.w(_('Can not find file digest information %s') % _file)
            return False

        digest = file_digests.get(_type)
        if digest is None:
            if not silent:
                logger.w(_('Can not find %s digest from file %s') % (_type, _file))
            return False

        calc_digest = checksum.perform_checksum(_file, _type)[0]
        if digest != calc_digest:
            if not silent:
                logger.failed(_('Digest checksum failed'))
                logger.i(_('Origin digest:\t\t%s') % digest)
                logger.i(_('Calculate digest:\t%s') % calc_digest)
            return False

        if not silent:
            logger.success(_('Checksum valid: %s') % _file)
        return True