11#!/usr/bin/env python3
2+
3+ # vim: ai:ts=4:sw=4
4+
25import sys
36from os import walk
47import os
58import re
69import yaml
710import pprint
11+ import argparse
812
913from devicetree import parse_file
1014
@@ -503,7 +507,7 @@ def print_key_value(k, v, tabstop):
503507
504508 return
505509
506- def generate_include_file (defs ):
510+ def generate_include_file (defs , fixup ):
507511 compatible = reduced ['/' ]['props' ]['compatible' ][0 ]
508512
509513 sys .stdout .write ("/**************************************************\n " )
@@ -539,18 +543,40 @@ def generate_include_file(defs):
539543 print_key_value (prop , defs [node ].get (prop ), maxtabstop )
540544 sys .stdout .write ("\n " )
541545
542- sys .stdout .write ("#endif\n " );
543-
544- def main (args ):
545- if len (args ) < 2 :
546- print ('Usage: %s filename.dts path_to_yaml' % args [0 ])
546+ if fixup and os .path .exists (fixup ):
547+ sys .stdout .write ("\n " )
548+ sys .stdout .write ("/* Following definitions fixup the generated include */\n " )
549+ try :
550+ with open (fixup , "r" ) as fd :
551+ for line in fd .readlines ():
552+ sys .stdout .write (line )
553+ sys .stdout .write ("\n " )
554+ except :
555+ raise Exception ("Input file " + os .path .abspath (fixup ) + " does not exist." )
556+
557+ sys .stdout .write ("#endif\n " )
558+
559+ def parse_arguments ():
560+
561+ parser = argparse .ArgumentParser (description = __doc__ ,
562+ formatter_class = argparse .RawDescriptionHelpFormatter )
563+ parser .add_argument ("-d" , "--dts" , help = "DTS file" )
564+ parser .add_argument ("-y" , "--yaml" , help = "YAML file" )
565+ parser .add_argument ("-f" , "--fixup" , help = "Fixup file" )
566+
567+ return parser .parse_args ()
568+
569+ def main ():
570+ args = parse_arguments ()
571+ if not args .dts or not args .yaml :
572+ print ('Usage: %s -d filename.dts -y path_to_yaml' % sys .argv [0 ])
547573 return 1
548574
549575 try :
550- with open (args [ 1 ] , "r" ) as fd :
576+ with open (args . dts , "r" ) as fd :
551577 d = parse_file (fd )
552578 except :
553- raise Exception ("Input file " + os .path .abspath (args [ 1 ] ) + " does not exist." )
579+ raise Exception ("Input file " + os .path .abspath (args . dts ) + " does not exist." )
554580
555581 # compress list to nodes w/ paths, add interrupt parent
556582 compress_nodes (d ['/' ], '/' )
@@ -572,7 +598,7 @@ def main(args):
572598
573599 # scan YAML files and find the ones we are interested in
574600 yaml_files = []
575- for (dirpath , dirnames , filenames ) in walk (args [ 2 ] ):
601+ for (dirpath , dirnames , filenames ) in walk (args . yaml ):
576602 yaml_files .extend ([f for f in filenames if re .match ('.*\.yaml\Z' , f )])
577603 yaml_files = [dirpath + '/' + t for t in yaml_files ]
578604 break
@@ -628,9 +654,7 @@ def main(args):
628654 extract_reg_prop (chosen ['zephyr,sram' ], None , defs , "CONFIG_SRAM" , 1024 )
629655
630656 # generate include file
631- generate_include_file (defs )
657+ generate_include_file (defs , args . fixup )
632658
633659if __name__ == '__main__' :
634- # test1.py executed as script
635- # do something
636- sys .exit (main (sys .argv ))
660+ main ()
0 commit comments