Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.egg-info
/build/
/dist/
.*
8 changes: 3 additions & 5 deletions fabfile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from fabpolish import polish, update_sniff
from fabpolish.contrib import (
find_merge_conflict_leftovers,
find_pep8_violations
)
""" This is a testing file. """
from fabpolish import update_sniff
from fabpolish.contrib import find_pep8_violations

update_sniff(find_pep8_violations, severity='major', timing='fast')
52 changes: 32 additions & 20 deletions fabpolish/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""init for package import."""
import os
import sys

Expand All @@ -15,6 +16,7 @@


def info(text):
"""Get information about input."""
puts(green(text))


Expand All @@ -34,14 +36,16 @@ def validate_timing(timing):


def sniff(*args, **kwargs):
""" Decorator to collect sniffs and execute on polish
:param severity: Keyword argument only.
One of 'critical', 'major', 'minor', 'info'
Default: 'critical'
:type severity: str
:param timing: Keyword argument only. One of 'slow', 'fast'
Default: 'fast'
:type timing: str
"""
Decorate to collect sniffs and execute on polish.

:param severity: Keyword argument only.
One of 'critical', 'major', 'minor', 'info'
Default: 'critical'
:type severity: str
:param timing: Keyword argument only. One of 'slow', 'fast'
Default: 'fast'
:type timing: str
"""
DEFAULT_SEVERITY = 'critical'
DEFAULT_TIMING = 'fast'
Expand All @@ -68,7 +72,9 @@ def wrapper(*args, **kwargs):

@task
def polish(env='dev'):
"""Polish code by running some or all sniffs
"""
Polish code by running some or all sniffs.

:param env: Environment to determine what all sniffs to run
Options: 'dev', 'ci'
Default: 'dev'
Expand All @@ -78,23 +84,27 @@ def polish(env='dev'):
When environment is 'dev', only fast-critical and fast-major
sniffs are run.
"""
def check(sniff):
"""
Check function name in frabic_tasks and not fast also note
severity critical or major.
"""
return sniff['function'].name not in fabric_tasks or \
sniff['timing'] != 'fast' or \
sniff['severity'] not in ('critical', 'major')

fabric_tasks = list_commands('', 'short')
results = list()
with settings(warn_only=True):
sniffs_to_run = []
if env == 'ci':
sniffs_to_run = []
for sniff in _sniffs:
if sniff['function'].name not in fabric_tasks:
continue
for sniff in filter(
lambda each: each['function'].name in fabric_tasks, _sniffs
):
sniffs_to_run.append(sniff)
elif env == 'dev':
sniffs_to_run = []
for sniff in _sniffs:
if sniff['function'].name not in fabric_tasks:
continue
if sniff['timing'] != 'fast':
continue
if sniff['severity'] not in ('critical', 'major'):
if check(sniff):
continue
sniffs_to_run.append(sniff)
else:
Expand All @@ -107,10 +117,12 @@ def polish(env='dev'):


def update_sniff(function, severity=None, timing=None):
if type(function) == str:
"""Update sniff dict."""
if isinstance(function, str):
function_name = function
else:
function_name = function.name
sniff = {}
for sniff in _sniffs:
if sniff['function'].name == function_name:
break
Expand Down
47 changes: 26 additions & 21 deletions fabpolish/contrib.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,77 @@
"""checks on various features."""
from fabpolish import sniff, info, local


@sniff(severity='critical', timing='fast')
def find_merge_conflict_leftovers():
"""Find Merge conflict leftovers
"""
"""Find Merge conflict leftovers."""
info('Finding merge conflict leftovers...')
return local("! git grep -P '^(<|=|>){7}(?![<=>])'")


@sniff(severity='major', timing='slow')
def find_php_syntax_errors():
"""Find syntax error in php files
"""
"""Find syntax error in php files."""
info('Finding syntax error in php files...')
return local(
"git ls-files -z | "
"grep -PZz '\.(php|phtml)$' | "
"grep -PZz '\\.(php|phtml)$' | "
"xargs -0 -n 1 php -l >/tmp/debug"
)


@sniff(severity='major', timing='fast')
def python_code_analyzer():
"""Running static code analyzer"""
"""Run static code analyzer."""
info('Running static code analyzer')
return local(
"git ls-files -z | "
"grep -PZz '\.py$' | "
"grep -PZz '\\.py$' | "
"grep -PZvz 'fabfile.py' | "
"xargs -0 pyflakes"
)


@sniff(severity='minor', timing='slow')
def find_pep8_violations():
"""Run pep8 python coding standard check
"""
"""Run pep8 python coding standard check."""
info('Running coding standards check for python files...')
return local(
"git ls-files -z | "
"grep -PZz '\.py$' | "
"grep -PZz '\\.py$' | "
"xargs -0 pep8"
)


@sniff(severity='major', timing='fast')
def fix_file_permission():
"""Fixing permissions for files"""
"""Fix permissions for files."""
info('Fixing permissions for files')
return local(
"git ls-files -z | "
"grep -PvZz '\.sh$' | "
"grep -PvZz '\\.sh$' | "
"xargs -0 chmod -c 0664 > /dev/null 2>&1"
)


@sniff(severity='major', timing='fast')
def fix_script_permission():
# Fix script permissions
"""Fix script permissions."""
info('Fixing script permissions...')
return local(
"git ls-files -z | "
"grep -PZz '\.sh$' | "
"grep -PZz '\\.sh$' | "
"xargs -0 -r chmod 0775 >/dev/null 2>&1"
)


@sniff(severity='major', timing='fast')
def fix_white_space():
"""Fix White spaces."""
info('Fixing whitespace errors...')
return local(
"git ls-files -z | "
"grep -PZvz '\.(ico|jpg|png|gif|eot|ttf|woff|wav|xlxs)$' | "
"grep -PZvz '\\.(ico|jpg|png|gif|eot|ttf|woff|wav|xlxs)$' | "
"xargs -0 grep -PlZn '(\\s+$)|(\\t)' | "
"tee /dev/stderr | "
"xargs -0 -r sed -i -e 's/\\s\\+$//' "
Expand All @@ -81,10 +80,11 @@ def fix_white_space():

@sniff(severity='major', timing='fast')
def convert_tab_spaces():
"""Convert all tabe to spaces."""
info('Converting tab to spaces...')
return local(
"git ls-files -z | "
"grep -PZvz '\.(ico|jpg|png|gif|eot|ttf|woff|wav|xlxs)$' | "
"grep -PZvz '\\.(ico|jpg|png|gif|eot|ttf|woff|wav|xlxs)$' | "
"xargs -0 grep -PlZn '(\\s+$)|(\\t)' | "
"tee /dev/stderr | "
"xargs -0 -r sed -i -e 's/\\t/ /g' "
Expand All @@ -93,26 +93,27 @@ def convert_tab_spaces():

@sniff(severity='critical', timing='fast')
def check_migration_branch():
"""Checking migration branches"""
"""Check migration branches."""
info('Checking migration branches...')
return local("! alembic branches | grep branchpoint")


@sniff(severity='major', timing='fast')
def check_python_debug_info():
"""Check and remove debugging print statements"""
"""Check and remove debugging print statements."""
info('Checking for debug print statements')
return local(
"! git ls-files -z | "
"grep -PZvz 'fabfile.py' | "
"grep -PZz \.py$ | "
"grep -PZz \\.py$ | "
"xargs -0 grep -Pn \'(?<![Bb]lue|>>> )print\' | "
"grep -v NOCHECK"
)


@sniff(severity='major', timing='fast')
def check_php_debug_info():
"""Check for var echo or dump in php scripts."""
info('Checking for var_dump, echo or die statements...')
return local(
"! find ./src -name '*.php' -print0 | "
Expand All @@ -122,6 +123,7 @@ def check_php_debug_info():

@sniff(severity='major', timing='fast')
def check_image_edited():
"""Avoide Browser cashing during image update."""
# Check if image files have been edited
info('Checking if image files have been edited...')
info('Explanation: A new image should be created when '
Expand All @@ -130,28 +132,31 @@ def check_image_edited():
return local(
'! git diff master...' + branch +
' --name-only --diff-filter=M | ' +
'grep ".gif\|.png\|.jpg"'
'grep ".gif\\|.png\\|.jpg"'
)


@sniff(severity='critical', timing='fast')
def composer_validate():
"""Run composer validate."""
info('Running composer validate...')
return local('composer validate')


@sniff(severity='major', timing='fast')
def run_eslint():
"""Run ESLint."""
info('Running ESLint...')
return local(
"git ls-files | "
"grep '\.js$' | "
"grep '\\.js$' | "
"xargs ./node_modules/eslint/bin/eslint.js"
)


@sniff(severity='major', timing='fast')
def check_preg_replace():
"""Use of preg_replace checks."""
info('Checking use of preg_replace...')
return local(
"! find src -name '*.php' -print0 | "
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This is setup.py. Ment for installation of package."""
from setuptools import setup

setup(name='fab-polish',
Expand Down