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
|
#!/usr/bin/env python
import os, codecs, cPickle, sys
import textutil
def save(filePath, content="", encoding="utf_8"):
# Normalize
filePath = normalize(filePath)
# Create directory
directory(os.path.dirname(filePath))
# Writing file
try:
outputFile = codecs.open(filePath, encoding=encoding, mode="w", errors="replace")
outputFile.write(content)
except IOError, (errno, strerror):
print " * I/O error(%s): %s" % (errno, strerror)
sys.exit(1)
except UnicodeDecodeError:
print " * Could not decode result to %s" % encoding
sys.exit(1)
except:
print " * Unexpected error:", sys.exc_info()[0]
sys.exit(1)
outputFile.flush()
outputFile.close()
def directory(dirname):
# Normalize
dirname = normalize(dirname)
# Check/Create directory
if dirname != "" and not os.path.exists(dirname):
os.makedirs(dirname)
def normalize(filename):
return os.path.normcase(os.path.normpath(filename))
def read(filePath, encoding="utf_8"):
try:
ref = codecs.open(filePath, encoding=encoding, mode="r")
content = ref.read()
ref.close()
return textutil.any2Unix(unicode(content))
except IOError, (errno, strerror):
print " * I/O error(%s): %s" % (errno, strerror)
sys.exit(1)
except ValueError:
print " * Invalid Encoding. Required encoding %s in %s" % (encoding, filePath)
sys.exit(1)
except:
print " * Unexpected error:", sys.exc_info()[0]
sys.exit(1)
def storeCache(cachePath, data):
try:
cPickle.dump(data, open(cachePath, 'w'), 2)
except EOFError or PickleError or PicklingError:
print " * Could not store cache to %s" % cachePath
sys.exit(1)
def readCache(cachePath):
try:
return cPickle.load(open(cachePath))
except EOFError or PickleError or UnpicklingError:
print " * Could not read cache from %s" % cachePath
sys.exit(1)
def checkCache(filePath, cachePath, internalModTime):
fileModTime = os.stat(filePath).st_mtime
try:
cacheModTime = os.stat(cachePath).st_mtime
except OSError:
cacheModTime = 0
if internalModTime > cacheModTime:
# print "Invalid cache: %s" % filePath
# print "%s > %s" % (internalModTime, cacheModTime)
return True
return fileModTime > cacheModTime
|