| 944 | |
| 945 | |
| 946 | def compute_checksum(filename, hashtype): |
| 947 | file = fs_encode(filename) |
| 948 | |
| 949 | if not exists(file): |
| 950 | return None |
| 951 | |
| 952 | buf = fsbsize(filename) |
| 953 | |
| 954 | if hashtype in ("adler32", "crc32"): |
| 955 | hf = getattr(zlib, hashtype) |
| 956 | last = 0 |
| 957 | |
| 958 | with open(file, "rb") as f: |
| 959 | for chunk in iter(lambda: f.read(buf), ''): |
| 960 | last = hf(chunk, last) |
| 961 | |
| 962 | return "%x" % last |
| 963 | |
| 964 | elif hashtype in hashlib.algorithms_available: |
| 965 | h = hashlib.new(hashtype) |
| 966 | |
| 967 | with open(file, "rb") as f: |
| 968 | for chunk in iter(lambda: f.read(buf * h.block_size), ''): |
| 969 | h.update(chunk) |
| 970 | |
| 971 | return h.hexdigest() |
| 972 | |
| 973 | else: |
| 974 | return None |
| 975 | |
| 976 | |
| 977 | def copy_tree(src, dst, overwrite=False, preserve_metadata=False): |