Find the top-level Django project directory.

    This function guesses the top-level Django project directory based on
    the current environment.  It looks for module containing the currently-
    active settings module, in both pre-1.4 and post-1.4 layours.

def guess_project_dir():
    """Find the top-level Django project directory.

    This function guesses the top-level Django project directory based on
    the current environment.  It looks for module containing the currently-
    active settings module, in both pre-1.4 and post-1.4 layours.
    """
    projname = settings.SETTINGS_MODULE.split(".",1)[0]
    projmod = import_module(projname)
    projdir = os.path.dirname(projmod.__file__)

    # For Django 1.3 and earlier, the manage.py file was located
    # in the same directory as the settings file.
    if os.path.isfile(os.path.join(projdir,"manage.py")):
        return projdir

    # For Django 1.4 and later, the manage.py file is located in
    # the directory *containing* the settings file.
    projdir = os.path.abspath(os.path.join(projdir, os.path.pardir))
    if os.path.isfile(os.path.join(projdir,"manage.py")):
        return projdir

    msg = "Unable to determine the Django project directory;"\
          " use --project-dir to specify it"
    raise RuntimeError(msg)