-
Notifications
You must be signed in to change notification settings - Fork 12
/
common.py
76 lines (61 loc) · 1.79 KB
/
common.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
#!/usr/bin/python
"""common.py: Common utility functions for TopOSM"""
import os, time, sys
from os import path
from threading import Lock
from env import *
__author__ = "Lars Ahlzen"
__copyright__ = "(c) Lars Ahlzen 2008-2011"
__license__ = "GPLv2"
class ConsoleManager:
lock = Lock()
def printMessage(self, message):
ConsoleManager.lock.acquire()
print message
ConsoleManager.lock.release()
def debugMessage(self, message):
if TOPOSM_DEBUG:
self.printMessage(message)
class ErrorLog:
lock = Lock()
def log(self, message, exception = None):
timestr = time.strftime('[%Y-%m-%d %H:%M:%S]')
ErrorLog.lock.acquire()
try:
file = open(ERRORLOG, 'a')
file.write("%s %s (%s)\n" % (timestr, message, str(exception)))
file.close()
except:
print "Failed to write to the error log:"
print "%s %s" % (sys.exc_info()[0], sys.exc_info()[1])
finally:
ErrorLog.lock.release()
console = ConsoleManager()
errorLog = ErrorLog()
# global locking object for file system ops (e.g. creating directories)
fslock = Lock()
def ensureDirExists(path):
fslock.acquire()
try:
if not os.path.isdir(path):
os.makedirs(path)
finally:
fslock.release()
def tryRemove(filename):
fslock.acquire()
try:
if path.isfile(filename):
os.remove(filename)
finally:
fslock.release()
def writeEmpty(filename):
"Overwrites the specified filename with a new empty file."
fslock.acquire()
try:
open(filename, 'w').close();
finally:
fslock.release()
def runSql(sql):
command = "psql -d %s -q -c \"%s\"" % (DATABASE, sql)
print command
os.system(command)