📄 pfind.py
字号:
#!/usr/bin/env python################################################################################ ## Copyright 2005 University of Cambridge Computer Laboratory. ## ## This file is part of Nprobe. ## ## Nprobe is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## Nprobe is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Nprobe; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## ################################################################################################################################################################ ## ## Search Python files/directories for text, regular expression, or numbered## lines, presenting matches in context## ############################################################################## ############################################################################ from string import *from sys import argvimport getoptimport osimport sysfrom stat import *from os.path import basename, dirname, join, isfile, isdirimport popen2import re ############################################################################ ############################################################################## Use pretty printing if possible#try: from print_col import cprint, cbprint, bprint from print_col import F_RED, F_GREEN, F_BLUE, F_MAGENTA, F_CYAN, F_YELLOW def print_red(s): cprint(F_RED, s) def print_green(s): cprint(F_GREEN, s) def print_blue(s): cprint(F_BLUE, s) def print_magenta(s): cprint(F_MAGENTA, s) def print_cyan(s): cprint(F_CYAN, s) def print_yellow(s): cprint(F_YELLOW, s) def print_boldred(s): cbprint(F_RED, s) def print_boldgreen(s): cbprint(F_GREEN, s) def print_boldblue(s): cbprint(F_BLUE, s) def print_boldmagenta(s): cbprint(F_MAGENTA, s) def print_boldcyan(s): cbprint(F_CYAN, s) def print_bold(s): bprint(s)except: redstring = chr(27) + chr(91) + chr(51) + chr(49) + chr(109) + chr(0) normstring = chr(27) + chr(91) + chr(51) + chr(59) + chr(109) + chr(0) sys.stderr.write('\n#########################################################################\n') try: sys.stderr.write('%sGet pretty printing for this utility%s\n' \ % (redstring, normstring)) except: sys.stderr.write('Get pretty printing for this utility\n') sys.stderr.write('Put \'/usr/groups/nprobe/jch1003/swig/utility/print_col.py on your PYPATH\'\n') sys.stderr.write('#########################################################################\n\n') def print_red(s): print(s) def print_green(s): print(s) def print_blue(s): print(s) def print_magenta(s): print(s) def print_cyan(s): print(s) def print_yellow(s): print(s) def print_boldred(s): print(s) def print_boldgreen(s): print(s) def print_boldblue(s): print(s) def print_boldmagenta(s): print(s) def print_boldcyan(s): print(s) def print_bold(s): print(s) ############################################################################## ##############################################################################def eprint(s): sys.stderr.write(s + '\n') ############################################################################## ##############################################################################def usage(nm, msg=None): if msg: eprint('%s: %s' % (nm, msg)) eprint('%s: Find text or RE in Python file, giving context' % (nm)) eprint('Usage:') eprint('\t%s <txt> [dir] - cwd if not dir given' % (nm)) eprint('\t%s -r <regexp> [dir]' % (nm)) eprint('Flags: \t[-h] this help\n\t[-d] recursively descend directory tree\n\t[-Dn] ditto to depth n\n\t[-s] use RE search for RE (default is match)\n\t[-f<file>] search for RE defined in file\n\t\t(One RE per line - multiple lines are treated as alternations)') eprint('\t[-l<file>|<list>] present numbered lines in context\n\t\t(lines as comma separated set of line numbers or ranges,\n\t\tor in file - one line number or range per line)') sys.exit(1)##############################################################################def get_files(args, recurse, ferr, matchfn, idre, classre, fnre, cmmtre, lines): for a in args: try: mode = os.stat(a)[ST_MODE] except OSError, s: ferr.append((a, 'stat', s)) continue if S_ISDIR(mode) and recurse: args2 = [] try: for f in os.listdir(a): args2.append(join(a, f)) get_files(args2, recurse-1, ferr, matchfn, idre, classre, fnre, cmmtre, lines) except OSError, s: ferr.append((a, 'ls', s)) elif S_ISREG(mode) and a[-3:] == '.py': #flist.append(a) do_file(a, ferr, matchfn, idre, classre, fnre, cmmtre, lines)##############################################################################def do_file(fnm, ferr, matchfn, idre, classre, fnre, cmmtre, lines): module_printed = 0 Class = None class_printed = 0 Fn = None fn_printed = 0 method = None method_printed = 0 method_ind = 0 submethod = None submethod_printed = 0 submethod_ind = 0 indok = 1 badindstart = None try: f = open(fnm, 'r') except IOError, s: ferr.append((a, 'open', s)) #print 'Couldn\'t open search file', s return lineno = 0 module = os.path.basename(fnm) #print 'lines', lines for l in f.readlines(): lineno += 1 l = l[:-1] if (not l): continue #print l.count('\t'), 'tabs', len(l)-len(l.lstrip()), 'spaces' comment = 0 if not cmmtre.match(l): # not a comment - look at syntax and indentation n = idre.match(l) if n: ind = len(n.group(1)) #print 'indent', ind, lineno else: # print 'no ind match' ind = 0 if ind == 0: # print 'cancelling Class, Fn, method, submethod line %d \'%s\'' % (lineno, l) Class = None Fn = None method = None submethod = None if submethod: if not submethod_ind: submethod_ind = ind if ind < submethod_ind: submethod = None submethod_ind = 0 n = classre.match(l) if n: # print 'got class', Class Class = n.group(1) class_printed = 0 n = fnre.match(l) if n: fnind = len(n.group(1)) if fnind == 0: Fn = n.group(2) # print 'got fn', Fn, 'ind', fnind fn_printed = 0 indok = 1 elif (Class and method and fnind > method_ind) \ or (Fn and (ind > 0)): submethod = n.group(2) submethod_printed = 0 #submethod_ind = fnind # print 'got submethod', submethod, 'ind', fnind indok = 1 elif Class: Fn = None method = n.group(2) method_printed = 0 method_ind = fnind submethod = None # print 'got method', method, 'ind', fnind indok = 1 else: #print 'XXX Don\'t understand indentation (%d) at %s line %d \'%s\' method ind = %d Class = %s' % (fnind, fnm, lineno, l, method_ind, Class) indok = 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -