summaryrefslogtreecommitdiffstats
path: root/nova/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'nova/utils.py')
-rw-r--r--nova/utils.py23
1 files changed, 15 insertions, 8 deletions
diff --git a/nova/utils.py b/nova/utils.py
index 10b27ffec..7683fc9f4 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -39,6 +39,7 @@ from nova.exception import ProcessExecutionError
FLAGS = flags.FLAGS
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
+
def import_class(import_str):
"""Returns a class from a string including module and class"""
mod_str, _sep, class_str = import_str.rpartition('.')
@@ -48,6 +49,7 @@ def import_class(import_str):
except (ImportError, ValueError, AttributeError):
raise exception.NotFound('Class %s cannot be found' % class_str)
+
def import_object(import_str):
"""Returns an object including a module or module and class"""
try:
@@ -57,6 +59,7 @@ def import_object(import_str):
cls = import_class(import_str)
return cls()
+
def fetchfile(url, target):
logging.debug("Fetching %s" % url)
# c = pycurl.Curl()
@@ -68,6 +71,7 @@ def fetchfile(url, target):
# fp.close()
execute("curl --fail %s -o %s" % (url, target))
+
def execute(cmd, process_input=None, addl_env=None, check_exit_code=True):
logging.debug("Running cmd: %s", cmd)
env = os.environ.copy()
@@ -83,7 +87,7 @@ def execute(cmd, process_input=None, addl_env=None, check_exit_code=True):
obj.stdin.close()
if obj.returncode:
logging.debug("Result was %s" % (obj.returncode))
- if check_exit_code and obj.returncode <> 0:
+ if check_exit_code and obj.returncode != 0:
(stdout, stderr) = result
raise ProcessExecutionError(exit_code=obj.returncode,
stdout=stdout,
@@ -106,7 +110,8 @@ def default_flagfile(filename='nova.conf'):
script_dir = os.path.dirname(inspect.stack()[-1][1])
filename = os.path.abspath(os.path.join(script_dir, filename))
if os.path.exists(filename):
- sys.argv = sys.argv[:1] + ['--flagfile=%s' % filename] + sys.argv[1:]
+ flagfile = ['--flagfile=%s' % filename]
+ sys.argv = sys.argv[:1] + flagfile + sys.argv[1:]
def debug(arg):
@@ -114,11 +119,11 @@ def debug(arg):
return arg
-def runthis(prompt, cmd, check_exit_code = True):
+def runthis(prompt, cmd, check_exit_code=True):
logging.debug("Running %s" % (cmd))
exit_code = subprocess.call(cmd.split(" "))
logging.debug(prompt % (exit_code))
- if check_exit_code and exit_code <> 0:
+ if check_exit_code and exit_code != 0:
raise ProcessExecutionError(exit_code=exit_code,
stdout=None,
stderr=None,
@@ -128,7 +133,7 @@ def runthis(prompt, cmd, check_exit_code = True):
def generate_uid(topic, size=8):
if topic == "i":
# Instances have integer internal ids.
- return random.randint(0, 2**32-1)
+ return random.randint(0, 2 ** 32 - 1)
else:
characters = '01234567890abcdefghijklmnopqrstuvwxyz'
choices = [random.choice(characters) for x in xrange(size)]
@@ -136,9 +141,10 @@ def generate_uid(topic, size=8):
def generate_mac():
- mac = [0x02, 0x16, 0x3e, random.randint(0x00, 0x7f),
- random.randint(0x00, 0xff), random.randint(0x00, 0xff)
- ]
+ mac = [0x02, 0x16, 0x3e,
+ random.randint(0x00, 0x7f),
+ random.randint(0x00, 0xff),
+ random.randint(0x00, 0xff)]
return ':'.join(map(lambda x: "%02x" % x, mac))
@@ -201,6 +207,7 @@ class LazyPluggable(object):
backend = self.__get_backend()
return getattr(backend, key)
+
def deferredToThread(f):
def g(*args, **kwargs):
return deferToThread(f, *args, **kwargs)