From c5e369e8730b02338d029ebe2c00e1bfb6feb58a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Poulain?= Date: Sat, 1 Aug 2020 11:16:45 +0200 Subject: [PATCH] =?UTF-8?q?feat(check):=20v=C3=A9rifie=20la=20coh=C3=A9ren?= =?UTF-8?q?ce=20de=20la=20description=20de=20la=20base=20de=20donn=C3=A9e?= =?UTF-8?q?=20de=20drupal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- drupal2spip_lal/base/apps.py | 3 ++ drupal2spip_lal/base/checks.py | 61 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 drupal2spip_lal/base/checks.py diff --git a/drupal2spip_lal/base/apps.py b/drupal2spip_lal/base/apps.py index cd9faf9..51b0108 100644 --- a/drupal2spip_lal/base/apps.py +++ b/drupal2spip_lal/base/apps.py @@ -4,3 +4,6 @@ from django.apps import AppConfig class BaseConfig(AppConfig): name = 'drupal2spip_lal.base' verbose_name = "Base" + + def ready(self): + from . import checks # noqa diff --git a/drupal2spip_lal/base/checks.py b/drupal2spip_lal/base/checks.py new file mode 100644 index 0000000..6752d12 --- /dev/null +++ b/drupal2spip_lal/base/checks.py @@ -0,0 +1,61 @@ +import io +from difflib import unified_diff + +from django.core.checks import Error, Warning, register +from django.core.management import call_command + +no_models = Warning( + "Unable to import drupal2spip_lal/drupal/models.py", + hint='Verify your database settings and that your DB has been ' + 'inspected.', + obj='drupal2spip_lal.base', + id='drupal2spip_lal.W003', +) + + +@register() +def check_inspected_database(app_configs, **kwargs): + buf = io.StringIO() + rc = call_command('inspectdb', '--database=drupal', stdout=buf) + if rc is not None: + return [ + Error( + "Command inspectdb failed.", + hint='Try to launch "./manage.py inspectdb --database=drupal" ' + 'and report an issue to .', + obj='drupal2spip_lal.base', + id='drupal2spip_lal.E001', + ) + ] + + buf.seek(0) + inspected_db = buf.read() + buf.close() + try: + with open('drupal2spip_lal/drupal/models.py', 'r') as f: + if f.read() != inspected_db: + f.seek(0) + for line in unified_diff( + f.read().split('\n'), inspected_db.split('\n') + ): + print(line) + return [ + Warning( + "Outdated drupal2spip_lal/drupal/models.py", + hint='Refresh it using "make inspectdb" ' + 'and then run migrations.', + obj='drupal2spip_lal.base', + id='drupal2spip_lal.W002', + ) + ] + except Exception: + return [ + Warning( + "Missing drupal2spip_lal/drupal/models.py", + hint='Create it using "make inspectdb" ' + 'and then run migrations.', + obj='drupal2spip_lal.base', + id='drupal2spip_lal.W001', + ) + ] + return []