mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 03:56:54 +00:00
Added loaderon-hooks logic
This commit is contained in:
parent
0b70e285e3
commit
0a66e6635d
23 changed files with 612 additions and 0 deletions
0
pre_commit_hooks/loaderon-hooks/util/__init__.py
Normal file
0
pre_commit_hooks/loaderon-hooks/util/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
class CheckFailedException(Exception):
|
||||
pass
|
||||
58
pre_commit_hooks/loaderon-hooks/util/file_helpers.py
Normal file
58
pre_commit_hooks/loaderon-hooks/util/file_helpers.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
def read_file(file_path, only_first_line=False):
|
||||
opened_file = open(file_path)
|
||||
if only_first_line:
|
||||
lines = opened_file.readline()
|
||||
else:
|
||||
lines = opened_file.readlines()
|
||||
opened_file.close()
|
||||
return lines
|
||||
|
||||
|
||||
def read_file_line(file_path):
|
||||
return read_file(file_path, True)
|
||||
|
||||
|
||||
def read_file_lines(file_path):
|
||||
return read_file(file_path)
|
||||
|
||||
|
||||
def find_file_starting_from_reference_file_directory(reference_file, file_to_find):
|
||||
"""Attempts to find file_to_find by navigating through directories from reference_file parent directory."""
|
||||
file_path = os.path.realpath(reference_file)
|
||||
folder_path = os.path.dirname(file_path)
|
||||
for root, unused_dirs, files in os.walk(folder_path):
|
||||
if file_to_find in files:
|
||||
return os.path.join(root, file_to_find)
|
||||
return None
|
||||
|
||||
|
||||
def get_indexes_of_lines_per_regex(file_lines, regex):
|
||||
regex_pattern = re.compile(regex)
|
||||
indexes_of_lines_defining_regex = []
|
||||
for index, line in enumerate(file_lines):
|
||||
if regex_pattern.match(line):
|
||||
indexes_of_lines_defining_regex.append(index)
|
||||
return indexes_of_lines_defining_regex
|
||||
|
||||
|
||||
def get_bunches_of_lines_dividing_by_regex(file_lines, regex):
|
||||
lines = []
|
||||
indexes_of_lines_per_regex = get_indexes_of_lines_per_regex(file_lines, regex)
|
||||
for index, index_of_lines in enumerate(indexes_of_lines_per_regex):
|
||||
try:
|
||||
next_regex_index = indexes_of_lines_per_regex[index + 1]
|
||||
current_regex_lines = file_lines[index_of_lines:next_regex_index]
|
||||
except IndexError:
|
||||
current_regex_lines = file_lines[index_of_lines:]
|
||||
lines.append(current_regex_lines)
|
||||
return lines
|
||||
|
||||
|
||||
def split_by_regexp(filename, regex):
|
||||
file_lines = read_file_lines(filename)
|
||||
return get_bunches_of_lines_dividing_by_regex(file_lines, regex)
|
||||
9
pre_commit_hooks/loaderon-hooks/util/git_helpers.py
Normal file
9
pre_commit_hooks/loaderon-hooks/util/git_helpers.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import subprocess
|
||||
|
||||
|
||||
def get_current_branch_name():
|
||||
output = subprocess.check_output(['git', 'symbolic-ref', 'HEAD'])
|
||||
output = output.splitlines()
|
||||
head_branches = output[0]
|
||||
return head_branches.strip().split('/')[-1]
|
||||
11
pre_commit_hooks/loaderon-hooks/util/string_helpers.py
Normal file
11
pre_commit_hooks/loaderon-hooks/util/string_helpers.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
|
||||
|
||||
def matches_any_regexp(string, regexp_list):
|
||||
"""Checks if string matches any of regexp in regexp_list."""
|
||||
for regexp in regexp_list:
|
||||
pattern = re.compile(regexp)
|
||||
if pattern.match(string):
|
||||
return True
|
||||
return False
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import argparse
|
||||
from abc import abstractmethod
|
||||
from argparse import ArgumentTypeError
|
||||
|
||||
from pre_commit_logic.util.check_failed_exception import CheckFailedException
|
||||
|
||||
|
||||
class CheckerTemplateMethod(object):
|
||||
def __init__(self, argv):
|
||||
self.parser = argparse.ArgumentParser()
|
||||
self._add_arguments_to_parser()
|
||||
self.args = self.parser.parse_args(argv)
|
||||
self._check_arguments()
|
||||
|
||||
def _add_arguments_to_parser(self):
|
||||
pass
|
||||
|
||||
def _check_arguments(self):
|
||||
pass
|
||||
|
||||
def run(self):
|
||||
"""Prepare args and then check locations."""
|
||||
try:
|
||||
self._perform_checks()
|
||||
return 0
|
||||
except ArgumentTypeError as ex:
|
||||
print ex.message
|
||||
return 1
|
||||
except CheckFailedException as ex:
|
||||
print ex.message
|
||||
return 2
|
||||
|
||||
@abstractmethod
|
||||
def _perform_checks(self):
|
||||
pass
|
||||
|
||||
def inform_check_failure(self, message):
|
||||
raise CheckFailedException(message)
|
||||
|
||||
def check_arguments_size_match(self, arguments_one, arguments_two):
|
||||
if not arguments_one:
|
||||
arguments_one = []
|
||||
if not arguments_two:
|
||||
arguments_two = []
|
||||
if len(arguments_one) != len(arguments_two):
|
||||
self.inform_check_failure('Las listas de argumentos no tienen el mismo largo.')
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from abc import abstractmethod
|
||||
|
||||
from pre_commit_logic.util.template_methods.files_bunches_checker_template_method import \
|
||||
FileBunchesCheckerTemplateMethod
|
||||
from pre_commit_logic.util.template_methods.lines_checker_template_method import LinesCheckerTemplateMethod
|
||||
|
||||
|
||||
class FileBunchesLinesCheckerTemplateMethod(FileBunchesCheckerTemplateMethod, LinesCheckerTemplateMethod):
|
||||
@abstractmethod
|
||||
def _get_regexp(self):
|
||||
pass
|
||||
|
||||
def _check_bunch(self):
|
||||
"""
|
||||
This method uses LinesCheckerTemplateMethod's _check_lines. Which receives self._file_lines. In this case, our
|
||||
'file lines' will be the bunches of lines got by split_by_classes.
|
||||
"""
|
||||
self._file_lines = self._bunch_of_lines
|
||||
self._check_lines()
|
||||
|
||||
@abstractmethod
|
||||
def _check_line(self):
|
||||
pass
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from abc import abstractmethod
|
||||
|
||||
from pre_commit_logic.util.template_methods.checker_template_method import CheckerTemplateMethod
|
||||
|
||||
|
||||
class FileCheckerTemplateMethod(CheckerTemplateMethod):
|
||||
def __init__(self, argv):
|
||||
super(FileCheckerTemplateMethod, self).__init__(argv)
|
||||
self.filename = ''
|
||||
|
||||
def _add_arguments_to_parser(self):
|
||||
self.parser.add_argument('filenames', nargs='*')
|
||||
|
||||
def _perform_checks(self):
|
||||
"""For each file, check it's location."""
|
||||
for self.filename in self.args.filenames:
|
||||
self._check_file()
|
||||
|
||||
@abstractmethod
|
||||
def _check_file(self):
|
||||
pass
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from abc import abstractmethod
|
||||
|
||||
from pre_commit_logic.util.file_helpers import split_by_regexp
|
||||
from pre_commit_logic.util.template_methods.file_checker_template_method import FileCheckerTemplateMethod
|
||||
|
||||
|
||||
class FileBunchesCheckerTemplateMethod(FileCheckerTemplateMethod):
|
||||
def __init__(self, argv):
|
||||
super(FileBunchesCheckerTemplateMethod, self).__init__(argv)
|
||||
self._bunch_of_lines = []
|
||||
|
||||
def _check_file(self):
|
||||
regexp = self._get_regexp()
|
||||
bunches_of_lines = split_by_regexp(self.filename, regexp)
|
||||
for self._bunch_of_lines in bunches_of_lines:
|
||||
self._check_bunch()
|
||||
|
||||
@abstractmethod
|
||||
def _get_regexp(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _check_bunch(self):
|
||||
pass
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from abc import abstractmethod
|
||||
|
||||
from pre_commit_logic.util.template_methods.file_checker_template_method import FileCheckerTemplateMethod
|
||||
from pre_commit_logic.util.file_helpers import read_file_lines
|
||||
|
||||
|
||||
class LinesCheckerTemplateMethod(FileCheckerTemplateMethod):
|
||||
def __init__(self, argv):
|
||||
super(LinesCheckerTemplateMethod, self).__init__(argv)
|
||||
self._file_lines = []
|
||||
self._file_line = ''
|
||||
self._file_line_index = 0
|
||||
|
||||
def _check_file(self):
|
||||
self._file_lines = read_file_lines(self.filename)
|
||||
self._check_lines()
|
||||
|
||||
def _check_lines(self):
|
||||
for self._file_line_index, self._file_line in enumerate(self._file_lines):
|
||||
self._check_line()
|
||||
|
||||
@abstractmethod
|
||||
def _check_line(self):
|
||||
pass
|
||||
Loading…
Add table
Add a link
Reference in a new issue