Example #1
0
 def __init__(self, config):
     self.config = config
     self.db = CoreDB()
     self._lm = LibraryManager(config.library_root)
Example #2
0
class CoreManager:
    def __init__(self, config):
        self.config = config
        self.db = CoreDB()
        self._lm = LibraryManager(config.library_root)

    def find_cores(self, library):
        found_cores = []
        path = os.path.expanduser(library.location)
        exclude = {".git"}
        if os.path.isdir(path) == False:
            raise OSError(path + " is not a directory")
        logger.debug("Checking for cores in " + path)
        for root, dirs, files in os.walk(path, followlinks=True):
            if "FUSESOC_IGNORE" in files:
                del dirs[:]
                continue
            dirs[:] = [directory for directory in dirs if directory not in exclude]
            for f in files:
                if f.endswith(".core"):
                    core_file = os.path.join(root, f)
                    try:
                        core = Core(core_file, self.config.cache_root)
                        found_cores.append(core)
                    except SyntaxError as e:
                        w = "Parse error. Ignoring file " + core_file + ": " + e.msg
                        logger.warning(w)
                    except ImportError as e:
                        w = 'Failed to register "{}" due to unknown provider: {}'
                        logger.warning(w.format(core_file, str(e)))
        return found_cores

    def _load_cores(self, library):
        found_cores = self.find_cores(library)
        for core in found_cores:
            self.db.add(core, library)

    def add_library(self, library):
        """ Register a library """
        abspath = os.path.abspath(os.path.expanduser(library.location))
        _library = self._lm.get_library(abspath, "location")
        if _library:
            _s = "Not adding library {} ({}). Library {} already registered for this location"
            logger.warning(_s.format(library.name, abspath, _library.name))
            return

        self._load_cores(library)
        self._lm.add_library(library)

    def get_libraries(self):
        """ Get all registered libraries """
        return self._lm.get_libraries()

    def get_depends(self, core, flags):
        """ Get an ordered list of all dependencies of a core

        All direct and indirect dependencies are resolved into a dependency
        tree, the tree is flattened, and an ordered list of dependencies is
        created.

        The first element in the list is a leaf dependency, the last element
        is the core at the root of the dependency tree.
        """
        logger.debug(
            "Calculating dependencies for {}{} with flags {}".format(
                core.relation, str(core), str(flags)
            )
        )
        resolved_core = self.db.find(core)
        deps = self.db.solve(resolved_core.name, flags)
        logger.debug(" Resolved core to {}".format(str(resolved_core.name)))
        logger.debug(" with dependencies " + ", ".join([str(c.name) for c in deps]))
        return deps

    def get_cores(self):
        """ Get a dict with all cores, indexed by the core name """
        return {str(x.name): x for x in self.db.find()}

    def get_core(self, name):
        """ Get a core with a given name """
        c = self.db.find(name)
        c.name.relation = "=="
        return c

    def get_generators(self):
        """ Get a dict with all registered generators, indexed by name """
        generators = {}
        for core in self.db.find():
            if hasattr(core, "get_generators"):
                _generators = core.get_generators({})
                if _generators:
                    generators[str(core.name)] = _generators
        return generators
Example #3
0
class CoreManager(object):
    def __init__(self, config):
        self.config = config
        self.db = CoreDB()
        self._lm = LibraryManager(config.library_root)

    def load_cores(self, library):
        path = os.path.expanduser(library.location)
        if os.path.isdir(path) == False:
            raise IOError(path + " is not a directory")
        logger.debug("Checking for cores in " + path)
        for root, dirs, files in os.walk(path, followlinks=True):
            if "FUSESOC_IGNORE" in files:
                del dirs[:]
                continue
            for f in files:
                if f.endswith(".core"):
                    core_file = os.path.join(root, f)
                    try:
                        core = Core(core_file, self.config.cache_root)
                        self.db.add(core, library)
                    except SyntaxError as e:
                        w = "Parse error. Ignoring file " + core_file + ": " + e.msg
                        logger.warning(w)
                    except ImportError as e:
                        w = 'Failed to register "{}" due to unknown provider: {}'
                        logger.warning(w.format(core_file, str(e)))

    def add_library(self, library):
        abspath = os.path.abspath(os.path.expanduser(library.location))
        _library = self._lm.get_library(abspath, "location")
        if _library:
            _s = "Not adding library {} ({}). Library {} already registered for this location"
            logger.warning(_s.format(library.name, abspath, _library.name))
            return

        self.load_cores(library)
        self._lm.add_library(library)

    def get_libraries(self):
        return self._lm.get_libraries()

    def get_depends(self, core, flags):
        logger.debug("Calculating dependencies for {}{} with flags {}".format(
            core.relation, str(core), str(flags)))
        resolved_core = self.db.find(core)
        deps = self.db.solve(resolved_core.name, flags)
        logger.debug(" Resolved core to {}".format(str(resolved_core.name)))
        logger.debug(" with dependencies " +
                     ", ".join([str(c.name) for c in deps]))
        return deps

    def get_cores(self):
        return {str(x.name): x for x in self.db.find()}

    def get_core(self, name):
        c = self.db.find(name)
        c.name.relation = "=="
        return c

    def get_generators(self):
        generators = {}
        for core in self.db.find():
            if hasattr(core, "get_generators"):
                _generators = core.get_generators({})
                if _generators:
                    generators[str(core.name)] = _generators
        return generators
Example #4
0
class CoreManager:
    def __init__(self, config):
        self.config = config
        self.db = CoreDB()
        self._lm = LibraryManager(config.library_root)

    def find_cores(self, library):
        found_cores = []
        path = os.path.expanduser(library.location)
        exclude = {".git"}
        if os.path.isdir(path) == False:
            raise OSError(path + " is not a directory")
        logger.debug("Checking for cores in " + path)
        for root, dirs, files in os.walk(path, followlinks=True):
            if "FUSESOC_IGNORE" in files:
                del dirs[:]
                continue
            dirs[:] = [
                directory for directory in dirs if directory not in exclude
            ]
            for f in files:
                if f.endswith(".core"):
                    core_file = os.path.join(root, f)
                    try:
                        if self._detect_capi_version(core_file) != 2:
                            # Skip core files which are not in CAPI2 format.
                            logger.error(
                                "Core file {} is in CAPI1 format, which is not supported "
                                "any more since FuseSoC 2.0. The core file is ignored. "
                                "Please migrate your cores to the CAPI2 file format, or "
                                "use FuseSoC 1.x as stop-gap.".format(
                                    core_file))
                            continue

                        core = Core(
                            core_file,
                            self.config.cache_root,
                        )
                        found_cores.append(core)
                    except SyntaxError as e:
                        w = "Parse error. Ignoring file " + core_file + ": " + e.msg
                        logger.warning(w)
                    except ImportError as e:
                        w = 'Failed to register "{}" due to unknown provider: {}'
                        logger.warning(w.format(core_file, str(e)))
        return found_cores

    def _detect_capi_version(self, core_file) -> int:
        """Detect the CAPI version in a .core file

        Returns:
            Version of the core file (1 or 2)
        """

        with open(core_file) as f:
            l = f.readline().split()
            if l:
                first_line = l[0]
            else:
                first_line = ""
            if first_line == "CAPI=1":
                return 1
            elif first_line == "CAPI=2:":
                return 2
            else:
                error_msg = ("The first line of the core file {} must be "
                             ' "CAPI=1" or "CAPI=2:".'.format(core_file))
                error_msg += '  The first line of this core file is "{}".'.format(
                    first_line)
                if first_line == "CAPI=2":
                    error_msg += "  Just add a colon on the end!"
                logger.warning(error_msg)
                raise ValueError(
                    "Unable to determine CAPI version from core file {}.".
                    format(core_file))

    def _load_cores(self, library):
        found_cores = self.find_cores(library)
        for core in found_cores:
            self.db.add(core, library)

    def add_library(self, library):
        """ Register a library """
        abspath = os.path.abspath(os.path.expanduser(library.location))
        _library = self._lm.get_library(abspath, "location")
        if _library:
            _s = "Not adding library {} ({}). Library {} already registered for this location"
            logger.warning(_s.format(library.name, abspath, _library.name))
            return

        self._load_cores(library)
        self._lm.add_library(library)

    def get_libraries(self):
        """ Get all registered libraries """
        return self._lm.get_libraries()

    def get_depends(self, core, flags):
        """Get an ordered list of all dependencies of a core

        All direct and indirect dependencies are resolved into a dependency
        tree, the tree is flattened, and an ordered list of dependencies is
        created.

        The first element in the list is a leaf dependency, the last element
        is the core at the root of the dependency tree.
        """
        logger.debug("Calculating dependencies for {}{} with flags {}".format(
            core.relation, str(core), str(flags)))
        resolved_core = self.db.find(core)
        deps = self.db.solve(resolved_core.name, flags)
        logger.debug(" Resolved core to {}".format(str(resolved_core.name)))
        logger.debug(" with dependencies " +
                     ", ".join([str(c.name) for c in deps]))
        return deps

    def get_cores(self):
        """ Get a dict with all cores, indexed by the core name """
        return {str(x.name): x for x in self.db.find()}

    def get_core(self, name):
        """ Get a core with a given name """
        c = self.db.find(name)
        c.name.relation = "=="
        return c

    def get_generators(self):
        """ Get a dict with all registered generators, indexed by name """
        generators = {}
        for core in self.db.find():
            if hasattr(core, "get_generators"):
                _generators = core.get_generators({})
                if _generators:
                    generators[str(core.name)] = _generators
        return generators