Switch to OptionParser

main
Michael Hayes 15 years ago
parent b7ad5ed76a
commit 3ec5297548

@ -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,62 +348,71 @@ 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: version = __doc__.split ('\n')[0]
opts, args = getopt.gnu_getopt (argv[1:], "?h", \
["help", "builddir=", "objext=", parser = OptionParser (usage = '%prog', version = version,
"exeext=", description = __doc__)
"relpath", "debug", "template=",
"files", "modules", "exclude=", parser.add_option('--showops', action = 'store_true',
"calls"]) dest = 'showops', default = False,
except getopt.error, msg: help = 'show operations')
raise Usage (msg)
parser.add_option('--modules', action = 'store_true',
if not opts and not args: dest = 'modules', default = False,
print __doc__ help = 'generate module dependencies')
sys.exit (0)
parser.add_option('--calls', action = 'store_true',
dest = 'calls', default = False,
help = 'generate callgraph')
parser.add_option('--files', action = 'store_true',
dest = 'files', default = False,
help = 'generate file dependencies')
parser.add_option('--debug', action = 'store_true',
dest = 'debug', default = False,
help = 'enable debugging')
parser.add_option('--exclude', dest = 'exclude',
default = '',
help = 'files to exclude')
parser.add_option('--objext', dest = 'objext',
default = '.o',
help = 'object file extension')
parser.add_option('--exeext', dest = 'exeext',
default = '.out',
help = 'executable file extension')
parser.add_option('--relpath', action = 'store_true',
dest = 'relpath', default = False,
help = 'use relative paths')
parser.add_option('--outfile', dest = 'outfilename',
default = None,
help = 'output filename')
parser.add_option('--builddir', dest = 'builddir',
default = '',
help = 'build dirname')
parser.add_option('--template', dest = 'template',
default = None,
help = 'template filename')
(options, args) = parser.parse_args ()
if len (args) < 1: if len (args) < 1:
print __doc__ print __doc__
sys.exit (0) sys.exit (0)
mopts = {}
mopts['builddir'] = '' if ',' in options.exclude:
mopts['objext'] = '.o' options.exclude = options.exclude.split(',')
mopts['exeext'] = '.out' else:
mopts['relpath'] = False options.exclude = options.exclude.split()
mopts['template'] = None
mopts['files'] = False
mopts['modules'] = False
mopts['calls'] = False
mopts['exclude'] = []
debug = False
# Process options
for o, a in opts:
if o in ("-?", "-h", "--help"):
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] maincfilename = args[0]
@ -416,23 +420,23 @@ def main(argv = None):
if len (args) > 1: if len (args) > 1:
search_list.extend (args[1:len (args)]) search_list.extend (args[1:len (args)])
if debug: if options.debug:
print >> sys.stderr, search_list print >> sys.stderr, search_list
search_path = pathsep.join (search_list) search_path = pathsep.join (search_list)
if debug: if options.debug:
print >> sys.stderr, 'template', mopts['template'] print >> sys.stderr, 'template', options.template
print >> sys.stderr, 'cfile', maincfilename print >> sys.stderr, 'cfile', maincfilename
print >> sys.stderr, 'search_path', search_path print >> sys.stderr, 'search_path', search_path
print >> sys.stderr, 'CWD = ', os.getcwd() print >> sys.stderr, 'CWD = ', os.getcwd()
if os.path.isdir (maincfilename): if os.path.isdir (maincfilename):
if debug: if options.debug:
print >> sys.stderr, 'Searching ' + maincfilename print >> sys.stderr, 'Searching ' + maincfilename
maincfilename = maincfilename_find (maincfilename) maincfilename = maincfilename_find (maincfilename)
if not maincfilename: if not maincfilename:
sys.exit (1) sys.exit (1)
if debug: if options.debug:
print >> sys.stderr, 'Found C file ' + maincfilename print >> sys.stderr, 'Found C file ' + maincfilename
includes = '-I' + ' -I'.join (search_list) includes = '-I' + ' -I'.join (search_list)
@ -446,11 +450,11 @@ def main(argv = None):
filedeps = {} filedeps = {}
moduledeps = {} moduledeps = {}
functiondeps = {} functiondeps = {}
files_find (gcc, maincfilename, search_path, filedeps, moduledeps, '', debug) files_find (gcc, maincfilename, search_path, filedeps, moduledeps, '', options.debug)
cfilelist = cfiles_get (filedeps) cfilelist = cfiles_get (filedeps)
ofilelist = [cfile[:-2] + mopts['objext'] for cfile in cfilelist] ofilelist = [cfile[:-2] + options.objext for cfile in cfilelist]
outfile = maincfilename[:-2] + mopts['exeext'] outfile = maincfilename[:-2] + options.exeext
filedeps[outfile] = ofilelist filedeps[outfile] = ofilelist
for ofile in ofilelist: for ofile in ofilelist:
deps = [] deps = []
@ -460,31 +464,25 @@ def main(argv = None):
# print >> sys.stderr, moduledeps # print >> sys.stderr, moduledeps
# print >> sys.stderr, filedeps # print >> sys.stderr, filedeps
if mopts['calls']: if options.calls:
for cfile in cfilelist: for cfile in cfilelist:
functions_find (gcc, cfile, functiondeps) functions_find (gcc, cfile, functiondeps)
deps_print ('main', functiondeps, mopts) deps_print ('main', functiondeps, options)
if mopts['files']: if options.files:
deps_print (outfile, filedeps, mopts) deps_print (outfile, filedeps, options)
if mopts['modules']: if options.modules:
target, ext = os.path.splitext (os.path.basename (maincfilename)) target, ext = os.path.splitext (os.path.basename (maincfilename))
deps_print (target, moduledeps, mopts) deps_print (target, moduledeps, options)
if mopts['template']: if options.template:
makefile_print (mopts, mopts['template'], maincfilename, filedeps, makefile_print (options, options.template, maincfilename, filedeps,
search_list, debug) search_list)
return 0 return 0
except Usage, err:
print >> sys.stderr, err.msg
print >> sys.stderr, "for help use --help"
return 2
if __name__ == "__main__": if __name__ == "__main__":
sys.exit (main()) sys.exit (main())

Loading…
Cancel
Save