def test_get_project_package(self): """ A handful of projects were created by ocpidev in setUp and listed in PROJECT_PACKAGES. Each one has different project name and package settings. Verify that each is correct from different CWDs. """ logging.info("===========================\nTesting 'get_project_package'") for proj, pkg in list(PROJECT_PACKAGES.items()): logging.info("Project \"" + proj + "\" should have full-package: " + pkg) self.assertEqual(ocpiutil.get_project_package(proj), pkg) logging.info("---------------------------") logging.info("Rerunning full-package name tests from " + "within subdirs in each project.") for proj, pkg in list(PROJECT_PACKAGES.items()): with ocpiutil.cd(proj): logging.info("Project \"" + proj + "\" should have full-package: " + pkg) self.assertEqual(ocpiutil.get_project_package(), pkg) self.assertEqual(ocpiutil.get_project_package("."), pkg) components_dir = "components" if not os.path.exists("components"): components_dir = "lib/components" if os.path.exists(components_dir): with ocpiutil.cd(components_dir): self.assertEqual(ocpiutil.get_project_package(), pkg) self.assertEqual(ocpiutil.get_project_package("."), pkg) self.assertEqual(ocpiutil.get_project_package(".."), pkg) logging.info("---------------------------") logging.info("Project package for a directory outside a project " + "or invalid should be None.") self.assertEqual(ocpiutil.get_project_package("/"), None) self.assertEqual(ocpiutil.get_project_package(None), None)
def contains(self, package_id=None, directory=None): """ Given a project's package-ID or directory, determine if the project is present in this registry. """ # If neither package_id nor directory are provided, exception if package_id is None: if directory is None: raise ocpiutil.OCPIException( "Could determine whether project exists because " + "the package_id and directory provided were both " + "None.\nProvide one or both of these arguments.") # Project's package-ID is determined from its directory if not provided package_id = ocpiutil.get_project_package(directory) if package_id is None: raise ocpiutil.OCPIException( "Could not determine package-ID of project located " + "at \"" + directory + "\".\nDouble check this " + "configurations.") # If the project is registered here by package-ID, return True. # If not, or if a different project is registered here with that pacakge-ID, return False if package_id in self.__projects: # pylint:disable=bad-continuation if (directory is not None and os.path.realpath(directory) != self.__projects[package_id].directory): logging.warning("Registry at \"" + self.directory + "\" contains a project with " + "package-ID \"" + package_id + "\", but it is not the same " + "project as \"" + directory + "\".") return False # pylint:enable=bad-continuation return True return False
def remove(self, package_id=None, directory=None): """ Given a project's package-ID or directory, determine if the project is present in this registry. If so, remove it from this registry's __projects dictionary and remove the registered symlink. """ logging.debug("package_id=" + str(package_id) + " directory=" + str(directory)) if package_id is None: package_id = ocpiutil.get_project_package(directory) if package_id is None: raise ocpiutil.OCPIException( "Could not unregister project located at \"" + directory + "\" because the project's package-ID " + "could not be determined.\nIs it really a project?") if package_id not in self.__projects: link_path = self.directory + "/" + package_id if os.path.exists(link_path) and not os.path.exists( os.readlink(link_path)): logging.debug( "Removing the following broken link from the registry:\n" + link_path + " -> " + os.readlink(link_path)) self.remove_link(package_id) print("Successfully unregistered the " + package_id + " project: " + os.path.realpath(directory) + "\nFrom the registry: " + os.path.realpath(self.directory) + "\n") return raise ocpiutil.OCPIException( "Could not unregister project with package-ID \"" + package_id + "\" because the project is not in the " + "registry.\n Run 'ocpidev show registry --table' for " + "information about the currently registered projects.\n") # if a project is deleted from disk underneath our feet this could be None (AV-4483) if self.__projects[package_id] is not None: project_link = self.__projects[package_id].directory if directory is not None and os.path.realpath( directory) != project_link: raise ocpiutil.OCPIException( "Failure to unregister project with package '" + package_id + "'.\nThe registered project with link '" + package_id + " --> " + project_link + "' does not " + "point to the specified project '" + os.path.realpath(directory) + "'." + "\nThis " + "project does not appear to be registered.") if directory is None: directory = str(self.__projects[package_id].directory) # Remove the symlink registry/package-ID --> project self.remove_link(package_id) # Remove the project from this registry's dict self.__projects.pop(package_id) print("Successfully unregistered the " + package_id + " project: " + os.path.realpath(directory) + "\nFrom the registry: " + os.path.realpath(self.directory) + "\n")
def get_all_dict(cls): """ returns a dictionary with all available rcc platforms from the RccAllPlatforms make variable """ rcc_dict = ocpiutil.get_make_vars_rcc_targets() try: rcc_plats = rcc_dict["RccAllPlatforms"] except TypeError: raise ocpiutil.OCPIException( "No RCC platforms found. Make sure the core project is " + "registered or in the OCPI_PROJECT_PATH.") plat_dict = {} for plat in rcc_plats: plat_dict[plat] = {} plat_dict[plat]["target"] = rcc_dict["RccTarget_" + plat][0] proj_top = ocpiutil.get_project_package(rcc_dict["RccPlatDir_" + plat][0]) plat_dict[plat]["package_id"] = proj_top + ".platform." + plat plat_dict[plat]["directory"] = rcc_dict["RccPlatDir_" + plat][0] return plat_dict
def collect_projects_from_path(cls): """ Finds all projects in the OCPI_PROJECT_PATH envriment variable and in the registry """ project_path = os.environ.get('OCPI_PROJECT_PATH') projects_from_env = {} if not project_path is None and not project_path.strip() == "": projects_from_path = project_path.split(':') for proj in projects_from_path: proj_package = ocpiutil.get_project_package(proj) if proj_package is None: proj_package = os.path.basename(proj.rstrip("/")) proj_exists = False else: proj_exists = True projects_from_env[proj_package] = {} projects_from_env[proj_package]["exists"] = proj_exists projects_from_env[proj_package]["registered"] = False projects_from_env[proj_package][ "real_path"] = os.path.realpath(proj) return projects_from_env
def main(): if len(sys.argv) < 2: print("ERROR: need to specify the path to the project") sys.exit(1) if ((len(sys.argv) == 3) and (sys.argv[2] == "force")): force = True else: force = False mydir = sys.argv[1] mydir = ocpiutil.get_path_to_project_top(mydir) if (isStale(mydir, force)): # Get the project name, add it as an attribute in the project element. strings = mydir.split("/") splitLen = strings.__len__() if splitLen == 1: projectName = strings[0] else: projectName = strings[splitLen - 1] full_proj_name = ocpiutil.get_project_package(mydir) root = ET.Element("project", {"name": full_proj_name}) hdl = ET.SubElement(root, "hdl") rcc = ET.SubElement(root, "rcc") assys = ET.SubElement(hdl, "assemblies") prims = ET.SubElement(hdl, "primitives") if os.path.isdir(mydir + "/specs"): top_specs = ET.SubElement(root, "specs") addSpecs(top_specs, mydir) comps = None if os.path.isdir(mydir + "/components"): comps = ET.SubElement(root, "components") if os.path.isdir(mydir + "/applications"): apps = ET.SubElement(root, "applications") sub_dirs = onlyfiles = [ dir for dir in os.listdir(mydir + "/applications") if not os.path.isfile(os.path.join(mydir + "/applications", dir)) ] addApplications(apps, sub_dirs, mydir + "/applications") for dirName, subdirList, fileList in os.walk(mydir): if "exports" in dirName or "imports" in dirName: continue elif dirName == mydir + "/components": if ocpiutil.get_dirtype(dirName) == "library": addLibs(comps, ["components"]) else: addLibs(comps, subdirList) elif dirName.endswith("/hdl/platforms"): platforms = ET.SubElement(hdl, "platforms") addPlatforms(platforms, subdirList, dirName) elif dirName.endswith("/rcc/platforms"): platforms = ET.SubElement(rcc, "platforms") addPlatforms(platforms, subdirList, dirName) elif dirName.endswith("/cards"): hdlLibs = hdl.findall("libraries") if hdlLibs.__len__() == 0: hdlLibs = [ET.SubElement(hdl, "libraries")] cards = ET.SubElement(hdlLibs[0], "library") cards.set('name', "cards") addWorkers(cards, subdirList, dirName) elif dirName.endswith("/devices"): if "/platforms/" not in dirName: # This is the hdl/devices directory hdlLibs = hdl.findall("libraries") if hdlLibs.__len__() == 0: hdlLibs = [ET.SubElement(hdl, "libraries")] devs = ET.SubElement(hdlLibs[0], "library") devs.set('name', "devices") addWorkers(devs, subdirList, dirName) else: # this is a devices directory under a platform dirSplit = dirName.split('/') platName = [] index = list(range(0, len(dirSplit) - 1)) for i, sub in zip(index, dirSplit): if (sub == "platforms"): platName = dirSplit[i + 1] platformsEl = hdl.findall("platforms") if platformsEl.__len__() > 0: plats = platformsEl[0] platformTag = "platform[@name='" + platName + "']" plat = plats.findall(platformTag) if plat.__len__() > 0: devs = ET.SubElement(plat[0], "library") devs.set('name', "devices") addWorkers(devs, subdirList, dirName) elif dirName.endswith("hdl/assemblies"): addAssemblies(assys, subdirList, dirName) elif dirName.endswith("hdl/primitives"): addPrimitives(prims, subdirList, dirName) retVal = dirIsLib(dirName, comps) if (retVal[0]): addWorkers(retVal[1], subdirList, dirName) print("Updating project metadata...") indent(root) tree = ET.ElementTree(root) tree.write(mydir + "/project.xml") else: print("metadata is not stale, not regenerating")