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 []