def test_tree_is_unchanged(self): for path, contents in yield_examples(): expected = cmp.parse(contents, path) actual = cmp.parse(str(cmp.parse(contents, path))) msg = 'Failed on %s.\nExpected\n%s\n\nGot\n%s' % (path, expected, actual) self.assertEqual(expected, actual, msg)
def test_line_numbers_in_exceptions(self): input = '''\ FIND_PACKAGE(ITK) INCLUDE( ''' try: parse(input) self.fail('Expected an exception, but none was raised.') except Exception as e: self.assertTrue('line 2' in str(e))
def main(): # Parse arguments parser = argparse.ArgumentParser(description='Pretty-print CMakeLists files.') parser.add_argument('files', type=str, nargs='*', help='files to pretty print (default is stdin)') parser.add_argument('-t', '--tree', action='store_true', help='print out the syntax trees') args = parser.parse_args() # Gather files filenames = args.files files = [('<stdin>', sys.stdin)] if filenames: files = [(name, open(name)) for name in filenames] # Process files for (name, file) in files: with file: input = file.read() tree = cmp.parse(input, path=name) if args.tree: # Print out AST print(repr(tree)) else: # Pretty print print(str(tree), end='')
def main(): # Parse arguments parser = argparse.ArgumentParser( description='Pretty-print CMakeLists files.') parser.add_argument('files', type=str, nargs='*', help='files to pretty print (default is stdin)') parser.add_argument('-t', '--tree', action='store_true', help='print out the syntax trees') args = parser.parse_args() # Gather files filenames = args.files files = [('<stdin>', sys.stdin)] if filenames: files = [(name, open(name)) for name in filenames] # Process files for (name, file) in files: with file: input = file.read() tree = cmp.parse(input, path=name) if args.tree: # Print out AST print(repr(tree)) else: # Pretty print print(str(tree), end='')
def test_parse_nonempty2(self): input = '''\ # Top level comment FIND_PACKAGE(ITK REQUIRED) INCLUDE(${ITK_USE_FILE}) ADD_EXECUTABLE(CastImageFilter CastImageFilter.cxx) TARGET_LINK_LIBRARIES(CastImageFilter # inline comment 1 vtkHybrid #inline comment 2 ITKIO ITKBasicFilters ITKCommon ) ''' output = parse(input) expected = File([ Comment('# Top level comment'), Command('FIND_PACKAGE', [Arg('ITK'), Arg('REQUIRED')]), Command('INCLUDE', [Arg('${ITK_USE_FILE}')]), BlankLine(), Command('ADD_EXECUTABLE', [Arg('CastImageFilter'), Arg('CastImageFilter.cxx')]), Command('TARGET_LINK_LIBRARIES', [Arg('CastImageFilter', comments=['# inline comment 1']), Arg('vtkHybrid', comments=['#inline comment 2']), Arg('ITKIO'), Arg('ITKBasicFilters'), Arg('ITKCommon')]), ]) msg = '\nexpected\n%s\ngot\n%s' % (expected, output) self.assertEqual(expected, output, msg)
def test_idempotency_of_parse_unparse(self): round_trip = lambda s, path='<string>': str(cmp.parse(s, path)) for path, contents in yield_examples(): print 'Testing on %s' % path self.assertEqual(round_trip(contents, path), round_trip(round_trip(contents, path)), 'Failed on %s' % path)
def main(argv): obj = [] with open("CMakeLists.txt", 'r') as fin: obj = cmp.parse(fin.read()) project = parse(obj) transform(project, "CMake")
def test_arg_comments_preserved(self): input = ''' some_Command ( x # inline comment about x ) ''' output = str(parse(input))
def test_idempotency_of_parsing_and_unparsing(self): input = '''\ # Top level comment FIND_PACKAGE(ITK REQUIRED) INCLUDE(${ITK_USE_FILE}) ''' round_trip = lambda s: str(parse(s)) self.assertEqual(round_trip(input), round_trip(round_trip(input)))
def cmake_to_package(): cmake_path = os.path.join(os.getcwd(), "CMakeLists.txt") with open(cmake_path, "r") as f: cmake_file = cmake.parse(f.read()) cmake_pkgs = set() for elem in cmake_file: try: name = elem.name if name != "find_package": continue find_pkg_args = elem.body except: continue if len(find_pkg_args) == 0 or find_pkg_args[0].contents != "catkin": continue cmake_pkgs = set([arg.contents for arg in find_pkg_args][1:]) if "REQUIRED" in cmake_pkgs: cmake_pkgs.remove("REQUIRED") if "COMPONENTS" in cmake_pkgs: cmake_pkgs.remove("COMPONENTS") break if len(cmake_pkgs) == 0: error("no package is defined in CMakeLists.txt") return False xml_path = os.path.join(os.getcwd(), "package.xml") doc = etree.parse(xml_path) xml_pkgs = set([e.text for e in doc.findall("build_depend")]) info("Checking package.xml -> CMakeLists.txt") diff_pkgs = xml_pkgs - cmake_pkgs if len(diff_pkgs) > 0: warn("some build dependencies are missed in CMakeLists.txt:") for pkg in sorted(list(diff_pkgs)): warn(" " + pkg) else: info( "All packages defined in package.xml are also defined in CMakeLists.txt" ) info("Checking CMakeLists.txt -> package.xml") diff_pkgs = cmake_pkgs - xml_pkgs if len(diff_pkgs) == 0: info( "All packages defined in CMakeLists.txt are also defined in package.xml" ) return True warn("following packages are not defined in package.xml:") for p in list(diff_pkgs): warn(" - " + p) if ask_yn("Append them to package.xml?"): for pkg in list(diff_pkgs): e = etree.Element("build_depend") e.text = pkg doc.getroot().append(e) xml = etree.tostring(doc, pretty_print=True) return update_with_diff(xml_path, xml) else: return False
def parse_file(self, path): os.chdir(self.root) try: with open(path, 'r') as f: fp = cmlp.parse(f.read()) for cmd in fp: if type(cmd).__name__ == 'Command': self.parse_command(cmd, os.path.dirname(path)) except IOError: utils.log('Could not open file {}'.format(path))
def test_multiline_string(self): s = ''' string containing newlines ''' input = '''\ set (MY_STRING "%s") ''' % s tree = parse(input) expected = File([Command('set', [Arg('MY_STRING'), Arg('"' + s + '"')])]) self.assertEqual(expected, tree)
def test_ifs_indented(self): input = ''' if(a) if(b) set(X 1) endif() else(a) if(c) set(Y 2) endif(c) endif(a) ''' self.assertMultiLineEqual(input, str(parse(input)))
def test_comments_preserved(self): input = '''\ # file comment # more about the file # comment above Command1 Command1(VERSION 2.6) # inline comment for Command1 Command2(x # inline comment about x "y" # inline comment about a quoted string "y" ) # inline comment for Command2 ''' output = str(parse(input)) self.assertMultiLineEqual(input, output)
def test_parse_nonempty1(self): input = 'FIND_PACKAGE(ITK REQUIRED)' output = parse(input) expected = File([Command('FIND_PACKAGE', [Arg('ITK'), Arg('REQUIRED')])]) msg = '\nexpected\n%s\ngot\n%s' % (repr(expected), repr(output)) self.assertEqual(expected, output, msg)
def test_arg_with_a_slash(self): tree = parse('include_directories (${HELLO_SOURCE_DIR}/Hello)') expected = File([ Command('include_directories', [Arg('${HELLO_SOURCE_DIR}/Hello')]) ]) self.assertEqual(expected, tree)
def test_command_with_no_args(self): tree = parse('cmd()') expected = File([Command('cmd', [])]) self.assertEqual(expected, tree)
def test_parse_empty_raises_exception(self): self.assertEqual(File([]), parse(''))