예제 #1
0
    def run(self, ext_dep, _inputs=None):
        """Verify the cache file contents, if it exists"""
        cache_ok = False
        filename = self.get_action_file(ext_dep, "cache")
        self.log_debug("Checking settings in {}".format(filename))
        with LineReader.OpenFileCM(filename, "rt") as cache_cm:
            if cache_cm:
                self.log_debug("Reading values")
                # Not providing a real target class is OK as long as we don't
                # try to stringize the LineInfo object.
                reader = LineReader.ReadLineInfoIter(cache_cm.get_file(),
                                                     target="")
                cache_lines = [line_info.get_text() for line_info in reader]
                if cache_lines == self.arg:
                    cache_ok = True
                else:
                    self.log_debug("Expected:")
                    for line in self.arg:
                        self.log_debug("  '{}'".format(line))
                    self.log_debug("Cached:")
                    for line in cache_lines:
                        self.log_debug("  '{}'".format(line))
            else:
                self.log_debug("No valid cache; (re)running actions")

        return cache_ok
    def create_parser(self):
        # The DefTree and reader are not under test, but simple enough to use directly
        self.deftree = DefTree.new()
        self.parser = DefinitionParser.new(self.deftree, debug_mode=True)

        # The reader is not under test, but it provides a good way to feed strings to the parser
        self.reader = LineReader.new(self.parser)
예제 #3
0
    def generate(self, definitions, out_dir):
        """Generate a settings file"""
        java_file = self.filename.lower()
        package_dir = os.path.dirname(java_file)
        package_name = os.path.relpath(package_dir, start=out_dir)
        package_name = self.PACKAGE_RE.sub(".", package_name)
        class_name = self.CLASS_RE.sub("", os.path.basename(self.filename))
        os.makedirs(Util.get_abs_path(package_dir, out_dir), exist_ok=True)
        with LineReader.OpenFileCM(java_file, "wt") as hdr_cm:
            if not hdr_cm:
                Log.E("Could not open {}: {}".format(self.filename,
                                                     hdr_cm.get_err_msg()))

            hdr_file = hdr_cm.get_file()

            # File header
            hdr_file.write("\n".join([
                "/* GENERATED BY GLOBIFEST -- DO NOT EDIT */", "",
                "package {}".format(package_name), "",
                "public final class {}".format(class_name), "{\n"
            ]))

            # Add values
            template = "    public final static {} {} = {};\n"
            for d in definitions:
                ptype = d.param.get_type()
                pid = d.param.get_identifier()
                value = d.value

                # Write implicit values first
                implicit_id = None
                for implicit_value in d.param.get_implicit_values():
                    hdr_file.write(
                        template.format("int", implicit_value[0],
                                        implicit_value[1]))
                    if value == implicit_value[1]:
                        implicit_id = implicit_value[0]

                # Write the parameter
                if ptype == DefTree.PARAM_TYPE.INT:
                    hdr_file.write(template.format("int", pid, value))
                elif ptype == DefTree.PARAM_TYPE.FLOAT:
                    # Default type is double precision
                    hdr_file.write(template.format("double", pid, value))
                elif ptype == DefTree.PARAM_TYPE.STRING:
                    hdr_file.write(template.format("String", pid, value))
                elif ptype == DefTree.PARAM_TYPE.BOOL:
                    hdr_file.write(
                        template.format("boolean", pid, value.lower()))
                elif ptype == DefTree.PARAM_TYPE.ENUM:
                    if implicit_id:
                        value = implicit_id
                    hdr_file.write(template.format("int", pid, value))
                else:
                    # TODO: Handle more complex literal types
                    Log.E("Unhandled value of type {}".format(
                        str(d.param.ptype)))

            # File footer
            hdr_file.write("}\n")
예제 #4
0
    def create_parser(self, configs = Util.Container()):
        # The manifest and reader are not under test, but simple enough to use directly
        self.manifest = Helpers.new_manifest()
        self.configs = Helpers.new_settings(configs)
        self.parser = ManifestParser.new(self.manifest, self.configs, debug_mode=True, validate_files=False)

        # The reader is not under test, but it provides a good way to feed strings to the parser
        self.reader = LineReader.new(self.parser)
예제 #5
0
def build_definition(in_fname):
    """
      Build a definition
    """
    def_tree = DefTree.new(in_fname)
    parser = DefinitionParser.new(def_tree)
    reader = LineReader.new(parser)

    reader.read_file_by_name(in_fname)
    return def_tree
예제 #6
0
def build_config(in_fname):
    """
      Build a config
    """
    config = Config.new(in_fname)
    parser = ConfigParser.new(config)
    reader = LineReader.new(parser)

    reader.read_file_by_name(in_fname)
    return config
예제 #7
0
def build_manifest(in_fname, settings, pkg_root, validate_files=True):
    """
      Build a manifest with the given settings
    """
    manifest = Manifest.new(in_fname, pkg_root)
    parser = ManifestParser.new(manifest,
                                settings,
                                validate_files=validate_files,
                                def_parser=build_definition)
    reader = LineReader.new(parser)

    reader.read_file_by_name(in_fname)
    return manifest
예제 #8
0
    def _include_file(self, filename):
        """
            Include the contents of another file as if it was directly placed in this file
        """
        abs_filename = Util.get_abs_path(filename, self.prj_root)

        # Save the project root so that files paths can be relative to the included file
        old_prj_root = self.prj_root
        self.prj_root = os.path.dirname(abs_filename)

        # Read the file using this object as the parser
        reader = LineReader.new(self, do_end=False)
        reader.read_file_by_name(abs_filename)

        # Restore the original project root
        self.prj_root = old_prj_root
예제 #9
0
def read_project(in_fname, out_dir):
    """
        Read project

        @return Tuple containing (Project object with parsed result,
            Project directory, output directory)
    """
    project = Project.new(in_fname, err_fatal=True)
    parser = ProjectParser.new(project)
    reader = LineReader.new(parser)

    reader.read_file_by_name(in_fname)
    cwd = os.getcwd()
    prj_dir = Util.get_abs_path(os.path.dirname(project.get_filename()), cwd)
    out_dir = Util.get_abs_path(out_dir, cwd)

    return (project, prj_dir, out_dir)
예제 #10
0
    def _include_file(self, filename):
        """
            Include the contents of another file as if it was directly placed in this file
        """
        abs_filename = Util.get_abs_path(filename, self.pkg_root)

        if not self.validate_files:
            # For testing, just add the include file as a source
            self.debug("ADD_AUX: {}".format(abs_filename))
            self.manifest.add_entry("aux_files", abs_filename)
            return

        # Save the package root so that files paths can be relative to the included file
        old_pkg_root = self.pkg_root
        self.pkg_root = os.path.dirname(abs_filename)

        # Read the file using this object as the parser
        reader = LineReader.new(self, do_end=False)
        reader.read_file_by_name(abs_filename)

        # Restore the original package root
        self.pkg_root = old_pkg_root
예제 #11
0
 def setUp(self):
     self.parser = Helpers.new_parser()
     self.reader = LineReader.new(self.parser)
예제 #12
0
    def generate(self, definitions, out_dir):
        """Generate a settings file"""
        include_guard = os.path.relpath(self.filename, start=out_dir)
        include_guard = "_{}".format(
            CGenerator.INCLUDE_GUARD_REPLACE.sub("_", include_guard))
        include_guard = include_guard.upper()
        with LineReader.OpenFileCM(self.filename, "wt") as hdr_cm:
            if not hdr_cm:
                Log.E("Could not open {}: {}".format(self.filename,
                                                     hdr_cm.get_err_msg()))

            hdr_file = hdr_cm.get_file()

            # File header
            hdr_file.write("\n".join([
                "/* GENERATED BY GLOBIFEST -- DO NOT EDIT */", "",
                "#ifndef {}".format(include_guard),
                "#define {}".format(include_guard), "\n"
            ]))

            # Add values; preprocessor directives are used for maximum type flexibility
            for d in definitions:
                ptype = d.param.get_type()
                pid = d.param.get_identifier()
                value = d.value

                # Write implicit values first
                implicit_id = None
                for implicit_value in d.param.get_implicit_values():
                    hdr_file.write("#define {} ({})\n".format(
                        implicit_value[0], implicit_value[1]))
                    if value == implicit_value[1]:
                        implicit_id = implicit_value[0]

                # Write the parameter
                if ptype in [DefTree.PARAM_TYPE.INT, DefTree.PARAM_TYPE.FLOAT]:
                    # Parentheses prevent conflicts with surrounding code
                    # Default type of INT is int (i.e., signed  literal)
                    # Default type of FLOAT is double precision
                    hdr_file.write("#define {} ({})\n".format(pid, value))
                elif ptype == DefTree.PARAM_TYPE.STRING:
                    # Strings are not surrounded to allow compile-time concatenation
                    hdr_file.write("#define {} {}\n".format(pid, value))
                elif ptype == DefTree.PARAM_TYPE.BOOL:
                    # Define as 1/0, since C89 did not define TRUE and FALSE values
                    if value == "FALSE":
                        value = 0
                    else:
                        value = 1
                    hdr_file.write("#define {} ({})\n".format(pid, value))
                elif ptype == DefTree.PARAM_TYPE.ENUM:
                    if implicit_id:
                        hdr_file.write("#define {} {}\n".format(
                            pid, implicit_id))
                    else:
                        hdr_file.write("#define {} ({})\n".format(pid, value))
                else:
                    # TODO: Handle more complex literal types:
                    # - Integral types U/L/UL/LL/ULL
                    # - Float suffixes F/L for floats
                    Log.E("Unhandled value of type {}".format(
                        str(d.param.ptype)))

            # File footer
            hdr_file.write("\n#endif /* {} */\n".format(include_guard))
예제 #13
0
    def create_parser(self):
        self.project = Project.new()
        self.parser = ProjectParser.new(self.project, debug_mode=True)

        # The reader is not under test, but it provides a good way to feed strings to the parser
        self.reader = LineReader.new(self.parser)