-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmakerUtilities.py
executable file
·110 lines (101 loc) · 3.03 KB
/
makerUtilities.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import sys
import os
import shutil
import time
import cPickle
def readFile(pathToFile, asLines=False, lineRange=[], binary=False):
conts = ''
if asLines: conts = []
if not os.path.exists(pathToFile): return conts
flag = 'r'
if binary: flag = 'rb'
handle = None
try:
handle = open(pathToFile, flag)
if asLines:
conts = handle.readlines()
if lineRange:
start = lineRange[0]
try:
stop = lineRange[1]
conts = conts[start:stop]
except:
if not conts:
conts = ''
else:
conts = conts[start]
else:
conts = handle.read()
finally:
if handle: handle.close()
return conts
def writeFile(pathToFile, fileConts, asLines=False, binary=False, append=False):
flag = 'w'
if append: flag = 'a'
if binary: flag += 'b'
handle = None
try:
handle = open(pathToFile, flag)
if asLines:
handle.writelines(fileConts)
else:
handle.write(fileConts)
finally:
if handle: handle.close()
def copyFileTree(src, dst, ignore = [], callback = None, *args):
"""
is copying a tree of files one file at a time
callback is a function to be called after each file
e.g. to indicate progress, the callback function has to
accept a string as argument
"""
if not src.endswith("/"):
src += "/"
if not os.path.isdir(dst):
os.mkdir(dst)
tree = os.walk(src)
for item in tree:
try:
# remove the leading dirs from the src path
aditionalPath = item[0].replace(src, "")
if callback:
callback("copying: " + aditionalPath)
if aditionalPath != "":
os.mkdir(os.path.join(dst, aditionalPath))
except Exception, e:
print "\nUnable to create dir: ", str(e)
raise e
for file in item[2]:
if callback:
callback(file)
try:
if file not in ignore:
shutil.copyfile(os.path.join(item[0], file),
os.path.join(dst, aditionalPath, file))
except Exception, e:
print "\ncopy failed: ", str(e)
raise e
def verifyLatinChars(string):
""" returns True if string contains only Latin chars"""
try:
new = string.encode("latin-1")
return True
except:
return False
def readDataFromFile(fileName):
"""
read serialized data from a file
"""
bFile = open(fileName, "rb")
data = cPickle.load(bFile)
finalData = cPickle.loads(data)
bFile.close()
return finalData
def writeDataToFile(data, fileName):
"""
write serialized data to a file
"""
bytes = cPickle.dumps(data, 2)
bFile = open(fileName, "wb")
cPickle.dump(bytes, bFile, 2)
bFile.close()