Skip to content

Instantly share code, notes, and snippets.

@camilojd
Created May 31, 2015 14:59
Show Gist options
  • Save camilojd/364f106835c7b0432ed6 to your computer and use it in GitHub Desktop.
Save camilojd/364f106835c7b0432ed6 to your computer and use it in GitHub Desktop.
Lint RSTs in a dir, generate a report
import codecs
import collections
import os
import restructuredtext_lint
import sys
## Misc functions
## tomado de http://stackoverflow.com/questions/898669/how-can-i-detect-if-a-file-is-binary-non-text-in-python
textchars = bytearray([7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x100))
is_binary_string = lambda bytes: bool(bytes.translate(None, textchars))
SEVERITY_FILE_IS_BINARY = 'FILE_IS_BINARY'
def print_section(handle, severity, err_list):
global repo_dir
initial_path_len = len(repo_dir) + 1
handle.write('Severidad: ' + severity + '\n')
handle.write('======================\n')
for item in err_list:
item_line = item[initial_path_len:]
handle.write('- ' + item_line + '\n')
handle.write('\n')
## main()
if len(sys.argv) == 1:
print 'Se debe especificar el directorio donde se encuentra el clone del repo'
sys.exit(1)
repo_dir = sys.argv[1]
errors_dict = collections.defaultdict(list)
for r, dir, files in os.walk(repo_dir):
for f in files:
cur_file = os.path.join(r, f)
file_binary = open(cur_file, 'rb').read()
if len(file_binary) == 0:
continue
if is_binary_string(file_binary):
# :-(
errors_dict[SEVERITY_FILE_IS_BINARY].append(cur_file)
continue
file_contents = file_binary.decode('utf-8')
result = restructuredtext_lint.lint(file_contents)
for detail in result:
detail_fixed = detail.message if not detail.message.startswith('Substitution def') else 'Substitution definition --trimmed-- missing contents'
cur_detail = u'{0}: {1} - {2}'.format(cur_file.decode('ascii', 'ignore'), detail.line, detail_fixed)
errors_dict[detail.type].append(cur_detail)
with codecs.open('report.rst', 'w', encoding='utf-8') as handle:
handle.write('Reporte de errores\n')
handle.write('==================\n\n')
for cur_level in [SEVERITY_FILE_IS_BINARY, 'SEVERE', 'ERROR', 'WARNING', 'INFO']:
print_section(handle, cur_level, errors_dict[cur_level])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment