def main(): """Handle and process arguments from sys.argv.""" logger.setup() args = _handle_arguments() logger.setup(args.verbose) # Reset logging level if args.multiprocess <= 0: log.error('number of processes must be > 0') sys.exit(1) processor = _file_processor(args.pattern, args.dry_run) files = list(_find_files(args.input, args.extension)) if bool(files) != bool(args.input): log.error('could not find any files for the given paths and extension') sys.exit(1) if not files: # Single process mode if stdin log.info('running in stdin/stdout mode') processor(None) elif len(files) == 1 or args.multiprocess == 1: # Single process if only one file or only one process log.info('running across {} file(s) using a single process' .format(len(files))) processor(files[0]) else: process_pool = multiprocessing.Pool(args.multiprocess) try: result = process_pool.map_async( processor, files, ) process_pool.close() log.info('running across {} file(s) using {} processes' .format(len(files), args.multiprocess)) # Cannot do process_pool.wait() because it prevents KeyboardInterrupt from being sent # See http://stackoverflow.com/questions/1408356/keyboard-interrupts-with-pythons-multiprocessing-pool while not result.ready(): time.sleep(0.01) if not result.successful(): log.error('multiprocessing failed (are your replace functions pickleable?)') sys.exit(1) result = result.get() assert len(result) == len(files) if not all(result): log.error('failed to process {} files' .format(len(result) - sum(result))) sys.exit(1) except: process_pool.terminate() raise finally: process_pool.join()
def main(args=sys.argv[1:]): """Handle and process arguments from args.""" args = _handle_arguments(args) logger.setup(args.verbose) patterns = load_patterns(args.pattern) files = args.files if not files: log.info('running in stdin/stdout mode') process(patterns, None, args.dry_run) return log.info('running across {} file(s)'.format(len(files))) for f in files: process(patterns, f, args.dry_run)
def main(): """Handle and process arguments from sys.argv.""" args = _handle_arguments() logger.setup(args.verbose) processor = _file_processor(args.pattern, args.dry_run) files = args.files if not files: log.info('running in stdin/stdout mode') processor(None) return log.info('running across {} file(s)'.format(len(files))) for f in files: processor(f)
def test_setup_verbose_false(mock_log): setup(verbose=False) mock_log.setLevel.assert_called_once_with(logging.ERROR)