2013-03-18 05:06:52 +08:00
|
|
|
#
|
2013-10-28 04:59:47 +08:00
|
|
|
# Copyright 2006, 2013 Red Hat, Inc.
|
2013-03-18 05:06:52 +08:00
|
|
|
#
|
2018-04-04 21:35:41 +08:00
|
|
|
# This work is licensed under the GNU GPLv2 or later.
|
2018-03-21 03:00:02 +08:00
|
|
|
# See the COPYING file in the top-level directory.
|
2013-04-11 22:27:02 +08:00
|
|
|
#
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2020-07-18 06:46:54 +08:00
|
|
|
import os
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2020-07-18 06:58:00 +08:00
|
|
|
class DevError(RuntimeError):
|
|
|
|
def __init__(self, msg):
|
|
|
|
RuntimeError.__init__(self, "programming error: %s" % msg)
|
|
|
|
|
|
|
|
|
2013-04-11 22:27:02 +08:00
|
|
|
def listify(l):
|
|
|
|
if l is None:
|
|
|
|
return []
|
2017-10-11 19:35:41 +08:00
|
|
|
elif not isinstance(l, list):
|
2013-04-14 02:34:52 +08:00
|
|
|
return [l]
|
2013-04-11 22:27:02 +08:00
|
|
|
else:
|
|
|
|
return l
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-04-12 20:26:21 +08:00
|
|
|
def xml_escape(xml):
|
|
|
|
"""
|
|
|
|
Replaces chars ' " < > & with xml safe counterparts
|
|
|
|
"""
|
2019-06-10 04:39:15 +08:00
|
|
|
if xml:
|
|
|
|
xml = xml.replace("&", "&")
|
|
|
|
xml = xml.replace("'", "'")
|
|
|
|
xml = xml.replace("\"", """)
|
|
|
|
xml = xml.replace("<", "<")
|
|
|
|
xml = xml.replace(">", ">")
|
2013-04-12 20:26:21 +08:00
|
|
|
return xml
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
|
2019-05-11 00:54:30 +08:00
|
|
|
def get_prop_path(obj, prop_path):
|
|
|
|
"""Return value of attribute identified by `prop_path`
|
|
|
|
|
|
|
|
Look up the attribute of `obj` identified by `prop_path`
|
|
|
|
(separated by "."). If any component along the path is missing an
|
|
|
|
`AttributeError` is raised.
|
|
|
|
|
|
|
|
"""
|
|
|
|
parent = obj
|
|
|
|
pieces = prop_path.split(".")
|
|
|
|
for piece in pieces[:-1]:
|
|
|
|
parent = getattr(parent, piece)
|
|
|
|
|
|
|
|
return getattr(parent, pieces[-1])
|
|
|
|
|
|
|
|
|
|
|
|
def set_prop_path(obj, prop_path, value):
|
|
|
|
"""Set value of attribute identified by `prop_path`
|
|
|
|
|
|
|
|
Set the attribute of `obj` identified by `prop_path` (separated by
|
|
|
|
".") to `value`. If any component along the path is missing an
|
|
|
|
`AttributeError` is raised.
|
|
|
|
"""
|
|
|
|
parent = obj
|
|
|
|
pieces = prop_path.split(".")
|
|
|
|
for piece in pieces[:-1]:
|
|
|
|
parent = getattr(parent, piece)
|
|
|
|
|
|
|
|
return setattr(parent, pieces[-1], value)
|
2019-06-10 06:16:51 +08:00
|
|
|
|
|
|
|
|
2020-07-18 06:46:54 +08:00
|
|
|
def in_testsuite():
|
|
|
|
return "VIRTINST_TEST_SUITE" in os.environ
|
2020-09-05 20:29:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
def diff(origstr, newstr, fromfile="Original", tofile="New"):
|
|
|
|
import difflib
|
|
|
|
dlist = difflib.unified_diff(
|
|
|
|
origstr.splitlines(1), newstr.splitlines(1),
|
|
|
|
fromfile=fromfile, tofile=tofile)
|
|
|
|
return "".join(dlist)
|