Example #1
0
    def printModule(self, module, plat, full):
        self.extend = full

        cwd = os.getcwd()

        self.dataTypes = ModuleParser().getDataTypes(plat)

        # print parsing
        print("\n\n{0} MODULE:\n\n".format(module.name.upper()))
        # list topics
        print("TOPICS")
        for topic in module.topics:
            self.printTopic(topic, 1)

        # check for parameters
        if len(module.params) > 0:
            print(
                "\n------------------------------------------------------------\n"
            )

            print("PARAMETERS\n")
            for param in module.params:
                print("\t" + param.name + ":")
                print(getTabs(2) + "type: " + param.type)
                self.printValue(2, "unit", param.unit)
                self.printValue(2, "description", param.desc)
                print(
                    getTabs(2) + "mandatory: " +
                    ("yes" if param.mandatory else "no"))
                self.printValue(2, "value", param.value)
                print("\n")
Example #2
0
def main(args):
    try:
        parser = ModuleParser()
        if args.action == "show":
            extend = args.extend
            module = parser.parseFile(args.filePath)
            ModulePrinter().printModule(module, args.platform, extend)
        elif args.action == "generate":
            path = os.getcwd()

            # if generic package exists we delete it
            if os.path.exists(os.path.join(os.getcwd(), "generated",
                                           "generic")):
                shutil.rmtree(os.path.join(os.getcwd(), "generated",
                                           "generic"))

            generic = parser.parseBase(
                os.path.join(path, "models", "generic", "base.xml"))
            compiler = ModuleCompiler()
            compiler.compileGeneric(generic, args.platform)
            print "Succesfully generated " + args.platform + " implementation of HRIM's generic package."
            os.chdir(path)
            geometry = parser.parseBase(
                os.path.join(path, "models", "geometry", "geometry.xml"))

            # if geometry package exists we delete it
            if os.path.exists(
                    os.path.join(os.getcwd(), "generated", "geometry")):
                shutil.rmtree(
                    os.path.join(os.getcwd(), "generated", "geometry"))

            compiler.compileGeneric(geometry, args.platform, "geometry")
            print "Succesfully generated " + args.platform + " implementation of HRIM's geometry package."
            os.chdir(path)
            # check for file generation shorthands
            if args.filePath == "all":
                fileList = findModels(os.path.join(path, "models"))
                for item in fileList:
                    # if the model isn't a development file process it
                    if not bool(re.search('.*_clean.xml$', item)):
                        os.chdir(path)
                        module = parser.parseFile(item)
                        compiler.compileModule(module, args.platform)
                        print "Succesfully generated " + args.platform + " implementation of " + module.name + " module."
            elif args.filePath == "allClean":
                fileList = findModels(os.path.join(path, "models"))
                for item in fileList:
                    # if the model is a development file process it
                    if bool(re.search('.*_clean.xml$', item)):
                        os.chdir(path)
                        module = parser.parseFile(item)
                        compiler.compileModule(module, args.platform)
                        print "Succesfully generated " + args.platform + " implementation of " + module.name + " module."
            # else try to generate the implementation based on the passed file
            else:
                module = parser.parseFile(args.filePath)
                compiler.compileModule(module, args.platform)
                print "Succesfully generated " + args.platform + " implementation of " + module.name + " module."
        elif args.action == "list":
            if args.filePath == "models":
                modelList = findModels(os.path.join(os.getcwd(), "models"))
                for model in sorted(modelList):
                    if not bool(re.search('.*_clean.xml$', model)):
                        pathList = model.split(os.sep)
                        print pathList[-3] + "/" + pathList[
                            -2] + "/" + pathList[-1].replace('.xml', "")
            elif args.filePath == "implementations":
                impList = os.listdir("generated")
                if len(impList) > 0:
                    for implementation in sorted(impList):
                        print implementation
                else:
                    print "There's no generated implementations."
        elif args.action == "clear":
            if len(os.listdir("generated")) > 0:
                if args.filePath == "all":
                    delDirs = os.listdir("generated")
                    for delPath in sorted(delDirs):
                        fullPath = os.path.join(os.getcwd(), "generated",
                                                delPath)
                        shutil.rmtree(fullPath)
                        print "Deleted " + fullPath
                else:
                    fullPath = os.path.join(os.getcwd(), "generated",
                                            args.filePath)
                    if os.path.exists(fullPath):
                        shutil.rmtree(fullPath)
                        print "Deleted " + fullPath
                    else:
                        print "Couldn't find passed directory for deletion."
            else:
                print "There is no implementation to delete (generated directory is empty)."
        else:
            print "Unknown command"
    except:
        print "An error occurred during command execution"
        sys.exit(1)
Example #3
0
    def compileModule(self, module, plat):
        try:
            messages = False
            services = False
            dependency = False
            self.pkgDeps = []

            cwd = os.getcwd()

            self.dataTypes = ModuleParser().getDataTypes(plat)
            self.msgPkgName = "hrim_" + module.type + "_" + module.name + "_msgs"

            # parse the templates necessary for the package
            os.chdir("templates")
            with open('package.txt', 'r') as myfile:
                pkg = myfile.read()

            # insert the package's name and description in package.xml's content
            srvPkg = pkg.replace(
                "%PKGNAME%",
                "hrim_" + module.type + "_" + module.name + "_srvs")
            srvPkg = srvPkg.replace("%PKGDESC%", module.desc)

            with open('cmake.txt', 'r') as myfile:
                makeFile = myfile.read()

            # insert the package's name and description in CMakeLists.txt's content
            srvMakeFile = makeFile.replace(
                "%PKGNAME%",
                "hrim_" + module.type + "_" + module.name + "_srvs")

            os.chdir(cwd)

            # if the container directory for the generated module doesn't exist, create it
            if not os.path.exists(cwd + "/generated"):
                os.mkdir("generated")
            os.chdir("generated")
            cwd = os.getcwd()

            # if the module directory for the generated files doesn't exist, create it
            if not os.path.exists(cwd + "/" + module.name):
                os.mkdir(module.name)

            os.chdir(module.name)
            cwd = os.getcwd()

            # list of files for CMakeLists.txt
            self.msgFolderPath = ""
            self.ownFiles = []
            srvFiles = []

            # for each topic of the module
            for topic in module.topics:

                # reposition ourselves for each topic
                os.chdir(cwd)

                if topic.type == "publish" or topic.type == "subscribe":
                    messages = True
                    self.processMessage(module, topic)

                elif topic.type == "service":

                    services = True

                    # check if file has already been generated
                    if topic.fileName not in srvFiles:

                        # package folder naming
                        srvFolderPath = os.getcwd(
                        ) + "/hrim_" + module.type + "_" + module.name + "_srvs/srv"

                        # if the package directories don't exist, create them
                        if not os.path.exists(srvFolderPath):
                            os.makedirs(srvFolderPath)

                        # position ourselves on the package's srv folder
                        os.chdir(srvFolderPath)
                        srv = ""

                        # check for an overall service description
                        if topic.desc is not None and len(topic.desc) > 0:
                            srv += "# " + topic.desc + "\n\n"

                        for prop in topic.properties:

                            if prop.fileName is not None:
                                dependency = True
                                os.chdir(cwd)
                                self.processMessage(module, prop)
                                os.chdir(srvFolderPath)
                            # check for enumeration types
                            if prop.unit is not None and prop.unit == "enum":

                                # sort enumeration values for readability
                                for value in sorted(
                                    ((v, k)
                                     for k, v in prop.enumeration.iteritems()
                                     )):
                                    srv += prop.type + " " + value[
                                        1] + "=" + str(value[0]) + "\n"

                            # process each property, checking if it's value is an array and if it has any description
                            srv += self.formatProperty(prop)

                        srv += "---\n"

                        for prop in topic.response:
                            # check for enumeration types
                            if prop.unit is not None and prop.unit == "enum":

                                # sort enumeration values for readability
                                for value in sorted(
                                    ((v, k)
                                     for k, v in prop.enumeration.iteritems()
                                     )):
                                    srv += prop.type + " " + value[
                                        1] + "=" + str(value[0]) + "\n"

                            # process each property, checking if it's value is an array and if it has any description
                            srv += self.formatProperty(prop)

                        # generate each .srv file and add it to the list
                        if topic.fileName is None:
                            fileName = topic.name.title() + ".srv"
                            srvFiles.append(topic.name.title())
                        else:
                            fileName = topic.fileName + ".srv"
                            srvFiles.append(topic.fileName)

                        text_file = open(fileName, "w")
                        text_file.write(srv)
                        text_file.close()

            buildDeps = ""
            execDeps = ""
            pkgFind = ""
            pkgDep = ""
            for pkgName in self.pkgDeps:
                buildDeps = buildDeps + (
                    "\n\t<build_depend>{}</build_depend>").format(pkgName)
                execDeps = execDeps + (
                    "\n\t<exec_depend>{}</exec_depend>").format(pkgName)
                pkgFind = pkgFind + (
                    "\nfind_package({} REQUIRED)").format(pkgName)
                pkgDep = pkgDep + ("\n\t{}").format(pkgName)

            # insert the package's name and description in package.xml's content
            msgPkg = pkg.replace("%PKGNAME%", self.msgPkgName)
            msgPkg = msgPkg.replace("%PKGDESC%", module.desc)
            msgPkg = msgPkg.replace("%PKGBUILD%", buildDeps)
            msgPkg = msgPkg.replace("%PKGEXEC%", execDeps)

            # insert the package's name and description in CMakeLists.txt's content
            msgMakeFile = makeFile.replace("%PKGNAME%", self.msgPkgName)
            msgMakeFile = msgMakeFile.replace("%PKGFIND%", pkgFind)
            msgMakeFile = msgMakeFile.replace("%PKGDEP%", pkgDep)

            # if the package has messages
            if messages:
                # reposition ourselves on the package's root
                os.chdir(self.msgFolderPath[:-4])

                # generate the package.xml file
                package = open("package.xml", "w")
                package.write(msgPkg)
                package.close()

                # insert the .msg list in the CMakeLists.txt
                msgList = ""
                for tmp in self.ownFiles:
                    msgList += "\t\"msg/" + tmp + ".msg\"\n"

                msgMakeFile = msgMakeFile.replace("%PKGFILES%", msgList[:-1])

                # generate the CMakeLists.txt file
                cmake = open("CMakeLists.txt", "w")
                cmake.write(msgMakeFile)
                cmake.close()

        # if the package has services
            if services:
                # reposition ourselves on the package's root
                os.chdir(srvFolderPath[:-4])

                if dependency:
                    srvMakeFile = srvMakeFile.replace(
                        "%PKGFIND%",
                        "\nfind_package({} REQUIRED)".format(self.msgPkgName))
                    srvMakeFile = srvMakeFile.replace("%PKGDEP%",
                                                      "\n\t" + self.msgPkgName)
                    srvPkg = srvPkg.replace(
                        "%PKGBUILD%",
                        "\n\t<build_depend>{}</build_depend>".format(
                            self.msgPkgName))
                    srvPkg = srvPkg.replace(
                        "%PKGEXEC%",
                        "\n\t<exec_depend>{}</exec_depend>".format(
                            self.msgPkgName))
                else:
                    srvMakeFile = srvMakeFile.replace("%PKGFIND%", "")
                    srvMakeFile = srvMakeFile.replace("%PKGDEP%", "")
                    srvPkg = srvPkg.replace("%PKGBUILD%", "")
                    srvPkg = srvPkg.replace("%PKGEXEC%", "")

                # generate the package.xml file
                package = open("package.xml", "w")
                package.write(srvPkg)
                package.close()

                # insert the .srv list in the CMakeLists.txt
                srvList = ""
                for tmp in srvFiles:
                    srvList += "\t\"srv/" + tmp + ".srv\"\n"

                srvMakeFile = srvMakeFile.replace("%PKGFILES%", srvList[:-1])

                # generate the CMakeLists.txt file
                cmake = open("CMakeLists.txt", "w")
                cmake.write(srvMakeFile)
                cmake.close()

            # reposition ourselves on the generated module's root folder
            os.chdir(cwd)
            manParams = ""
            optParams = ""

            for param in module.params:

                # mandatory parameters parsing
                if param.mandatory:
                    if param.desc is not None:
                        manParams += getTabs(1) + "# " + param.desc + "\n"
                    manParams += getTabs(1) + param.name + ": " + (str(
                        param.value) if param.value is not None else
                                                                   "") + "\n\n"

                # optional parameters parsing
                else:
                    if param.desc is not None:
                        optParams += getTabs(1) + "# " + param.desc + "\n"
                    optParams += getTabs(1) + param.name + ": " + (str(
                        param.value) if param.value is not None else
                                                                   "") + "\n\n"

            if len(manParams) > 0:
                params = open("mandatory_parameters.yaml", "w")
                params.write(module.name + ":\n" + manParams)
                params.close()

            if len(optParams) > 0:
                params = open("optional_parameters.yaml", "w")
                params.write(module.name + ":\n" + optParams)
                params.close()
        except:
            print "Error while processing module: " + module.name
            raise
Example #4
0
    def compileGeneric(self, topics, plat, package="generic"):
        try:
            cwd = os.getcwd()

            self.msgPkgName = "hrim_" + package + "_msgs"

            self.dataTypes = ModuleParser().getDataTypes(plat)

            os.chdir("templates")
            with open('package.txt', 'r') as myfile:
                pkg = myfile.read()

            with open('cmake.txt', 'r') as myfile:
                makeFile = myfile.read()

            os.chdir(cwd)

            genPath = os.path.join(cwd, "generated", package, self.msgPkgName,
                                   "msg")

            # if the generic package directory doesn't exist, create it
            if not os.path.exists(genPath):
                os.makedirs(genPath)

            os.chdir(genPath)

            self.ownFiles = []

            for topic in topics:

                # reposition ourselves for each topic
                os.chdir(genPath)

                msg = ""

                # check for an overall message description
                if topic.desc is not None and len(topic.desc) > 0:
                    msg += "# " + topic.desc + "\n\n"

                for prop in topic.properties:

                    if prop.fileName is not None:
                        self.processSubProperty(prop, True)
                    else:

                        # check for enumeration types
                        if prop.unit is not None and prop.unit == "enum":

                            # sort enumeration values for readability
                            for value in sorted(
                                ((v, k)
                                 for k, v in prop.enumeration.iteritems())):
                                msg += prop.type + " " + value[1] + "=" + str(
                                    value[0]) + "\n"

                    # process each property, checking if it's value is an array and if it has any description
                    msg += self.formatProperty(prop)

                genName = topic.fileName if topic.fileName is not None else topic.name.title(
                )
                checkFile = self.checkGenerated(genName)

                if checkFile == False:
                    fileName = genName + ".msg"
                    self.msgFiles.append(genName)
                    text_file = open(fileName, "w")
                    text_file.write(msg)
                    text_file.close()
                else:
                    print checkFile

            self.pkgDeps.remove(self.msgPkgName)

            # update the list of generated files at self.generatedFiles
            self.listGenerated()

            # insert the .msg list in the CMakeLists.txt
            msgList = ""
            for tmp in sorted(self.generatedFiles[self.msgPkgName]):
                msgList += "\t\"msg/" + tmp + ".msg\"\n"

            buildDeps = ""
            execDeps = ""
            pkgFind = ""
            pkgDep = ""
            for pkgName in self.pkgDeps:
                buildDeps = buildDeps + (
                    "\n\t<build_depend>{}</build_depend>").format(pkgName)
                execDeps = execDeps + (
                    "\n\t<exec_depend>{}</exec_depend>").format(pkgName)
                pkgFind = pkgFind + (
                    "\nfind_package({} REQUIRED)").format(pkgName)
                pkgDep = pkgDep + ("\n\t{}").format(pkgName)

            # insert the package's name and description in CMakeLists.txt's content
            msgMakeFile = makeFile.replace("%PKGNAME%", self.msgPkgName)
            msgMakeFile = msgMakeFile.replace("%PKGFIND%", pkgFind)
            msgMakeFile = msgMakeFile.replace("%PKGDEP%", pkgDep)

            msgMakeFile = msgMakeFile.replace("%PKGFILES%", msgList[:-1])

            genPkg = pkg.replace("%PKGNAME%", self.msgPkgName)
            genPkg = genPkg.replace(
                "%PKGDESC%",
                "defines the " + package + " HRIM messages used by modules")
            genPkg = genPkg.replace("%PKGBUILD%", buildDeps)
            genPkg = genPkg.replace("%PKGEXEC%", execDeps)

            os.chdir("..")

            # generate the CMakeLists.txt file
            cmake = open("CMakeLists.txt", "w")
            cmake.write(msgMakeFile)
            cmake.close()

            # generate the package.xml file
            package = open("package.xml", "w")
            package.write(genPkg)
            package.close()

        except Exception as e:
            print "Error while compiling generic package"
            raise