-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_util.py
46 lines (34 loc) · 1.19 KB
/
common_util.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
"""
common_util
Common snippets that all libraries make use of.
"""
import os, sys
import datetime
progname = os.path.basename(sys.argv[0])
verbose = False
def nowtime():
return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
def Babble(s):
if verbose:
print "%s (%s): %s" % (progname, nowtime(), s)
def Msg(s):
print "%s (%s): %s" % (progname, nowtime(), s)
def Warn(s):
print >>sys.stderr, "%s (%s) WARNING: %s" % (progname, nowtime(), s)
def Error(s):
print >>sys.stderr, "%s (%s) ERROR: %s" % (progname, nowtime(), s)
def Die(msg):
print >> sys.stderr, "%s (%s) FATAL ERROR: %s" % (progname, nowtime(), msg)
sys.exit(1)
def uniquePath(p):
"""Uniquify the file path given by <p>, i.e. try to ensure that <p> does not
already exist by adding digits if neccessary. Technically, this method has a
race condition, as other processes may interfere after the existence tests
have finished and the method returns. Does not create the file."""
path, name = os.path.split(p)
base, ext = os.path.splitext(name)
n = 0
while os.path.exists(p):
p = os.path.join(path, "%s.%d%s" % (base, n, ext))
n += 1
return p