#!/usr/bin/env python """This script searches files for functions that are just aliases in PHP source code. This is not 100% reliable, so it should not be automated, but it's useful to run once in a while to make sure that all of the matches it finds are not really legitimate aliases. Usage: parse_aliases.py [PHP source code filename]... """ import sys # Fetch this URL to get the file that is parsed into the aliases list alias_url = 'http://www.zend.com/phpfunc/all_aliases.php' header_tok = ''; footer_tok = ''; # Example line of the table that we parse: # 'bzclosephp-src/ext/bz2/bz2.cfclose' import re line_re = re.compile(r''' \A ]+"> ([^<>]+) ]+">[^<>]+ (?: ]+\.php"> ( [^<>]+ ) | ( [^<>]+ ) ) \Z ''', re.VERBOSE) def parseString(s): _, rest = s.split(header_tok, 1) body, _ = rest.split(footer_tok, 1) lines = body.split('\n') assert [s.strip() for s in lines[-2:]] == ['', ''] assert lines[0].strip().startswith('|\$|)\s*\b(%s)\b' % ('|'.join(aliases.keys()))) def checkAliasesFile(alias_re, f): found = [] line_num = 1 for line in f: for mo in alias_re.finditer(line): if mo.group(1): continue alias = mo.group(2) found.append((line_num, alias)) line_num += 1 return found def checkAliases(alias_re, filename): return checkAliasesFile(alias_re, file(filename, 'r')) def checkAliasesFiles(alias_re, filenames): found = [] for filename in filenames: file_found = checkAliases(alias_re, filename) found.extend([(filename, n, a) for (n, a) in file_found]) return found def dumpResults(aliases, found, out=sys.stdout): for filename, n, a in found: print >>out, "%s:%d %s -> %s" % (filename, n, a, aliases[a]) def main(alias_file, *filenames): aliases = parseFileName(alias_file) alias_re = getAliasRE(aliases) found = checkAliasesFiles(alias_re, filenames) dumpResults(aliases, found) return found if __name__ == '__main__': found = main(*sys.argv[1:]) if found: sys.exit(1)