mirror of
https://github.com/projectacrn/acrn-hypervisor.git
synced 2025-06-19 20:22:46 +00:00
acrn-config: change source file format to unix
The file format of 2 source files is dos format with line endings as CRLF, change to unix format with line endings as LF. Tracked-On: #3760 Signed-off-by: Shuang Zheng <shuang.zheng@intel.com> Reviewed-by: Victor Sun <victor.sun@intel.com>
This commit is contained in:
parent
9456d91b76
commit
bdf3a89e6d
@ -1,288 +1,288 @@
|
|||||||
# Copyright (C) 2019 Intel Corporation.
|
# Copyright (C) 2019 Intel Corporation.
|
||||||
# SPDX-License-Identifier: BSD-3-Clause
|
# SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
"""Controller for config app.
|
"""Controller for config app.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import xml.etree.ElementTree as ElementTree
|
import xml.etree.ElementTree as ElementTree
|
||||||
|
|
||||||
|
|
||||||
class XmlConfig:
|
class XmlConfig:
|
||||||
"""The core class to analyze and modify acrn config xml files"""
|
"""The core class to analyze and modify acrn config xml files"""
|
||||||
def __init__(self, path=None, default=True):
|
def __init__(self, path=None, default=True):
|
||||||
self._xml_path = path
|
self._xml_path = path
|
||||||
self._default = default
|
self._default = default
|
||||||
self._curr_xml = None
|
self._curr_xml = None
|
||||||
self._curr_xml_tree = None
|
self._curr_xml_tree = None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_xml_type(xml_file):
|
def _get_xml_type(xml_file):
|
||||||
"""
|
"""
|
||||||
get the config type by file.
|
get the config type by file.
|
||||||
:param xml_file: the file path of xml file.
|
:param xml_file: the file path of xml file.
|
||||||
:return: the xml type.
|
:return: the xml type.
|
||||||
:raises: ValueError, OSError, SyntaxError.
|
:raises: ValueError, OSError, SyntaxError.
|
||||||
"""
|
"""
|
||||||
xml_type = ''
|
xml_type = ''
|
||||||
if os.path.splitext(xml_file)[1] != '.xml':
|
if os.path.splitext(xml_file)[1] != '.xml':
|
||||||
return xml_type
|
return xml_type
|
||||||
try:
|
try:
|
||||||
tree = ElementTree.parse(xml_file)
|
tree = ElementTree.parse(xml_file)
|
||||||
root = tree.getroot()
|
root = tree.getroot()
|
||||||
if 'uos_launcher' in root.attrib:
|
if 'uos_launcher' in root.attrib:
|
||||||
xml_type = 'uos_launcher'
|
xml_type = 'uos_launcher'
|
||||||
elif 'scenario' in root.attrib:
|
elif 'scenario' in root.attrib:
|
||||||
xml_type = 'scenario'
|
xml_type = 'scenario'
|
||||||
elif 'board' in root.attrib:
|
elif 'board' in root.attrib:
|
||||||
xml_type = 'board'
|
xml_type = 'board'
|
||||||
elif 'board_setting' in root.attrib:
|
elif 'board_setting' in root.attrib:
|
||||||
xml_type = 'board_setting'
|
xml_type = 'board_setting'
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print('xml parse error: {}'.format(xml_file))
|
print('xml parse error: {}'.format(xml_file))
|
||||||
xml_type = ''
|
xml_type = ''
|
||||||
except OSError:
|
except OSError:
|
||||||
print('xml open error: {}'.format(xml_file))
|
print('xml open error: {}'.format(xml_file))
|
||||||
xml_type = ''
|
xml_type = ''
|
||||||
except SyntaxError:
|
except SyntaxError:
|
||||||
print('xml syntax error: {}'.format(xml_file))
|
print('xml syntax error: {}'.format(xml_file))
|
||||||
xml_type = ''
|
xml_type = ''
|
||||||
|
|
||||||
return xml_type
|
return xml_type
|
||||||
|
|
||||||
def list_all(self, xml_type=None):
|
def list_all(self, xml_type=None):
|
||||||
"""
|
"""
|
||||||
list all xml config files by type.
|
list all xml config files by type.
|
||||||
:param xml_type: the xml type.
|
:param xml_type: the xml type.
|
||||||
:return: he list of xml config files.
|
:return: he list of xml config files.
|
||||||
"""
|
"""
|
||||||
xmls = []
|
xmls = []
|
||||||
user_xmls = []
|
user_xmls = []
|
||||||
|
|
||||||
if self._xml_path is None or not os.path.exists(self._xml_path):
|
if self._xml_path is None or not os.path.exists(self._xml_path):
|
||||||
return xmls, user_xmls
|
return xmls, user_xmls
|
||||||
for test_file in os.listdir(self._xml_path):
|
for test_file in os.listdir(self._xml_path):
|
||||||
test_file_path = os.path.join(self._xml_path, test_file)
|
test_file_path = os.path.join(self._xml_path, test_file)
|
||||||
if os.path.isfile(test_file_path):
|
if os.path.isfile(test_file_path):
|
||||||
if XmlConfig._get_xml_type(test_file_path) == xml_type:
|
if XmlConfig._get_xml_type(test_file_path) == xml_type:
|
||||||
xmls.append(os.path.splitext(test_file)[0])
|
xmls.append(os.path.splitext(test_file)[0])
|
||||||
user_path = os.path.join(self._xml_path, 'user_defined')
|
user_path = os.path.join(self._xml_path, 'user_defined')
|
||||||
if os.path.isdir(user_path):
|
if os.path.isdir(user_path):
|
||||||
for test_file in os.listdir(user_path):
|
for test_file in os.listdir(user_path):
|
||||||
test_file_path = os.path.join(user_path, test_file)
|
test_file_path = os.path.join(user_path, test_file)
|
||||||
if os.path.isfile(test_file_path):
|
if os.path.isfile(test_file_path):
|
||||||
if XmlConfig._get_xml_type(test_file_path) == xml_type:
|
if XmlConfig._get_xml_type(test_file_path) == xml_type:
|
||||||
user_xmls.append(os.path.splitext(test_file)[0])
|
user_xmls.append(os.path.splitext(test_file)[0])
|
||||||
|
|
||||||
return xmls, user_xmls
|
return xmls, user_xmls
|
||||||
|
|
||||||
def set_curr(self, xml):
|
def set_curr(self, xml):
|
||||||
"""
|
"""
|
||||||
set current xml file to analyze.
|
set current xml file to analyze.
|
||||||
:param xml: the xml file.
|
:param xml: the xml file.
|
||||||
:return: None.
|
:return: None.
|
||||||
:raises: ValueError, OSError, SyntaxError.
|
:raises: ValueError, OSError, SyntaxError.
|
||||||
"""
|
"""
|
||||||
if self._xml_path is None:
|
if self._xml_path is None:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
self._curr_xml = xml
|
self._curr_xml = xml
|
||||||
|
|
||||||
xml_path = os.path.join(self._xml_path, self._curr_xml + '.xml') \
|
xml_path = os.path.join(self._xml_path, self._curr_xml + '.xml') \
|
||||||
if self._default \
|
if self._default \
|
||||||
else os.path.join(self._xml_path, 'user_defined', self._curr_xml + '.xml')
|
else os.path.join(self._xml_path, 'user_defined', self._curr_xml + '.xml')
|
||||||
|
|
||||||
tree = ElementTree.parse(xml_path)
|
tree = ElementTree.parse(xml_path)
|
||||||
self._curr_xml_tree = tree
|
self._curr_xml_tree = tree
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print('xml parse error: {}'.format(xml))
|
print('xml parse error: {}'.format(xml))
|
||||||
self._curr_xml = None
|
self._curr_xml = None
|
||||||
self._curr_xml_tree = None
|
self._curr_xml_tree = None
|
||||||
except OSError:
|
except OSError:
|
||||||
print('xml open error: {}'.format(xml))
|
print('xml open error: {}'.format(xml))
|
||||||
self._curr_xml = None
|
self._curr_xml = None
|
||||||
self._curr_xml_tree = None
|
self._curr_xml_tree = None
|
||||||
except SyntaxError:
|
except SyntaxError:
|
||||||
print('xml syntax error: {}'.format(xml))
|
print('xml syntax error: {}'.format(xml))
|
||||||
self._curr_xml = None
|
self._curr_xml = None
|
||||||
self._curr_xml_tree = None
|
self._curr_xml_tree = None
|
||||||
|
|
||||||
def get_curr(self):
|
def get_curr(self):
|
||||||
"""
|
"""
|
||||||
get current xml config file.
|
get current xml config file.
|
||||||
:return: current xml config file name.
|
:return: current xml config file name.
|
||||||
"""
|
"""
|
||||||
return self._curr_xml
|
return self._curr_xml
|
||||||
|
|
||||||
def get_curr_root(self):
|
def get_curr_root(self):
|
||||||
"""
|
"""
|
||||||
get the xml root of current xml config file.
|
get the xml root of current xml config file.
|
||||||
:return: the xml root of current xml config file.
|
:return: the xml root of current xml config file.
|
||||||
"""
|
"""
|
||||||
if self._curr_xml_tree is None:
|
if self._curr_xml_tree is None:
|
||||||
return None
|
return None
|
||||||
return self._curr_xml_tree.getroot()
|
return self._curr_xml_tree.getroot()
|
||||||
|
|
||||||
def get_curr_value(self, *args):
|
def get_curr_value(self, *args):
|
||||||
"""
|
"""
|
||||||
get the value of the element by its path.
|
get the value of the element by its path.
|
||||||
:param args: the path of the element.
|
:param args: the path of the element.
|
||||||
:return: the value of the element.
|
:return: the value of the element.
|
||||||
"""
|
"""
|
||||||
if self._curr_xml_tree is None:
|
if self._curr_xml_tree is None:
|
||||||
return None
|
return None
|
||||||
dest_node = self._get_dest_node(*args)
|
dest_node = self._get_dest_node(*args)
|
||||||
if dest_node is None:
|
if dest_node is None:
|
||||||
return None
|
return None
|
||||||
if dest_node.text is None or dest_node.text.strip() == '':
|
if dest_node.text is None or dest_node.text.strip() == '':
|
||||||
return ''
|
return ''
|
||||||
return dest_node.text
|
return dest_node.text
|
||||||
|
|
||||||
def set_curr_value(self, value, *args):
|
def set_curr_value(self, value, *args):
|
||||||
"""
|
"""
|
||||||
set the value of the element by its path.
|
set the value of the element by its path.
|
||||||
:param value: the value of the element.
|
:param value: the value of the element.
|
||||||
:param args: the path of the element.
|
:param args: the path of the element.
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
if self._curr_xml_tree is None:
|
if self._curr_xml_tree is None:
|
||||||
return
|
return
|
||||||
dest_node = self._get_dest_node(*args)
|
dest_node = self._get_dest_node(*args)
|
||||||
dest_node.text = value
|
dest_node.text = value
|
||||||
|
|
||||||
def set_curr_list(self, values, *args):
|
def set_curr_list(self, values, *args):
|
||||||
"""
|
"""
|
||||||
set a list of sub element for the element by its path.
|
set a list of sub element for the element by its path.
|
||||||
:param values: the list of values of the element.
|
:param values: the list of values of the element.
|
||||||
:param args: the path of the element.
|
:param args: the path of the element.
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
if self._curr_xml_tree is None:
|
if self._curr_xml_tree is None:
|
||||||
return
|
return
|
||||||
tag = args[-1]
|
tag = args[-1]
|
||||||
args = args[:-1]
|
args = args[:-1]
|
||||||
dest_node = self._get_dest_node(*args)
|
dest_node = self._get_dest_node(*args)
|
||||||
for node in dest_node.getchildren():
|
for node in dest_node.getchildren():
|
||||||
dest_node.remove(node)
|
dest_node.remove(node)
|
||||||
for value in values:
|
for value in values:
|
||||||
new_node = ElementTree.SubElement(dest_node, tag)
|
new_node = ElementTree.SubElement(dest_node, tag)
|
||||||
new_node.text = value
|
new_node.text = value
|
||||||
|
|
||||||
def set_curr_attr(self, attr_name, attr_value, *args):
|
def set_curr_attr(self, attr_name, attr_value, *args):
|
||||||
"""
|
"""
|
||||||
set the attribute of the element by its path.
|
set the attribute of the element by its path.
|
||||||
:param attr_name: the attribute name of the element.
|
:param attr_name: the attribute name of the element.
|
||||||
:param attr_value: the attribute value of the element.
|
:param attr_value: the attribute value of the element.
|
||||||
:param args: the path of the element.
|
:param args: the path of the element.
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
if self._curr_xml_tree is None:
|
if self._curr_xml_tree is None:
|
||||||
return
|
return
|
||||||
dest_node = self._get_dest_node(*args)
|
dest_node = self._get_dest_node(*args)
|
||||||
dest_node.attrib[attr_name] = attr_value
|
dest_node.attrib[attr_name] = attr_value
|
||||||
|
|
||||||
def add_curr_value(self, key, desc, value, *args):
|
def add_curr_value(self, key, desc, value, *args):
|
||||||
"""
|
"""
|
||||||
add a sub element for the element by its path.
|
add a sub element for the element by its path.
|
||||||
:param key: the tag of the sub element.
|
:param key: the tag of the sub element.
|
||||||
:param desc: the attribute desc of the sub element.
|
:param desc: the attribute desc of the sub element.
|
||||||
:param value: the value of the sub element.
|
:param value: the value of the sub element.
|
||||||
:param args: the path of the element.
|
:param args: the path of the element.
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
if self._curr_xml_tree is None:
|
if self._curr_xml_tree is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
dest_node = self._get_dest_node(*args)
|
dest_node = self._get_dest_node(*args)
|
||||||
|
|
||||||
if key in ['vm']:
|
if key in ['vm']:
|
||||||
ElementTree.SubElement(dest_node, key, attrib={'id': value, 'desc': desc})
|
ElementTree.SubElement(dest_node, key, attrib={'id': value, 'desc': desc})
|
||||||
else:
|
else:
|
||||||
new_node = ElementTree.SubElement(dest_node, key, attrib={'desc': desc})
|
new_node = ElementTree.SubElement(dest_node, key, attrib={'desc': desc})
|
||||||
new_node.text = value
|
new_node.text = value
|
||||||
|
|
||||||
def delete_curr_key(self, *args):
|
def delete_curr_key(self, *args):
|
||||||
"""
|
"""
|
||||||
delete the element by its path.
|
delete the element by its path.
|
||||||
:param args: the path of the element.
|
:param args: the path of the element.
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
if self._curr_xml_tree is None:
|
if self._curr_xml_tree is None:
|
||||||
return
|
return
|
||||||
dest_node = self._get_dest_node(*args)
|
dest_node = self._get_dest_node(*args)
|
||||||
self._curr_xml_tree.getroot().remove(dest_node)
|
self._curr_xml_tree.getroot().remove(dest_node)
|
||||||
|
|
||||||
def _get_dest_node(self, *args):
|
def _get_dest_node(self, *args):
|
||||||
"""
|
"""
|
||||||
get the destination element by its path.
|
get the destination element by its path.
|
||||||
:param args: the path of the element.
|
:param args: the path of the element.
|
||||||
:return: the destination element.
|
:return: the destination element.
|
||||||
"""
|
"""
|
||||||
if self._curr_xml_tree is None:
|
if self._curr_xml_tree is None:
|
||||||
return None
|
return None
|
||||||
dest_node = self._curr_xml_tree.getroot()
|
dest_node = self._curr_xml_tree.getroot()
|
||||||
path = '.'
|
path = '.'
|
||||||
for arg in args:
|
for arg in args:
|
||||||
# tag:attr=xxx
|
# tag:attr=xxx
|
||||||
# tag:attr
|
# tag:attr
|
||||||
# tag
|
# tag
|
||||||
tag = None
|
tag = None
|
||||||
attr_name = None
|
attr_name = None
|
||||||
attr_value = None
|
attr_value = None
|
||||||
if ':' not in arg:
|
if ':' not in arg:
|
||||||
tag = arg
|
tag = arg
|
||||||
elif '=' not in arg:
|
elif '=' not in arg:
|
||||||
# tag = arg.split(':')[0]
|
# tag = arg.split(':')[0]
|
||||||
# attr_name = arg.split(':')[1]
|
# attr_name = arg.split(':')[1]
|
||||||
raise Exception('unsupported xml path: tag:attr')
|
raise Exception('unsupported xml path: tag:attr')
|
||||||
else:
|
else:
|
||||||
tag = arg.split(':')[0]
|
tag = arg.split(':')[0]
|
||||||
attr = arg.split(':')[1]
|
attr = arg.split(':')[1]
|
||||||
attr_name = attr.split('=')[0]
|
attr_name = attr.split('=')[0]
|
||||||
attr_value = attr.split('=')[1]
|
attr_value = attr.split('=')[1]
|
||||||
|
|
||||||
if attr_value is None:
|
if attr_value is None:
|
||||||
path += ("/" + tag)
|
path += ("/" + tag)
|
||||||
else:
|
else:
|
||||||
path += ("/" + tag + "[@" + attr_name + "='" + attr_value + "']")
|
path += ("/" + tag + "[@" + attr_name + "='" + attr_value + "']")
|
||||||
|
|
||||||
dest_node = dest_node.findall(path)
|
dest_node = dest_node.findall(path)
|
||||||
if dest_node is not None and dest_node != []:
|
if dest_node is not None and dest_node != []:
|
||||||
return dest_node[0]
|
return dest_node[0]
|
||||||
|
|
||||||
raise Exception('can not find node by {} from xml'.format(args))
|
raise Exception('can not find node by {} from xml'.format(args))
|
||||||
|
|
||||||
def save(self, xml=None):
|
def save(self, xml=None):
|
||||||
"""
|
"""
|
||||||
save current xml to file.
|
save current xml to file.
|
||||||
:param xml: the file name to save; if not specified, save current xml to default names.
|
:param xml: the file name to save; if not specified, save current xml to default names.
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
if self._curr_xml_tree is None:
|
if self._curr_xml_tree is None:
|
||||||
return
|
return
|
||||||
if xml is None:
|
if xml is None:
|
||||||
xml = self._curr_xml
|
xml = self._curr_xml
|
||||||
|
|
||||||
xml_path = os.path.join(self._xml_path, 'user_defined')
|
xml_path = os.path.join(self._xml_path, 'user_defined')
|
||||||
if not os.path.isdir(xml_path):
|
if not os.path.isdir(xml_path):
|
||||||
os.makedirs(xml_path)
|
os.makedirs(xml_path)
|
||||||
|
|
||||||
self._format_xml(self._curr_xml_tree.getroot())
|
self._format_xml(self._curr_xml_tree.getroot())
|
||||||
self._curr_xml_tree.write(os.path.join(xml_path, xml+'.xml'), encoding='utf-8',
|
self._curr_xml_tree.write(os.path.join(xml_path, xml+'.xml'), encoding='utf-8',
|
||||||
xml_declaration=True, method='xml')
|
xml_declaration=True, method='xml')
|
||||||
|
|
||||||
def _format_xml(self, element, depth=0):
|
def _format_xml(self, element, depth=0):
|
||||||
i = "\n" + depth * " "
|
i = "\n" + depth * " "
|
||||||
if element:
|
if element:
|
||||||
if not element.text or not element.text.strip():
|
if not element.text or not element.text.strip():
|
||||||
element.text = i + " "
|
element.text = i + " "
|
||||||
if not element.tail or not element.tail.strip():
|
if not element.tail or not element.tail.strip():
|
||||||
element.tail = i
|
element.tail = i
|
||||||
for element in element:
|
for element in element:
|
||||||
self._format_xml(element, depth + 1)
|
self._format_xml(element, depth + 1)
|
||||||
if not element.tail or not element.tail.strip():
|
if not element.tail or not element.tail.strip():
|
||||||
element.tail = i
|
element.tail = i
|
||||||
else:
|
else:
|
||||||
if depth and (not element.tail or not element.tail.strip()):
|
if depth and (not element.tail or not element.tail.strip()):
|
||||||
element.tail = i
|
element.tail = i
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user