#!/usr/bin/env python2 import os import re def read_dbc(filename): with open(os.path.join(dir_name, filename), 'r') as file_in: return file_in.read() cur_path = os.path.dirname(os.path.realpath(__file__)) generator_path = os.path.join(cur_path, '../') include_pattern = r'CM_ "IMPORT (.*?)"' for dir_name, _, filenames in os.walk(cur_path): if dir_name == cur_path: continue print dir_name for filename in filenames: if filename.startswith('_'): continue print filename dbc_file_in = read_dbc(filename) includes = re.findall(include_pattern, dbc_file_in) output_filename = filename.replace('.dbc', '_generated.dbc') with open(os.path.join(generator_path, output_filename), 'w') as dbc_file_out: dbc_file_out.write('CM_ "AUTOGENERATED FILE, DO NOT EDIT"\n') for include_filename in reversed(includes): include_file_header = '\n\nCM_ "Imported file %s starts here"\n' % include_filename dbc_file_out.write(include_file_header) include_file = read_dbc(include_filename) dbc_file_out.write(include_file) dbc_file_out.write('\nCM_ "%s starts here"\n' % filename) dbc_file_out.write(dbc_file_in)