-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzzzutils.py
66 lines (54 loc) · 2.08 KB
/
zzzutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys
import os
import shutil
import hashlib
def query_yes_no(question):
valid = {"yes": True, "y": True, "no": False, "n": False}
while True:
sys.stdout.write(question + " [y/n] ")
choice = input().lower()
if choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' " + "(or 'y' or 'n').\n")
def copyFile(src, dst, buffer_size=10485760, perserveFileDate=True):
'''
Copies a file to a new location. Much faster performance than Apache Commons due to use of larger buffer
@param src: Source File
@param dst: Destination File (not file path)
@param buffer_size: Buffer size to use during copy
@param perserveFileDate: Preserve the original file date
'''
# Check to make sure destination directory exists. If it doesn't create the directory
# dstParent, dstFileName = os.path.split(dst)
# if(not(os.path.exists(dstParent))):
# os.makedirs(dstParent)
# Optimize the buffer for small files
buffer_size = min(buffer_size, os.path.getsize(src))
if (buffer_size == 0):
buffer_size = 1024
if shutil._samefile(src, dst):
raise shutil.Error("`%s` and `%s` are the same file" % (src, dst))
for fn in [src, dst]:
try:
st = os.stat(fn)
except OSError:
# File most likely does not exist
pass
else:
# XXX What about other special files? (sockets, devices...)
if shutil.stat.S_ISFIFO(st.st_mode):
raise shutil.SpecialFileError("`%s` is a named pipe" % fn)
with open(src, 'rb') as fsrc:
with open(dst, 'wb') as fdst:
shutil.copyfileobj(fsrc, fdst, buffer_size)
if (perserveFileDate):
shutil.copystat(src, dst)
def hashfile(afile, hasher, blocksize=65536):
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
return hasher.hexdigest()
def calc_hash_md5(file_name):
return hashfile(open(file_name, 'rb'), hashlib.md5())