author Kenny Heaton <>
Tue, 03 Jan 2012 08:53:39 -0800
changeset 85021 8c43976e73a452516bd687583e7e4a5fb74855f3
parent 51793 d2800a98321ed4564f080f60f0518a30ed665e49
child 96742 f4157e8c410708d76703f19e4dfb61859bfe32d8
permissions -rw-r--r--
Bug 690552. ScratchPad should display exceptions as comments, just as it does for results. r=felipe,robcee

# Version: MPL 1.1/GPL 2.0/LGPL 2.1
# for the specific language governing rights and limitations under the
# License.
# The Original Code is Mozilla build system.
# The Initial Developer of the Original Code is
# Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2008
# the Initial Developer. All Rights Reserved.
# Contributor(s):
#  Axel Hecht <>

'''Utility methods to be used by python build infrastructure.

import os
import errno
import sys
import time
import stat

class LockFile(object):
  '''LockFile is used by the lockFile method to hold the lock.

  This object should not be used directly, but only through
  the lockFile method below.
  def __init__(self, lockfile):
    self.lockfile = lockfile
  def __del__(self):
    while True:
      except OSError, e:
        if e.errno == errno.EACCES:
          # another process probably has the file open, we'll retry.
          # just a short sleep since we want to drop the lock ASAP
          # (but we need to let some other process close the file first)
          # re-raise unknown errors

def lockFile(lockfile, max_wait = 600):
  '''Create and hold a lockfile of the given name, with the given timeout.

  To release the lock, delete the returned object.
  while True:
      fd =, os.O_EXCL | os.O_RDWR | os.O_CREAT)
      # we created the lockfile, so we're the owner
    except OSError, e:
      if e.errno == errno.EEXIST or \
         (sys.platform == "win32" and e.errno == errno.EACCES):
        # should not occur
      # the lock file exists, try to stat it to get its age
      # and read its contents to report the owner PID
      f = open(lockfile, "r")
      s = os.stat(lockfile)
    except EnvironmentError, e:
      if e.errno == errno.ENOENT or e.errno == errno.EACCES:
        # we didn't create the lockfile, so it did exist, but it's
        # gone now. Just try again
      sys.exit("%s exists but stat() failed: %s" %
               (lockfile, e.strerror))
    # we didn't create the lockfile and it's still there, check
    # its age
    now = int(time.time())
    if now - s[stat.ST_MTIME] > max_wait:
      pid = f.readline().rstrip()
      sys.exit("%s has been locked for more than " \
               "%d seconds (PID %s)" % (lockfile, max_wait,
    # it's not been locked too long, wait a while and retry
  # if we get here. we have the lockfile. Convert the file
  # descriptor into a Python file object and record our PID in it
  f = os.fdopen(fd, "w")
  f.write("%d\n" % os.getpid())
  return LockFile(lockfile)

class pushback_iter(object):
  '''Utility iterator that can deal with pushed back elements.

  This behaves like a regular iterable, just that you can call
  to get the givem item as next item in the iteration.
  def __init__(self, iterable): = iter(iterable)
    self.pushed_back = []

  def __iter__(self):
    return self

  def __nonzero__(self):
    if self.pushed_back:
      return True

    except StopIteration:
      return False
      return True

  def next(self):
    if self.pushed_back:
      return self.pushed_back.pop()

  def pushback(self, item):