~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to mozilla/config/utils.py

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/.
 
4
 
 
5
'''Utility methods to be used by python build infrastructure.
 
6
'''
 
7
 
 
8
import os
 
9
import errno
 
10
import sys
 
11
import time
 
12
import stat
 
13
 
 
14
class LockFile(object):
 
15
  '''LockFile is used by the lockFile method to hold the lock.
 
16
 
 
17
  This object should not be used directly, but only through
 
18
  the lockFile method below.
 
19
  '''
 
20
  def __init__(self, lockfile):
 
21
    self.lockfile = lockfile
 
22
  def __del__(self):
 
23
    while True:
 
24
      try:
 
25
        os.remove(self.lockfile)
 
26
        break
 
27
      except OSError, e:
 
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)
 
32
          time.sleep(0.1)
 
33
        else:
 
34
          # re-raise unknown errors
 
35
          raise
 
36
 
 
37
def lockFile(lockfile, max_wait = 600):
 
38
  '''Create and hold a lockfile of the given name, with the given timeout.
 
39
 
 
40
  To release the lock, delete the returned object.
 
41
  '''
 
42
  while True:
 
43
    try:
 
44
      fd = os.open(lockfile, os.O_EXCL | os.O_RDWR | os.O_CREAT)
 
45
      # we created the lockfile, so we're the owner
 
46
      break
 
47
    except OSError, e:
 
48
      if e.errno == errno.EEXIST or \
 
49
         (sys.platform == "win32" and e.errno == errno.EACCES):
 
50
        pass
 
51
      else:
 
52
        # should not occur
 
53
        raise
 
54
  
 
55
    try:
 
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")
 
59
      s = os.stat(lockfile)
 
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
 
64
        continue
 
65
      sys.exit("%s exists but stat() failed: %s" %
 
66
               (lockfile, e.strerror))
 
67
  
 
68
    # we didn't create the lockfile and it's still there, check
 
69
    # its age
 
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,
 
75
                                        pid))
 
76
  
 
77
    # it's not been locked too long, wait a while and retry
 
78
    f.close()
 
79
    time.sleep(1)
 
80
  
 
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
 
83
  
 
84
  f = os.fdopen(fd, "w")
 
85
  f.write("%d\n" % os.getpid())
 
86
  f.close()
 
87
  return LockFile(lockfile)
 
88
 
 
89
class pushback_iter(object):
 
90
  '''Utility iterator that can deal with pushed back elements.
 
91
 
 
92
  This behaves like a regular iterable, just that you can call
 
93
    iter.pushback(item)
 
94
  to get the givem item as next item in the iteration.
 
95
  '''
 
96
  def __init__(self, iterable):
 
97
    self.it = iter(iterable)
 
98
    self.pushed_back = []
 
99
 
 
100
  def __iter__(self):
 
101
    return self
 
102
 
 
103
  def __nonzero__(self):
 
104
    if self.pushed_back:
 
105
      return True
 
106
 
 
107
    try:
 
108
      self.pushed_back.insert(0, self.it.next())
 
109
    except StopIteration:
 
110
      return False
 
111
    else:
 
112
      return True
 
113
 
 
114
  def next(self):
 
115
    if self.pushed_back:
 
116
      return self.pushed_back.pop()
 
117
    return self.it.next()
 
118
 
 
119
  def pushback(self, item):
 
120
    self.pushed_back.append(item)