/dploi_fabric/django_utils.py

https://github.com/dploi/dploi-fabric · Python · 128 lines · 98 code · 18 blank · 12 comment · 11 complexity · 59121691a7e452b152ac1eaf78c43bd4 MD5 · raw file

  1. import ConfigParser
  2. import StringIO
  3. from fabric.decorators import task
  4. from fabric.api import env, get, put
  5. import os
  6. from fabric.contrib.files import exists
  7. from .utils import config
  8. import posixpath
  9. from .utils import STATIC_COLLECTED, DATA_DIRECTORY
  10. from pprint import pformat
  11. from fabric.operations import run
  12. def django_exec(dictionary = {}, tool="buildout"):
  13. # TODO: Remove this and change dependants to use utils.config
  14. config = ConfigParser.RawConfigParser()
  15. config_file = os.path.join(env.path, "config.ini")
  16. django_base = "." # default to current dir
  17. if exists(config_file):
  18. output = StringIO.StringIO()
  19. get(u"%s" % config_file, output)
  20. output.seek(0)
  21. config.readfp(output)
  22. try:
  23. tool = config.get("checkout", "tool")
  24. except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
  25. tool = "buildout" # default to buildout
  26. try:
  27. django_base = config.get("django", "base")
  28. except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
  29. pass
  30. if django_base == ".":
  31. django_base = ""
  32. if tool == "buildout":
  33. cmd = os.path.join(env.path, django_base, "bin/django")
  34. django_settings = os.path.join(env.path, django_base, "settings.py")
  35. else:
  36. cmd = "%s %s" % (os.path.join(env.path, "bin/python"), os.path.join(env.path, django_base, "manage.py"))
  37. django_settings = os.path.join(env.path, django_base, "settings.py")
  38. dictionary['django_exec'] = cmd
  39. dictionary['django_settings'] = django_settings
  40. dictionary['checkout_tool'] = tool
  41. return dictionary
  42. def django_settings_file(dictionary = {}): # TODO: Remove this and change dependants to use utils.config
  43. return django_exec().get("django_settings")
  44. @task
  45. def manage(*args):
  46. """
  47. Proxy for manage.py
  48. """
  49. config.django_manage(" ".join(args))
  50. @task
  51. def collectstatic(staticdir='static'): # As defined in puppet config
  52. # TODO: Use utils.config
  53. run(('cd %(path)s; mkdir -p ' + staticdir) % env)
  54. manage("collectstatic", "--noinput", "--link")
  55. @task
  56. def load_fixture(file_path):
  57. remote_path = put(file_path, '~/tmp/')[0]
  58. manage('loaddata %s' % remote_path)
  59. run('rm %s' % remote_path)
  60. @task
  61. def append_settings():
  62. # TODO: make it work with multisites!
  63. append = config.sites["main"].get("django").get("append_settings", False)
  64. if append:
  65. site_config = config.sites["main"]
  66. settings_file_path = django_settings_file()
  67. print "Appending auto generated settings to", settings_file_path
  68. output = StringIO.StringIO()
  69. get(u"%s" % os.path.join(env.path, "../config/django.py"), output)
  70. output.seek(0)
  71. manual_settings = output.read()
  72. # START OF DIRTY DATABASE HACK
  73. additional_settings = """if "DATABASES" in locals():\n"""
  74. # DATABASES
  75. #additional_settings = "DATABASES = %s\n" % pformat(config.sites["main"].get("deployment").get("databases"))
  76. additional_settings +=" DATABASES = %s\n" % pformat(config.sites["main"].get("deployment").get("databases"))
  77. db_old_dict = config.sites["main"].get("deployment").get("databases")["default"]
  78. db_old_dict["ENGINE"] = db_old_dict["ENGINE"].replace("django.db.backends.", "")
  79. additional_settings += """else:
  80. DATABASE_ENGINE = "%(ENGINE)s"
  81. DATABASE_NAME = "%(NAME)s"
  82. DATABASE_USER = "%(USER)s"
  83. DATABASE_PASSWORD = "%(PASSWORD)s"
  84. DATABASE_HOST = "%(HOST)s"
  85. """ % db_old_dict
  86. # // END OF DIRTY DATABASE HACK
  87. # CACHES
  88. processes = config.sites["main"].get("processes")
  89. cache_dict = {
  90. 'default': {
  91. 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
  92. 'LOCATION': 'unix:%s' % [processes[x] for x in processes if processes[x]["type"] == "memcached"][0].get("socket"),
  93. }
  94. }
  95. additional_settings += "CACHES = %s\n" % pformat(cache_dict)
  96. # PATHS
  97. additional_settings += """
  98. STATIC_ROOT = "%(static_root)s"
  99. MEDIA_ROOT = "%(media_root)s"
  100. """ % {
  101. 'static_root': posixpath.join(site_config.get("deployment").get("path"), STATIC_COLLECTED),
  102. 'media_root': posixpath.join(site_config.get("deployment").get("path"), DATA_DIRECTORY, 'media/'),
  103. }
  104. output = StringIO.StringIO()
  105. get(settings_file_path, output)
  106. output.seek(0)
  107. settings_file = output.read()
  108. run("mkdir -p %s" % posixpath.join(site_config.get("deployment").get("path"), "_gen/"))
  109. put(StringIO.StringIO("%s\n%s\n%s" % (settings_file, additional_settings, manual_settings)), site_config.get("deployment").get("generated_settings_path"))
  110. put(StringIO.StringIO(""), posixpath.join(site_config.get("deployment").get("path"), "_gen/__init__.py"))