diff options
author | Derrell Lipman <derrell@samba.org> | 2007-01-03 20:17:37 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 14:36:09 -0500 |
commit | 626bb8efb0c825f332c937ffaaadc9b402079539 (patch) | |
tree | 1c95f69d157b24f64edff470143f5f55a09cfca6 /webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/filetool.py | |
parent | eeddcf8cc8eb655d7c40f1fd5f7fd422529f4f98 (diff) | |
download | samba-626bb8efb0c825f332c937ffaaadc9b402079539.tar.gz samba-626bb8efb0c825f332c937ffaaadc9b402079539.tar.bz2 samba-626bb8efb0c825f332c937ffaaadc9b402079539.zip |
r20517: re-add cleaned-up webapps
(This used to be commit 5a3d6ad0b7cf0ecf8b57b4088b19f7d4291c990b)
Diffstat (limited to 'webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/filetool.py')
-rwxr-xr-x | webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/filetool.py | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/filetool.py b/webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/filetool.py new file mode 100755 index 0000000000..089b33b966 --- /dev/null +++ b/webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/filetool.py @@ -0,0 +1,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 |