Switch to OptionParser

This commit is contained in:
Michael Hayes
2011-08-27 11:19:52 +00:00
parent b7ad5ed76a
commit 3ec5297548
+141 -143
View File
@@ -41,12 +41,12 @@ There are special strings that are replaced in the template file:
# http://www.cs.umd.edu/~nspring/software/style-check-readme.html # http://www.cs.umd.edu/~nspring/software/style-check-readme.html
import sys import sys
import getopt
import re import re
import os import os
import subprocess import subprocess
from os import pathsep from os import pathsep
import os.path import os.path
from optparse import OptionParser
def unique (list): def unique (list):
@@ -77,7 +77,7 @@ def file_search (filename, search_path, debug):
return None return None
def hfiles_get (cfile, filedeps, mopts): def hfiles_get (cfile, filedeps, options):
deps = filedeps[cfile] deps = filedeps[cfile]
@@ -89,12 +89,12 @@ def hfiles_get (cfile, filedeps, mopts):
for hfile in filedeps[cfile]: for hfile in filedeps[cfile]:
if hfile[-2:] == '.h': if hfile[-2:] == '.h':
if mopts['relpath']: if options.relpath:
hfile = os.path.relpath (hfile) hfile = os.path.relpath (hfile)
hfilelist.append (hfile) hfilelist.append (hfile)
for hfile in filedeps[cfile]: for hfile in filedeps[cfile]:
hfilelist.extend (hfiles_get (hfile, filedeps, mopts)) hfilelist.extend (hfiles_get (hfile, filedeps, options))
return unique (hfilelist) return unique (hfilelist)
@@ -127,17 +127,12 @@ def file_parse (pathname, indent, debug):
return hfilelist return hfilelist
def makefile_print (mopts, template, maincfilename, filedeps, def makefile_print (options, template, maincfilename, filedeps,
search_list, debug): search_list):
basecfilelist = [] cfilelist = cfiles_get (filedeps)
cfilelist = []
for target in filedeps:
if target[-2:] == '.c':
cfilelist.append (target)
basecfilelist.append (os.path.basename (target))
basecfilelist.sort ()
cfilelist.sort () cfilelist.sort ()
basecfilelist = [os.path.basename (cfile) for cfile in cfilelist]
project = os.path.splitext (os.path.basename (maincfilename)) project = os.path.splitext (os.path.basename (maincfilename))
project = project[0] project = project[0]
@@ -152,13 +147,13 @@ def makefile_print (mopts, template, maincfilename, filedeps,
src = ' '.join (basecfilelist) src = ' '.join (basecfilelist)
obj = src obj = src
if mopts['builddir'] != '': if options.builddir != '':
objfilelist = [os.path.join (mopts['builddir'], obj1) for obj1 in basecfilelist] objfilelist = [os.path.join (options.builddir, obj1) for obj1 in basecfilelist]
objfilelist.sort () objfilelist.sort ()
obj = ' '.join (objfilelist) obj = ' '.join (objfilelist)
project = os.path.join (mopts['builddir'], project) project = os.path.join (options.builddir, project)
obj = re.sub (r'([a-zA-Z0-9/.-_]*)[.]c', r'\1' + mopts['objext'], obj) obj = re.sub (r'([a-zA-Z0-9/.-_]*)[.]c', r'\1' + options.objext, obj)
text = re.sub (r'@PROJECT@', project, text) text = re.sub (r'@PROJECT@', project, text)
text = re.sub (r'@VPATH@', vpath, text) text = re.sub (r'@VPATH@', vpath, text)
@@ -175,21 +170,21 @@ def makefile_print (mopts, template, maincfilename, filedeps,
for cfile in cfilelist: for cfile in cfilelist:
cfilebase = os.path.basename (cfile) cfilebase = os.path.basename (cfile)
if mopts['relpath']: if options.relpath:
cfile1 = os.path.relpath (cfile) cfile1 = os.path.relpath (cfile)
else: else:
cfile1 = cfilebase cfile1 = cfilebase
if mopts['builddir'] != '': if options.builddir != '':
rules = rules + os.path.join (mopts['builddir'], '') rules = rules + os.path.join (options.builddir, '')
rules = rules + re.sub ('([a-zA-Z0-9/.-_]*)[.]c', r'\1' + mopts['objext'], cfilebase) + ': ' + cfile1 rules = rules + re.sub ('([a-zA-Z0-9/.-_]*)[.]c', r'\1' + options.objext, cfilebase) + ': ' + cfile1
hfilelist = hfiles_get (cfile, filedeps, mopts) hfilelist = hfiles_get (cfile, filedeps, options)
hfilelist.sort () hfilelist.sort ()
if debug: if options.debug:
print >> sys.stderr, 'Need hfiles', hfilelist, 'for', cfile print >> sys.stderr, 'Need hfiles', hfilelist, 'for', cfile
for hfile in hfilelist: for hfile in hfilelist:
@@ -308,23 +303,23 @@ def files_find (gcc, filepath, search_path, filedeps, moduledeps, indent, debug)
files_find (gcc, file, search_path, filedeps, moduledeps, indent + ' ', debug) files_find (gcc, file, search_path, filedeps, moduledeps, indent + ' ', debug)
def alldeps_print (depsdir, mopts): def alldeps_print (depsdir, options):
for target in depsdir.keys (): for target in depsdir.keys ():
targetbase = os.path.basename (target) targetbase = os.path.basename (target)
if targetbase in mopts['exclude']: if targetbase in options.exclude:
continue continue
deps = depsdir[target] deps = depsdir[target]
deps = [dep for dep in deps if os.path.basename (dep) not in mopts['exclude']] deps = [dep for dep in deps if os.path.basename (dep) not in options.exclude]
if mopts['relpath']: if options.relpath:
deps = [os.path.relpath (dep) for dep in deps] deps = [os.path.relpath (dep) for dep in deps]
print os.path.relpath (target) + ': ' + ' '.join (deps) + '\n' print os.path.relpath (target) + ': ' + ' '.join (deps) + '\n'
def deps_print (target, depsdir, mopts, record = {}): def deps_print (target, depsdir, options, record = {}):
if record.has_key (target): if record.has_key (target):
return return
@@ -332,11 +327,11 @@ def deps_print (target, depsdir, mopts, record = {}):
return return
deps = depsdir[target] deps = depsdir[target]
deps = [dep for dep in deps if os.path.basename (dep) not in mopts['exclude']] deps = [dep for dep in deps if os.path.basename (dep) not in options.exclude]
for dep in deps: for dep in deps:
deps_print (dep, depsdir, mopts, record) deps_print (dep, depsdir, options, record)
if mopts['relpath']: if options.relpath:
deps = [os.path.relpath (dep) for dep in deps] deps = [os.path.relpath (dep) for dep in deps]
record[target] = True record[target] = True
@@ -353,136 +348,139 @@ class Usage (Exception):
def main(argv = None): def main(argv = None):
if argv is None: if argv is None:
argv = sys.argv argv = sys.argv
try:
try:
opts, args = getopt.gnu_getopt (argv[1:], "?h", \
["help", "builddir=", "objext=",
"exeext=",
"relpath", "debug", "template=",
"files", "modules", "exclude=",
"calls"])
except getopt.error, msg:
raise Usage (msg)
if not opts and not args: version = __doc__.split ('\n')[0]
print __doc__
sys.exit (0)
if len (args) < 1: parser = OptionParser (usage = '%prog', version = version,
print __doc__ description = __doc__)
sys.exit (0)
mopts = {} parser.add_option('--showops', action = 'store_true',
mopts['builddir'] = '' dest = 'showops', default = False,
mopts['objext'] = '.o' help = 'show operations')
mopts['exeext'] = '.out'
mopts['relpath'] = False
mopts['template'] = None
mopts['files'] = False
mopts['modules'] = False
mopts['calls'] = False
mopts['exclude'] = []
debug = False
# Process options parser.add_option('--modules', action = 'store_true',
for o, a in opts: dest = 'modules', default = False,
if o in ("-?", "-h", "--help"): help = 'generate module dependencies')
print __doc__
sys.exit (0)
elif o == "--builddir":
mopts['builddir'] = a
elif o == "--objext":
mopts['objext'] = a
elif o == "--exeext":
mopts['exeext'] = a
elif o == "--relpath":
mopts['relpath'] = True
elif o == "--debug":
debug = True
elif o == "--template":
mopts['template'] = a
elif o == "--files":
mopts['files'] = True
elif o == "--modules":
mopts['modules'] = True
elif o == "--calls":
mopts['calls'] = True
elif o == "--exclude":
mopts['exclude'] = a.split (' ')
maincfilename = args[0] parser.add_option('--calls', action = 'store_true',
dest = 'calls', default = False,
help = 'generate callgraph')
search_list = [] parser.add_option('--files', action = 'store_true',
if len (args) > 1: dest = 'files', default = False,
search_list.extend (args[1:len (args)]) help = 'generate file dependencies')
if debug: parser.add_option('--debug', action = 'store_true',
print >> sys.stderr, search_list dest = 'debug', default = False,
search_path = pathsep.join (search_list) help = 'enable debugging')
if debug:
print >> sys.stderr, 'template', mopts['template']
print >> sys.stderr, 'cfile', maincfilename
print >> sys.stderr, 'search_path', search_path
print >> sys.stderr, 'CWD = ', os.getcwd()
if os.path.isdir (maincfilename): parser.add_option('--exclude', dest = 'exclude',
if debug: default = '',
print >> sys.stderr, 'Searching ' + maincfilename help = 'files to exclude')
maincfilename = maincfilename_find (maincfilename)
if not maincfilename:
sys.exit (1)
if debug: parser.add_option('--objext', dest = 'objext',
print >> sys.stderr, 'Found C file ' + maincfilename default = '.o',
help = 'object file extension')
includes = '-I' + ' -I'.join (search_list) parser.add_option('--exeext', dest = 'exeext',
cflags = '-mmcu=atmega32u2' default = '.out',
opts = '-Os' help = 'executable file extension')
gcc = 'avr-gcc' + ' ' + cflags + ' ' + opts + ' ' + includes
# Search main c file looking for header files included with #include parser.add_option('--relpath', action = 'store_true',
# and any header files included by the header files dest = 'relpath', default = False,
help = 'use relative paths')
filedeps = {} parser.add_option('--outfile', dest = 'outfilename',
moduledeps = {} default = None,
functiondeps = {} help = 'output filename')
files_find (gcc, maincfilename, search_path, filedeps, moduledeps, '', debug)
cfilelist = cfiles_get (filedeps) parser.add_option('--builddir', dest = 'builddir',
ofilelist = [cfile[:-2] + mopts['objext'] for cfile in cfilelist] default = '',
outfile = maincfilename[:-2] + mopts['exeext'] help = 'build dirname')
filedeps[outfile] = ofilelist
for ofile in ofilelist:
deps = []
deps.append (ofile[:-2] + '.c')
filedeps[ofile] = deps
# print >> sys.stderr, moduledeps parser.add_option('--template', dest = 'template',
# print >> sys.stderr, filedeps default = None,
help = 'template filename')
if mopts['calls']: (options, args) = parser.parse_args ()
for cfile in cfilelist:
functions_find (gcc, cfile, functiondeps)
deps_print ('main', functiondeps, mopts)
if mopts['files']: if len (args) < 1:
deps_print (outfile, filedeps, mopts) print __doc__
sys.exit (0)
if mopts['modules']:
target, ext = os.path.splitext (os.path.basename (maincfilename))
deps_print (target, moduledeps, mopts)
if mopts['template']:
makefile_print (mopts, mopts['template'], maincfilename, filedeps,
search_list, debug)
return 0
except Usage, err: if ',' in options.exclude:
print >> sys.stderr, err.msg options.exclude = options.exclude.split(',')
print >> sys.stderr, "for help use --help" else:
return 2 options.exclude = options.exclude.split()
maincfilename = args[0]
search_list = []
if len (args) > 1:
search_list.extend (args[1:len (args)])
if options.debug:
print >> sys.stderr, search_list
search_path = pathsep.join (search_list)
if options.debug:
print >> sys.stderr, 'template', options.template
print >> sys.stderr, 'cfile', maincfilename
print >> sys.stderr, 'search_path', search_path
print >> sys.stderr, 'CWD = ', os.getcwd()
if os.path.isdir (maincfilename):
if options.debug:
print >> sys.stderr, 'Searching ' + maincfilename
maincfilename = maincfilename_find (maincfilename)
if not maincfilename:
sys.exit (1)
if options.debug:
print >> sys.stderr, 'Found C file ' + maincfilename
includes = '-I' + ' -I'.join (search_list)
cflags = '-mmcu=atmega32u2'
opts = '-Os'
gcc = 'avr-gcc' + ' ' + cflags + ' ' + opts + ' ' + includes
# Search main c file looking for header files included with #include
# and any header files included by the header files
filedeps = {}
moduledeps = {}
functiondeps = {}
files_find (gcc, maincfilename, search_path, filedeps, moduledeps, '', options.debug)
cfilelist = cfiles_get (filedeps)
ofilelist = [cfile[:-2] + options.objext for cfile in cfilelist]
outfile = maincfilename[:-2] + options.exeext
filedeps[outfile] = ofilelist
for ofile in ofilelist:
deps = []
deps.append (ofile[:-2] + '.c')
filedeps[ofile] = deps
# print >> sys.stderr, moduledeps
# print >> sys.stderr, filedeps
if options.calls:
for cfile in cfilelist:
functions_find (gcc, cfile, functiondeps)
deps_print ('main', functiondeps, options)
if options.files:
deps_print (outfile, filedeps, options)
if options.modules:
target, ext = os.path.splitext (os.path.basename (maincfilename))
deps_print (target, moduledeps, options)
if options.template:
makefile_print (options, options.template, maincfilename, filedeps,
search_list)
return 0
if __name__ == "__main__": if __name__ == "__main__":