Created
February 23, 2012 16:30
-
-
Save gustavohenrique/1893592 to your computer and use it in GitHub Desktop.
Find string in files recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os, re | |
EXCLUDE_DIRS = ['.svn', '.settings', 'target', 'builds'] | |
EXCLUDE_FILES = ['config.xml', 'pom.xml', 'build.xml', 'changelog.xml'] | |
FILETYPES = ['.java', '.properties', '.jsp', '.html', '.jrxml', '.xml', '.ini'] | |
PATTERNS = ['telefone', ' tel ', 'tel:', 'fone', 'celular', 'clulr', ' cel ', 'cel:'] | |
report = {} | |
def _remove(list, excludes): | |
for item in excludes: | |
if item in list: list.remove(item) | |
return list | |
def _found(pattern, line): | |
return re.search(pattern, line, re.IGNORECASE) | |
def _project_extension(f): | |
for extension in FILETYPES: | |
if f.endswith(extension): | |
return extension | |
return False | |
def _add_in_report(project, extension): | |
if report.has_key(project): | |
extensions = report.get(project) | |
extensions.append(extension) | |
else: | |
extensions = [extension] | |
report.update({project: extensions}) | |
""" | |
def _add_in_report(path, line): | |
if report.has_key(path): | |
lines = report.get(path) | |
lines.append(line) | |
else: | |
lines = [line] | |
report.update({path: lines}) | |
def _print_report(): | |
for path in report.keys(): | |
print 'Arquivo: %s' % path | |
for line in report.get(path): | |
print line | |
""" | |
def _save_report(): | |
out = open('/tmp/report_fone.txt', 'w') | |
for project in report.keys(): | |
out.write('\n%s' % project) | |
extensions = report.get(project) | |
for filetype in FILETYPES: | |
total = extensions.count(filetype) | |
out.write('\n%s: %s' % (filetype, total)) | |
out.write('\n\n') | |
out.close() | |
projects = os.listdir('/opt/ips/config/build/jobs') | |
for p in projects: | |
project = '/opt/ips/config/build/jobs/%s' % p | |
for root, dirs, files in os.walk(project): | |
dirs =_remove(dirs, EXCLUDE_DIRS) | |
files = _remove(files, EXCLUDE_FILES) | |
path_viewed = '' | |
for file in files: | |
extension = _project_extension(file) | |
if extension: | |
path = os.path.join(root, file) | |
if not path == path_viewed: | |
path_viewed = path | |
for line in open(path, 'r'): | |
for pattern in PATTERNS: | |
if _found(pattern, line): | |
_add_in_report(project, extension) | |
break | |
""" | |
for root, dirs, files in os.walk('/opt/ips/config/build/jobs'): | |
dirs =_remove(dirs, EXCLUDE_DIRS) | |
files = _remove(files, EXCLUDE_FILES) | |
path_viewed = '' | |
for file in files: | |
if _authorized(file): | |
path = os.path.join(root, file) | |
if not path == path_viewed: | |
path_viewed = path | |
for line in open(path, 'r'): | |
for pattern in PATTERNS: | |
found = _found(pattern, line) | |
if found: | |
line = found.string.lstrip() | |
_add_in_report(path, line) | |
break | |
""" | |
_save_report() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment