Esempio n. 1
0
File: misc.py Progetto: zde/dnf
def checksum(sumtype, file, CHUNK=2**16, datasize=None):
    """takes filename, hand back Checksum of it
       sumtype = md5 or sha/sha1/sha256/sha512 (note sha == sha1)
       filename = /path/to/file
       CHUNK=65536 by default"""

    # chunking brazenly lifted from Ryan Tomayko
    try:
        if not isinstance(file, basestring):
            fo = file # assume it's a file-like-object
        else:
            fo = open(file, 'rb', CHUNK)

        data = Checksums([sumtype])
        while data.read(fo, CHUNK):
            if datasize is not None and data.length > datasize:
                break

        if is_py2str_py3bytes(file):
            fo.close()
            del fo

        # This screws up the length, but that shouldn't matter. We only care
        # if this checksum == what we expect.
        if datasize is not None and datasize != data.length:
            return '!%u!%s' % (datasize, data.hexdigest(sumtype))

        return data.hexdigest(sumtype)
    except (IOError, OSError) as e:
        raise MiscError('Error opening file for checksum: %s' % file)
Esempio n. 2
0
File: i18n.py Progetto: auchytil/dnf
def to_unicode(obj, encoding='utf-8', errors='replace'):
    """ Convert string to unicode.

        Consider using the more general dnf.i18n.ucd().
    """
    if is_py2str_py3bytes(obj):
        obj = unicode(obj, encoding, errors)
    return obj