summaryrefslogtreecommitdiffstats
path: root/nova/utils.py
diff options
context:
space:
mode:
authorjaypipes@gmail.com <>2010-10-27 10:51:37 -0400
committerjaypipes@gmail.com <>2010-10-27 10:51:37 -0400
commit24e19b43af5efe193bf28bed468e85ee57ce76df (patch)
tree76b9f1e5ed0453e10e2e2d1a4784afd56fd0b035 /nova/utils.py
parent198af0ef9e65bc4c2efe74b9d93cf40210eb77bc (diff)
parenteb82a8a7d8220adf31db3afb46849f24924ec973 (diff)
downloadnova-24e19b43af5efe193bf28bed468e85ee57ce76df.tar.gz
nova-24e19b43af5efe193bf28bed468e85ee57ce76df.tar.xz
nova-24e19b43af5efe193bf28bed468e85ee57ce76df.zip
Merge trunk and resolve conflicts
Diffstat (limited to 'nova/utils.py')
-rw-r--r--nova/utils.py55
1 files changed, 41 insertions, 14 deletions
diff --git a/nova/utils.py b/nova/utils.py
index 10b27ffec..bc495a691 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -28,6 +28,7 @@ import random
import subprocess
import socket
import sys
+from xml.sax import saxutils
from twisted.internet.threads import deferToThread
@@ -39,6 +40,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 +50,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 +60,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 +72,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 +88,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 +111,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 +120,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,
@@ -126,19 +132,16 @@ 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)
- else:
- characters = '01234567890abcdefghijklmnopqrstuvwxyz'
- choices = [random.choice(characters) for x in xrange(size)]
- return '%s-%s' % (topic, ''.join(choices))
+ characters = '01234567890abcdefghijklmnopqrstuvwxyz'
+ choices = [random.choice(characters) for x in xrange(size)]
+ return '%s-%s' % (topic, ''.join(choices))
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,7 +204,31 @@ class LazyPluggable(object):
backend = self.__get_backend()
return getattr(backend, key)
+
def deferredToThread(f):
def g(*args, **kwargs):
return deferToThread(f, *args, **kwargs)
return g
+
+
+def xhtml_escape(value):
+ """Escapes a string so it is valid within XML or XHTML.
+
+ Code is directly from the utf8 function in
+ http://github.com/facebook/tornado/blob/master/tornado/escape.py
+
+ """
+ return saxutils.escape(value, {'"': "&quot;"})
+
+
+def utf8(value):
+ """Try to turn a string into utf-8 if possible.
+
+ Code is directly from the utf8 function in
+ http://github.com/facebook/tornado/blob/master/tornado/escape.py
+
+ """
+ if isinstance(value, unicode):
+ return value.encode("utf-8")
+ assert isinstance(value, str)
+ return value