#!/usr/bin/env python #Copyright (c) 2009, Eli Stevens # #Permission is hereby granted, free of charge, to any person obtaining a copy #of this software and associated documentation files (the "Software"), to deal #in the Software without restriction, including without limitation the rights #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #copies of the Software, and to permit persons to whom the Software is #furnished to do so, subject to the following conditions: # #The above copyright notice and this permission notice shall be included in #all copies or substantial portions of the Software. # #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #THE SOFTWARE. import sys if sys.version_info < (2,6): print "Python 2.6 or higher required; you have:" print sys.version sys.exit(1) import os def env_setup(): wowspibin_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'bin')) sys.path.append(wowspibin_path) wowspilib_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'lib')) sys.path.append(wowspilib_path) import optparse import glob def usage(sys_argv): from sqliteutils import DataRun usage_list = [] if '--help' in sys_argv or '-h' in sys_argv: module_dict = {} for path_str in ('bin', 'lib'): module_dict[path_str] = [] #print glob.glob(os.path.join(os.path.dirname(__file__), path_str, '*.py')) for py_path in sorted(glob.glob(os.path.join(os.path.dirname(__file__), path_str, '*.py'))): try: module_str = py_path.rsplit('/')[-1].split('.')[0] module = __import__(module_str) for runner_str in dir(module): if issubclass(getattr(module, runner_str), DataRun) and getattr(module, runner_str) != DataRun: module_dict[path_str].append('%s.%s' % (module_str, runner_str)) #else: # print "no:", runner_str except Exception, e: #print e pass usage_list.append("\nTry the following modules for more information:") usage_list.append('\n'.join([" wowspi %s --help" % module_str for module_str in module_dict['bin']])) usage_list.append("\nModules that implement intermediate steps (for internal use):") usage_list.append('\n'.join([" wowspi %s --help" % module_str for module_str in module_dict['lib']])) parser = optparse.OptionParser("Usage: wowspi [options] [module options]" + '\n'.join(usage_list)) parser.disable_interspersed_args() usage_setup(parser) return parser.parse_args(sys_argv) def usage_setup(parser, **kwargs): if kwargs.get('profile', True): parser.add_option("--profile" , help="Use cProfile to profile the application while running; save data to FILE." , metavar="FILE" , dest="profile_path" , action="store" , type="str" #, default="profile.out" ) def main(options, arguments): if not arguments: usage(['--help']) else: module_str, runner_str = arguments[0].split('.') if options.profile_path: import cProfile cProfile.runctx('''getattr(__import__(module_str), runner_str)().main(arguments[1:])''', globals(), locals(), options.profile_path) else: getattr(__import__(module_str), runner_str)().main(arguments[1:]) if __name__ == "__main__": env_setup() options, arguments = usage(sys.argv[1:]) sys.exit(main(options, arguments) or 0)