Skip to content

Commit

Permalink
Simplify the code formatting script (#8910)
Browse files Browse the repository at this point in the history
After #8611 and #8866, it's no longer necessary to apply the formatters
to each module separately. So don't!

In addition, update the black exclusion list in `pyproject.toml` to only
contain files that were _not_ listed in `format_python_code.sh`. This
way, formatting will automatically be enforced for all new files.

For a few of the files reformatting only creates a small diff, so don't
even bother excluding them - reformat them instead.

Also, delete a few `pyproject.toml` files that are now redundant.
  • Loading branch information
SpecLad authored Jan 22, 2025
1 parent c2bfb21 commit f9b7138
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 123 deletions.
6 changes: 0 additions & 6 deletions cvat/apps/analytics_report/pyproject.toml

This file was deleted.

1 change: 0 additions & 1 deletion cvat/apps/dataset_manager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Copyright (C) 2021-2022 Intel Corporation
#
# SPDX-License-Identifier: MIT

1 change: 0 additions & 1 deletion cvat/apps/dataset_manager/management/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Copyright (C) CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT

1 change: 0 additions & 1 deletion cvat/apps/dataset_manager/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Copyright (C) CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT

12 changes: 0 additions & 12 deletions cvat/apps/engine/pyproject.toml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ def get_data(scope, context, ownership, privilege, membership, resource, same_or
"scope": scope,
"auth": {
"user": {"id": random.randrange(0, 100), "privilege": privilege},
"organization": {
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
} if context == "organization" else None,
"organization": (
{
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None
),
},
"resource": resource,
}
Expand Down Expand Up @@ -172,27 +176,26 @@ def is_valid(scope, context, ownership, privilege, membership, resource, same_or
if context == "sandbox" and same_org:
return False


return True


def gen_test_rego(name):
with open(f"{name}_test.gen.rego", "wt") as f:
f.write(f"package {name}\nimport rego.v1\n\n")
for scope, context, ownership, privilege, membership, same_org, in product(
SCOPES, CONTEXTS, OWNERSHIPS, GROUPS, ORG_ROLES, SAME_ORG,
for scope, context, ownership, privilege, membership, same_org in product(
SCOPES, CONTEXTS, OWNERSHIPS, GROUPS, ORG_ROLES, SAME_ORG
):
for resource in RESOURCES(scope):
if not is_valid(
scope, context, ownership, privilege, membership, resource, same_org,
scope, context, ownership, privilege, membership, resource, same_org
):
continue

data = get_data(
scope, context, ownership, privilege, membership, resource, same_org
)
test_name = get_name(
scope, context, ownership, privilege, membership, resource, same_org,
scope, context, ownership, privilege, membership, resource, same_org
)
result = eval_rule(scope, context, ownership, privilege, membership, data)
f.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ def get_data(scope, context, ownership, privilege, membership, resource, same_or
"scope": scope,
"auth": {
"user": {"id": random.randrange(0, 100), "privilege": privilege},
"organization": {
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None,
"organization": (
{
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None
),
},
"resource": resource,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,15 @@ def get_data(scope, context, ownership, privilege, membership, resource, same_or
"scope": scope,
"auth": {
"user": {"id": random.randrange(0, 100), "privilege": privilege},
"organization": {
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None,
"organization": (
{
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None
),
},
"resource": resource,
}
Expand Down
16 changes: 9 additions & 7 deletions cvat/apps/engine/rules/tests/generators/issues_test.gen.rego.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ def get_data(scope, context, ownership, privilege, membership, resource, same_or
"scope": scope,
"auth": {
"user": {"id": random.randrange(0, 100), "privilege": privilege},
"organization": {
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None,
"organization": (
{
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None
),
},
"resource": resource,
}
Expand Down
16 changes: 9 additions & 7 deletions cvat/apps/engine/rules/tests/generators/jobs_test.gen.rego.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ def get_data(scope, context, ownership, privilege, membership, resource, same_or
"scope": scope,
"auth": {
"user": {"id": random.randrange(0, 100), "privilege": privilege},
"organization": {
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None,
"organization": (
{
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None
),
},
"resource": resource,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ def get_data(scope, context, ownership, privilege, membership, resource, same_or
"scope": scope,
"auth": {
"user": {"id": random.randrange(0, 100), "privilege": privilege},
"organization": {
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None,
"organization": (
{
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None
),
},
"resource": resource,
}
Expand Down
16 changes: 9 additions & 7 deletions cvat/apps/engine/rules/tests/generators/server_test.gen.rego.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ def get_data(scope, context, ownership, privilege, membership):
"scope": scope,
"auth": {
"user": {"id": random.randrange(0, 100), "privilege": privilege},
"organization": {
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None,
"organization": (
{
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None
),
},
}

Expand Down
16 changes: 9 additions & 7 deletions cvat/apps/engine/rules/tests/generators/tasks_test.gen.rego.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ def get_data(scope, context, ownership, privilege, membership, resource, same_or
"scope": scope,
"auth": {
"user": {"id": random.randrange(0, 100), "privilege": privilege},
"organization": {
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None,
"organization": (
{
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None
),
},
"resource": resource,
}
Expand Down
17 changes: 10 additions & 7 deletions cvat/apps/engine/rules/tests/generators/users_test.gen.rego.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def RESOURCES(scope):
{"id": random.randrange(300, 400), "membership": {"role": role}} for role in ORG_ROLES
]


def eval_rule(scope, context, ownership, privilege, membership, data):
if privilege == "admin":
return True
Expand Down Expand Up @@ -81,13 +82,15 @@ def get_data(scope, context, ownership, privilege, membership, resource):
"scope": scope,
"auth": {
"user": {"id": random.randrange(0, 100), "privilege": privilege},
"organization": {
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None,
"organization": (
{
"id": random.randrange(100, 200),
"owner": {"id": random.randrange(200, 300)},
"user": {"role": membership},
}
if context == "organization"
else None
),
},
"resource": resource,
}
Expand Down
6 changes: 0 additions & 6 deletions cvat/apps/quality_control/pyproject.toml

This file was deleted.

38 changes: 12 additions & 26 deletions dev/format_python_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

set -e

REPO_ROOT="$(dirname "$0")/.."
PYTHON="${PYTHON:-python3}"
BLACK="${PYTHON} -m black"
ISORT="${PYTHON} -m isort"
Expand All @@ -15,29 +16,14 @@ if ! ${ISORT} --version >/dev/null 2>&1; then
exit 1
fi

# The commands must be run on each module directory separately,
# otherwise tools confuse the "current" module
for paths in \
"cvat-sdk" \
"cvat-cli" \
"tests/python/" \
"cvat/apps/quality_control" \
"cvat/apps/analytics_report" \
"cvat/apps/engine/lazy_list.py" \
"cvat/apps/engine/background.py" \
"cvat/apps/engine/frame_provider.py" \
"cvat/apps/engine/cache.py" \
"cvat/apps/engine/default_settings.py" \
"cvat/apps/engine/field_validation.py" \
"cvat/apps/engine/model_utils.py" \
"cvat/apps/engine/task_validation.py" \
"cvat/apps/dataset_manager/cron.py" \
"cvat/apps/dataset_manager/tests/test_annotation.py" \
"cvat/apps/dataset_manager/tests/utils.py" \
"cvat/apps/events/signals.py" \
"cvat/apps/engine/management/commands/syncperiodicjobs.py" \
"cvat/apps/dataset_manager/management/commands/cleanuplegacyexportcache.py" \
; do
${BLACK} -- ${paths}
${ISORT} -- ${paths}
done
cd -- "${REPO_ROOT}"
${BLACK} .

# isort is slow at skipping large Git-ignored directories when invoked as `isort .`,
# so pass it an explicit list of files instead.

# isort will only skip files listed in the config file corresponding to the first file
# on the command line, which would normally be cvat-cli/pyproject.toml. Force the first
# file to be manage.py, so that it uses `pyproject.toml` instead.
git ls-files -z --exclude-standard --deduplicate --cached --others '*.py' \
| xargs -0 ${ISORT} --resolve-all-configs --filter-files manage.py
47 changes: 46 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,52 @@ line-length = 100
target-version = ['py39']
extend-exclude = """
# TODO: get rid of these
^/cvat/apps/(dataset_manager|engine)/
^/cvat/apps/(
dataset_manager/(
annotation.py
|apps.py
|bindings.py
|formats/
|project.py
|serializers.py
|task.py
|tests/test_formats.py
|tests/test_rest_api_formats.py
|util.py
|views.py
)
|engine/(
admin.py
|apps.py
|backup.py
|cloud_provider.py
|filters.py
|handlers.py
|location.py
|log.py
|media_extractors.py
|middleware.py
|migrations/
|mime_types.py
|mixins.py
|models.py
|pagination.py
|parsers.py
|permissions.py
|plugins.py
|renderers.py
|rq_job_handler.py
|schema.py
|serializers.py
|signals.py
|task.py
|tests/
|urls.py
|utils.py
|view_utils.py
|views.py
)
)
| ^/cvat/settings/
| ^/serverless/
| ^/utils/dataset_manifest/
Expand Down
4 changes: 1 addition & 3 deletions site/content/en/docs/contributing/coding-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ for indentation of nested blocks and statements.

For Python, we use [Black](https://github.com/psf/black) and
[isort](https://pycqa.github.io/isort/) to enforce the coding style and autoformat files.
Currently, not all components implement formatting, the actual information about the enabled
components is available in the CI checks [here](https://github.com/cvat-ai/cvat/tree/develop/.github/workflows)
and in the formatting script at `dev/format_python_code.sh`.
You can use `dev/format_python_code.sh` to apply these formatters.

0 comments on commit f9b7138

Please sign in to comment.