south.management.commands.datamigration: 54 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/south/management/commands/datamigration.py

Stats: 0 executed, 41 missed, 13 excluded, 70 ignored

  1. """
  2. Data migration creation command
  3. """
  4. import sys
  5. import os
  6. import re
  7. from optparse import make_option
  8. try:
  9. set
  10. except NameError:
  11. from sets import Set as set
  12. from django.core.management.base import BaseCommand
  13. from django.core.management.color import no_style
  14. from django.db import models
  15. from django.conf import settings
  16. from south.migration import Migrations
  17. from south.exceptions import NoMigrations
  18. from south.creator import freezer
  19. class Command(BaseCommand):
  20. option_list = BaseCommand.option_list + (
  21. make_option('--freeze', action='append', dest='freeze_list', type='string',
  22. help='Freeze the specified app(s). Provide an app name with each; use the option multiple times for multiple apps'),
  23. make_option('--stdout', action='store_true', dest='stdout', default=False,
  24. help='Print the migration to stdout instead of writing it to a file.'),
  25. )
  26. help = "Creates a new template data migration for the given app"
  27. usage_str = "Usage: ./manage.py datamigration appname migrationname [--stdout] [--freeze appname]"
  28. def handle(self, app=None, name="", freeze_list=None, stdout=False, verbosity=1, **options):
  29. # Any supposed lists that are None become empty lists
  30. freeze_list = freeze_list or []
  31. # --stdout means name = -
  32. if stdout:
  33. name = "-"
  34. # Only allow valid names
  35. if re.search('[^_\w]', name) and name != "-":
  36. self.error("Migration names should contain only alphanumeric characters and underscores.")
  37. # if not name, there's an error
  38. if not name:
  39. self.error("You must provide a name for this migration\n" + self.usage_str)
  40. if not app:
  41. self.error("You must provide an app to create a migration for.\n" + self.usage_str)
  42. # Get the Migrations for this app (creating the migrations dir if needed)
  43. migrations = Migrations(app, force_creation=True, verbose_creation=verbosity > 0)
  44. # See what filename is next in line. We assume they use numbers.
  45. new_filename = migrations.next_filename(name)
  46. # Work out which apps to freeze
  47. apps_to_freeze = self.calc_frozen_apps(migrations, freeze_list)
  48. # So, what's in this file, then?
  49. file_contents = MIGRATION_TEMPLATE % {
  50. "frozen_models": freezer.freeze_apps_to_string(apps_to_freeze),
  51. "complete_apps": apps_to_freeze and "complete_apps = [%s]" % (", ".join(map(repr, apps_to_freeze))) or ""
  52. }
  53. # - is a special name which means 'print to stdout'
  54. if name == "-":
  55. print file_contents
  56. # Write the migration file if the name isn't -
  57. else:
  58. fp = open(os.path.join(migrations.migrations_dir(), new_filename), "w")
  59. fp.write(file_contents)
  60. fp.close()
  61. print >>sys.stderr, "Created %s." % new_filename
  62. def calc_frozen_apps(self, migrations, freeze_list):
  63. """
  64. Works out, from the current app, settings, and the command line options,
  65. which apps should be frozen.
  66. """
  67. apps_to_freeze = []
  68. for to_freeze in freeze_list:
  69. if "." in to_freeze:
  70. self.error("You cannot freeze %r; you must provide an app label, like 'auth' or 'books'." % to_freeze)
  71. # Make sure it's a real app
  72. if not models.get_app(to_freeze):
  73. self.error("You cannot freeze %r; it's not an installed app." % to_freeze)
  74. # OK, it's fine
  75. apps_to_freeze.append(to_freeze)
  76. if getattr(settings, 'SOUTH_AUTO_FREEZE_APP', True):
  77. apps_to_freeze.append(migrations.app_label())
  78. return apps_to_freeze
  79. def error(self, message, code=1):
  80. """
  81. Prints the error, and exits with the given code.
  82. """
  83. print >>sys.stderr, message
  84. sys.exit(code)
  85. MIGRATION_TEMPLATE = """# -*- coding: utf-8 -*-
  86. import datetime
  87. from south.db import db
  88. from south.v2 import DataMigration
  89. from django.db import models
  90. class Migration(DataMigration):
  91. def forwards(self, orm):
  92. "Write your forwards methods here."
  93. # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."
  94. def backwards(self, orm):
  95. "Write your backwards methods here."
  96. models = %(frozen_models)s
  97. %(complete_apps)s
  98. symmetrical = True
  99. """