Example #1
0
    def __init__(self):
        """Initialise the module.

        Module initialisation will ensures the class is correctly constructed.
        If the xml_schema is set, initialisation will set the schema based on the xml_schema.

        """
        if len(set([self.schema, self.xml_schema, self.xml_schema_path])) > 2:
            raise Exception(
                "Class '{}' in {} has multiple schema sources set.".format(
                    self.__class__.__name__,
                    os.path.abspath(inspect.getfile(self.__class__))))

        if self.schema is NOTHING:
            if self.xml_schema_path is not NOTHING:
                xml_schema_document = xml_parse_file(self.xml_schema_path)
            elif self.xml_schema is not NOTHING:
                filename = sys.modules[self.__class__.__module__].__file__
                xml_schema_document = xml_parse_string(
                    self.xml_schema, '{}!xml_schema'.format(filename))
            else:
                raise Exception(
                    "Class '{}' in {} has none of the possible schema sources (schema, xml_schema, \
xml_schema_path) set as a class member.".format(
                        self.__class__.__name__,
                        os.path.abspath(inspect.getfile(self.__class__))))
            self.schema = xml2schema(xml_schema_document)
Example #2
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)
Example #3
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)
Example #4
0
    def __init__(self):
        """Initialise the module.

        Module initialisation will ensures the class is correctly constructed.
        If the xml_schema is set, initialisation will set the schema based on the xml_schema.

        """
        if len(set([self.schema, self.xml_schema, self.xml_schema_path])) > 2:
            raise Exception("Class '{}' in {} has multiple schema sources set.".format(self.__class__.__name__,
                            os.path.abspath(inspect.getfile(self.__class__))))

        if self.schema is NOTHING:
            if self.xml_schema_path is not NOTHING:
                xml_schema_document = xml_parse_file(self.xml_schema_path)
            elif self.xml_schema is not NOTHING:
                filename = sys.modules[self.__class__.__module__].__file__
                xml_schema_document = xml_parse_string(self.xml_schema, '{}!xml_schema'.format(filename))
            else:
                raise Exception("Class '{}' in {} has none of the possible schema sources (schema, xml_schema, \
xml_schema_path) set as a class member.".format(self.__class__.__name__,
                                                os.path.abspath(inspect.getfile(self.__class__))))
            self.schema = xml2schema(xml_schema_document)
Example #5
0
    def __init__(self, filename, search_paths=None, prx_include_paths=None):
        """Parses the project definition file `filename` and any imported system and module definition files.

        If filename is None, then a default 'empty' project is created.

        The search path for a project is a list of paths in which modules will be searched.
        The order of the search path matters; the first path in which an entity is found is used.

        The search path consists of the 'user' search paths, and the 'built-in' search paths.
        'user' search paths are searched before 'built-in' search paths.

        The 'user' search paths consist of the 'param' search paths as passed explicitly to the class and
        the 'project' search paths, which are any search paths specified in the project file.
        'param' search paths are searched before the 'project' search paths.
        If no 'param' search paths or 'project' search paths are specified, then the 'user' search path
        defaults to the project file's directory (or the current working directory if no project file is specified.)

        """
        if filename is None:
            self.dom = xml_parse_string('<project></project>')
            self.project_dir = os.getcwd()
        else:
            self.dom = xml_parse_file(filename)
            self.project_dir = os.path.dirname(filename)

        self.entities = {}

        # Find all startup-script items.
        ss_els = self.dom.getElementsByTagName('startup-script')
        for ss in ss_els:
            command = single_text_child(ss)
            if command.split()[0].endswith('.py'):
                # prepend full path of python interpreter as .py files are not necessarily executable on Windows
                # and the command 'python3.3' is likely not in PATH
                command = '{} {}'.format(sys.executable, command)
            ret_code = os.system(command)
            if ret_code != 0:
                err = xml_error_str(
                    ss, "Error running startup-script"
                    ": '{}' {}".format(command, show_exit(ret_code)))
                raise ProjectStartupError(err)

        param_search_paths = search_paths if search_paths is not None else []
        project_search_paths = list(get_paths_from_dom(self.dom,
                                                       'search-path'))
        user_search_paths = param_search_paths + project_search_paths
        if len(user_search_paths) == 0:
            user_search_paths = [self.project_dir]

        built_in_search_paths = []
        if frozen:
            base_file = sys.executable if frozen else __file__
            base_file = follow_link(base_file)

            base_dir = canonical_path(os.path.dirname(base_file))

            def find_share(cur):
                cur = canonical_path(cur)
                maybe_share_path = os.path.join(cur, 'share')
                if os.path.exists(maybe_share_path):
                    return maybe_share_path
                else:
                    up = canonical_path(os.path.join(cur, os.path.pardir))
                    if up == cur:
                        return None
                    return find_share(up)

            share_dir = find_share(base_dir)
            if share_dir is None or not os.path.isdir(share_dir):
                logger.warning("Unable to find 'share' directory.")
            else:
                packages_dir = os.path.join(share_dir, 'packages')
                if not os.path.exists(packages_dir) or not os.path.isdir(
                        packages_dir):
                    logger.warning(
                        "Can't find 'packages' directory in '{}'".format(
                            share_dir))
                else:
                    built_in_search_paths.append(packages_dir)

        self.search_paths = user_search_paths + built_in_search_paths

        logger.debug("search_paths %s", self.search_paths)

        param_prx_include_paths = prx_include_paths if prx_include_paths is not None else []
        self._prx_include_paths = list(
            get_paths_from_dom(self.dom,
                               'prx-include-path')) + param_prx_include_paths

        output_el = maybe_single_named_child(self.dom, 'output')
        if output_el:
            path = get_attribute(output_el, 'path')
        else:
            path = 'out'
        if os.path.isabs(path):
            self.output = path
        else:
            self.output = os.path.join(self.project_dir, path)
Example #6
0
    def __init__(self, filename, search_paths=None, prx_include_paths=None):
        """Parses the project definition file `filename` and any imported system and module definition files.

        If filename is None, then a default 'empty' project is created.

        The search path for a project is a list of paths in which modules will be searched.
        The order of the search path matters; the first path in which an entity is found is used.

        The search path consists of the 'user' search paths, and the 'built-in' search paths.
        'user' search paths are searched before 'built-in' search paths.

        The 'user' search paths consist of the 'param' search paths as passed explicitly to the class and
        the 'project' search paths, which are any search paths specified in the project file.
        'param' search paths are searched before the 'project' search paths.
        If no 'param' search paths or 'project' search paths are specified, then the 'user' search path
        defaults to the project file's directory (or the current working directory if no project file is specified.)

        """
        if filename is None:
            self.dom = xml_parse_string('<project></project>')
            self.project_dir = os.getcwd()
        else:
            self.dom = xml_parse_file(filename)
            self.project_dir = os.path.dirname(filename)

        self.entities = {}

        # Find all startup-script items.
        ss_els = self.dom.getElementsByTagName('startup-script')
        for ss in ss_els:
            command = single_text_child(ss)
            if command.split()[0].endswith('.py'):
                # prepend full path of python interpreter as .py files are not necessarily executable on Windows
                # and the command 'python3.3' is likely not in PATH
                command = '{} {}'.format(sys.executable, command)
            ret_code = os.system(command)
            if ret_code != 0:
                err = xml_error_str(ss, "Error running startup-script"
                                        ": '{}' {}".format(command, show_exit(ret_code)))
                raise ProjectStartupError(err)

        param_search_paths = search_paths if search_paths is not None else []
        project_search_paths = list(get_paths_from_dom(self.dom, 'search-path'))
        user_search_paths = param_search_paths + project_search_paths
        if len(user_search_paths) == 0:
            user_search_paths = [self.project_dir]

        built_in_search_paths = []
        if frozen:
            base_file = sys.executable if frozen else __file__
            base_file = follow_link(base_file)

            base_dir = canonical_path(os.path.dirname(base_file))

            def find_share(cur):
                cur = canonical_path(cur)
                maybe_share_path = os.path.join(cur, 'share')
                if os.path.exists(maybe_share_path):
                    return maybe_share_path
                else:
                    up = canonical_path(os.path.join(cur, os.path.pardir))
                    if up == cur:
                        return None
                    return find_share(up)
            share_dir = find_share(base_dir)
            if share_dir is None or not os.path.isdir(share_dir):
                logger.warning("Unable to find 'share' directory.")
            else:
                packages_dir = os.path.join(share_dir, 'packages')
                if not os.path.exists(packages_dir) or not os.path.isdir(packages_dir):
                    logger.warning("Can't find 'packages' directory in '{}'".format(share_dir))
                else:
                    built_in_search_paths.append(packages_dir)

        self.search_paths = user_search_paths + built_in_search_paths

        logger.debug("search_paths %s", self.search_paths)

        param_prx_include_paths = prx_include_paths if prx_include_paths is not None else []
        self._prx_include_paths = list(get_paths_from_dom(self.dom, 'prx-include-path')) + param_prx_include_paths

        output_el = maybe_single_named_child(self.dom, 'output')
        if output_el:
            path = get_attribute(output_el, 'path')
        else:
            path = 'out'
        if os.path.isabs(path):
            self.output = path
        else:
            self.output = os.path.join(self.project_dir, path)