Files
zulip/zerver/lib/migration_status.py
PieterCK 7a2b91ae97 migration_status: Update ANSI code clean up regex.
in `get_migrations_status`, we clean up the printed output of any ANSI
codes used to format the output. Currently the regex only cleans up bold
ANSI escape code (\x1b[1m) and style reset code (\x1b[0m). So it won't
be able to clean up basic ANSI escape codes such as "\x1b\31;1m" which
is used to format `showmigrations` output for apps with no migrations.
   e.g, "\x1b\31;1m (no migrations)"

This commit updates the regex to catch a wider range of basic ANSI
codes.
2025-01-24 17:08:37 -08:00

41 lines
1.2 KiB
Python

import os
import re
from importlib import import_module
from io import StringIO
from typing import Any
def get_migration_status(**options: Any) -> str:
from django.apps import apps
from django.core.management import call_command
from django.db import DEFAULT_DB_ALIAS
from django.utils.module_loading import module_has_submodule
verbosity = options.get("verbosity", 1)
for app_config in apps.get_app_configs():
if module_has_submodule(app_config.module, "management"):
import_module(".management", app_config.name)
app_label = options["app_label"] if options.get("app_label") else None
db = options.get("database", DEFAULT_DB_ALIAS)
out = StringIO()
command_args = ["--list"]
if app_label:
command_args.append(app_label)
call_command(
"showmigrations",
*command_args,
database=db,
no_color=options.get("no_color", False),
settings=options.get("settings", os.environ["DJANGO_SETTINGS_MODULE"]),
stdout=out,
skip_checks=options.get("skip_checks", True),
traceback=options.get("traceback", True),
verbosity=verbosity,
)
out.seek(0)
output = out.read()
return re.sub(r"\x1b\[[0-9;]*m", "", output)