1#!/usr/bin/env python 2 3import re 4import sys 5 6def extract_config(f): 7 conf_patt = re.compile('# Configurations') 8 split_patt = re.compile('#={69}') 9 var_patt = re.compile('libbcc_([A-Z_]+)\\s*:=\\s*([01])') 10 11 STATE_PRE_CONFIG = 0 12 STATE_FOUND_CONFIG = 1 13 STATE_IN_CONFIG = 2 14 15 state = STATE_PRE_CONFIG 16 17 for line in f: 18 if state == STATE_PRE_CONFIG: 19 if conf_patt.match(line.strip()): 20 state = STATE_FOUND_CONFIG 21 22 elif state == STATE_FOUND_CONFIG: 23 if split_patt.match(line.strip()): 24 # Start reading the configuration 25 print '/* BEGIN USER CONFIG */' 26 state = STATE_IN_CONFIG 27 28 elif state == STATE_IN_CONFIG: 29 match = var_patt.match(line.strip()) 30 if match: 31 print '#define', match.group(1), match.group(2) 32 33 elif split_patt.match(line.strip()): 34 # Stop reading the configuration 35 print '/* END USER CONFIG */' 36 break 37 38def main(): 39 if len(sys.argv) != 1: 40 print >> sys.stderr, 'USAGE:', sys.argv[0] 41 sys.exit(1) 42 43 extract_config(sys.stdin) 44 45 46if __name__ == '__main__': 47 main() 48