author Nathan Froyd <>
Thu, 11 Apr 2013 16:57:54 -0400
changeset 128513 ebcfc07b631511d2a5c9bd52abbe2ff76428a10f
parent 123442 8f83edc05fa4d458d82668a95853c907544d0b8d
permissions -rw-r--r--
Bug 860860 - followup: fix json.dump call and be more explicit about object ordering; r=gps It turns out the simple s/simplejson/json/ didn't work because build_tooltool_manifest used item_sort_key to ensure that the objects read in were written out in the same order. This scheme was brittle, since it only understand a couple of keys from the manifest file, and it doesn't work with the stdlib json. Instead, use object_pairs_hook=collections.OrderedDict with json.load, which ensures that current objects get written out in the same order they were read in *and* that any future keys will be handled automagically. DONTBUILD because NPOTB

# >>sys.stderr,  This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at

'''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 as 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 as 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 as 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("{0} exists but stat() failed: {1}"
               .format(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("{0} has been locked for more than "
               "{1} seconds (PID {2})".format(lockfile, max_wait, pid))
    # 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")
  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 given 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):