Beispiel #1
0
def main():
    """
    Function that is called if this module is called as a mian function
    """
    args = parse_cl_vars()
    directory = None
    name = None
    try:
        cur_dir = args['cur_dir']
        with ocpiutil.cd(cur_dir):
            dir_type = ocpiutil.get_dirtype()
            # args['name'] could be None if no name is provided at the command line
            name = args['name']
            directory = ocpiutil.get_ocpidev_working_dir(
                origin_path=".",
                noun=args.get("noun", ""),
                name=name,
                library=args['library'],
                hdl_library=args['hdl_library'],
                hdl_platform=args['hdl_plat_dir'])
            if (name is None) and (dir_type
                                   in [n for n in NOUNS if n != "tests"]):
                name = os.path.basename(os.path.realpath('.'))
            del args['name']
            if args['noun'] is None:
                if dir_type in [n for n in NOUNS if n != "tests"]:
                    args['noun'] = dir_type
                else:
                    raise ocpiutil.OCPIException(
                        "Invalid directory type \"" + str(dir_type) +
                        "\" Valid directory types are: " +
                        ", ".join([n for n in NOUNS if n != "tests"]))
            if ocpiutil.get_dirtype(
                    directory) == "libraries" and args['noun'] == "library":
                args['noun'] = "libraries"
            set_init_values(args, dir_type)
            ocpiutil.logging.debug("creating asset as the following \nnoun: " +
                                   args['noun'] + "\nname: " + str(name) +
                                   "\n" + "directory: " + str(directory) +
                                   "\nargs: " + str(args))
            my_asset = ocpifactory.AssetFactory.factory(
                args['noun'], directory, name, **args)
            sys.exit(my_asset.run())
    except ocpiutil.OCPIException as ex:
        ocpiutil.logging.error(ex)
        if args['noun'] is not None:
            my_noun = '' if args[
                'noun'] is None else " \"" + args['noun'] + "\""
            my_name = '' if name is None else " named \"" + name + "\""
            my_dir = '' if directory is None else " in directory \"" + directory + "\""
            run_dbg = "Unable to run" + my_noun + my_name + my_dir + " due to previous errors"
        else:
            run_dbg = "Unable to run due to previous errors"
        ocpiutil.logging.error(run_dbg)
        sys.exit(1)
Beispiel #2
0
 def test_get_dirtype(self):
     """
     Iterate through the PROJECT0_DIRTYPES dictionary and confirm that
     each directory in PROJECT0 is recognized by the correct dirtype.
     """
     logging.info("===========================\nTesting 'get_dirtype'")
     for path, dirtype in list(PROJECT0_DIRTYPES.items()):
         logging.info("\nDirtype for path '" + path + "' should be: " + dirtype + "\n")
         self.assertEqual(ocpiutil.get_dirtype(PROJECT0 + "/" + path), dirtype)
     logging.info("---------------------------")
     logging.info("Dirtype for path '.' should be: None")
     self.assertEqual(ocpiutil.get_dirtype(), None)
Beispiel #3
0
 def remove(cls, directory=None, instance=None):
     """
     Removes an instance from the static class variable __assets by dierectory or the instance
     itself.  Throws an exception if neither optional argument is provided.
     """
     if directory is not None:
         real_dir = os.path.realpath(directory)
         import _opencpi.assets.project
         import _opencpi.assets.registry
         dirtype_dict = {
             "project": _opencpi.assets.project.Project,
             "registry": _opencpi.assets.registry.Registry
         }
         dirtype = ocpiutil.get_dirtype(real_dir)
         cls.__assets[dirtype_dict[dirtype]].pop(real_dir, None)
     elif instance is not None:
         cls.__assets[instance.__class__] = {
             k: v
             for k, v in cls.__assets[instance.__class__].items()
             if v is not instance
         }
     else:
         raise ocpiutil.OCPIException(
             "Invalid use of AssetFactory.remove() both directory " +
             "and instance are None.")
Beispiel #4
0
def get_directory(args, name, lib):
    """
    Determine the correct directory for the asset that should be run given the cmd line arguments
    passed in
    """
    if lib == name:
        lib = ""
    dir_type = ocpiutil.get_dirtype()
    ret_val = os.path.realpath('.')
    if args['noun'] == "applications":
        ret_val = ocpiutil.get_path_to_project_top() + "/applications"
    elif args['noun'] in [
            "test", "library"
    ] and name and name != os.path.basename(os.path.realpath('.')):
        ret_val = os.path.realpath('.') + "/" + lib + "/" + name
        if args['noun'] == "test" and not ret_val.endswith(".test"):
            ret_val = ret_val + ".test"
    elif args['noun'] in ["test", "library"]:
        ret_val = os.path.realpath('.')
    elif args['noun'] == "application":
        if dir_type == "project" and name != os.path.basename(
                os.path.realpath('.')):
            ret_val = os.path.realpath('.') + "/applications/" + name
        elif dir_type == "applications" and name != os.path.basename(
                os.path.realpath('.')):
            ret_val = os.path.realpath('.') + "/" + name
    return ret_val
Beispiel #5
0
    def check_dirtype(dirtype, directory):
        """
        checks that the directory passed in is of the type passed in and if not an exception is
        thrown
        """
        if not os.path.isdir(directory):
            raise ocpiutil.OCPIException("Expected directory of type \"" +
                                         dirtype + "\" for a " +
                                         "directory that does not exist \"" +
                                         directory + "\"")

        if ocpiutil.get_dirtype(directory) != dirtype:
            raise ocpiutil.OCPIException("Expected directory of type \"" +
                                         dirtype + "\", but " +
                                         "found type \"" +
                                         str(ocpiutil.get_dirtype(directory)) +
                                         "\" for directory \"" + directory +
                                         "\"")
Beispiel #6
0
def main():
    (args, noun) = parseCLVars()
    action = "show"
    try:
        cur_dir = args['cur_dir']
        dir_type = ocpiutil.get_dirtype()
        # args['name'] could be None if no name is provided at the command line
        name = args.get("name", None)
        if not name:
            name = ""
        # Now that we have grabbed name, delete it from the args that will be passed into the
        # AssetFactory because name is explicitly passed to AssetFactory as a separate argument
        del args['name']

        check_scope_options(args.get("scope", None), noun)

        if noun in ["registry", "projects"]:
            directory = ocpiregistry.Registry.get_registry_dir()
        elif noun not in ["libraries", "hdlplatforms", "hdltargets", "rccplatforms", "rcctargets", "platforms", "targets"]:
            directory = ocpiutil.get_ocpidev_working_dir(origin_path=cur_dir,
                                                         noun=noun,
                                                         name=name,
                                                         library=args['library'],
                                                         hdl_library=args['hdl_library'],
                                                         hdl_platform=args['hdl_plat_dir'])
        elif noun == "libraries":
            directory = ocpiutil.get_path_to_project_top()
        else:
            directory = ""

        ocpiutil.logging.debug('Choose directory "' + directory + '" to operate in')
        if noun is None:
            noun = dir_type
        #TODO the noun libraries could be an asset or a plural need to add logic to deal with
        #     determining which one it is.  for now we are going to assume that it is always a
        #     plural

        # If name is not set, set it to the current directory's basename
        if name == "." and dir_type in DIR_TYPES:
            name = os.path.basename(os.path.realpath(cur_dir))
        set_init_values(args, noun)
        plural_noun_list = NOUN_PLURALS + ["hdlplatforms", "hdltargets", "rccplatforms",
                                           "rcctargets", "platforms", "targets"]
        if noun in plural_noun_list:
            (noun, action) = get_noun_from_plural(directory, args, noun)
        ocpiutil.logging.debug('Choose noun: "' + str(noun) + '" and action: "' + action +'"')
        if noun not in [None, "hdlplatforms", "hdltargets", "rccplatforms", "rcctargets",
                        "platforms", "targets", "projects"]:
           my_asset = ocpifactory.AssetFactory.factory(noun, directory, "", **args)
           action = ("my_asset." + action + '("' + args["details"] + '", ' +
                    str(args["verbose"]) + ')')

        ocpiutil.logging.debug("running show action: '" + action + "'")
        #not using anything that is user defined so eval is fine
        eval(action)
    except ocpiutil.OCPIException as ex:
        ocpiutil.logging.error(ex)
Beispiel #7
0
 def delete(self, force=False):
     """
     Remove the Asset from disk.  Any additional cleanup on a per asset basis can be done in
     the child implementations of this function
     """
     if not force:
         prompt = ("removing " + ocpiutil.get_dirtype(self.directory) +
                   " at directory: " + self.directory)
         force = ocpiutil.get_ok(prompt=prompt)
     if force:
         shutil.rmtree(self.directory)
Beispiel #8
0
    def show_utilization(self):
        """
        Show the utilization Report for this asset and print/record the results.

        This default behavior is likely sufficient, but subclasses that are collections of OpenCPI
        assets may override this function to instead iterate over children assets and call their
        show_utilization functions.
        """
        # Get the directory type to add in the header/caption for the utilization info
        dirtype = ocpiutil.get_dirtype(self.directory)
        if dirtype is None:
            dirtype = "asset"
        caption = "Resource Utilization Table for " + dirtype + " \"" + self.name + "\""

        # Get the utilization using this class' hopefully overridden get_utilization() function
        util_report = self.get_utilization()
        if not util_report:
            if dirtype == "hdl-platform":
                plat_obj = hdltargets.HdlToolFactory.factory(
                    "hdlplatform", self.name)
                if not plat_obj.get_toolset().is_simtool:
                    logging.warning("Skipping " + caption +
                                    " because the report is empty")
            else:
                logging.warning("Skipping " + caption +
                                " because the report is empty")
            return

        if self.output_format not in self.valid_formats:
            raise ocpiutil.OCPIException(
                "Valid formats for showing utilization are \"" +
                ", ".join(self.valid_formats) + "\", but \"" +
                self.output_format + "\" was chosen.")
        if self.output_format == "table":
            print(caption)
            # Maybe Report.print_table() should accept caption as well?
            # print the Report as a table
            util_report.print_table()
        if self.output_format == "latex":
            logging.info("Generating " + caption)
            # Record the utilization in LaTeX in a utilization.inc file for this asset
            util_file_path = self.directory + "/utilization.inc"
            with open(util_file_path, 'w') as util_file:
                # Get the LaTeX table string, and write it to the utilization file
                latex_table = util_report.get_latex_table(caption=caption)
                # Only write to the file if the latex table is non-empty
                if latex_table != "":
                    util_file.write(latex_table)
                    logging.info("  LaTeX Utilization Table was written to: " +
                                 util_file_path + "\n")
Beispiel #9
0
 def __init_package_id(self):
     """
     Determine the Package id based on the libary or project that the Worker resides in.  only
     a component will reside at the top level of a project.
     """
     parent_dir = self.directory + "/../"
     if ocpiutil.get_dirtype(parent_dir) == "library":
         ret_val = ocpiutil.set_vars_from_make(
             mk_file=parent_dir + "/Makefile",
             mk_arg="showpackage ShellLibraryVars=1",
             verbose=True)["Package"][0]
     elif ocpiutil.get_dirtype(parent_dir) == "project":
         ret_val = ocpiutil.set_vars_from_make(
             mk_file=parent_dir + "/Makefile",
             mk_arg="projectpackage ShellProjectVars=1",
             verbose=True)["ProjectPackage"][0]
     elif ocpiutil.get_dirtype(parent_dir) == "hdl-platforms":
         ret_val = "N/A"
     else:
         raise ocpiutil.OCPIException(
             "Could not determine Package-ID of component dirtype of " +
             "parent directory: " + parent_dir + " dirtype: " +
             str(ocpiutil.get_dirtype(parent_dir)))
     return ret_val
Beispiel #10
0
    def show_config_params_report(self):
        """
        Print out the Report of this Worker's configuration parameters.
        Each row will represent a single configuration, with each column representing
        either the Configuration index or a parameter value.

        Modes can be:
            table: plain text table to terminal
            latex: print table in LaTeX format to configurations.inc file in this
                   HdlLibraryWorker's directory
        """
        # TODO should this function and its output modes be moved into a superclass?
        dirtype = ocpiutil.get_dirtype(self.directory)
        caption = "Table of Worker Configurations for " + str(
            dirtype) + ": " + str(self.name)
        if self.output_format == "table":
            print(caption)
            # Print the resulting Report as a table
            self.get_config_params_report().print_table()
        elif self.output_format == "latex":
            logging.info("Generating " + caption)
            # Record the report in LaTeX in a configurations.inc file for this asset
            util_file_path = self.directory + "/configurations.inc"
            with open(util_file_path, 'w') as util_file:
                # Get the LaTeX table string, and write it to the configurations file
                latex_table = self.get_config_params_report().get_latex_table(
                    caption=caption)
                # Only write to the file if the latex table is non-empty
                if latex_table != "":
                    util_file.write(latex_table)
                    logging.info(
                        "  LaTeX Configurations Table was written to: " +
                        util_file_path + "\n")
        else:
            raise ocpiutil.OCPIException(
                "Valid formats for showing worker configurations are \"" +
                ", ".join(self.valid_formats) + "\", but \"" +
                self.output_format + "\" was chosen.")
Beispiel #11
0
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")
Beispiel #12
0
    def get_ocpigen_metadata(self, xml_file):
        """
        Ask ocpigen (the code generator)  to parse the worker(OWD) or component(OCS) xml file and
        spit out an artifact xml that this fuction parses into class variables.
          property_list - list of every property, each property in this list will be a dictionary of
                          all the xml attributes asscoiated with it from the artifact xml
          port_list     - list of every port each port in this list will be a dictionary of
                          all the xml attributes asscoiated with it from the artifact xml some of
                          which are unused
          slave_list    - list of every slave worker's name expected to be blank for an OCS

        Function attribtes:
          xml_file - the file to have ocpigen parse
        """
        #get list of locations to look for include xmls from make
        if ocpiutil.get_dirtype(self.directory + "/../") == "library":
            xml_dirs = ocpiutil.set_vars_from_make(
                mk_file=self.directory + "/../" + "/Makefile",
                mk_arg="showincludes ShellLibraryVars=1",
                verbose=True)["XmlIncludeDirsInternal"]
        elif ocpiutil.get_dirtype(self.directory + "/../") == "project":
            xml_dirs = ocpiutil.set_vars_from_make(
                mk_file=self.directory + "/../" + "/Makefile",
                mk_arg="projectincludes ShellProjectVars=1",
                verbose=True)["XmlIncludeDirsInternal"]
        #call ocpigen -G
        ocpigen_cmd = [
            "ocpigen", "-G", "-O", "none", "-V", "none", "-H", "none"
        ]
        for inc_dir in xml_dirs:
            ocpigen_cmd.append("-I")
            ocpigen_cmd.append(inc_dir)

        ocpigen_cmd.append(xml_file)
        ocpiutil.logging.debug("running ocpigen cmd: " + str(ocpigen_cmd))
        old_log_level = os.environ.get("OCPI_LOG_LEVEL", "0")
        os.environ["OCPI_LOG_LEVEL"] = "0"
        comp_xml = subprocess.Popen(ocpigen_cmd,
                                    stdout=subprocess.PIPE).communicate()[0]
        os.environ["OCPI_LOG_LEVEL"] = old_log_level

        #put xml ouput file into an ElementTree object
        ocpiutil.logging.debug("Component Artifact XML from ocpigen: \n" +
                               str(comp_xml))
        try:
            parsed_xml = ET.fromstring(comp_xml)
        except ET.ParseError:
            raise ocpiutil.OCPIException(
                "Error with xml file from ocpigen.\n\nocpigen command: " +
                str(ocpigen_cmd) + "\n\nxml output: \n" + str(comp_xml))

        self.property_list = []
        self.port_list = []
        self.slave_list = []
        #set up self.property_list from the xml
        for props in parsed_xml.findall("property"):
            temp_dict = props.attrib
            enum = temp_dict.get("enums")
            if enum:
                temp_dict["enums"] = enum.split(",")
            if props.attrib.get("type") == "Struct":
                temp_dict["Struct"] = self.get_struct_dict_from_xml(
                    props.findall("member"))
            self.property_list.append(temp_dict)
        #set up self.port_list from this dict
        for port in parsed_xml.findall("port"):
            port_details = port.attrib
            for child in port:
                if child.tag == "protocol":
                    port_details["protocol"] = child.attrib.get(
                        "padding", "N/A")
            self.port_list.append(port_details)
        #set up self.slave_list from the xml
        for slave in parsed_xml.findall("slave"):
            self.slave_list.append(slave.attrib["worker"])
Beispiel #13
0
def main():
    """
    Function that is called if this module is called as a mian function
    """
    args = parse_cl_vars()
    try:
        cur_dir = args['cur_dir']
        with ocpiutil.cd(cur_dir):
            cur_dir_basename = os.path.basename(os.path.realpath("."))

            name = args['name']
            # Now that we have grabbed name, delete it from the args that will be passed into the
            # AssetFactory because name is explicitly passed to AssetFactory as a separate argument
            del args['name']

            # From the current directory and directory-modifier options, determine
            # the directory to operate from for this ocpidev command
            directory = ocpiutil.get_ocpidev_working_dir(
                origin_path=".",
                noun=args.get("noun", ""),
                name=name,
                library=args['library'],
                hdl_library=args['hdl_library'],
                hdl_platform=args['hdl_plat_dir'])

            ocpiutil.logging.debug('Choose directory "' + directory +
                                   '" to operate in')

            dir_type = ocpiutil.get_dirtype(directory)

            # Check dir_type for errors:
            # If there is no noun, and the working directory is not a supported type
            if args['noun'] is None and dir_type not in DIR_TYPES:
                raise ocpiutil.OCPIException('Invalid directory type "' +
                                             str(dir_type) +
                                             '" Valid directory types are: ' +
                                             ", ".join(DIR_TYPES))

            # If name is not set, set it to the current directory's basename
            if name == "." and dir_type in DIR_TYPES:
                name = cur_dir_basename

            # Initialize settings to be used by Asset classes
            set_init_values(args, dir_type)

            # Check worker authoring model and strip authoring model for Worker construction
            if args['noun'] == "worker" or (args['noun'] is None
                                            and dir_type == "worker"):
                if not name.endswith(".hdl"):
                    ocpiutil.logging.warning(
                        "Can only show utilization for workers of authoring " +
                        "model 'hdl'.")
                    sys.exit()
                name = name.rsplit('.', 1)[0]

            ocpiutil.logging.debug(
                "Creating asset object with the following \nname: " +
                str(name) + "\n" + "directory: " + str(directory) +
                "\nargs: " + str(args))

            my_asset = ocpifactory.AssetFactory.factory(
                dir_type, directory, name, **args)

            my_asset.show_utilization()

    except ocpiutil.OCPIException as ex:
        ocpiutil.logging.error(ex)
        # Construct error message and exit
        if args['noun'] is not None:
            my_noun = '' if args['noun'] is None else ' "' + args['noun'] + '"'
            my_name = '' if name == "." else ' named "' + name + '"'
            my_dir = ' in directory "' + args['cur_dir'] + '"'
            ocpiutil.logging.error("Unable to show utilization for " +
                                   my_noun + my_name + my_dir +
                                   " due to previous errors")
        else:
            ocpiutil.logging.error(
                "Unable to show utilization due to previous errors.")
        sys.exit(1)