Esempio n. 1
0
def check_xml_parse_file_with_includes_with_xml(xml, included_xml=None, nested_included_xml=None):
    if nested_included_xml:
        nested_included_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
        nested_included_file.write(nested_included_xml)
        nested_included_file.close()
        included_xml = included_xml.format(nested_included_file.name)

    if included_xml:
        included_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
        included_file.write(included_xml)
        included_file.close()
        xml = xml.format(included_file.name)

    prx_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
    prx_file.write(xml)
    prx_file.close()

    try:
        return xml_parse_file_with_includes(prx_file.name)
    finally:
        os.remove(prx_file.name)
        if included_xml:
            os.remove(included_file.name)
        if nested_included_xml:
            os.remove(nested_included_file.name)
Esempio n. 2
0
def check_xml_parse_file_with_includes_with_xml(xml, included_xml=None, nested_included_xml=None):
    if nested_included_xml:
        nested_included_file = tempfile.NamedTemporaryFile(mode="w", delete=False)
        nested_included_file.write(nested_included_xml)
        nested_included_file.close()
        included_xml = included_xml.format(nested_included_file.name)

    if included_xml:
        included_file = tempfile.NamedTemporaryFile(mode="w", delete=False)
        included_file.write(included_xml)
        included_file.close()
        xml = xml.format(included_file.name)

    prx_file = tempfile.NamedTemporaryFile(mode="w", delete=False)
    prx_file.write(xml)
    prx_file.close()

    try:
        return xml_parse_file_with_includes(prx_file.name)
    finally:
        os.remove(prx_file.name)
        if included_xml:
            os.remove(included_file.name)
        if nested_included_xml:
            os.remove(nested_included_file.name)
def check_xml_parse_file_with_includes__absolute_relative(absolute):
    try:
        included_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<include_root><included_element /></include_root>"""
        main_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<system>
  <include file="{}" />
</system>"""
        expected_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<system>
  <included_element />
</system>"""

        included_dir = tempfile.TemporaryDirectory()
        main_dir = tempfile.TemporaryDirectory()

        included_file = tempfile.NamedTemporaryFile(mode='w',
                                                    delete=False,
                                                    dir=included_dir.name)
        included_file.write(included_xml)
        included_file.close()

        with open(
                os.path.join(main_dir.name,
                             os.path.basename(included_file.name)),
                'w') as empty_file:
            empty_file.write(
                """<?xml version="1.0" encoding="UTF-8" ?><include_root></include_root>"""
            )

        if absolute:
            included_path = os.path.abspath(included_file.name)
        else:
            included_path = os.path.join('..',
                                         os.path.basename(included_dir.name),
                                         os.path.basename(included_file.name))
        main_xml = main_xml.format(included_path)
        main_file = tempfile.NamedTemporaryFile(mode='w',
                                                delete=False,
                                                dir=main_dir.name)
        main_file.write(main_xml)
        main_file.close()

        result_dom = xml_parse_file_with_includes(main_file.name)
        expected_dom = xml_parse_string(expected_xml)

        assert result_dom.toxml() == expected_dom.toxml()

    finally:
        for dir in [included_dir, main_dir]:
            dir.cleanup()
Esempio n. 4
0
    def _parse_import(self, entity_name, path):
        """Parse an entity decribed in the specified path.

        Return the approriate object as determined by the file extension.

        """
        ext = os.path.splitext(path)[1]
        if ext == '.prx':
            os.makedirs(self.output, exist_ok=True)
            try:
                return System(
                    entity_name,
                    xml_parse_file_with_includes(
                        path, self._prx_include_paths,
                        os.path.join(self.output, entity_name + ext)), self)
            except ExpatError as e:
                raise EntityLoadError(
                    "Error parsing system import '{}:{}': {!s}".format(
                        e.path, e.lineno, e))
        elif ext == '.py':
            try:
                py_module = imp.load_source("__prj.%s" % entity_name, path)
            except:
                exc_type, exc_value, tb = sys.exc_info()
                tb_str = ''.join(
                    traceback.format_exception(exc_type,
                                               exc_value,
                                               tb.tb_next,
                                               chain=False))
                msg = "An error occured while loading '{}'".format(path)
                detail = "Traceback:\n{}".format(tb_str)
                raise EntityLoadError(msg, detail)

            py_module.__path__ = os.path.dirname(py_module.__file__)
            if hasattr(py_module, 'system_build'):
                return Builder(entity_name, py_module)
            elif hasattr(py_module, 'system_load'):
                return Loader(entity_name, py_module)
            elif hasattr(py_module, 'module'):
                return py_module.module
            else:
                raise EntityLoadError(
                    "Python entity '%s' from path %s doesn't match any interface"
                    % (entity_name, path))
        elif ext in ['.c', '.s', '.asm']:
            return SourceModule(entity_name, path)
        else:
            raise EntityLoadError("Unhandled extension '{}'".format(ext))
def test_xml_parse_file_with_includes__include_paths():
    try:
        included_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<include_root><included_element /></include_root>"""
        main_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<system>
  <include file="{}" />
</system>"""
        expected_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<system>
  <included_element />
</system>"""

        include_dirs = [tempfile.TemporaryDirectory() for _ in range(3)]
        included_dir = include_dirs[1]
        main_dir = tempfile.TemporaryDirectory()

        included_file = tempfile.NamedTemporaryFile(mode='w',
                                                    delete=False,
                                                    dir=included_dir.name)
        included_file.write(included_xml)
        included_file.close()

        with open(
                os.path.join(include_dirs[2].name,
                             os.path.basename(included_file.name)),
                'w') as empty_file:
            empty_file.write(
                """<?xml version="1.0" encoding="UTF-8" ?><include_root></include_root>"""
            )

        main_xml = main_xml.format(os.path.basename(included_file.name))
        main_file = tempfile.NamedTemporaryFile(mode='w',
                                                delete=False,
                                                dir=main_dir.name)
        main_file.write(main_xml)
        main_file.close()

        result_dom = xml_parse_file_with_includes(
            main_file.name, [d.name for d in include_dirs])
        expected_dom = xml_parse_string(expected_xml)

        assert result_dom.toxml() == expected_dom.toxml()

    finally:
        for dir in include_dirs:
            dir.cleanup()
Esempio n. 6
0
    def test_xml_parse_file_with_includes_without_include(self):
        prx_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<system>
  <modules>
    <module name="foo">
      <bar>baz</bar>
    </module>
  </modules>
</system>"""
        prx_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
        prx_file.write(prx_xml)
        prx_file.close()
        try:
            result = xml_parse_file_with_includes(prx_file.name)
            result_of_xml_parse_file = xml_parse_file(prx_file.name)
            self.assertEqual(result.toxml(), result_of_xml_parse_file.toxml())
        finally:
            os.remove(prx_file.name)
Esempio n. 7
0
def test_xml_parse_file_with_includes_without_include():
    prx_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<system>
  <modules>
    <module name="foo">
      <bar>baz</bar>
    </module>
  </modules>
</system>"""
    prx_file = tempfile.NamedTemporaryFile(mode="w", delete=False)
    prx_file.write(prx_xml)
    prx_file.close()
    try:
        result = xml_parse_file_with_includes(prx_file.name)
        result_of_xml_parse_file = xml_parse_file(prx_file.name)
        assert result.toxml() == result_of_xml_parse_file.toxml()
    finally:
        os.remove(prx_file.name)
Esempio n. 8
0
def check_xml_parse_file_with_includes__absolute_relative(absolute):
    try:
        included_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<include_root><included_element /></include_root>"""
        main_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<system>
  <include file="{}" />
</system>"""
        expected_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<system>
  <included_element />
</system>"""

        included_dir = tempfile.TemporaryDirectory()
        main_dir = tempfile.TemporaryDirectory()

        included_file = tempfile.NamedTemporaryFile(mode="w", delete=False, dir=included_dir.name)
        included_file.write(included_xml)
        included_file.close()

        with open(os.path.join(main_dir.name, os.path.basename(included_file.name)), "w") as empty_file:
            empty_file.write("""<?xml version="1.0" encoding="UTF-8" ?><include_root></include_root>""")

        if absolute:
            included_path = os.path.abspath(included_file.name)
        else:
            included_path = os.path.join(
                "..", os.path.basename(included_dir.name), os.path.basename(included_file.name)
            )
        main_xml = main_xml.format(included_path)
        main_file = tempfile.NamedTemporaryFile(mode="w", delete=False, dir=main_dir.name)
        main_file.write(main_xml)
        main_file.close()

        result_dom = xml_parse_file_with_includes(main_file.name)
        expected_dom = xml_parse_string(expected_xml)

        assert result_dom.toxml() == expected_dom.toxml()

    finally:
        for dir in [included_dir, main_dir]:
            dir.cleanup()
Esempio n. 9
0
    def _parse_import(self, entity_name, path):
        """Parse an entity decribed in the specified path.

        Return the approriate object as determined by the file extension.

        """
        ext = os.path.splitext(path)[1]
        if ext == '.prx':
            os.makedirs(self.output, exist_ok=True)
            try:
                return System(entity_name,
                              xml_parse_file_with_includes(path, self._prx_include_paths,
                                                           os.path.join(self.output, entity_name + ext)),
                              self)
            except ExpatError as e:
                raise EntityLoadError("Error parsing system import '{}:{}': {!s}".format(e.path, e.lineno, e))
        elif ext == '.py':
            try:
                py_module = imp.load_source("__prj.%s" % entity_name, path)
            except:
                exc_type, exc_value, tb = sys.exc_info()
                tb_str = ''.join(traceback.format_exception(exc_type, exc_value, tb.tb_next, chain=False))
                msg = "An error occured while loading '{}'".format(path)
                detail = "Traceback:\n{}".format(tb_str)
                raise EntityLoadError(msg, detail)

            py_module.__path__ = os.path.dirname(py_module.__file__)
            if hasattr(py_module, 'system_build'):
                return Builder(entity_name, py_module)
            elif hasattr(py_module, 'system_load'):
                return Loader(entity_name, py_module)
            elif hasattr(py_module, 'module'):
                return py_module.module
            else:
                raise EntityLoadError("Python entity '%s' from path %s doesn't match any interface" %
                                      (entity_name, path))
        elif ext in ['.c', '.s', '.asm']:
            return SourceModule(entity_name, path)
        else:
            raise EntityLoadError("Unhandled extension '{}'".format(ext))
Esempio n. 10
0
def test_xml_parse_file_with_includes__include_paths():
    try:
        included_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<include_root><included_element /></include_root>"""
        main_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<system>
  <include file="{}" />
</system>"""
        expected_xml = """<?xml version="1.0" encoding="UTF-8" ?>
<system>
  <included_element />
</system>"""

        include_dirs = [tempfile.TemporaryDirectory() for _ in range(3)]
        included_dir = include_dirs[1]
        main_dir = tempfile.TemporaryDirectory()

        included_file = tempfile.NamedTemporaryFile(mode="w", delete=False, dir=included_dir.name)
        included_file.write(included_xml)
        included_file.close()

        with open(os.path.join(include_dirs[2].name, os.path.basename(included_file.name)), "w") as empty_file:
            empty_file.write("""<?xml version="1.0" encoding="UTF-8" ?><include_root></include_root>""")

        main_xml = main_xml.format(os.path.basename(included_file.name))
        main_file = tempfile.NamedTemporaryFile(mode="w", delete=False, dir=main_dir.name)
        main_file.write(main_xml)
        main_file.close()

        result_dom = xml_parse_file_with_includes(main_file.name, [d.name for d in include_dirs])
        expected_dom = xml_parse_string(expected_xml)

        assert result_dom.toxml() == expected_dom.toxml()

    finally:
        for dir in include_dirs:
            dir.cleanup()