def process(patterns, text): """Return text modified by patterns.""" for i in range(len(patterns)): if isinstance(patterns[i], tuple): inner_patterns = [patterns[i]] else: inner_patterns = patterns[i] found = [] for grammar, replace in inner_patterns: find_and_replace = create_find_and_replace(grammar, replace) results = parse_grammar(find_and_replace, text) if not results: break else: found.append(len(results)) text = _transform_results(results, text) if found: log.info('=> pattern {} found {} time(s) in {} pass(es)'.format( i + 1, sum(found), len(found))) else: log.info('__ pattern {} not found'.format(i + 1)) return text
def _process_file(patterns, text_file): log.info('undebting {}'.format(text_file)) text = _load_text(text_file) try: result_text = process(patterns, text) except Exception: log.exception(traceback.format_exc()) return False else: _write_result_text(result_text, text_file) return True
def process(patterns, text_file, dry_run): log.info('undebting {}'.format(text_file)) text = _load_text(text_file) try: result_text = logic.process(patterns, text) except Exception: log.exception(traceback.format_exc()) return False else: if result_text != text: _write_result_text(result_text, text_file, dry_run) return True
def get_verify_replace(token): verify = "" log.info(token) if token == "once()": verify = "verify()" if token == "never()": verify = "verify(never)" token_split = token.split("(") if token_split[0] == "atLeast": verify = "verify(" + token + ")" if token_split[0] == "atMost": verify = "verify(" + token + ")" return verify
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 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 process(patterns, text): """Return text modified by patterns.""" for i, p in enumerate(patterns): pattern = _fix_pattern(p) found = [] for grammar, replace in pattern: find_and_replace = create_find_and_replace(grammar, replace) results = parse_grammar(find_and_replace, text) if results: found.append(len(results)) text = _transform_results(results, text) if found: log.info('=> pattern {} found {} time(s) in {} pass(es)' .format(i + 1, sum(found), len(found))) else: log.info('__ pattern {} not found' .format(i + 1)) return text