PathNameGenerator.py 902 Bytes
Newer Older
1 2
import hashlib

3

4
class PathNameGenerator:
5 6
    """
    Ensures that actual path lengths do not exceed the UNIX 255 chars.
7

8 9 10 11
    Directory names that are longer than 255 chars will be shortened to a sha
    hash. The mapping can later be written to stdout or file to have access to
    the original filename.
    """
12

13
    def __init__(self):
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
        self.mapping = {}
        self.max_path_length = 160

    def __str__(self):
        mapping = ""
        for hash_value, dir_name in self.mapping.items():
            mapping += hash_value + " => " + dir_name + "\n"

        return mapping

    def compress(self, path_name):
        if len(path_name) < self.max_path_length:
            return path_name

        h = hashlib.new('ripemd160')
ext-neveu_n's avatar
ext-neveu_n committed
29
        h.update(path_name.encode('utf-8'))
30 31 32 33 34
        path_name_hex = h.hexdigest()

        self.mapping[path_name_hex] = path_name

        return path_name_hex