def main_help_text(self,argv=None): """ Returns the script's main help text, as a string. """ import thepian prog_name = os.path.basename(argv[0]) style = color_style() usage = [ style.HEADING('%s <subcommand> [options] [args]' % prog_name), 'Thepian command line tool, version %s' % thepian.get_version(), "Type '%s help <subcommand>' for help on a specific subcommand." % prog_name, 'Available subcommands:', ] commands = self.get_commands().keys() commands.sort() return '\n'.join(usage + [' %s' % cmd for cmd in commands])
def determine_settings_module(argv): """Parse command line options to determine what settings module to use. In the absense of an option use environment variable MAESTRO_SETTINGS_MODULE, or guess based on machine recognition """ # Preprocess options to extract --settings and --pythonpath. # These options could affect the commands that are available, so they # must be processed early. import thepian parser = LaxOptionParser(version=thepian.get_version(), option_list=BaseCommand.option_list) #TODO should the option_list be a param? try: options, args = parser.parse_args(argv) handle_default_options(options) except: if options.traceback: import traceback print >>sys.stderr, 'couldn\'t handle default options failed' traceback.print_exc() from thepian.conf import structure if not '%s_SETTINGS_MODULE' % structure.COMMAND_VARIABLE_PREFIX in os.environ: os.environ['%s_SETTINGS_MODULE' % structure.COMMAND_VARIABLE_PREFIX] = 'development' #TODO development vs production return os.environ['%s_SETTINGS_MODULE' % structure.COMMAND_VARIABLE_PREFIX]
def execute(self,argv=None): """ Given the command-line arguments, this figures out which subcommand is being run, creates a parser appropriate to that command, and runs it. """ argv = argv or sys.argv[:] prog_name = os.path.basename(argv[0]) try: subcommand = argv[1] except IndexError: sys.stderr.write("Type '%s help' for usage.\n" % prog_name) sys.exit(1) try: if subcommand == 'help': import thepian parser = LaxOptionParser(version=thepian.get_version(), option_list=BaseCommand.option_list) options, args = parser.parse_args(argv) if len(args) > 2: self[args[2]].print_help(prog_name, args[2]) else: sys.stderr.write(self.main_help_text(argv) + '\n') sys.exit(1) else: wrapper = self[subcommand] if getattr(wrapper.cmd,'requires_machine',False): from thepian.conf import structure if not structure.machine.known: sys.stderr.write('Machine is not known (mac %s), cannot execute %s\n' % (structure.machine['mac'],repr(wrapper.cmd))) return #TODO return error code wrapper.run_from_argv(argv) except CommandError, e: sys.stderr.write("%s\nType '%s help' for usage\n" % (e.message,os.path.basename(argv[0])))