diff options
| author | Carlton Gibson <carlton.gibson@noumenal.es> | 2022-02-08 12:38:43 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-11 12:23:26 +0100 |
| commit | d113b5a837f726d1c638d76c4e88445e6cd59fd5 (patch) | |
| tree | 37191ed160b698bfbf81559b56735d3e83fd7f92 /django | |
| parent | f9ec777a826816e20e68021c0e73b5a76be650af (diff) | |
Refs #33476 -- Made management commands use black.
Run black on generated files, if it is available on PATH.
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/management/commands/makemigrations.py | 5 | ||||
| -rw-r--r-- | django/core/management/commands/squashmigrations.py | 2 | ||||
| -rw-r--r-- | django/core/management/templates.py | 6 | ||||
| -rw-r--r-- | django/core/management/utils.py | 13 |
4 files changed, 25 insertions, 1 deletions
diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py index 5afa209136..8938fb6309 100644 --- a/django/core/management/commands/makemigrations.py +++ b/django/core/management/commands/makemigrations.py @@ -6,6 +6,7 @@ from itertools import takewhile from django.apps import apps from django.conf import settings from django.core.management.base import BaseCommand, CommandError, no_translations +from django.core.management.utils import run_formatters from django.db import DEFAULT_DB_ALIAS, OperationalError, connections, router from django.db.migrations import Migration from django.db.migrations.autodetector import MigrationAutodetector @@ -88,6 +89,7 @@ class Command(BaseCommand): @no_translations def handle(self, *app_labels, **options): + self.written_files = [] self.verbosity = options["verbosity"] self.interactive = options["interactive"] self.dry_run = options["dry_run"] @@ -276,6 +278,7 @@ class Command(BaseCommand): migration_string = writer.as_string() with open(writer.path, "w", encoding="utf-8") as fh: fh.write(migration_string) + self.written_files.append(writer.path) elif self.verbosity == 3: # Alternatively, makemigrations --dry-run --verbosity 3 # will log the migrations rather than saving the file to @@ -286,6 +289,7 @@ class Command(BaseCommand): ) ) self.log(writer.as_string()) + run_formatters(self.written_files) def handle_merge(self, loader, conflicts): """ @@ -382,6 +386,7 @@ class Command(BaseCommand): # Write the merge migrations file to the disk with open(writer.path, "w", encoding="utf-8") as fh: fh.write(writer.as_string()) + run_formatters([writer.path]) if self.verbosity > 0: self.log("\nCreated new merge migration %s" % writer.path) if self.scriptable: diff --git a/django/core/management/commands/squashmigrations.py b/django/core/management/commands/squashmigrations.py index aafa5e7bcc..2d6e0ebfa3 100644 --- a/django/core/management/commands/squashmigrations.py +++ b/django/core/management/commands/squashmigrations.py @@ -3,6 +3,7 @@ import os from django.apps import apps from django.conf import settings from django.core.management.base import BaseCommand, CommandError +from django.core.management.utils import run_formatters from django.db import DEFAULT_DB_ALIAS, connections, migrations from django.db.migrations.loader import AmbiguityError, MigrationLoader from django.db.migrations.migration import SwappableTuple @@ -220,6 +221,7 @@ class Command(BaseCommand): ) with open(writer.path, "w", encoding="utf-8") as fh: fh.write(writer.as_string()) + run_formatters([writer.path]) if self.verbosity > 0: self.stdout.write( diff --git a/django/core/management/templates.py b/django/core/management/templates.py index 58005c23ed..72db9651b0 100644 --- a/django/core/management/templates.py +++ b/django/core/management/templates.py @@ -12,7 +12,7 @@ from urllib.request import build_opener import django from django.conf import settings from django.core.management.base import BaseCommand, CommandError -from django.core.management.utils import handle_extensions +from django.core.management.utils import handle_extensions, run_formatters from django.template import Context, Engine from django.utils import archive from django.utils.version import get_docs_version @@ -80,6 +80,7 @@ class TemplateCommand(BaseCommand): ) def handle(self, app_or_project, name, target=None, **options): + self.written_files = [] self.app_or_project = app_or_project self.a_or_an = "an" if app_or_project == "app" else "a" self.paths_to_remove = [] @@ -200,6 +201,7 @@ class TemplateCommand(BaseCommand): else: shutil.copyfile(old_path, new_path) + self.written_files.append(new_path) if self.verbosity >= 2: self.stdout.write("Creating %s" % new_path) try: @@ -222,6 +224,8 @@ class TemplateCommand(BaseCommand): else: shutil.rmtree(path_to_remove) + run_formatters(self.written_files) + def handle_template(self, template, subdir): """ Determine where the app or project templates are. diff --git a/django/core/management/utils.py b/django/core/management/utils.py index c12d90f6ae..e3e1122409 100644 --- a/django/core/management/utils.py +++ b/django/core/management/utils.py @@ -1,5 +1,7 @@ import fnmatch import os +import shutil +import subprocess from pathlib import Path from subprocess import run @@ -153,3 +155,14 @@ def is_ignored_path(path, ignore_patterns): ) return any(ignore(pattern) for pattern in normalize_path_patterns(ignore_patterns)) + + +def run_formatters(written_files): + """ + Run the black formatter on the specified files. + """ + if black_path := shutil.which("black"): + subprocess.run( + [black_path, "--fast", "--", *written_files], + capture_output=True, + ) |
