def idsl_pool(self): if hasattr(self, '__idsl_pool'): return self.__idsl_pool else: idsl_pool = IDSLPool(self.imports, []) interface_list = self.requires + self.implements + self.subscribesTo + \ self.publishes for interface_required in interface_list: if not idsl_pool.module_providing_interface( interface_required.name): raise rcExceptions.InterfaceNotFound( interface_required.name, idsl_pool.interfaces()) self.__idsl_pool = idsl_pool return idsl_pool
from parseIDSL import * import rcExceptions import sys component = CDSLParsing.fromFile(inputFile, includeDirectories=includeDirectories) imports = ''.join( [ imp+'#' for imp in component['imports'] ] ) # verification pool = IDSLPool(imports, includeDirectories) interface_list = component['requires'] + component['implements'] + component['subscribesTo'] + component['publishes'] for interface_required in interface_list: interface_required = interface_required if type(interface_required) == str else interface_required[0] if not pool.moduleProviding(interface_required): raise rcExceptions.InterfaceNotFound(interface_required, pool.interfaces()) if component['language'].lower() == 'cpp': # # Check output directory # if not os.path.exists(outputPath): creaDirectorio(outputPath) # Create directories within the output directory try: creaDirectorio(outputPath+"/bin") creaDirectorio(outputPath+"/etc") creaDirectorio(outputPath+"/src") except: print 'There was a problem creating a directory' sys.exit(1)
def main(): parser = MyParser( description= 'This application create components files from cdsl files or .ice from idsl\n' '\ta) to generate code from a CDSL file: ' + sys.argv[0].split('/')[-1] + ' INPUT_FILE.CDSL OUTPUT_PATH\n' + '\tb) to generate a new CDSL file: ' + sys.argv[0].split('/')[-1] + ' NEW_COMPONENT_DESCRIPTOR.CDSL', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument("-I", "--include_dirs", nargs='*', help="Include directories", action=FullPaths, default=[]) parser.add_argument("-d", '--diff', dest='diff', choices=DIFF_TOOLS, action='store') parser.add_argument("input_file", help="The input dsl file") parser.add_argument("output_path", nargs='?', help="The path to put the files") args = parser.parse_args() if args.output_path is None: if args.input_file.endswith(".cdsl"): generateDummyCDSL(args.input_file) generateDummySMDSL("statemachine.smdsl") sys.exit(0) else: print(args.output_path, args.input_file) print(parser.error("No output path with non .cdsl file")) sys.exit(-1) inputFile = args.input_file outputPath = args.output_path sys.path.append('/opt/robocomp/python') new_existing_files = {} if inputFile.endswith(".cdsl"): component = DSLFactory().from_file( inputFile, includeDirectories=args.include_dirs) imports = ''.join([imp + '#' for imp in component['imports']]) # verification pool = IDSLPool(imports, args.include_dirs) interface_list = component['requires'] + component[ 'implements'] + component['subscribesTo'] + component['publishes'] for interface_required in interface_list: interface_required = interface_required if isinstance( interface_required, str) else interface_required[0] if not pool.moduleProviding(interface_required): raise rcExceptions.InterfaceNotFound(interface_required, pool.interfaces()) if component['language'].lower( ) == 'cpp' or component['language'].lower() == 'cpp11': # # Check output directory # if not os.path.exists(outputPath): create_directory(outputPath) # Create directories within the output directory try: create_directory(outputPath + "/bin") create_directory(outputPath + "/etc") create_directory(outputPath + "/src") except: print('There was a problem creating a directory') sys.exit(1) pass # # Generate regular files # files = [ 'CMakeLists.txt', 'DoxyFile', 'README-STORM.txt', 'README.md', 'etc/config', 'src/main.cpp', 'src/CMakeLists.txt', 'src/CMakeListsSpecific.txt', 'src/commonbehaviorI.h', 'src/commonbehaviorI.cpp', 'src/genericmonitor.h', 'src/genericmonitor.cpp', 'src/config.h', 'src/specificmonitor.h', 'src/specificmonitor.cpp', 'src/genericworker.h', 'src/genericworker.cpp', 'src/specificworker.h', 'src/specificworker.cpp', 'src/mainUI.ui' ] specificFiles = [ 'src/specificworker.h', 'src/specificworker.cpp', 'src/CMakeListsSpecific.txt', 'src/mainUI.ui', 'src/specificmonitor.h', 'src/specificmonitor.cpp', 'README.md', 'etc/config' ] for f in files: ofile = outputPath + '/' + f if f in specificFiles and os.path.exists(ofile): print('Not overwriting specific file "' + ofile + '", saving it to ' + ofile + '.new') new_existing_files[os.path.abspath( ofile)] = os.path.abspath(ofile) + '.new' ofile += '.new' ifile = "/opt/robocomp/share/robocompdsl/templateCPP/" + f if f != 'src/mainUI.ui' or component['gui'] is not None: print('Generating', ofile) run = "cog.py -z -d -D theCDSL=" + inputFile + " -D theIDSLs=" + imports + ' -D theIDSLPaths=' + '#'.join( args.include_dirs) + " -o " + ofile + " " + ifile run = run.split(' ') ret = Cog().main(run) if ret != 0: print('ERROR') sys.exit(-1) replaceTagsInFile(ofile) # # Generate interface-dependent files # for ima in component['implements']: im = ima if type(im) != type(''): im = im[0] if communicationIsIce(ima): for f in ["SERVANT.H", "SERVANT.CPP"]: ofile = outputPath + '/src/' + im.lower( ) + 'I.' + f.split('.')[-1].lower() print('Generating ', ofile, ' (servant for', im + ')') # Call cog run = "cog.py -z -d -D theCDSL=" + inputFile + " -D theIDSLs=" + imports + ' -D theIDSLPaths=' + '#'.join( args.include_dirs ) + " -D theInterface=" + im + " -o " + ofile + " " + "/opt/robocomp/share/robocompdsl/templateCPP/" + f run = run.split(' ') ret = Cog().main(run) if ret != 0: print('ERROR') sys.exit(-1) replaceTagsInFile(ofile) for imp in component['subscribesTo']: im = imp if type(im) != type(''): im = im[0] if communicationIsIce(imp): for f in ["SERVANT.H", "SERVANT.CPP"]: ofile = outputPath + '/src/' + im.lower( ) + 'I.' + f.split('.')[-1].lower() print('Generating ', ofile, ' (servant for', im + ')') # Call cog theInterfaceStr = im if type(theInterfaceStr) == type([]): theInterfaceStr = str(';'.join(im)) run = "cog.py -z -d -D theCDSL=" + inputFile + " -D theIDSLs=" + imports + ' -D theIDSLPaths=' + '#'.join( args.include_dirs ) + " -D theInterface=" + theInterfaceStr + " -o " + ofile + " " + "/opt/robocomp/share/robocompdsl/templateCPP/" + f #print(run run = run.split(' ') ret = Cog().main(run) if ret != 0: print('ERROR') sys.exit(-1) replaceTagsInFile(ofile) elif component['language'].lower() == 'python': # # Check output directory # if not os.path.exists(outputPath): create_directory(outputPath) # Create directories within the output directory try: create_directory(outputPath + "/etc") create_directory(outputPath + "/src") except: print('There was a problem creating a directory') sys.exit(1) pass needStorm = False for pub in component['publishes']: if communicationIsIce(pub): needStorm = True for sub in component['subscribesTo']: if communicationIsIce(sub): needStorm = True # # Generate regular files # files = [ 'CMakeLists.txt', 'DoxyFile', 'README-STORM.txt', 'README.md', 'etc/config', 'src/main.py', 'src/genericworker.py', 'src/specificworker.py', 'src/mainUI.ui' ] specificFiles = [ 'src/specificworker.py', 'src/mainUI.ui', 'README.md', 'etc/config' ] for f in files: if f == 'src/main.py': ofile = outputPath + '/src/' + component['name'] + '.py' else: ofile = outputPath + '/' + f if f in specificFiles and os.path.exists(ofile): print('Not overwriting specific file "' + ofile + '", saving it to ' + ofile + '.new') new_existing_files[os.path.abspath( ofile)] = os.path.abspath(ofile) + '.new' ofile += '.new' ifile = "/opt/robocomp/share/robocompdsl/templatePython/" + f ignoreFile = False if f == 'src/mainUI.ui' and component['gui'] is None: ignoreFile = True if f == 'CMakeLists.txt' and component['gui'] is None: ignoreFile = True if f == 'README-STORM.txt' and needStorm == False: ignoreFile = True if not ignoreFile: print('Generating', ofile) run = "cog.py -z -d -D theCDSL=" + inputFile + " -D theIDSLs=" + imports + ' -D theIDSLPaths=' + '#'.join( args.include_dirs) + " -o " + ofile + " " + ifile run = run.split(' ') ret = Cog().main(run) if ret != 0: print('ERROR') sys.exit(-1) replaceTagsInFile(ofile) if f == 'src/main.py': os.chmod(ofile, os.stat(ofile).st_mode | 0o111) # # Generate interface-dependent files # for imp in component['implements'] + component['subscribesTo']: if type(imp) != type(''): im = imp[0] else: im = imp if communicationIsIce(imp): for f in ["SERVANT.PY"]: ofile = outputPath + '/src/' + im.lower( ) + 'I.' + f.split('.')[-1].lower() print('Generating', ofile, ' (servant for', im + ')') # Call cog run = "cog.py -z -d -D theCDSL=" + inputFile + " -D theIDSLs=" + imports + ' -D theIDSLPaths=' + '#'.join( args.include_dirs ) + " -D theInterface=" + im + " -o " + ofile + " " + "/opt/robocomp/share/robocompdsl/templatePython/" + f run = run.split(' ') ret = Cog().main(run) if ret != 0: print('ERROR') sys.exit(-1) replaceTagsInFile(ofile) else: print('Unsupported language', component['language']) if component['usingROS'] == True: for imp in component['imports']: generateROSHeaders(imp, outputPath + "/src", component, args.include_dirs) # Code to launch diff tool on .new files to be compared with their old version if args.diff is not None: diff_tool, _ = get_diff_tool(prefered=args.diff) print( "Executing diff tool for existing files. Close if no change is needed." ) for o_file, n_file in new_existing_files.items(): if not filecmp.cmp(o_file, n_file): print([diff_tool, o_file, n_file]) try: subprocess.call([diff_tool, o_file, n_file]) except KeyboardInterrupt as e: print( "Comparasion interrupted. All files have been generated. Check this .new files manually:" ) for o_file2, n_file2 in new_existing_files.items(): if not filecmp.cmp(o_file2, n_file2): print("%s %s" % (o_file2, n_file2)) break except Exception as e: print("Exception trying to execute %s" % (diff_tool)) print(e.message) else: print("Binary equal files %s and %s" % (o_file, n_file)) elif inputFile.endswith(".idsl"): # idsl = IDSLParsing.fromFileIDSL(inputFile) print('Generating ICE file ', outputPath) # Call cog run = "cog.py -z -d" + " -D theIDSL=" + inputFile + ' -D theIDSLPaths=' + '#'.join( args.include_dirs ) + " -o " + outputPath + " /opt/robocomp/share/robocompdsl/TEMPLATE.ICE" run = run.split(' ') ret = Cog().main(run) if ret != 0: print('ERROR') sys.exit(-1) replaceTagsInFile(outputPath)
w = open(path, 'w') w.write(text) w.close() imports = ''.join( [ imp.split('/')[-1]+'#' for imp in component['imports'] ] ) # verification import rcExceptions from parseIDSL import * pool = IDSLPool(imports) for interface_required in component['requires'] + component['implements'] + component['subscribesTo'] + component['publishes']: interface_required = interface_required if type(interface_required) == str else interface_required[0] if not pool.moduleProviding(interface_required): raise rcExceptions.InterfaceNotFound(interface_required) if component['language'].lower() == 'cpp': # # Check output directory # if not os.path.exists(outputPath): creaDirectorio(outputPath) # Create directories within the output directory try: creaDirectorio(outputPath+"/bin") creaDirectorio(outputPath+"/etc") creaDirectorio(outputPath+"/src") except: print 'There was a problem creating a directory' sys.exit(1)