summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.5-sdk/frontend/framework/tool/modules/filetool.py
blob: 0cf23612b28f9072cbf5cd88f4d649b1e57ba7e3 (plain)
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
111
112
113
114
#!/usr/bin/env python
################################################################################
#
#  qooxdoo - the new era of web development
#
#  http://qooxdoo.org
#
#  Copyright:
#    2006-2007 1&1 Internet AG, Germany, http://www.1and1.org
#
#  License:
#    LGPL: http://www.gnu.org/licenses/lgpl.html
#    EPL: http://www.eclipse.org/org/documents/epl-v10.php
#    See the LICENSE file in the project's top-level directory for details.
#
#  Authors:
#    * Sebastian Werner (wpbasti)
#
################################################################################

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, 'wb'), 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, 'rb'))

  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