Skip to content

Commit

Permalink
[WIP] pre-commit and ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
swrichards committed Jan 6, 2025
1 parent 235d0f0 commit 3ea3ad2
Show file tree
Hide file tree
Showing 751 changed files with 834 additions and 1,434 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Pre-commit Check

on: pull_request

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Install pre-commit
run: pip install pre-commit

- name: Get changed files
id: changed-files
run: |
echo "files=$(git diff --name-only origin/main...HEAD | tr '\n' ' ')" >> $GITHUB_OUTPUT
- name: Run pre-commit
continue-on-error: true
run: |
pre-commit run --files ${{ steps.changed-files.outputs.files }}
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.8.4
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
2 changes: 1 addition & 1 deletion bin/check_celery_worker_liveness.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@
sys.exit(1)

print("Celery worker heartbeat found: OK.")
sys.exit(0)
sys.exit(0)
166 changes: 97 additions & 69 deletions bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,36 @@
Set up my development environment for me!
"""

project_name = 'open_inwoner'
project_name = "open_inwoner"

parser = argparse.ArgumentParser(description=description)
parser.add_argument('target', choices=['production', 'staging', 'test', 'jenkins', 'dev'],
help='production/staging/test/jenkins/dev')
parser.add_argument('--project', default=project_name,
help='Name of the project in your src directory, "%s" by default' % project_name)
parser.add_argument('--init', action='store_true',
help='Initialize a fresh "startproject" by pinning the requirements using pip-tools compile. '
'Automatically done if requirements/base.txt does not yet exist.')
parser.add_argument('--env', default='env',
help='Directory name for virtualenv, "env" by default')
parser.add_argument(
"target",
choices=["production", "staging", "test", "jenkins", "dev"],
help="production/staging/test/jenkins/dev",
)
parser.add_argument(
"--project",
default=project_name,
help='Name of the project in your src directory, "%s" by default' % project_name,
)
parser.add_argument(
"--init",
action="store_true",
help='Initialize a fresh "startproject" by pinning the requirements using pip-tools compile. '
"Automatically done if requirements/base.txt does not yet exist.",
)
parser.add_argument(
"--env", default="env", help='Directory name for virtualenv, "env" by default'
)

args = parser.parse_args()


def replace_or_append(file_path, search_val, replace_val):
file_handle, abs_path = mkstemp()
new_file = open(abs_path, 'w')
old_file = open(file_path, 'r')
new_file = open(abs_path, "w")
old_file = open(file_path)
found = False
for line in old_file:
if line.startswith(search_val):
Expand All @@ -51,89 +61,107 @@ def replace_or_append(file_path, search_val, replace_val):


def replace_wsgi_settings(target):
path = os.path.join('src', project_name, 'wsgi.py')
path = os.path.join("src", project_name, "wsgi.py")
replace_or_append(
path, 'os.environ.setdefault',
'os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n' % (project_name, target)
path,
"os.environ.setdefault",
'os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n'
% (project_name, target),
)


def replace_manage_settings(target):
path = os.path.join('src', project_name, 'manage.py')
path = os.path.join("src", project_name, "manage.py")
replace_or_append(
path, ' os.environ.setdefault',
' os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n' % (project_name, target)
path,
" os.environ.setdefault",
' os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n'
% (project_name, target),
)


def append_settings_activate(project, target, env):
if os.name == 'posix':
path = '%s/bin/activate' % env
replace_or_append(path, 'export DJANGO_SETTINGS_MODULE=',
'export DJANGO_SETTINGS_MODULE=\'%s.conf.%s\'\n' %
(project, target))
elif os.name == 'nt':
path = '%s\\Scripts\\activate.bat' % env
replace_or_append(path, 'set DJANGO_SETTINGS_MODULE=',
'set DJANGO_SETTINGS_MODULE=%s.conf.%s\n' %
(project, target))
path = '%s\\Scripts\\deactivate.bat' % env
replace_or_append(path, 'set DJANGO_SETTINGS_MODULE=',
'set DJANGO_SETTINGS_MODULE=\n')
if os.name == "posix":
path = "%s/bin/activate" % env
replace_or_append(
path,
"export DJANGO_SETTINGS_MODULE=",
"export DJANGO_SETTINGS_MODULE='%s.conf.%s'\n" % (project, target),
)
elif os.name == "nt":
path = "%s\\Scripts\\activate.bat" % env
replace_or_append(
path,
"set DJANGO_SETTINGS_MODULE=",
"set DJANGO_SETTINGS_MODULE=%s.conf.%s\n" % (project, target),
)
path = "%s\\Scripts\\deactivate.bat" % env
replace_or_append(
path, "set DJANGO_SETTINGS_MODULE=", "set DJANGO_SETTINGS_MODULE=\n"
)


def pip_compile_pin_requirements(virtualenv):
print('\n== Compiling base requirements ==\n')
if os.name == 'posix':
pip_path = os.path.join(virtualenv, 'bin', 'pip')
elif os.name == 'nt':
pip_path = os.path.join(virtualenv, 'Scripts', 'pip')
cmd_tpl = '{pip} install pip-tools'.format(pip=pip_path)
print("\n== Compiling base requirements ==\n")
if os.name == "posix":
pip_path = os.path.join(virtualenv, "bin", "pip")
elif os.name == "nt":
pip_path = os.path.join(virtualenv, "Scripts", "pip")
cmd_tpl = f"{pip_path} install pip-tools"
call(cmd_tpl, shell=True)
print('Error: Run `. env/bin/activate && ./bin/compile_dependencies.sh` to ensure you have requirements/base.txt and requirements/dev.txt')
print('After that rerun bootstrap.py')
print(
"Error: Run `. env/bin/activate && ./bin/compile_dependencies.sh` to ensure you have requirements/base.txt and requirements/dev.txt"
)
print("After that rerun bootstrap.py")
sys.exit(1)


def main():
virtualenv = args.env
if not hasattr(sys, 'real_prefix'):
print('\n== Creating virtual environment ==\n')
call('virtualenv {0} --python=python3 --prompt="({1}-{2}) "'.format(
virtualenv, args.project, args.target
), shell=True)
print('\n== Set "%s.conf.%s" as default settings ==\n' % (args.project, args.target))
if not hasattr(sys, "real_prefix"):
print("\n== Creating virtual environment ==\n")
call(
f'virtualenv {virtualenv} --python=python3 --prompt="({args.project}-{args.target}) "',
shell=True,
)
print(
'\n== Set "%s.conf.%s" as default settings ==\n' % (args.project, args.target)
)
append_settings_activate(args.project, args.target, args.env)

if os.name == 'posix':
if os.name == "posix":
# Make manage.py executable
st = os.stat('src/manage.py')
os.chmod('src/manage.py', st.st_mode | stat.S_IEXEC)
django_admin_symlink = os.path.join(virtualenv, 'bin', 'django')
st = os.stat("src/manage.py")
os.chmod("src/manage.py", st.st_mode | stat.S_IEXEC)
django_admin_symlink = os.path.join(virtualenv, "bin", "django")
if not os.path.exists(django_admin_symlink):
os.symlink('../../src/manage.py', django_admin_symlink)

print('\n== Upgrading Pip ==\n')
if os.name == 'posix':
pip_path = os.path.join(virtualenv, 'bin', 'pip')
elif os.name == 'nt':
pip_path = os.path.join(virtualenv, 'Scripts', 'pip')
cmd_tpl = '{pip} install --upgrade pip'.format(pip=pip_path)
os.symlink("../../src/manage.py", django_admin_symlink)

print("\n== Upgrading Pip ==\n")
if os.name == "posix":
pip_path = os.path.join(virtualenv, "bin", "pip")
elif os.name == "nt":
pip_path = os.path.join(virtualenv, "Scripts", "pip")
cmd_tpl = f"{pip_path} install --upgrade pip"
call(cmd_tpl, shell=True)

if args.init \
or not os.path.exists(os.path.join('requirements', 'base.txt')) \
or not os.path.exists(os.path.join('requirements', 'dev.txt')):
if (
args.init
or not os.path.exists(os.path.join("requirements", "base.txt"))
or not os.path.exists(os.path.join("requirements", "dev.txt"))
):
pip_compile_pin_requirements(virtualenv)

print('\n== Installing %s requirements ==\n' % args.target)
if os.name == 'posix':
os.environ['TMPDIR'] = '/var/tmp/'
pip_path = os.path.join(virtualenv, 'bin', 'pip')
cmd_tpl = '{pip} install -r requirements/{target}.txt'
elif os.name == 'nt':
pip_path = os.path.join(virtualenv, 'Scripts', 'pip')
cmd_tpl = '{pip} install -r requirements\\{target}.txt'
print("\n== Installing %s requirements ==\n" % args.target)
if os.name == "posix":
os.environ["TMPDIR"] = "/var/tmp/"
pip_path = os.path.join(virtualenv, "bin", "pip")
cmd_tpl = "{pip} install -r requirements/{target}.txt"
elif os.name == "nt":
pip_path = os.path.join(virtualenv, "Scripts", "pip")
cmd_tpl = "{pip} install -r requirements\\{target}.txt"
return call(cmd_tpl.format(pip=pip_path, target=args.target), shell=True)

if __name__ == '__main__':

if __name__ == "__main__":
sys.exit(main())
88 changes: 88 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pyenv",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"site-packages",
"venv",
]

# Same as Black.
line-length = 88
indent-width = 4

# Assume Python 3.11
target-version = "py311"

[lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
# McCabe complexity (`C901`) by default.
select = [
# https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
"B",
"E",
"F",
"S",
"I",
"BLE",
"C90",
"S106",
"UP",
]
ignore = ["E501"]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

# Enable auto-formatting of code examples in docstrings. Markdown,
# reStructuredText code/literal blocks and doctests are all supported.
#
# This is currently disabled by default, but it is planned for this
# to be opt-out in the future.
docstring-code-format = false

# Set the line length limit used when formatting code snippets in
# docstrings.
#
# This only has an effect when the `docstring-code-format` setting is
# enabled.
docstring-code-line-length = "dynamic"
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def delete_old_celery_tasks(apps, _):


class Migration(migrations.Migration):

dependencies = [("django_celery_beat", "0018_improve_crontab_helptext")]

operations = [
Expand Down
3 changes: 1 addition & 2 deletions src/eherkenning/backends.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from django.contrib.auth import get_user_model

from digid_eherkenning.backends import eHerkenningBackend as _eHerkenningBackend
from digid_eherkenning.exceptions import eHerkenningError
from digid_eherkenning.utils import get_client_ip
from django.contrib.auth import get_user_model

from open_inwoner.kvk.branches import KVK_BRANCH_SESSION_VARIABLE

Expand Down
5 changes: 2 additions & 3 deletions src/eherkenning/mock/backends.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import logging
import re

from django.contrib.auth import get_user_model

from digid_eherkenning.backends import BaseBackend
from digid_eherkenning.utils import get_client_ip
from django.contrib.auth import get_user_model

logger = logging.getLogger(__name__)

Expand All @@ -19,7 +18,7 @@ class eHerkenningBackend(BaseBackend):
"eherkenning_no_kvk": "Login failed due to no KvK being returned by eHerkenning.",
"eherkenning_len_kvk": "Login failed due to no KvK having more then 8 digits.",
"eherkenning_num_kvk": "Login failed due to no KvK not being numerical.",
}
},
)

def get_or_create_user(self, request, kvk):
Expand Down
Loading

0 comments on commit 3ea3ad2

Please sign in to comment.