~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/utils.py

  • Committer: Package Import Robot
  • Author(s): Adam Gandelman, Adam Gandelman, Chuck Short
  • Date: 2012-08-27 15:37:18 UTC
  • mfrom: (1.1.60)
  • Revision ID: package-import@ubuntu.com-20120827153718-lj8er44eqqz1gsrj
Tags: 2012.2~rc1~20120827.15815-0ubuntu1
[ Adam Gandelman ]
* New upstream release.

[ Chuck Short ]
* debian/patches/0001-Update-tools-hacking-for-pep8-1.2-and-
  beyond.patch: Dropped we dont run pep8 tests anymore.
* debian/control: Drop pep8 build depends
* debian/*.upstart.in: Make sure we transition correctly from runlevel
  1 to 2. (LP: #820694)

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
import struct
36
36
import sys
37
37
import tempfile
38
 
import threading
39
38
import time
40
39
import uuid
41
40
import weakref
42
41
from xml.sax import saxutils
43
42
 
44
 
from eventlet import corolocal
45
43
from eventlet import event
46
44
from eventlet.green import subprocess
47
45
from eventlet import greenthread
628
626
            self.unlock()
629
627
            self.lockfile.close()
630
628
        except IOError:
631
 
            LOG.exception(_("Could not release the aquired lock `%s`")
 
629
            LOG.exception(_("Could not release the acquired lock `%s`")
632
630
                             % self.fname)
633
631
 
634
632
    def trylock(self):
717
715
                    LOG.debug(_('Attempting to grab file lock "%(lock)s" for '
718
716
                                'method "%(method)s"...'),
719
717
                              {'lock': name, 'method': f.__name__})
720
 
                    lock_file_path = os.path.join(FLAGS.lock_path,
721
 
                                                  'nova-%s' % name)
 
718
                    lock_path = FLAGS.lock_path or tempfile.mkdtemp()
 
719
                    lock_file_path = os.path.join(lock_path, 'nova-%s' % name)
722
720
                    lock = InterProcessLock(lock_file_path)
723
 
                    with lock:
724
 
                        LOG.debug(_('Got file lock "%(lock)s" for '
725
 
                                    'method "%(method)s"...'),
726
 
                                  {'lock': name, 'method': f.__name__})
727
 
                        retval = f(*args, **kwargs)
 
721
                    try:
 
722
                        with lock:
 
723
                            LOG.debug(_('Got file lock "%(lock)s" for '
 
724
                                        'method "%(method)s"...'),
 
725
                                      {'lock': name, 'method': f.__name__})
 
726
                            retval = f(*args, **kwargs)
 
727
                    finally:
 
728
                        # NOTE(vish): This removes the tempdir if we needed
 
729
                        #             to create one. This is used to cleanup
 
730
                        #             the locks left behind by unit tests.
 
731
                        if not FLAGS.lock_path:
 
732
                            shutil.rmtree(lock_path)
728
733
                else:
729
734
                    retval = f(*args, **kwargs)
730
735
 
913
918
    try:
914
919
        return True if int(val) else False
915
920
    except ValueError:
916
 
        return val.lower() == 'true'
 
921
        return val.lower() == 'true' or \
 
922
               val.lower() == 'yes' or \
 
923
               val.lower() == 'y'
917
924
 
918
925
 
919
926
def is_valid_ipv4(address):
1221
1228
    return result == 0
1222
1229
 
1223
1230
 
1224
 
def sys_platform_translate(arch):
1225
 
    """Translate cpu architecture into supported platforms."""
1226
 
    if (arch[0] == 'i' and arch[1].isdigit() and arch[2:4] == '86'):
1227
 
        arch = 'i686'
1228
 
    elif arch.startswith('arm'):
1229
 
        arch = 'arm'
1230
 
    return arch
1231
 
 
1232
 
 
1233
1231
def walk_class_hierarchy(clazz, encountered=None):
1234
1232
    """Walk class hierarchy, yielding most derived classes first"""
1235
1233
    if not encountered:
1268
1266
                LOG.exception(msg, **kwargs)
1269
1267
 
1270
1268
            self._rollback()
 
1269
 
 
1270
 
 
1271
def ensure_tree(path):
 
1272
    """Create a directory (and any ancestor directories required)
 
1273
 
 
1274
    :param path: Directory to create
 
1275
    """
 
1276
    try:
 
1277
        os.makedirs(path)
 
1278
    except OSError as exc:
 
1279
        if exc.errno == errno.EEXIST:
 
1280
            if not os.path.isdir(path):
 
1281
                raise
 
1282
        else:
 
1283
            raise