예제 #1
0
    def save(self, output=None, options=None):
        self.options = options or {}
        output = self.open_out_stream(output)
        self.prefixes.clear()
        self.reverse_nsmap.clear()

        serialize_default = \
            self.options.get(XMIOptions.SERIALIZE_DEFAULT_VALUES,
                             False)
        nsmap = {XMI: XMI_URL, XSI: XSI_URL}

        if len(self.contents) == 1:
            root = self.contents[0]
            self.register_eobject_epackage(root)
            tmp_xmi_root = self._go_across(root, serialize_default)
        else:
            tag = etree.QName(XMI_URL, 'XMI')
            tmp_xmi_root = etree.Element(tag)
            for root in self.contents:
                root_node = self._go_across(root, serialize_default)
                tmp_xmi_root.append(root_node)

        # update nsmap with prefixes register during the nodes creation
        nsmap.update(self.prefixes)
        xmi_root = etree.Element(tmp_xmi_root.tag, nsmap=nsmap)
        xmi_root[:] = tmp_xmi_root[:]
        xmi_root.attrib.update(tmp_xmi_root.attrib)
        xmi_version = etree.QName(XMI_URL, 'version')
        xmi_root.attrib[xmi_version] = '2.0'
        tree = etree.ElementTree(xmi_root)
        tree.write(output,
                   pretty_print=True,
                   xml_declaration=True,
                   encoding=tree.docinfo.encoding)
        self.uri.close_stream()
예제 #2
0
 def __init__(self, steps, queue, module):
     threading.Thread.__init__(self, None, self.do)
     # assign the arguments
     self.queue = queue
     self._test = steps
     self._module = module
     # create empty data
     self._run_methods = []
     self._modules_classes = []
     self.cls = None
     self.time_start = datetime.now()
     self.data = TestData.TestRunData()
     # create a XML element that will serve as a root
     self.xml = et.Element("test_run")
예제 #3
0
def make_prop_el():
    """Wrapper for etree.Element, that takes care of unsupported nsmap option."""
    if use_lxml:
        return etree.Element("{DAV:}prop", nsmap={"D": "DAV:"})
    return etree.Element("{DAV:}prop")
예제 #4
0
def makeMultistatusEL():
    """Wrapper for etree.Element, that takes care of unsupported nsmap option."""
    if useLxml:
        return etree.Element("{DAV:}multistatus", nsmap={"D": "DAV:"})
    return etree.Element("{DAV:}multistatus")
예제 #5
0
    def generate_tree():
        """
        Creates an XML tree complying to SimGrid DTD.
        """
        def main_zone():
            """
            Contains master zone and all clusters.
            """

            return xml.SubElement(platform_xml,
                                  "zone",
                                  attrib={
                                      "id": "main",
                                      "routing": "Full"
                                  })

        def master_zone():
            """
            Hosts the master node which schedules jobs onto resources.
            """
            def master_host():
                """
                Executes the scheduling algorithms.
                """

                xml.SubElement(master_zone_xml,
                               "host",
                               attrib={
                                   "id": "master_host",
                                   "speed": "1Gf"
                               })

            master_zone_xml = xml.SubElement(main_zone_xml,
                                             "zone",
                                             attrib={
                                                 "id": "master",
                                                 "routing": "None"
                                             })
            master_host()

        def config_node():
            """
            Holds user defined properties concerning node types.
            """
            def config_zone():
                """
                Define node and proc config properties.
                """
                return xml.SubElement(main_zone_xml,
                                      "zone",
                                      attrib={
                                          "id": "config",
                                          "routing": "None"
                                      })

            return xml.SubElement(config_zone(),
                                  "zone",
                                  attrib={
                                      "id": "node",
                                      "routing": "None"
                                  })

        def clusters():
            """
            Groups of nodes inside the data centre.
            """
            def nodes():
                """
                Systems available in the data centre, contain processors and other resources (v. gr. memory).
                They are connected to a common cluster backbone by up / down links.
                """
                def record_node_type():
                    """
                    Inserts the node type in the already configured ones.
                    """

                    if node["type"] not in recorded_nodes:
                        config_node_type_xml = xml.SubElement(config_node_xml,
                                                              "zone",
                                                              attrib={
                                                                  "id":
                                                                  node["type"],
                                                                  "routing":
                                                                  "None"
                                                              })
                        xml.SubElement(config_node_type_xml,
                                       "prop",
                                       attrib={
                                           "id": "memory",
                                           "value": node_template["memory_gib"]
                                       })
                        recorded_nodes[node["type"]] = True

                def udlink():
                    """
                    Link between the node and the backbone.
                    """

                    udlink_id = "udl_{}".format(node_id)
                    _udlink_attrs = {
                        "id": udlink_id,
                        "sharing_policy": "SHARED"
                    }
                    _udlink_attrs.update(
                        network_types[cluster["cluster_network"]])
                    xml.SubElement(cluster_xml, "link", attrib=_udlink_attrs)
                    return udlink_id

                def procs():
                    """
                    Computing resources available in the data centre. These can be CPUs, GPUs, MICs, ...
                    They have a set of cores and power consumption properties.
                    """
                    def cores():
                        """
                        Individual computing units inside a processor.
                        """
                        def core_properties():
                            """
                            Defines node type and power consumption properties.
                            """

                            xml.SubElement(core_xml,
                                           "prop",
                                           attrib={
                                               "id": "node_type",
                                               "value": node["type"]
                                           })
                            for prop in proc_template["core_properties"]:
                                xml.SubElement(core_xml, "prop", attrib=prop)

                        def link_association():
                            """
                            Associates up / down link with the core.
                            """

                            xml.SubElement(cluster_xml,
                                           "host_link",
                                           attrib={
                                               "id": core_id,
                                               "up": udlink_id,
                                               "down": udlink_id
                                           })

                        for core_idx in range(int(proc_template["nb_cores"])):
                            core_id = "cor_{}_{}".format(core_idx, proc_id)
                            _core_attrs = {"id": core_id}
                            _core_attrs.update(
                                proc_template["core_attributes"])
                            core_xml = xml.SubElement(cluster_xml,
                                                      "host",
                                                      attrib=_core_attrs)
                            core_properties()
                            link_association()

                    for proc in node_template["processors"]:
                        proc_template = processor_types[proc["type"]][
                            proc["model"]]
                        for proc_idx in range(int(proc_template["nb_cores"])):
                            proc_id = "{}_{}_{}".format(
                                proc_template["id"], proc_idx, node_id)
                            cores()

                for node in cluster["nodes"]:
                    node_template = node_types[node["type"]]
                    record_node_type()
                    for node_idx in range(int(node["number"])):
                        node_id = "{}_{}_{}".format(node_template["id"],
                                                    node_idx, cluster_id)
                        udlink_id = udlink()
                        procs()

            def router():
                """
                Gateway for inter-cluster connections.
                """

                xml.SubElement(cluster_xml,
                               "router",
                               attrib={"id": "rou_{}".format(cluster_idx)})

            def backbone():
                """
                Intra-cluster connections.
                """

                _backbone_attrs = {"id": "bbo_{}".format(cluster_idx)}
                _backbone_attrs.update(
                    network_types[cluster["cluster_network"]])
                xml.SubElement(cluster_xml, "backbone", attrib=_backbone_attrs)

            cluster_idx = 0
            recorded_nodes = {}
            for cluster in platform["clusters"]:
                cluster_id = "clu_{}".format(cluster_idx)
                cluster_xml = xml.SubElement(main_zone_xml,
                                             "zone",
                                             attrib={
                                                 "id": cluster_id,
                                                 "routing": "Cluster"
                                             })
                nodes()
                router()
                backbone()
                cluster_idx += 1

        def global_links():
            """
            Links from clusters to the master zone.
            """

            for cluster_idx in range(len(platform["clusters"])):
                _global_link_attrs = {"id": "tomh_clu_{}".format(cluster_idx)}
                _global_link_attrs.update(
                    network_types[platform["dc_network"]])
                xml.SubElement(main_zone_xml,
                               "link",
                               attrib=_global_link_attrs)

        def routes():
            """
            Routes over global links.
            """

            for cluster_idx in range(len(platform["clusters"])):
                route_xml = xml.SubElement(main_zone_xml,
                                           "zoneRoute",
                                           attrib={
                                               "src":
                                               "clu_{}".format(cluster_idx),
                                               "dst":
                                               "master",
                                               "gw_src":
                                               "rou_{}".format(cluster_idx),
                                               "gw_dst":
                                               "master_host"
                                           })
                xml.SubElement(
                    route_xml,
                    "link_ctn",
                    attrib={"id": "tomh_clu_{}".format(cluster_idx)})

        platform_xml = xml.Element("platform", attrib={"version": "4.1"})
        main_zone_xml = main_zone()
        master_zone()
        config_node_xml = config_node()
        clusters()
        global_links()
        routes()
        return platform_xml
예제 #6
0
    def _go_across(self, obj, serialize_default=False):
        self.register_eobject_epackage(obj)
        eclass = obj.eClass
        if not obj.eContainmentFeature():  # obj is the root
            epackage = eclass.ePackage
            nsURI = epackage.nsURI
            tag = etree.QName(nsURI, eclass.name) if nsURI else eclass.name
            node = etree.Element(tag)
        else:
            node = etree.Element(obj.eContainmentFeature().name)
            if obj.eContainmentFeature().eType != eclass:
                self._add_explicit_type(node, obj)

        if self.use_uuid:
            self._assign_uuid(obj)
            xmi_id = '{{{0}}}id'.format(XMI_URL)
            node.attrib[xmi_id] = obj._internal_id

        for feat in obj._isset:
            if feat.derived or feat.transient:
                continue
            feat_name = feat.name
            value = obj.__getattribute__(feat_name)
            if hasattr(feat.eType, 'eType') and feat.eType.eType is dict:
                for key, val in value.items():
                    entry = etree.Element(feat_name)
                    entry.attrib['key'] = key
                    entry.attrib['value'] = val
                    node.append(entry)
            elif isinstance(feat, Ecore.EAttribute):
                etype = feat.eType
                if feat.many and value:
                    to_str = etype.to_string
                    has_special_char = False
                    result_list = []
                    for v in value:
                        string = to_str(v)
                        if any(x.isspace() for x in string):
                            has_special_char = True
                        result_list.append(string)
                    if has_special_char:
                        for v in result_list:
                            sub = etree.SubElement(node, feat_name)
                            sub.text = v
                    else:
                        node.attrib[feat_name] = ' '.join(result_list)
                    continue
                default_value = feat.get_default_value()
                if value != default_value or serialize_default:
                    if value is None:
                        node.append(self._build_none_node(feat_name))
                    else:
                        node.attrib[feat_name] = etype.to_string(value)
                continue

            elif isinstance(feat, Ecore.EReference) and \
                    feat.eOpposite and feat.eOpposite.containment:
                continue
            elif isinstance(feat, Ecore.EReference) \
                    and not feat.containment:
                if not value:
                    if serialize_default and value is None:
                        node.append(self._build_none_node(feat_name))
                    continue
                if feat.many:
                    results = [self._build_path_from(x) for x in value]
                    embedded = []
                    crossref = []
                    for i, result in enumerate(results):
                        frag, cref = result
                        if cref:
                            crossref.append((i, frag))
                        else:
                            embedded.append(frag)
                    if embedded:
                        result = ' '.join(embedded)
                        node.attrib[feat_name] = result
                    for i, ref in crossref:
                        sub = etree.SubElement(node, feat_name)
                        sub.attrib['href'] = ref
                        self._add_explicit_type(sub, value[i])
                else:
                    frag, is_crossref = self._build_path_from(value)
                    if is_crossref:
                        sub = etree.SubElement(node, feat_name)
                        sub.attrib['href'] = frag
                        self._add_explicit_type(sub, value)
                    else:
                        node.attrib[feat_name] = frag

            if isinstance(feat, Ecore.EReference) and feat.containment:
                children = obj.__getattribute__(feat_name)
                children = children if feat.many else [children]
                for child in children:
                    node.append(self._go_across(child, serialize_default))
        return node
예제 #7
0
 def _build_none_node(self, feature_name):
     sub = etree.Element(feature_name)
     xsi_null = etree.QName(self.xsi_type_url(), 'nil')
     sub.attrib[xsi_null] = 'true'
     return sub