__init__.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import os
  2. import sys
  3. from importlib import import_module
  4. import django
  5. from django.conf import settings
  6. # Version number reading ...
  7. fname = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'version.txt')
  8. with open(fname, encoding='utf-8') as filehandler:
  9. __version__ = filehandler.read().strip().replace("\n", "")
  10. del fname
  11. instance = None # instance reference will be stored here
  12. def setup(fuzzy_path=None, _safe_mode=False):
  13. """
  14. Configure IEPY internals,
  15. Reads IEPY instance configuration if any path provided.
  16. Detects out of dated instances.
  17. Returns the absolute path to the IEPY instance if provided, None if not.
  18. """
  19. # Prevent nosetests messing up with this
  20. if not isinstance(fuzzy_path, (type(None), str)):
  21. # nosetests is grabing this function because its named "setup"... .
  22. return
  23. if not settings.configured:
  24. if fuzzy_path is None:
  25. if not os.getenv('DJANGO_SETTINGS_MODULE'):
  26. os.environ['DJANGO_SETTINGS_MODULE'] = 'iepy.webui.webui.settings'
  27. result = None
  28. else:
  29. path, project_name, old = _actual_path(fuzzy_path)
  30. sys.path.insert(0, path)
  31. if old:
  32. django_settings_module = "{0}_settings".format(project_name)
  33. sys.path.insert(0, os.path.join(path, project_name))
  34. else:
  35. django_settings_module = "{0}.settings".format(project_name)
  36. os.environ['DJANGO_SETTINGS_MODULE'] = django_settings_module
  37. result = os.path.join(path, project_name)
  38. import_instance(project_name)
  39. django.setup()
  40. if not _safe_mode and settings.IEPY_VERSION != __version__:
  41. sys.exit(
  42. 'Instance version is {} and current IEPY installation is {}.\n'
  43. 'Run iepy --upgrade on the instance.'.format(settings.IEPY_VERSION,
  44. __version__)
  45. )
  46. return result
  47. def import_instance(project_name):
  48. """
  49. Imports the project_name instance and stores it
  50. on the global variable `instance`.
  51. """
  52. global instance
  53. instance = import_module(project_name)
  54. def _actual_path(fuzzy_path):
  55. """
  56. Given the fuzzy_path path, walks-up until it finds a folder containing a iepy-instance.
  57. Returns the path where the folder is contained, the folder name and a boolean to indicate
  58. if its an instance older than 0.9.2 where the settings file was different.
  59. """
  60. def _find_settings_file(folder_path):
  61. folder_name = os.path.basename(folder_path)
  62. expected_file = os.path.join(folder_path, "settings.py")
  63. old_settings_file = os.path.join(
  64. folder_path, "{}_settings.py".format(folder_name)
  65. )
  66. if os.path.exists(expected_file):
  67. return expected_file
  68. elif os.path.exists(old_settings_file):
  69. return old_settings_file
  70. # first, make sure we are handling an absolute path
  71. original = fuzzy_path # used for debug
  72. fuzzy_path = os.path.abspath(fuzzy_path)
  73. while True:
  74. settings_filepath = _find_settings_file(fuzzy_path)
  75. if settings_filepath is not None:
  76. old = True if settings_filepath.endswith("_settings.py") else False
  77. return os.path.dirname(fuzzy_path), os.path.basename(fuzzy_path), old
  78. else:
  79. parent = os.path.dirname(fuzzy_path)
  80. if parent == fuzzy_path:
  81. raise ValueError("There's no IEPY instance on the provided path {}".format(original))
  82. fuzzy_path = parent