def main(self): """ If called from __main__ module, run script's main() method with arguments passed and global options parsed. """ main_module = Inspection.find_calling_module() if main_module != '__main__': # only support if __name__ == '__main__' return # Pull in modules in twitter.common.app.modules if not self._import_module('twitter.common.app.modules'): print('Unable to import twitter app modules!', file=sys.stderr) sys.exit(1) # defer init as long as possible. self.init() try: caller_main = Inspection.find_main_from_caller() except Inspection.InternalError: caller_main = None if None in self._commands: assert caller_main is None, "Error: Cannot define both main and a default command." else: self._commands[None] = caller_main main_method = self._commands[self._command] if main_method is None: commands = sorted(self.get_commands()) if commands: print('Must supply one of the following commands:', ', '.join(commands), file=sys.stderr) else: print( 'No main() or command defined! Application must define one of these.', file=sys.stderr) sys.exit(1) try: argspec = inspect.getargspec(main_method) except TypeError as e: print('Malformed main(): %s' % e, file=sys.stderr) sys.exit(1) if len(argspec.args) == 1: args = [self._argv] elif len(argspec.args) == 2: args = [self._argv, self._option_values] else: if len(self._argv) != 0: print( 'main() takes no arguments but got leftover arguments: %s!' % ' '.join(self._argv), file=sys.stderr) sys.exit(1) args = [] rc = self._run_main(main_method, *args) self.quit(rc)
def main(self): """ If called from __main__ module, run script's main() method with arguments passed and global options parsed. """ main_module = Inspection.find_calling_module() if main_module != '__main__': # only support if __name__ == '__main__' return # Pull in modules in twitter.common.app.modules if not self._import_module('twitter.common.app.modules'): print('Unable to import twitter app modules!', file=sys.stderr) sys.exit(1) # defer init as long as possible. self.init() try: caller_main = Inspection.find_main_from_caller() except Inspection.InternalError: caller_main = None if None in self._commands: assert caller_main is None, "Error: Cannot define both main and a default command." else: self._commands[None] = caller_main main_method = self._commands[self._command] if main_method is None: commands = sorted(self.get_commands()) if commands: print('Must supply one of the following commands:', ', '.join(commands), file=sys.stderr) else: print('No main() or command defined! Application must define one of these.', file=sys.stderr) sys.exit(1) try: argspec = inspect.getargspec(main_method) except TypeError as e: print('Malformed main(): %s' % e, file=sys.stderr) sys.exit(1) if len(argspec.args) == 1: args = [self._argv] elif len(argspec.args) == 2: args = [self._argv, self._option_values] else: if len(self._argv) != 0: print('main() takes no arguments but got leftover arguments: %s!' % ' '.join(self._argv), file=sys.stderr) sys.exit(1) args = [] rc = self._run_main(main_method, *args) self.quit(rc)