def test_setup_logger(self): err_out = io.StringIO() setup_loggers(self.pav_cfg, err_out=err_out) # Log through each of the logging mechanisms. # Check the result logger result_logger = logging.getLogger('common_results') result_msg = json.dumps({ "name": str(uuid.uuid4()), }) result_logger.error(result_msg) # Make sure our message got logged. result_log_data = self.pav_cfg.result_log.open().read() self.assertIn(result_msg + '\n', result_log_data) # Check that yapsy errors go to stderr (or the stream we replaced # stderr with). yapsy_logger = logging.getLogger('yapsy') yapsy_msg = str(uuid.uuid4()) yapsy_logger.error("Testing logging through yapsy. %s", yapsy_msg) self.assertIn(yapsy_msg, err_out.getvalue()) # Check that exceptions get logged too. exc_logger = logging.getLogger('exceptions') exc_msg = str(uuid.uuid4()) exc_logger.error(exc_msg) exc_log_data = self.pav_cfg.exception_log.open().read() self.assertIn(exc_msg, exc_log_data) # This should log through the 'root' logger. my_logger = logging.getLogger(__file__) root_msg = str(uuid.uuid4()) my_logger.error(root_msg) root_log_data = (self.pav_cfg.working_dir / 'pav.log').open().read() # All data goes to the root log as well. self.assertIn(result_msg, root_log_data) self.assertIn(yapsy_msg, root_log_data) self.assertIn(exc_msg, root_log_data) self.assertIn(root_msg, root_log_data)
def main(): """Setup Pavilion and run a command.""" # Pavilion is compatible with python >= 3.4 if sys.version_info[0] != 3 or sys.version_info[1] < 4: output.fprint("Pavilion requires python 3.4 or higher.", color=output.RED, file=sys.stderr) sys.exit(-1) # Get the config, and try: pav_cfg = config.find() except Exception as err: output.fprint("Error getting config, exiting: {}".format(err), file=sys.stderr, color=output.RED) sys.exit(-1) # Create the basic directories in the working directory and the .pavilion # directory. for path in [ config.USER_HOME_PAV, config.USER_HOME_PAV / 'working_dir', pav_cfg.working_dir / 'builds', pav_cfg.working_dir / 'series', pav_cfg.working_dir / 'test_runs', pav_cfg.working_dir / 'users' ]: try: path = path.expanduser() path.mkdir(exist_ok=True) except OSError as err: output.fprint( "Could not create base directory '{}': {}".format(path, err), color=output.RED, file=sys.stderr, ) sys.exit(1) # Setup all the loggers for Pavilion if not log_setup.setup_loggers(pav_cfg): sys.exit(1) # This has to be done before we initialize plugins parser = arguments.get_parser() # Initialize all the plugins try: plugins.initialize_plugins(pav_cfg) except plugins.PluginError as err: output.fprint("Error initializing plugins: {}".format(err), color=output.RED, file=sys.stderr) sys.exit(-1) pav_cfg.pav_vars = pavilion_variables.PavVars() # Parse the arguments try: args = parser.parse_args() except Exception: # TODO: Handle argument parsing errors correctly. raise if args.command_name is None: parser.print_help() sys.exit(0) try: cmd = commands.get_command(args.command_name) except KeyError: output.fprint("Unknown command '{}'.".format(args.command_name), color=output.RED, file=sys.stderr) sys.exit(-1) try: sys.exit(cmd.run(pav_cfg, args)) except Exception as err: exc_info = { 'traceback': traceback.format_exc(), 'args': vars(args), 'config': pav_cfg, } json_data = output.json_dumps(exc_info) logger = logging.getLogger('exceptions') logger.error(json_data) output.fprint( "Unknown error running command {}: {}.".format( args.command_name, err), color=output.RED, file=sys.stderr, ) traceback.print_exc(file=sys.stderr) output.fprint( "Traceback logged to {}".format(pav_cfg.exception_log), color=output.RED, file=sys.stderr, ) sys.exit(-1)
def main(): """Setup Pavilion and run a command.""" # Pavilion is compatible with python >= 3.4 if sys.version_info[0] != 3 or sys.version_info[1] < 5: output.fprint("Pavilion requires python 3.5 or higher.", color=output.RED, file=sys.stderr) sys.exit(-1) # This has to be done before we initialize plugins parser = arguments.get_parser() # Get the config, and try: pav_cfg = config.find() except Exception as err: output.fprint("Error getting config, exiting: {}".format(err), file=sys.stderr, color=output.RED) sys.exit(-1) # Create the basic directories in the working directory and the .pavilion # directory. for path in [ config.USER_HOME_PAV, config.USER_HOME_PAV / 'working_dir', pav_cfg.working_dir / 'builds', pav_cfg.working_dir / 'series', pav_cfg.working_dir / 'test_runs', pav_cfg.working_dir / 'users' ]: try: path = path.expanduser() path.mkdir(exist_ok=True) except OSError as err: output.fprint( "Could not create base directory '{}': {}".format(path, err), color=output.RED, file=sys.stderr, ) sys.exit(1) # Setup all the loggers for Pavilion if not log_setup.setup_loggers(pav_cfg): sys.exit(1) # Initialize all the plugins try: plugins.initialize_plugins(pav_cfg) except plugins.PluginError as err: output.fprint("Error initializing plugins: {}".format(err), color=output.RED, file=sys.stderr) sys.exit(-1) # Parse the arguments try: args = parser.parse_args() except Exception: raise if args.command_name is None: parser.print_help() sys.exit(0) pav_cfg.pav_vars = pavilion_variables.PavVars() if not args.profile: run_cmd(pav_cfg, args) else: import cProfile import pstats stats_path = '/tmp/{}_pav_pstats'.format(os.getlogin()) cProfile.runctx('run_cmd(pav_cfg, args)', globals(), locals(), stats_path) stats = pstats.Stats(stats_path) print("Profile Table") stats.strip_dirs().sort_stats(args.profile_sort)\ .print_stats(args.profile_count)