2003-09-05 21:53:46 +00:00
|
|
|
## $Id$
|
|
|
|
|
|
|
|
import configxml
|
2010-01-11 06:10:17 +00:00
|
|
|
try:
|
|
|
|
# use new hashlib if available
|
|
|
|
from hashlib import md5
|
|
|
|
except:
|
|
|
|
import md5
|
|
|
|
import os, shutil, binascii, filecmp
|
2009-08-24 19:08:05 +00:00
|
|
|
|
2004-11-25 23:41:26 +00:00
|
|
|
# from http://www.plope.com/software/uuidgen/view
|
2004-11-25 23:33:17 +00:00
|
|
|
_urandomfd = None
|
|
|
|
def urandom(n):
|
|
|
|
"""urandom(n) -> str
|
|
|
|
|
|
|
|
Return a string of n random bytes suitable for cryptographic use.
|
|
|
|
|
|
|
|
"""
|
|
|
|
global _urandomfd
|
|
|
|
if _urandomfd is None:
|
|
|
|
try:
|
|
|
|
_urandomfd = os.open("/dev/urandom", os.O_RDONLY)
|
|
|
|
except:
|
|
|
|
_urandomfd = NotImplementedError
|
|
|
|
if _urandomfd is NotImplementedError:
|
|
|
|
raise NotImplementedError("/dev/urandom (or equivalent) not found")
|
|
|
|
bytes = ""
|
|
|
|
while len(bytes) < n:
|
|
|
|
bytes += os.read(_urandomfd, n - len(bytes))
|
|
|
|
return bytes
|
|
|
|
|
|
|
|
def make_uuid():
|
2004-11-25 23:41:26 +00:00
|
|
|
return binascii.hexlify(urandom(16))
|
2004-11-25 23:33:17 +00:00
|
|
|
|
2003-09-05 21:53:46 +00:00
|
|
|
def md5_file(path):
|
2010-11-23 22:16:24 +00:00
|
|
|
"""
|
|
|
|
Return a 16-digit MD5 hex digest of a file's contents
|
|
|
|
Read the file in chunks
|
|
|
|
"""
|
|
|
|
|
|
|
|
chunk = 8096
|
|
|
|
|
|
|
|
try:
|
|
|
|
checksum = md5()
|
|
|
|
except NameError:
|
|
|
|
checksum = md5.new()
|
|
|
|
|
|
|
|
fp = open(path, 'r')
|
|
|
|
while True:
|
|
|
|
buffer = fp.read(chunk)
|
|
|
|
if not buffer:
|
|
|
|
break
|
|
|
|
checksum.update(buffer)
|
|
|
|
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
return checksum
|
2003-09-05 21:53:46 +00:00
|
|
|
|
|
|
|
def file_size(path):
|
|
|
|
"""Return the size of a file"""
|
|
|
|
f = open(path)
|
|
|
|
f.seek(0,2)
|
|
|
|
return f.tell()
|
|
|
|
|
2003-10-03 05:53:28 +00:00
|
|
|
def query_yesno(str):
|
|
|
|
'''Query user; default Yes'''
|
|
|
|
print str, "[Y/n] ",
|
|
|
|
return not raw_input().strip().lower().startswith('n')
|
|
|
|
|
|
|
|
def query_noyes(str):
|
|
|
|
'''Query user; default No'''
|
|
|
|
print str, "[y/N] ",
|
|
|
|
return raw_input().strip().lower().startswith('y')
|
2005-09-29 23:18:33 +00:00
|
|
|
|
|
|
|
def get_output_file_path(filename):
|
|
|
|
""" Return the filename's path in the upload directory
|
|
|
|
Use this if you're developing a validator/assimilator in Python
|
|
|
|
"""
|
|
|
|
config = configxml.default_config()
|
|
|
|
fanout = long(config.config.uldl_dir_fanout)
|
|
|
|
s = md5.new(filename).hexdigest()[1:8]
|
|
|
|
x = long(s, 16)
|
|
|
|
return "%s/%x/%s" % (config.config.upload_dir, x % fanout, filename)
|