Added loaderon-hooks logic

This commit is contained in:
Alvaro Andrés Rodríguez Scelza 2019-06-02 02:10:25 -03:00
parent 0b70e285e3
commit 0a66e6635d
23 changed files with 612 additions and 0 deletions

View file

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
import re
from pre_commit_logic.util.template_methods.checker_template_method import CheckerTemplateMethod
from pre_commit_logic.util.git_helpers import get_current_branch_name
class BranchNameChecker(CheckerTemplateMethod):
def _add_arguments_to_parser(self):
self.parser.add_argument('-r', '--regex', help='Regex that git current branch must match.')
def _perform_checks(self):
regular_expression = self.args.regex
pattern = re.compile(regular_expression)
current_branch_name = get_current_branch_name()
if not pattern.match(current_branch_name):
self.inform_check_failure('El nombre de la rama debe ser del estilo {}'.format(regular_expression))
def main(argv=None):
return BranchNameChecker(argv).run()
if __name__ == '__main__':
exit(main())

View file

@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
import re
from pre_commit_logic.util.template_methods.lines_checker_template_method import LinesCheckerTemplateMethod
class ClassDocstringChecker(LinesCheckerTemplateMethod):
def _check_line(self):
regular_expression = r'^(\t| )*class .+\(.*\):'
pattern = re.compile(regular_expression)
if pattern.match(self._file_line):
self.__check_class_docstring(self._file_line_index)
def __check_class_docstring(self, class_line_index):
class_first_line = self._file_lines[class_line_index + 1]
if class_first_line in ['\n', '\r\n']:
self.inform_check_failure('El docstring de la clase {} está separado de su clase por una o más líneas en '
'blanco.'.format(self._file_lines[class_line_index]))
if not class_first_line.strip().startswith('\"\"\"'):
self.inform_check_failure('La clase {} no tiene docstring.'.format(self._file_lines[class_line_index]))
def main(argv=None):
return ClassDocstringChecker(argv).run()
if __name__ == '__main__':
exit(main())

View file

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
import re
from pre_commit_logic.util.template_methods.lines_checker_template_method import LinesCheckerTemplateMethod
class LinesChecker(LinesCheckerTemplateMethod):
def _add_arguments_to_parser(self):
super(LinesChecker, self)._add_arguments_to_parser()
self.parser.add_argument('-l', '--line-to-check', action='append', help='Regex to check.')
self.parser.add_argument('-r', '--regexp-to-match', action='append', help='Regex to match.')
def _check_arguments(self):
self.check_arguments_size_match(self.args.line_to_check, self.args.regexp_to_match)
def _check_line(self):
for line_index, line_to_check in enumerate(self.args.line_to_check):
line_to_check_pattern = re.compile(line_to_check)
if line_to_check_pattern.match(self._file_line):
line_regexp_to_match = self.args.regexp_to_match[line_index]
correct_pattern = re.compile(line_regexp_to_match)
if not correct_pattern.match(self._file_line):
self.inform_check_failure(
"Una de las líneas con '{}' no está correctamente formulada. Línea {}: \n\n{}\n"
"Debería cumplir la expresión regular: {}".format(
line_to_check,
self._file_line_index + 1,
self._file_line,
line_regexp_to_match
)
)
def main(argv=None):
return LinesChecker(argv).run()
if __name__ == '__main__':
exit(main())

View file

@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
"""Checks files locations."""
import os
import re
from pre_commit_logic.util.template_methods.file_checker_template_method import FileCheckerTemplateMethod
from pre_commit_logic.util.string_helpers import matches_any_regexp
class LocationChecker(FileCheckerTemplateMethod):
def _add_arguments_to_parser(self):
super(LocationChecker, self)._add_arguments_to_parser()
self.parser.add_argument(
'-ed', '--directories', action='append',
help='Directory regex to be added to white list. Can be set multiple times to add'
'multiple directories to white list.',
)
self.parser.add_argument(
'-ef', '--files', action='append',
help="Files regex, separated by a white space, to be added to it's corresponded"
"directory's white list. Order of this argument declaration matches order of"
"--directories declaration, so as to add files to that directory's files whitelist."
"This argument can be therefore be set multiple times so as to match --directories sets.",
)
def _check_file(self):
"""Check filename location against enabled directories and their enabled files."""
self.check_arguments_size_match(self.args.directories, self.args.files)
file_directory_path = os.path.dirname(self.filename)
file_name = os.path.basename(self.filename)
location_enabled = False
for directory_regexp in self.args.directories:
pattern = re.compile(directory_regexp)
if pattern.match(file_directory_path):
location_enabled = True
file_enabled = self.file_enabled_for_directory(directory_regexp, file_name)
if not file_enabled:
self.inform_check_failure('El archivo {} no está permitido para el directorio {}'
.format(self.filename, file_directory_path))
if not location_enabled:
self.inform_check_failure('El directorio {} no está habilitado'.format(file_directory_path))
def file_enabled_for_directory(self, directory_regexp, filename):
directory_position_in_arguments = self.args.directories.index(directory_regexp)
directory_files_whitelist = self.args.files[directory_position_in_arguments]
files_reg_expressions = directory_files_whitelist.split(' ')
return matches_any_regexp(filename, files_reg_expressions)
def main(argv=None):
return LocationChecker(argv).run()
if __name__ == '__main__':
exit(main())

View file

@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
"""Executes Pylint checks on all git modified and added files."""
import re
from pylint.lint import Run
from pylint.reporters.text import TextReporter
from pre_commit_logic.util.template_methods.file_checker_template_method import FileCheckerTemplateMethod
from pre_commit_logic.util.file_helpers import find_file_starting_from_reference_file_directory
class PylintChecker(FileCheckerTemplateMethod):
def __init__(self, argv):
super(PylintChecker, self).__init__(argv)
self.pylint_output = WritableObject()
def _add_arguments_to_parser(self):
super(PylintChecker, self)._add_arguments_to_parser()
self.parser.add_argument('-e', '--exclude', help='Excluded files to check.')
def _check_file(self):
try:
self.run_pylint_except_in_excluded_files()
except SystemExit as exception:
self.manage_pylint_result(exception)
def run_pylint_except_in_excluded_files(self):
excluded_files_regex = self.args.exclude
pattern = re.compile(excluded_files_regex)
if not pattern.match(self.filename):
self.run_pylint_on_file()
def run_pylint_on_file(self):
"""Runs pylint with .pylint config file if found, default config otherwise."""
pylint_configuration_file = find_file_starting_from_reference_file_directory(__file__, '.pylintrc')
if pylint_configuration_file:
Run(['--rcfile', pylint_configuration_file, self.filename], reporter=TextReporter(self.pylint_output))
else:
Run([self.filename], reporter=TextReporter(self.pylint_output))
def manage_pylint_result(self, exception):
print self.pylint_output.read()
if exception.code != 0:
self.inform_check_failure('')
class WritableObject(object):
"""Simple writable object for pylint."""
def __init__(self):
self.__content = ''
def write(self, line):
"""Adds line to object content."""
self.__content = self.__content + '\n' + line
def read(self):
"""Returns object content."""
return self.__content
def main(argv=None):
return PylintChecker(argv).run()
if __name__ == '__main__':
exit(main())

View file

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
from pre_commit_logic.util.template_methods.file_checker_template_method import FileCheckerTemplateMethod
from pre_commit_logic.util.file_helpers import read_file_line
class XMLEncodingChecker(FileCheckerTemplateMethod):
def _add_arguments_to_parser(self):
super(XMLEncodingChecker, self)._add_arguments_to_parser()
self.parser.add_argument('-e', '--encoding', help='Desired encoding.')
def _check_file(self):
first_line = read_file_line(self.filename)
desired_encoding = self.args.encoding.rstrip()
first_line = first_line.rstrip('\n')
if first_line != desired_encoding:
self.inform_check_failure(
'El archivo {} no comienza con la línea "{}" (verifica también espacios en blanco)'.format(
self.filename,
desired_encoding
)
)
def main(argv=None):
return XMLEncodingChecker(argv).run()
if __name__ == '__main__':
exit(main())