Mercurial > treepkg > treepkg
comparison treepkg/util.py @ 458:61e72399914c
Use an exception to use either hashlib or md5
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Tue, 24 Aug 2010 15:41:48 +0000 |
parents | b90bb9f2bb77 |
children | 454967511f5c |
comparison
equal
deleted
inserted
replaced
457:b90bb9f2bb77 | 458:61e72399914c |
---|---|
12 import tempfile | 12 import tempfile |
13 import shutil | 13 import shutil |
14 import fnmatch | 14 import fnmatch |
15 import pwd | 15 import pwd |
16 import os.path | 16 import os.path |
17 import sys | 17 try: |
18 if sys.version_info < (2, 5): | 18 from hashlib import md5 as new_md5 |
19 import md5 | 19 except ImportError: |
20 else: | 20 # fall back to md5 for Python versions before 2.5 |
21 import hashlib | 21 from md5 import new as new_md5 |
22 | |
23 import run | 22 import run |
24 | 23 |
25 | 24 |
26 def import_dotted_name(dotted_name): | 25 def import_dotted_name(dotted_name): |
27 module = __import__(dotted_name) | 26 module = __import__(dotted_name) |
167 """ calculates the md5sum of a file """ | 166 """ calculates the md5sum of a file """ |
168 if not os.path.isfile(filename): | 167 if not os.path.isfile(filename): |
169 raise RuntimeError("Could not create md5sum. File not found: %s" | 168 raise RuntimeError("Could not create md5sum. File not found: %s" |
170 % filename) | 169 % filename) |
171 f = file(filename, 'rb') | 170 f = file(filename, 'rb') |
172 if sys.version_info < (2, 5): | 171 m = new_md5() |
173 m = md5.new() | |
174 else: | |
175 m = hashlib.md5() | |
176 while True: | 172 while True: |
177 d = f.read(8096) | 173 d = f.read(8096) |
178 if not d: | 174 if not d: |
179 break | 175 break |
180 m.update(d) | 176 m.update(d) |