Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
Created February 4, 2013 21:40
Show Gist options
  • Save SmileyChris/4709946 to your computer and use it in GitHub Desktop.
Save SmileyChris/4709946 to your computer and use it in GitHub Desktop.
This goes in a `settings` package, imported from settings modules with `from .base_dirs import PROJECT_DIR, VAR_ROOT`
"""
Calculates some project directory settings by assuming they are relative this
module's location.
``PROJECT_DIR``
The project module's path.
``VAR_ROOT``
A location to put run-time generated files (such as uploaded files, or the
collation of static files).
If running in a virtual environment, this will be ``$VIRTUAL_ENV/var``,
otherwise it will be ``$PROJECT_DIR/local``.
"""
import os
import sys
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
python_bin_dir = os.path.dirname(sys.executable)
# Assume that the presence of 'activate_this.py' in the python bin/ directory
# means that we're running in a virtual environment.
if os.path.exists(os.path.join(python_bin_dir, 'activate_this.py')):
# We're running with a virtualenv python executable.
VAR_ROOT = os.path.join(os.path.dirname(python_bin_dir), 'var')
else:
venv_dir = os.path.dirname(os.path.dirname(os.path.dirname(PROJECT_DIR)))
if venv_dir and os.path.exists(
os.path.join(venv_dir, 'bin', 'activate_this.py')):
# We're running in [virtualenv_root]/src/[project_name].
VAR_ROOT = os.path.join(venv_dir, 'var')
else:
# Set the variable root to the local configuration location (which is
# ignored by the repository).
VAR_ROOT = os.path.join(PROJECT_DIR, 'local', 'var')
if not os.path.exists(VAR_ROOT):
os.mkdir(VAR_ROOT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment