Skip to content

Commit

Permalink
Merge 87ac8f4 into 72f6243
Browse files Browse the repository at this point in the history
  • Loading branch information
lovato committed Jul 11, 2018
2 parents 72f6243 + 87ac8f4 commit c7ff197
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 82 deletions.
23 changes: 23 additions & 0 deletions .hooks4git.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[scripts]
flake8 = flake8 --max-line-length=120 --exclude .git,build,dist,.env,.venv
nosetests = nosetests --with-coverage

[hooks.pre-commit.scripts]
check.py = flake8

[hooks.pre-push.scripts]
tests.py = nosetests

[hooks.applypatch-msg]

[hooks.commit-msg]

[hooks.post-update]

[hooks.pre-applypatch]

[hooks.prepare-commit-msg]

[hooks.pre-rebase]

[hooks.update]
1 change: 0 additions & 1 deletion .hooks4git.yml

This file was deleted.

2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include LICENSE
include requirements.txt
include hooks4git/build.info
include hooks4git/.hooks4git.yml
include hooks4git/.hooks4git.ini
include LICENSE
include README.md

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![Build Status](https://travis-ci.org/lovato/hooks4git.svg?branch=master)](https://travis-ci.org/lovato/hooks4git)
[![Coverage Status](https://coveralls.io/repos/github/lovato/hooks4git/badge.svg?branch=master)](https://coveralls.io/github/lovato/hooks4git?branch=master)
[![PyPI version](https://badge.fury.io/py/hooks4git.svg)](https://badge.fury.io/py/hooks4git)
<!-- [![Python versions](https://img.shields.io/pypi/pyversions/hooks4git.svg)](https://badge.fury.io/py/hooks4git) -->
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Fully configurable language agnostic git hooks.
Expand Down
2 changes: 1 addition & 1 deletion hooks4git/.codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ engines:
exclude_paths:
- config/engines.yml
exclude_paths:
- tests/**
- '**/tests/**'
28 changes: 28 additions & 0 deletions hooks4git/.hooks4git.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[scripts]
echo = echo Wheeee! Running a hook!
flake8 = flake8 --max-line-length=120 --exclude .git,build,dist,.env,.venv
nosetests = nosetests --with-coverage
eslint = eslint -f checkstyle index.js > checkstyle-result.xml

[hooks.pre-commit.scripts]
msg = echo
# check.py = flake8

[hooks.pre-push.scripts]
msg = echo
# check.py = flake8
# tests.py = nosetests

[hooks.applypatch-msg]

[hooks.commit-msg]

[hooks.post-update]

[hooks.pre-applypatch]

[hooks.prepare-commit-msg]

[hooks.pre-rebase]

[hooks.update]
38 changes: 0 additions & 38 deletions hooks4git/.hooks4git.yml

This file was deleted.

2 changes: 1 addition & 1 deletion hooks4git/git/hooks/applypatch-msg
2 changes: 1 addition & 1 deletion hooks4git/git/hooks/commit-msg
2 changes: 1 addition & 1 deletion hooks4git/git/hooks/post-update
2 changes: 1 addition & 1 deletion hooks4git/git/hooks/pre-applypatch
2 changes: 1 addition & 1 deletion hooks4git/git/hooks/pre-commit
2 changes: 1 addition & 1 deletion hooks4git/git/hooks/pre-push
2 changes: 1 addition & 1 deletion hooks4git/git/hooks/pre-rebase
2 changes: 1 addition & 1 deletion hooks4git/git/hooks/prepare-commit-msg
2 changes: 1 addition & 1 deletion hooks4git/git/hooks/update
159 changes: 141 additions & 18 deletions hooks4git/git/hooks/hooks4git.py → hooks4git/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,131 @@
# -*- coding: utf-8 -*-
import os
import subprocess
import yaml
import sys
from colorama import Fore
from colorama import Back
from colorama import Style
import configparser
import datetime

# *****************************************************************************
# https://github.com/tartley/colorama/blob/83364bf1dc2bd5a53ca9bd0154fe21d769d6f90f/colorama/ansi.py
#
# THIS FILE WAS MODIFIED FROM ORIGINAL for Flake8 and Codacy passing
#
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
#
# This module generates ANSI character codes to printing colors to terminals.
# See: http://en.wikipedia.org/wiki/ANSI_escape_code

CSI = '\033['
OSC = '\033]'
BEL = '\007'


def code_to_chars(code):
return CSI + str(code) + 'm'


def set_title(title):
return OSC + '2;' + title + BEL


def clear_screen(mode=2):
return CSI + str(mode) + 'J'


def clear_line(mode=2):
return CSI + str(mode) + 'K'


class AnsiCodes(object):
def __init__(self):
# the subclasses declare class attributes which are numbers.
# Upon instantiation we define instance attributes, which are the same
# as the class attributes but wrapped with the ANSI escape sequence
for name in dir(self):
if not name.startswith('_'):
value = getattr(self, name)
setattr(self, name, code_to_chars(value))


class AnsiCursor(object):
@classmethod
def UP(cls, n=1):
return CSI + str(n) + 'A'

@classmethod
def DOWN(cls, n=1):
return CSI + str(n) + 'B'

@classmethod
def FORWARD(cls, n=1):
return CSI + str(n) + 'C'

@classmethod
def BACK(cls, n=1):
return CSI + str(n) + 'D'

@classmethod
def POS(cls, x=1, y=1):
return CSI + str(y) + ';' + str(x) + 'H'


class AnsiFore(AnsiCodes):
BLACK = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
WHITE = 37
RESET = 39

# These are fairly well supported, but not part of the standard.
LIGHTBLACK_EX = 90
LIGHTRED_EX = 91
LIGHTGREEN_EX = 92
LIGHTYELLOW_EX = 93
LIGHTBLUE_EX = 94
LIGHTMAGENTA_EX = 95
LIGHTCYAN_EX = 96
LIGHTWHITE_EX = 97


class AnsiBack(AnsiCodes):
BLACK = 40
RED = 41
GREEN = 42
YELLOW = 43
BLUE = 44
MAGENTA = 45
CYAN = 46
WHITE = 47
RESET = 49

# These are fairly well supported, but not part of the standard.
LIGHTBLACK_EX = 100
LIGHTRED_EX = 101
LIGHTGREEN_EX = 102
LIGHTYELLOW_EX = 103
LIGHTBLUE_EX = 104
LIGHTMAGENTA_EX = 105
LIGHTCYAN_EX = 106
LIGHTWHITE_EX = 107


class AnsiStyle(AnsiCodes):
BRIGHT = 1
DIM = 2
NORMAL = 22
RESET_ALL = 0


Fore = AnsiFore()
Back = AnsiBack()
Style = AnsiStyle()
Cursor = AnsiCursor()
# *****************************************************************************

# from hooks4git import __version__
__version__ = 0.1

Expand Down Expand Up @@ -90,46 +208,51 @@ def execute(cmd, files, settings):
# pass
# return files

def ini_as_dict(conf):
d = dict(conf._sections)
for k in d:
d[k] = dict(conf._defaults, **d[k])
d[k].pop('__name__', None)
return d


def main():
cmd = os.path.basename(__file__)
git_root = system('git', 'rev-parse', '--show-toplevel')[1].replace('\n', '')
configfile = "%s/.hooks4git.yml" % git_root
configfile = "%s/.hooks4git.ini" % git_root
config = configparser.ConfigParser()
try:
with open(configfile, 'r') as ymlfile:
cfg = yaml.safe_load(ymlfile)
config.read(configfile)
cfg = ini_as_dict(config)
except Exception as e: # noqa
cfg = {}
cfg = []

global steps_executed
steps_executed = 0
no_fails = True
try:
cfg = cfg.get('hooks', [])
hook = cfg.get(cmd, {'scripts': []})
if not hook:
hook = {'scripts': []}
commands = hook.get('scripts', [])
if not commands:
commands = []
scripts = cfg.get('scripts', {})
hook = cfg.get('hooks.%s.scripts' % cmd, {})
commands = hook.keys()
if len(commands) > 0:
title = "\nhooks4git v%s :: %s :: hook triggered" % (__version__, cmd.title())
title = Fore.YELLOW + Style.BRIGHT + title + Style.RESET_ALL
print(title)
for command in commands:
for command_item in commands:
divider()
steps_executed += 1
files = []
# if cmd == 'pre-commit':
# files = get_changed_files()
command = scripts[hook[command_item]]
result = execute(command.split()[0], files, command.split()[1:])
if result[0] != 0:
no_fails = False
style = Fore.RED + Style.BRIGHT
out('FAIL', "%s'%s' step failed to execute %s" % (style, command.split()[0], Style.RESET_ALL))
out('FAIL', "%s'%s' step failed to execute %s" % (style, command.split()[0], Style.RESET_ALL))
else:
style = Fore.GREEN
out('PASS', "%s'%s' step executed successfully %s" % (style, command.split()[0], Style.RESET_ALL))
out('PASS', "%s'%s' step executed successfully %s" % (style, command.split()[0], Style.RESET_ALL))
return no_fails
except Exception as e: # noqa
out('ERR!', str(e), color=Fore.RED)
Expand Down
Loading

0 comments on commit c7ff197

Please sign in to comment.