Skip to content
Snippets Groups Projects
Commit d978c23f authored by Remi  PLANEL's avatar Remi PLANEL
Browse files

Add pre-commit that use black and flake8

parent 1aeb5752
No related branches found
No related tags found
1 merge request!2PyPI publish package
Pipeline #21090 passed
...@@ -2,4 +2,5 @@ ...@@ -2,4 +2,5 @@
*.code-workspace *.code-workspace
*.pyc *.pyc
dist dist
*.egg-info *.egg-info
\ No newline at end of file __pycache__
#!/usr/bin/env python
"""File generated by pre-commit: https://pre-commit.com"""
from __future__ import print_function
import distutils.spawn
import os
import subprocess
import sys
# work around https://github.com/Homebrew/homebrew-core/issues/30445
os.environ.pop('__PYVENV_LAUNCHER__', None)
HERE = os.path.dirname(os.path.abspath(__file__))
Z40 = '0' * 40
ID_HASH = '138fd403232d2ddd5efb44317e38bf03'
# start templated
CONFIG = '.pre-commit-config.yaml'
HOOK_TYPE = 'pre-commit'
INSTALL_PYTHON = '/home/rplanel/.virtualenvs/crispr-bact/bin/python3'
SKIP_ON_MISSING_CONFIG = False
# end templated
class EarlyExit(RuntimeError):
pass
class FatalError(RuntimeError):
pass
def _norm_exe(exe):
"""Necessary for shebang support on windows.
roughly lifted from `identify.identify.parse_shebang`
"""
with open(exe, 'rb') as f:
if f.read(2) != b'#!':
return ()
try:
first_line = f.readline().decode('UTF-8')
except UnicodeDecodeError:
return ()
cmd = first_line.split()
if cmd[0] == '/usr/bin/env':
del cmd[0]
return tuple(cmd)
def _run_legacy():
if __file__.endswith('.legacy'):
raise SystemExit(
"bug: pre-commit's script is installed in migration mode\n"
'run `pre-commit install -f --hook-type {}` to fix this\n\n'
'Please report this bug at '
'https://github.com/pre-commit/pre-commit/issues'.format(
HOOK_TYPE,
),
)
if HOOK_TYPE == 'pre-push':
stdin = getattr(sys.stdin, 'buffer', sys.stdin).read()
else:
stdin = None
legacy_hook = os.path.join(HERE, '{}.legacy'.format(HOOK_TYPE))
if os.access(legacy_hook, os.X_OK):
cmd = _norm_exe(legacy_hook) + (legacy_hook,) + tuple(sys.argv[1:])
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE if stdin else None)
proc.communicate(stdin)
return proc.returncode, stdin
else:
return 0, stdin
def _validate_config():
cmd = ('git', 'rev-parse', '--show-toplevel')
top_level = subprocess.check_output(cmd).decode('UTF-8').strip()
cfg = os.path.join(top_level, CONFIG)
if os.path.isfile(cfg):
pass
elif SKIP_ON_MISSING_CONFIG or os.getenv('PRE_COMMIT_ALLOW_NO_CONFIG'):
print(
'`{}` config file not found. '
'Skipping `pre-commit`.'.format(CONFIG),
)
raise EarlyExit()
else:
raise FatalError(
'No {} file was found\n'
'- To temporarily silence this, run '
'`PRE_COMMIT_ALLOW_NO_CONFIG=1 git ...`\n'
'- To permanently silence this, install pre-commit with the '
'--allow-missing-config option\n'
'- To uninstall pre-commit run '
'`pre-commit uninstall`'.format(CONFIG),
)
def _exe():
with open(os.devnull, 'wb') as devnull:
for exe in (INSTALL_PYTHON, sys.executable):
try:
if not subprocess.call(
(exe, '-c', 'import pre_commit.main'),
stdout=devnull, stderr=devnull,
):
return (exe, '-m', 'pre_commit.main', 'run')
except OSError:
pass
if distutils.spawn.find_executable('pre-commit'):
return ('pre-commit', 'run')
raise FatalError(
'`pre-commit` not found. Did you forget to activate your virtualenv?',
)
def _rev_exists(rev):
return not subprocess.call(('git', 'rev-list', '--quiet', rev))
def _pre_push(stdin):
remote = sys.argv[1]
opts = ()
for line in stdin.decode('UTF-8').splitlines():
_, local_sha, _, remote_sha = line.split()
if local_sha == Z40:
continue
elif remote_sha != Z40 and _rev_exists(remote_sha):
opts = ('--origin', local_sha, '--source', remote_sha)
else:
# ancestors not found in remote
ancestors = subprocess.check_output((
'git', 'rev-list', local_sha, '--topo-order', '--reverse',
'--not', '--remotes={}'.format(remote),
)).decode().strip()
if not ancestors:
continue
else:
first_ancestor = ancestors.splitlines()[0]
cmd = ('git', 'rev-list', '--max-parents=0', local_sha)
roots = set(subprocess.check_output(cmd).decode().splitlines())
if first_ancestor in roots:
# pushing the whole tree including root commit
opts = ('--all-files',)
else:
cmd = ('git', 'rev-parse', '{}^'.format(first_ancestor))
source = subprocess.check_output(cmd).decode().strip()
opts = ('--origin', local_sha, '--source', source)
if opts:
return opts
else:
# An attempt to push an empty changeset
raise EarlyExit()
def _opts(stdin):
fns = {
'prepare-commit-msg': lambda _: ('--commit-msg-filename', sys.argv[1]),
'commit-msg': lambda _: ('--commit-msg-filename', sys.argv[1]),
'pre-commit': lambda _: (),
'pre-push': _pre_push,
}
stage = HOOK_TYPE.replace('pre-', '')
return ('--config', CONFIG, '--hook-stage', stage) + fns[HOOK_TYPE](stdin)
if sys.version_info < (3, 7): # https://bugs.python.org/issue25942
def _subprocess_call(cmd): # this is the python 2.7 implementation
return subprocess.Popen(cmd).wait()
else:
_subprocess_call = subprocess.call
def main():
retv, stdin = _run_legacy()
try:
_validate_config()
return retv | _subprocess_call(_exe() + _opts(stdin))
except EarlyExit:
return retv
except FatalError as e:
print(e.args[0])
return 1
except KeyboardInterrupt:
return 1
if __name__ == '__main__':
exit(main())
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
exclude: .hooks
language_version: python3.7
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: flake8
args: [--max-line-length=89]
from .predict import on_target_predict from .predict import on_target_predict
\ No newline at end of file
__all__ = ["on_target_predict"]
This diff is collapsed.
...@@ -25,8 +25,9 @@ biopython = "^1.75" ...@@ -25,8 +25,9 @@ biopython = "^1.75"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
pytest = "^5.2" pytest = "^5.2"
black = {version = "^18.3-alpha.0", allows-prereleases = true}
flake8 = "^3.7" flake8 = "^3.7"
pre-commit = "^1.20.0"
black = "^19.10b0"
[tool.poetry.scripts] [tool.poetry.scripts]
crisprbact= "crisprbact.cli:main" crisprbact= "crisprbact.cli:main"
...@@ -35,3 +36,30 @@ crisprbact= "crisprbact.cli:main" ...@@ -35,3 +36,30 @@ crisprbact= "crisprbact.cli:main"
requires = ["poetry>=0.12"] requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api" build-backend = "poetry.masonry.api"
[tool.black]
target-version = ['py37']
include = '\.pyi?$'
exclude = '''
(
/(
\.eggs # exclude a few common directories in the
| \.git # root of the project
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| \.hooks
| _build
| buck-out
| build
| dist
)/
| foo.py # also separately exclude a file named foo.py in
# the root of the project
)
'''
[flake8]
max-line-length = 89
max-complexity = 18
import pytest
import crisprbact import crisprbact
...@@ -11,7 +10,12 @@ def test_on_target_predict_empty(): ...@@ -11,7 +10,12 @@ def test_on_target_predict_empty():
def test_on_target_predict_size_guide(): def test_on_target_predict_size_guide():
size_guide = 20 size_guide = 20
predicted_targets = crisprbact.on_target_predict( predicted_targets = crisprbact.on_target_predict(
"TGCCTGTTTACGCGCCGATTGTTGCGAGATTTGGACGGACGTTGACGGGGTCTATACCTGCGACCCGCGTCAGGTGCCCGATGCGAGGTTGTTGAAGTCGATGTCCTACCAGGAAGCGATGGAGCTTTCCTACTTCGGCG" """ TGCCTGTTTACGCGCCGATTGTTGCG
AGATTTGGACGGACGTTGACGGGG
TCTATACCTGCGACCCGCGTCAGG
TGCCCGATGCGAGGTTGTTGAAGT
CGATGTCCTACCAGGAAGCGATGG
AGCTTTCCTACTTCGGCG"""
) )
guides = (predicted_target["guide"] for predicted_target in predicted_targets) guides = (predicted_target["guide"] for predicted_target in predicted_targets)
for guide in guides: for guide in guides:
...@@ -27,4 +31,3 @@ def test_on_target_predict_size_guide(): ...@@ -27,4 +31,3 @@ def test_on_target_predict_size_guide():
assert ( assert (
start_val - pam_val == 2 start_val - pam_val == 2
), "the difference between start and pam position is different than 2" ), "the difference between start and pam position is different than 2"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment