2020-08-01 11:16:45 +02:00
|
|
|
import io
|
|
|
|
from difflib import unified_diff
|
|
|
|
|
2020-08-01 15:53:56 +02:00
|
|
|
from django.conf import settings
|
2020-08-01 11:16:45 +02:00
|
|
|
from django.core.checks import Error, Warning, register
|
|
|
|
from django.core.management import call_command
|
|
|
|
|
|
|
|
|
|
|
|
@register()
|
2020-08-01 14:31:45 +02:00
|
|
|
def check_inspected_databases(app_configs, **kwargs):
|
2020-08-01 15:53:56 +02:00
|
|
|
r = []
|
|
|
|
r += check_inspected_db('drupal')
|
|
|
|
r += check_inspected_db('spip', tables_prefix=settings.SPIP_TABLES_PREFIX)
|
|
|
|
return r
|
2020-08-01 14:31:45 +02:00
|
|
|
|
|
|
|
|
2020-08-01 15:53:56 +02:00
|
|
|
def check_inspected_db(dbname, **kwargs):
|
2020-08-01 11:16:45 +02:00
|
|
|
buf = io.StringIO()
|
2020-08-01 15:53:56 +02:00
|
|
|
rc = call_command('inspectdb', '--database', dbname, stdout=buf, **kwargs)
|
2020-08-01 11:16:45 +02:00
|
|
|
if rc is not None:
|
|
|
|
return [
|
|
|
|
Error(
|
|
|
|
"Command inspectdb failed.",
|
2020-08-01 14:31:45 +02:00
|
|
|
hint='Try to launch "./manage.py inspectdb --database={}" '
|
|
|
|
'and report an issue.'.format(dbname),
|
2020-08-01 11:16:45 +02:00
|
|
|
obj='drupal2spip_lal.base',
|
|
|
|
id='drupal2spip_lal.E001',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
buf.seek(0)
|
|
|
|
inspected_db = buf.read()
|
|
|
|
buf.close()
|
|
|
|
try:
|
2020-08-01 14:31:45 +02:00
|
|
|
with open('drupal2spip_lal/{}/models.py'.format(dbname), 'r') as f:
|
2020-08-01 11:16:45 +02:00
|
|
|
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(
|
2020-08-01 14:31:45 +02:00
|
|
|
"Outdated drupal2spip_lal/{}/models.py".format(dbname),
|
|
|
|
hint='Refresh it using "make inspectdb".',
|
2020-08-01 11:16:45 +02:00
|
|
|
obj='drupal2spip_lal.base',
|
|
|
|
id='drupal2spip_lal.W002',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
except Exception:
|
|
|
|
return [
|
|
|
|
Warning(
|
2020-08-01 14:31:45 +02:00
|
|
|
"Missing drupal2spip_lal/{}/models.py".format(dbname),
|
|
|
|
hint='Create it using "make inspectdb".',
|
2020-08-01 11:16:45 +02:00
|
|
|
obj='drupal2spip_lal.base',
|
|
|
|
id='drupal2spip_lal.W001',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
return []
|