You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

writer.py 11KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import os
  2. import re
  3. from importlib import import_module
  4. from django import get_version
  5. from django.apps import apps
  6. # SettingsReference imported for backwards compatibility in Django 2.2.
  7. from django.conf import SettingsReference # NOQA
  8. from django.db import migrations
  9. from django.db.migrations.loader import MigrationLoader
  10. from django.db.migrations.serializer import Serializer, serializer_factory
  11. from django.utils.inspect import get_func_args
  12. from django.utils.module_loading import module_dir
  13. from django.utils.timezone import now
  14. class OperationWriter:
  15. def __init__(self, operation, indentation=2):
  16. self.operation = operation
  17. self.buff = []
  18. self.indentation = indentation
  19. def serialize(self):
  20. def _write(_arg_name, _arg_value):
  21. if (_arg_name in self.operation.serialization_expand_args and
  22. isinstance(_arg_value, (list, tuple, dict))):
  23. if isinstance(_arg_value, dict):
  24. self.feed('%s={' % _arg_name)
  25. self.indent()
  26. for key, value in _arg_value.items():
  27. key_string, key_imports = MigrationWriter.serialize(key)
  28. arg_string, arg_imports = MigrationWriter.serialize(value)
  29. args = arg_string.splitlines()
  30. if len(args) > 1:
  31. self.feed('%s: %s' % (key_string, args[0]))
  32. for arg in args[1:-1]:
  33. self.feed(arg)
  34. self.feed('%s,' % args[-1])
  35. else:
  36. self.feed('%s: %s,' % (key_string, arg_string))
  37. imports.update(key_imports)
  38. imports.update(arg_imports)
  39. self.unindent()
  40. self.feed('},')
  41. else:
  42. self.feed('%s=[' % _arg_name)
  43. self.indent()
  44. for item in _arg_value:
  45. arg_string, arg_imports = MigrationWriter.serialize(item)
  46. args = arg_string.splitlines()
  47. if len(args) > 1:
  48. for arg in args[:-1]:
  49. self.feed(arg)
  50. self.feed('%s,' % args[-1])
  51. else:
  52. self.feed('%s,' % arg_string)
  53. imports.update(arg_imports)
  54. self.unindent()
  55. self.feed('],')
  56. else:
  57. arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
  58. args = arg_string.splitlines()
  59. if len(args) > 1:
  60. self.feed('%s=%s' % (_arg_name, args[0]))
  61. for arg in args[1:-1]:
  62. self.feed(arg)
  63. self.feed('%s,' % args[-1])
  64. else:
  65. self.feed('%s=%s,' % (_arg_name, arg_string))
  66. imports.update(arg_imports)
  67. imports = set()
  68. name, args, kwargs = self.operation.deconstruct()
  69. operation_args = get_func_args(self.operation.__init__)
  70. # See if this operation is in django.db.migrations. If it is,
  71. # We can just use the fact we already have that imported,
  72. # otherwise, we need to add an import for the operation class.
  73. if getattr(migrations, name, None) == self.operation.__class__:
  74. self.feed('migrations.%s(' % name)
  75. else:
  76. imports.add('import %s' % (self.operation.__class__.__module__))
  77. self.feed('%s.%s(' % (self.operation.__class__.__module__, name))
  78. self.indent()
  79. for i, arg in enumerate(args):
  80. arg_value = arg
  81. arg_name = operation_args[i]
  82. _write(arg_name, arg_value)
  83. i = len(args)
  84. # Only iterate over remaining arguments
  85. for arg_name in operation_args[i:]:
  86. if arg_name in kwargs: # Don't sort to maintain signature order
  87. arg_value = kwargs[arg_name]
  88. _write(arg_name, arg_value)
  89. self.unindent()
  90. self.feed('),')
  91. return self.render(), imports
  92. def indent(self):
  93. self.indentation += 1
  94. def unindent(self):
  95. self.indentation -= 1
  96. def feed(self, line):
  97. self.buff.append(' ' * (self.indentation * 4) + line)
  98. def render(self):
  99. return '\n'.join(self.buff)
  100. class MigrationWriter:
  101. """
  102. Take a Migration instance and is able to produce the contents
  103. of the migration file from it.
  104. """
  105. def __init__(self, migration, include_header=True):
  106. self.migration = migration
  107. self.include_header = include_header
  108. self.needs_manual_porting = False
  109. def as_string(self):
  110. """Return a string of the file contents."""
  111. items = {
  112. "replaces_str": "",
  113. "initial_str": "",
  114. }
  115. imports = set()
  116. # Deconstruct operations
  117. operations = []
  118. for operation in self.migration.operations:
  119. operation_string, operation_imports = OperationWriter(operation).serialize()
  120. imports.update(operation_imports)
  121. operations.append(operation_string)
  122. items["operations"] = "\n".join(operations) + "\n" if operations else ""
  123. # Format dependencies and write out swappable dependencies right
  124. dependencies = []
  125. for dependency in self.migration.dependencies:
  126. if dependency[0] == "__setting__":
  127. dependencies.append(" migrations.swappable_dependency(settings.%s)," % dependency[1])
  128. imports.add("from django.conf import settings")
  129. else:
  130. dependencies.append(" %s," % self.serialize(dependency)[0])
  131. items["dependencies"] = "\n".join(dependencies) + "\n" if dependencies else ""
  132. # Format imports nicely, swapping imports of functions from migration files
  133. # for comments
  134. migration_imports = set()
  135. for line in list(imports):
  136. if re.match(r"^import (.*)\.\d+[^\s]*$", line):
  137. migration_imports.add(line.split("import")[1].strip())
  138. imports.remove(line)
  139. self.needs_manual_porting = True
  140. # django.db.migrations is always used, but models import may not be.
  141. # If models import exists, merge it with migrations import.
  142. if "from django.db import models" in imports:
  143. imports.discard("from django.db import models")
  144. imports.add("from django.db import migrations, models")
  145. else:
  146. imports.add("from django.db import migrations")
  147. # Sort imports by the package / module to be imported (the part after
  148. # "from" in "from ... import ..." or after "import" in "import ...").
  149. sorted_imports = sorted(imports, key=lambda i: i.split()[1])
  150. items["imports"] = "\n".join(sorted_imports) + "\n" if imports else ""
  151. if migration_imports:
  152. items["imports"] += (
  153. "\n\n# Functions from the following migrations need manual "
  154. "copying.\n# Move them and any dependencies into this file, "
  155. "then update the\n# RunPython operations to refer to the local "
  156. "versions:\n# %s"
  157. ) % "\n# ".join(sorted(migration_imports))
  158. # If there's a replaces, make a string for it
  159. if self.migration.replaces:
  160. items['replaces_str'] = "\n replaces = %s\n" % self.serialize(self.migration.replaces)[0]
  161. # Hinting that goes into comment
  162. if self.include_header:
  163. items['migration_header'] = MIGRATION_HEADER_TEMPLATE % {
  164. 'version': get_version(),
  165. 'timestamp': now().strftime("%Y-%m-%d %H:%M"),
  166. }
  167. else:
  168. items['migration_header'] = ""
  169. if self.migration.initial:
  170. items['initial_str'] = "\n initial = True\n"
  171. return MIGRATION_TEMPLATE % items
  172. @property
  173. def basedir(self):
  174. migrations_package_name, _ = MigrationLoader.migrations_module(self.migration.app_label)
  175. if migrations_package_name is None:
  176. raise ValueError(
  177. "Django can't create migrations for app '%s' because "
  178. "migrations have been disabled via the MIGRATION_MODULES "
  179. "setting." % self.migration.app_label
  180. )
  181. # See if we can import the migrations module directly
  182. try:
  183. migrations_module = import_module(migrations_package_name)
  184. except ImportError:
  185. pass
  186. else:
  187. try:
  188. return module_dir(migrations_module)
  189. except ValueError:
  190. pass
  191. # Alright, see if it's a direct submodule of the app
  192. app_config = apps.get_app_config(self.migration.app_label)
  193. maybe_app_name, _, migrations_package_basename = migrations_package_name.rpartition(".")
  194. if app_config.name == maybe_app_name:
  195. return os.path.join(app_config.path, migrations_package_basename)
  196. # In case of using MIGRATION_MODULES setting and the custom package
  197. # doesn't exist, create one, starting from an existing package
  198. existing_dirs, missing_dirs = migrations_package_name.split("."), []
  199. while existing_dirs:
  200. missing_dirs.insert(0, existing_dirs.pop(-1))
  201. try:
  202. base_module = import_module(".".join(existing_dirs))
  203. except (ImportError, ValueError):
  204. continue
  205. else:
  206. try:
  207. base_dir = module_dir(base_module)
  208. except ValueError:
  209. continue
  210. else:
  211. break
  212. else:
  213. raise ValueError(
  214. "Could not locate an appropriate location to create "
  215. "migrations package %s. Make sure the toplevel "
  216. "package exists and can be imported." %
  217. migrations_package_name)
  218. final_dir = os.path.join(base_dir, *missing_dirs)
  219. if not os.path.isdir(final_dir):
  220. os.makedirs(final_dir)
  221. for missing_dir in missing_dirs:
  222. base_dir = os.path.join(base_dir, missing_dir)
  223. with open(os.path.join(base_dir, "__init__.py"), "w"):
  224. pass
  225. return final_dir
  226. @property
  227. def filename(self):
  228. return "%s.py" % self.migration.name
  229. @property
  230. def path(self):
  231. return os.path.join(self.basedir, self.filename)
  232. @classmethod
  233. def serialize(cls, value):
  234. return serializer_factory(value).serialize()
  235. @classmethod
  236. def register_serializer(cls, type_, serializer):
  237. Serializer.register(type_, serializer)
  238. @classmethod
  239. def unregister_serializer(cls, type_):
  240. Serializer.unregister(type_)
  241. MIGRATION_HEADER_TEMPLATE = """\
  242. # Generated by Django %(version)s on %(timestamp)s
  243. """
  244. MIGRATION_TEMPLATE = """\
  245. %(migration_header)s%(imports)s
  246. class Migration(migrations.Migration):
  247. %(replaces_str)s%(initial_str)s
  248. dependencies = [
  249. %(dependencies)s\
  250. ]
  251. operations = [
  252. %(operations)s\
  253. ]
  254. """