1
# This Source Code Form is subject to the terms of the Mozilla Public
2
# License, v. 2.0. If a copy of the MPL was not distributed with this
3
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
'''Utility methods to be used by python build infrastructure.
14
class LockFile(object):
15
'''LockFile is used by the lockFile method to hold the lock.
17
This object should not be used directly, but only through
18
the lockFile method below.
20
def __init__(self, lockfile):
21
self.lockfile = lockfile
25
os.remove(self.lockfile)
28
if e.errno == errno.EACCES:
29
# another process probably has the file open, we'll retry.
30
# just a short sleep since we want to drop the lock ASAP
31
# (but we need to let some other process close the file first)
34
# re-raise unknown errors
37
def lockFile(lockfile, max_wait = 600):
38
'''Create and hold a lockfile of the given name, with the given timeout.
40
To release the lock, delete the returned object.
44
fd = os.open(lockfile, os.O_EXCL | os.O_RDWR | os.O_CREAT)
45
# we created the lockfile, so we're the owner
48
if e.errno == errno.EEXIST or \
49
(sys.platform == "win32" and e.errno == errno.EACCES):
56
# the lock file exists, try to stat it to get its age
57
# and read its contents to report the owner PID
58
f = open(lockfile, "r")
60
except EnvironmentError, e:
61
if e.errno == errno.ENOENT or e.errno == errno.EACCES:
62
# we didn't create the lockfile, so it did exist, but it's
63
# gone now. Just try again
65
sys.exit("%s exists but stat() failed: %s" %
66
(lockfile, e.strerror))
68
# we didn't create the lockfile and it's still there, check
70
now = int(time.time())
71
if now - s[stat.ST_MTIME] > max_wait:
72
pid = f.readline().rstrip()
73
sys.exit("%s has been locked for more than " \
74
"%d seconds (PID %s)" % (lockfile, max_wait,
77
# it's not been locked too long, wait a while and retry
81
# if we get here. we have the lockfile. Convert the os.open file
82
# descriptor into a Python file object and record our PID in it
84
f = os.fdopen(fd, "w")
85
f.write("%d\n" % os.getpid())
87
return LockFile(lockfile)
89
class pushback_iter(object):
90
'''Utility iterator that can deal with pushed back elements.
92
This behaves like a regular iterable, just that you can call
94
to get the givem item as next item in the iteration.
96
def __init__(self, iterable):
97
self.it = iter(iterable)
103
def __nonzero__(self):
108
self.pushed_back.insert(0, self.it.next())
109
except StopIteration:
116
return self.pushed_back.pop()
117
return self.it.next()
119
def pushback(self, item):
120
self.pushed_back.append(item)