def test_basic(self): pss_run( roots=[self.testdir1], pattern='abc', output_formatter=self.of, include_types=['cc']) self.assertEqual(sorted(self.of.output), sorted(self._gen_outputs_in_file( 'testdir1/filea.c', [('MATCH', (2, [(4, 7)]))]) + self._gen_outputs_in_file( 'testdir1/filea.h', [('MATCH', (1, [(8, 11)]))])))
def test_basic(self): pss_run(roots=[self.testdir1], pattern='abc', output_formatter=self.of, include_types=['cc']) self.assertEqual( sorted(self.of.output), sorted( self._gen_outputs_in_file('testdir1/filea.c', [('MATCH', (2, [(4, 7)]))]) + self._gen_outputs_in_file('testdir1/filea.h', [('MATCH', (1, [( 8, 11)]))])))
def test_basic(self): match_found = pss_run( roots=[self.testdir1], pattern='abc', output_formatter=self.of1, include_types=['cc']) self.assertEqual( sorted(self.of1.output), sorted( matches('testdir1/filea.c', [('MATCH', (2, [(4, 7)]))]) + matches('testdir1/filea.h', [('MATCH', (1, [(8, 11)]))]) )) self.assertEquals(match_found, True)
def test_basic(self): match_found = pss_run( roots=[self.testdir1], pattern='abc', output_formatter=self.of1, include_types=['cc']) self.assertEqual( sorted(self.of1.output), sorted( matches('testdir1/filea.c', [('MATCH', (2, [(4, 7)]))]) + matches('testdir1/filea.h', [('MATCH', (1, [(8, 11)]))]) )) self.assertEqual(match_found, True)
def test_only_find_files_with_include_pattern(self): match_found = pss_run( roots=[self.testdir4], pattern='Test', output_formatter=self.of4, only_find_files=True, include_patterns=['file[12]']) self.assertFoundFiles( self.of4, [ 'testdir4/file1.py', 'testdir4/file2.py', 'testdir4/file1.txt', 'testdir4/file2.txt', ] ) self.assertEquals(match_found, True)
def test_only_find_files_with_include_types(self): match_found = pss_run( roots=[self.testdir1], pattern='abc', output_formatter=self.of1, only_find_files=True, include_types=['cc']) self.assertFoundFiles( self.of1, [ 'testdir1/filea.c', 'testdir1/filea.h', 'testdir1/subdir1/filey.c', 'testdir1/subdir1/filez.c', ] ) self.assertEquals(match_found, True)
def test_find_in_files_with_exclude_patterns(self): match_found = pss_run( roots=[self.testdir4], pattern='Test', output_formatter=self.of4, only_find_files=False, exclude_patterns=['file[12].txt', 'file3', 'main.*.py']) self.assertEqual( sorted(self.of4.output), sorted( matches('testdir4/file1.py' , [('MATCH', (1, [(0, 4)]))]) + matches('testdir4/file2.py' , [('MATCH', (1, [(0, 4)]))]) + matches('testdir4/main1.txt', [('MATCH', (1, [(0, 4)]))]) + matches('testdir4/main2.txt', [('MATCH', (1, [(0, 4)]))]) + matches('testdir4/main3.txt', [('MATCH', (1, [(0, 4)]))]) )) self.assertEquals(match_found, True)
def test_only_find_files_with_include_pattern(self): match_found = pss_run( roots=[self.testdir4], pattern='Test', output_formatter=self.of4, only_find_files=True, include_patterns=['file[12]']) self.assertFoundFiles( self.of4, [ 'testdir4/file1.py', 'testdir4/file2.py', 'testdir4/file1.txt', 'testdir4/file2.txt', ] ) self.assertEqual(match_found, True)
def test_only_find_files_with_include_types(self): match_found = pss_run( roots=[self.testdir1], pattern='abc', output_formatter=self.of1, only_find_files=True, include_types=['cc']) self.assertFoundFiles( self.of1, [ 'testdir1/filea.c', 'testdir1/filea.h', 'testdir1/subdir1/filey.c', 'testdir1/subdir1/filez.c', ] ) self.assertEqual(match_found, True)
def test_find_in_files_with_exclude_patterns(self): match_found = pss_run( roots=[self.testdir4], pattern='Test', output_formatter=self.of4, only_find_files=False, exclude_patterns=['file[12].txt', 'file3', 'main.*.py']) self.assertEqual( sorted(self.of4.output), sorted( matches('testdir4/file1.py' , [('MATCH', (1, [(0, 4)]))]) + matches('testdir4/file2.py' , [('MATCH', (1, [(0, 4)]))]) + matches('testdir4/main1.txt', [('MATCH', (1, [(0, 4)]))]) + matches('testdir4/main2.txt', [('MATCH', (1, [(0, 4)]))]) + matches('testdir4/main3.txt', [('MATCH', (1, [(0, 4)]))]) )) self.assertEqual(match_found, True)
def main(argv=sys.argv, output_formatter=None): """ Main pss argv: Program arguments, similar to sys.argv output_formatter: An OutputFormatter object to emit output to. Set to None for the default. """ options, args, optparser = parse_cmdline(argv[1:]) # Handle the various "only find files" options. # only_find_files = False only_find_files_option = PssOnlyFindFilesOption.ALL_FILES search_pattern_expected = True if options.find_files: only_find_files = True search_pattern_expected = False elif options.find_files_matching_pattern is not None: only_find_files = True search_pattern_expected = False options.type_pattern = options.find_files_matching_pattern elif options.find_files_with_matches: only_find_files = True only_find_files_option = PssOnlyFindFilesOption.FILES_WITH_MATCHES elif options.find_files_without_matches: only_find_files = True only_find_files_option = PssOnlyFindFilesOption.FILES_WITHOUT_MATCHES # The --match option sets the pattern explicitly, so it's not expected # as an argument. if options.match: search_pattern_expected = False # Handle the various --help options, or just print help if pss is called # without arguments. if options.help_types: print_help_types() sys.exit(0) elif options.show_type_list: show_type_list() sys.exit(0) elif (len(args) == 0 and search_pattern_expected) or options.help: optparser.print_help() print(DESCRIPTION_AFTER_USAGE) sys.exit(0) # Unpack args. If roots are not specified, the current directory is the # only root. If no search pattern is expected, the whole of 'args' is roots. # if not search_pattern_expected: pattern = options.match or None roots = args else: pattern = args[0] roots = args[1:] if len(roots) == 0: roots = ['.'] # Partition the type list to included types (--<type>) and excluded types # (--no<type>) # include_types = [] exclude_types = [] for typ in getattr(options, 'typelist', []): if typ.startswith('no'): exclude_types.append(typ[2:]) else: include_types.append(typ) # If the context option is specified, it overrides both after-context # and before-context # ncontext_before = options.before_context ncontext_after = options.after_context if options.context is not None: ncontext_before = ncontext_after = options.context add_ignored_dirs = _splice_comma_names(options.ignored_dirs or []) remove_ignored_dirs = _splice_comma_names(options.noignored_dirs or []) # Finally, invoke pss_run with the default output formatter # try: pss_run(roots=roots, pattern=pattern, output_formatter=output_formatter, only_find_files=only_find_files, only_find_files_option=only_find_files_option, search_all_types=options.all_types, search_all_files_and_dirs=options.unrestricted, add_ignored_dirs=add_ignored_dirs, remove_ignored_dirs=remove_ignored_dirs, recurse=options.recurse, textonly=options.textonly, type_pattern=options.type_pattern, include_types=include_types, exclude_types=exclude_types, ignore_case=options.ignore_case, smart_case=options.smart_case, invert_match=options.invert_match, whole_words=options.word_regexp, literal_pattern=options.literal, max_match_count=options.max_count, do_colors=options.do_colors, match_color_str=options.color_match, filename_color_str=options.color_filename, lineno_color_str=options.color_lineno, do_break=options.do_break, do_heading=options.do_heading, prefix_filename_to_file_matches=options.prefix_filename, show_column_of_first_match=options.show_column, ncontext_before=ncontext_before, ncontext_after=ncontext_after) except KeyboardInterrupt: print('<<interrupted - exiting>>') sys.exit(0)
def main(argv=sys.argv, output_formatter=None): """ Main pss argv: Program arguments, similar to sys.argv output_formatter: An OutputFormatter object to emit output to. Set to None for the default. """ options, args, optparser = parse_cmdline(argv[1:]) # Handle the various "only find files" options. # only_find_files = False only_find_files_option = PssOnlyFindFilesOption.ALL_FILES search_pattern_expected = True if options.find_files: only_find_files = True search_pattern_expected = False elif options.find_files_matching_pattern is not None: only_find_files = True search_pattern_expected = False options.type_pattern = options.find_files_matching_pattern elif options.find_files_with_matches: only_find_files = True only_find_files_option = PssOnlyFindFilesOption.FILES_WITH_MATCHES elif options.find_files_without_matches: only_find_files = True only_find_files_option = PssOnlyFindFilesOption.FILES_WITHOUT_MATCHES # The --match option sets the pattern explicitly, so it's not expected # as an argument. if options.match: search_pattern_expected = False # Handle the various --help options, or just print help if pss is called # without arguments. if options.help_types: print_help_types() sys.exit(0) elif (len(args) == 0 and search_pattern_expected) or options.help: optparser.print_help() print(DESCRIPTION_AFTER_USAGE) sys.exit(0) # Unpack args. If roots are not specified, the current directory is the # only root. If no search pattern is expected, the whole of 'args' is roots. # if not search_pattern_expected: pattern = options.match or None roots = args else: pattern = args[0] roots = args[1:] if len(roots) == 0: roots = ['.'] # Partition the type list to included types (--<type>) and excluded types # (--no<type>) # include_types = [] exclude_types = [] for typ in getattr(options, 'typelist', []): if typ.startswith('no'): exclude_types.append(typ[2:]) else: include_types.append(typ) # If the context option is specified, it overrides both after-context # and before-context # ncontext_before = options.before_context ncontext_after = options.after_context if options.context is not None: ncontext_before = ncontext_after = options.context add_ignored_dirs = _splice_comma_names(options.ignored_dirs or []) remove_ignored_dirs = _splice_comma_names(options.noignored_dirs or []) # Finally, invoke pss_run with the default output formatter # try: pss_run(roots=roots, pattern=pattern, output_formatter=output_formatter, only_find_files=only_find_files, only_find_files_option=only_find_files_option, search_all_types=options.all_types, search_all_files_and_dirs=options.unrestricted, add_ignored_dirs=add_ignored_dirs, remove_ignored_dirs=remove_ignored_dirs, recurse=options.recurse, textonly=options.textonly, type_pattern=options.type_pattern, include_types=include_types, exclude_types=exclude_types, ignore_case=options.ignore_case, smart_case=options.smart_case, invert_match=options.invert_match, whole_words=options.word_regexp, literal_pattern=options.literal, max_match_count=options.max_count, do_colors=options.do_colors, match_color_str=options.color_match, filename_color_str=options.color_filename, lineno_color_str=options.color_lineno, do_break=options.do_break, do_heading=options.do_heading, prefix_filename_to_file_matches=options.prefix_filename, show_column_of_first_match=options.show_column, ncontext_before=ncontext_before, ncontext_after=ncontext_after) except KeyboardInterrupt: print('<<interrupted - exiting>>') sys.exit(0)
def main(argv=sys.argv, output_formatter=None): """ Main pss argv: Program arguments, similar to sys.argv output_formatter: An OutputFormatter object to emit output to. Set to None for the default. return: Return code to be used when exiting to system. 0: Match found or help/version printed. 1: No match. 2: Error. """ try: options, args, optparser = parse_cmdline(argv[1:]) except VersionPrinted: return 0 except SystemExit: return 2 # Handle --type-add-pattern, which modifies driver.TYPE_MAP # for spec in (options.type_add_patterns or []): if '=' not in spec: optparser.error('argument must be in TYPE=PATTERN format') typ, pattern = spec.split('=', 1) if typ not in TYPE_MAP: # Add type to TYPE_MAP if it doesn't already exist TYPE_MAP[typ] = TypeSpec([], []) TYPE_MAP[typ].patterns.append(pattern) # Handle the various "only find files" options. # only_find_files = False only_find_files_option = PssOnlyFindFilesOption.ALL_FILES search_pattern_expected = True if options.find_files: only_find_files = True search_pattern_expected = False elif options.find_files_matching_pattern is not None: only_find_files = True search_pattern_expected = False options.type_pattern = options.find_files_matching_pattern elif options.find_files_with_matches: only_find_files = True only_find_files_option = PssOnlyFindFilesOption.FILES_WITH_MATCHES elif options.find_files_without_matches: only_find_files = True only_find_files_option = PssOnlyFindFilesOption.FILES_WITHOUT_MATCHES # The --match option sets the pattern explicitly, so it's not expected # as an argument. if options.match: search_pattern_expected = False # Handle the various --help options, or just print help if pss is called # without arguments. if options.help_types: print_help_types() return 0 elif options.show_type_list: show_type_list() return 0 elif (len(args) == 0 and search_pattern_expected) or options.help: optparser.print_help() print(DESCRIPTION_AFTER_USAGE) return 0 # Unpack args. If roots are not specified, the current directory is the # only root. If no search pattern is expected, the whole of 'args' is roots. # if not search_pattern_expected: pattern = options.match or None roots = args else: pattern = args[0] roots = args[1:] if len(roots) == 0: roots = ['.'] # Partition the type list to included types (--<type>) and excluded types # (--no<type>) # include_types = [] exclude_types = [] for typ in getattr(options, 'typelist', []): if typ.startswith('no'): exclude_types.append(typ[2:]) else: include_types.append(typ) # If the context option is specified, it overrides both after-context # and before-context # ncontext_before = options.before_context ncontext_after = options.after_context if options.context is not None: ncontext_before = ncontext_after = options.context add_ignored_dirs = _splice_comma_names(options.ignored_dirs or []) remove_ignored_dirs = _splice_comma_names(options.noignored_dirs or []) # Finally, invoke pss_run with the default output formatter # try: match_found = pss_run( roots=roots, pattern=pattern, output_formatter=output_formatter, only_find_files=only_find_files, only_find_files_option=only_find_files_option, search_all_types=options.all_types, search_all_files_and_dirs=options.unrestricted, add_ignored_dirs=add_ignored_dirs, remove_ignored_dirs=remove_ignored_dirs, recurse=options.recurse, textonly=options.textonly, type_pattern=options.type_pattern, include_types=include_types, exclude_types=exclude_types, ignore_case=options.ignore_case, smart_case=options.smart_case, invert_match=options.invert_match, whole_words=options.word_regexp, literal_pattern=options.literal, max_match_count=options.max_count, do_colors=options.do_colors, match_color_str=options.color_match, filename_color_str=options.color_filename, lineno_color_str=options.color_lineno, do_break=options.do_break, do_heading=options.do_heading, prefix_filename_to_file_matches=options.prefix_filename, show_column_of_first_match=options.show_column, universal_newlines=options.universal_newlines, ncontext_before=ncontext_before, ncontext_after=ncontext_after) except KeyboardInterrupt: print('<<interrupted - exiting>>') return 2 except Exception as err: print('<<unexpected error: %s>>' % err) return 2 else: return 0 if match_found else 1