#!/usr/bin/env python import os, sys, re oj = os.path.join from glob import glob SQL_START = re.compile('^\s*SELECT|UPDATE|CREATE|DELETE|INSERT|BEGIN', re.I|re.M) TAGS_FORMAT_TMPL = '%s\t%s\t%s\n' def _notworthit(directory): if directory.endswith('CVS'): return True return False def _findlinenumber(code, charstart): t = 0 for i, c in enumerate(code): if c == '\n': t += 1 if i >= charstart: return t + 1 return t def append(t, d): fa = open(t, 'a') for sqlfile in glob(oj(d, '*.sql')): tagname = os.path.basename(sqlfile).replace('.sql','') tagfile = sqlfile.replace(os.path.dirname(t),'')[1:] sql = open(sqlfile).read() match = SQL_START.search(sql) if match: #print sqlfile, match.start() linenumber = _findlinenumber(sql, match.start()) line = TAGS_FORMAT_TMPL % (tagname, tagfile, linenumber) fa.write(line) else: print "PROB ", repr(sqlfile) fa.close() for f in os.listdir(d): if os.path.isdir(oj(d, f)): if _notworthit(oj(d, f)): continue append(t, oj(d, f)) def run(t, d): """ index all .sql files recursively and enter into the tags file. For more info on the TAGS format, see http://ctags.sourceforge.net/FORMAT """ append(t, d) if not os.path.isfile(t): raise OSError, "File %r does not exist" % t return 0 if __name__=='__main__': tagsfile = os.path.abspath(sys.argv[1]) directory = os.path.dirname(tagsfile) sys.exit(run(tagsfile, directory))