Skip to content

Commit

Permalink
Merge pull request #6885 from freedomofpress/ruff
Browse files Browse the repository at this point in the history
Use ruff to replace flake8, pylint, isort and add more checks
  • Loading branch information
rocodes authored Aug 2, 2023
2 parents f699393 + 3fbd586 commit 81a3c49
Show file tree
Hide file tree
Showing 108 changed files with 774 additions and 857 deletions.
13 changes: 0 additions & 13 deletions .flake8

This file was deleted.

4 changes: 2 additions & 2 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ if [[ -n "$PY_FILES" ]]; then
echo "$PY_FILES"
# Run black against changed python files for this commit
black --check --diff ${PY_FILES//$'\n'/ }
# Run flake8 against changed python files for this commit
flake8 ${PY_FILES//$'\n'/ } && echo "flake8 passed!"
# Run ruff (against all files, it's fast enough)
ruff check . --diff && echo "ruff passed!"
else
exit 0
fi
Expand Down
26 changes: 10 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ update-python3-requirements: ## Update Python 3 requirements with pip-compile.
--allow-unsafe \
--output-file requirements/python3/build-requirements.txt \
requirements/python3/build-requirements.in
@$(DEVSHELL) pip-compile --generate-hashes \
@SLIM_BUILD=1 $(DEVSHELL) pip-compile --generate-hashes \
--allow-unsafe \
--output-file requirements/python3/develop-requirements.txt \
../admin/requirements-ansible.in \
Expand Down Expand Up @@ -76,16 +76,6 @@ check-black: ## Check Python source code formatting with black
black: ## Update Python source code formatting with black
@black securedrop .

.PHONY: check-isort
check-isort: ## Check Python import organization with isort
@echo "███ Running isort check..."
@isort --check-only --diff .
@echo

.PHONY: isort
isort: ## Update Python import organization with isort
@isort .

.PHONY: ansible-config-lint
ansible-config-lint: ## Run custom Ansible linting tasks.
@echo "███ Linting Ansible configuration..."
Expand All @@ -108,12 +98,16 @@ app-lint-full: ## Test pylint compliance, with no checks disabled.
@cd securedrop && find . -name '*.py' -or -path './scripts/*' | xargs pylint
@echo

.PHONY: flake8
flake8: ## Validate PEP8 compliance for Python source files.
@echo "███ Running flake8..."
@flake8
.PHONY: check-ruff
check-ruff: ## Lint Python source files.
@echo "███ Running ruff..."
@ruff check . --show-source
@echo

.PHONY: ruff
ruff: ## Update Python source file formatting.
@ruff check . --fix

# The --disable=names is required to use the BEM syntax
# # https://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/
.PHONY: html-lint
Expand Down Expand Up @@ -148,7 +142,7 @@ yamllint: ## Lint YAML files (does not validate syntax!).
@echo

.PHONY: lint
lint: ansible-config-lint app-lint check-black check-isort flake8 html-lint shellcheck typelint yamllint ## Runs all lint checks
lint: ansible-config-lint check-ruff app-lint check-black html-lint shellcheck typelint yamllint ## Runs all lint checks

.PHONY: safety
safety: ## Run `safety check` to check python dependencies for vulnerabilities.
Expand Down
1 change: 0 additions & 1 deletion admin/requirements-dev.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
coverage>=5.0 # #6091
d2to1
flake8>=5.0.4
flaky
mock
pbr
Expand Down
16 changes: 1 addition & 15 deletions admin/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ dill==0.3.5.1 \
--hash=sha256:33501d03270bbe410c72639b350e941882a8b0fd55357580fbc873fba0c59302 \
--hash=sha256:d75e41f3eff1eee599d738e76ba8f4ad98ea229db8b085318aa2b3333a208c86
# via pylint
flake8==5.0.4 \
--hash=sha256:6fbe320aad8d6b95cec8b8e47bc933004678dc63095be98528b7bdd2a9f510db \
--hash=sha256:7a1cf6b73744f5806ab95e526f6f0d8c01c66d7bbe349562d22dfca20610b248
# via -r requirements-dev.in
flaky==3.4.0 \
--hash=sha256:4ad7880aef8c35a34ddb394d4fa33047765bca1e3d67d182bf6eba9c8eabf3a2 \
--hash=sha256:d0533f473a46b916e6db6e84e20b06d8a70656600a0c14e819b0760b63f70226
Expand Down Expand Up @@ -123,9 +119,7 @@ lazy-object-proxy==1.4.3 \
mccabe==0.7.0 \
--hash=sha256:348e0240c33b60bbdf4e523192ef919f28cb2c3d7d5c7794f74009290f236325 \
--hash=sha256:6c2d30ab6be0e4a46919781807b4f0d834ebdd6c6e3dca0bda5a15f863427b6e
# via
# flake8
# pylint
# via pylint
mock==2.0.0 \
--hash=sha256:5ce3c71c5545b472da17b72268978914d0252980348636840bd34a00b5cc96c1 \
--hash=sha256:b158b6df76edd239b8208d481dc46b6afd45a846b7812ff0ce58971cf5bc8bba
Expand Down Expand Up @@ -169,14 +163,6 @@ py==1.10.0 \
# pytest
# pytest-catchlog
# tox
pycodestyle==2.9.1 \
--hash=sha256:2c9607871d58c76354b697b42f5d57e1ada7d261c261efac224b664affdc5785 \
--hash=sha256:d1735fc58b418fd7c5f658d28d943854f8a849b01a5d0a1e6f3f3fdd0166804b
# via flake8
pyflakes==2.5.0 \
--hash=sha256:4579f67d887f804e67edb544428f264b7b24f435b263c4614f384135cea553d2 \
--hash=sha256:491feb020dca48ccc562a8c0cbe8df07ee13078df59813b83959cbdada312ea3
# via flake8
pylint==2.15.4 \
--hash=sha256:5441e9294335d354b7bad57c1044e5bd7cce25c433475d76b440e53452fa5cb8 \
--hash=sha256:629cf1dbdfb6609d7e7a45815a8bb59300e34aa35783b5ac563acaca2c4022e9
Expand Down
24 changes: 10 additions & 14 deletions admin/securedrop_admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ def validate(self, document: Document) -> bool:
class ValidateOSSECUsername(Validator):
def validate(self, document: Document) -> bool:
text = document.text
if text and "@" not in text and "test" != text:
if text and "@" not in text and text != "test":
return True
raise ValidationError(message="The SASL username should not include the domain name")

class ValidateOSSECPassword(Validator):
def validate(self, document: Document) -> bool:
text = document.text
if len(text) >= 8 and "password123" != text:
if len(text) >= 8 and text != "password123":
return True
raise ValidationError(message="Password for OSSEC email account must be strong")

Expand All @@ -252,7 +252,7 @@ class ValidateOSSECEmail(ValidateEmail):
def validate(self, document: Document) -> bool:
super().validate(document)
text = document.text
if "[email protected]" != text:
if text != "[email protected]":
return True
raise ValidationError(
message=("Must be set to something other than " "[email protected]")
Expand All @@ -266,10 +266,10 @@ def validate(self, document: Document) -> bool:

def __init__(self, args: argparse.Namespace) -> None:
self.args = args
self.config = {} # type: Dict
self.config: dict = {}
# Hold runtime configuration before save, to support
# referencing other responses during validation
self._config_in_progress = {} # type: Dict
self._config_in_progress: dict = {}

supported_locales = I18N_DEFAULT_LOCALES.copy()
i18n_conf_path = os.path.join(args.root, I18N_CONF)
Expand All @@ -279,7 +279,7 @@ def __init__(self, args: argparse.Namespace) -> None:
supported_locales.update(set(i18n_conf["supported_locales"].keys()))
locale_validator = SiteConfig.ValidateLocales(self.args.app_path, supported_locales)

self.desc = [
self.desc: List[_DescEntryType] = [
(
"ssh_users",
"sd",
Expand Down Expand Up @@ -516,7 +516,7 @@ def __init__(self, args: argparse.Namespace) -> None:
str.split,
lambda config: True,
),
] # type: List[_DescEntryType]
]

def load_and_update_config(self, validate: bool = True, prompt: bool = True) -> bool:
if self.exists():
Expand All @@ -542,9 +542,7 @@ def user_prompt_config(self) -> Dict[str, Any]:
if not condition(self._config_in_progress):
self._config_in_progress[var] = ""
continue
self._config_in_progress[var] = self.user_prompt_config_one(
desc, self.config.get(var)
) # noqa: E501
self._config_in_progress[var] = self.user_prompt_config_one(desc, self.config.get(var))
return self._config_in_progress

def user_prompt_config_one(self, desc: _DescEntryType, from_config: Optional[Any]) -> Any:
Expand Down Expand Up @@ -932,7 +930,7 @@ def check_for_updates(args: argparse.Namespace) -> Tuple[bool, str]:
# relied on to determine if we're on the latest tag or not.
current_tag = (
subprocess.check_output(["git", "describe"], cwd=args.root).decode("utf-8").rstrip("\n")
) # noqa: E501
)

# Fetch all branches
git_fetch_cmd = ["git", "fetch", "--all"]
Expand All @@ -945,7 +943,7 @@ def check_for_updates(args: argparse.Namespace) -> Tuple[bool, str]:
.decode("utf-8")
.rstrip("\n")
.split("\n")
) # noqa: E501
)

# Do not check out any release candidate tags
all_prod_tags = [x for x in all_tags if "rc" not in x]
Expand Down Expand Up @@ -1110,8 +1108,6 @@ class ArgParseFormatterCombo(
):
"""Needed to combine formatting classes for help output"""

pass

parser = argparse.ArgumentParser(description=__doc__, formatter_class=ArgParseFormatterCombo)
parser.add_argument(
"-v", action="store_true", default=False, help="Increase verbosity on output"
Expand Down
Loading

0 comments on commit 81a3c49

Please sign in to comment.