-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
108 lines (82 loc) · 3.32 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import sys
from fabric.api import local, env, prefix, task
from fabric.context_managers import settings, lcd
env.host_string = "localhost"
VIRTUAL_ENV_WRAPPER = "/usr/local/bin/virtualenvwrapper.sh"
"""requires virtualenvwrapper/virtualenv/pip/git sets up a virtual env with ipython and django and creates the project"""
class Project(object):
def __init__(self, name):
self.project_dir = "%s_project" % name
self.application_dir = os.path.join(self.project_dir, name)
self.front_end_dir = os.path.join(self.application_dir, "front_end")
self.app_name = name
def require_variable(var_name):
""" check if a variable exists in the environment """
if var_name not in os.environ:
sys.stderr.write("var_name %s not found.\n\n" % var_name)
sys.exit(-1)
def require_function(func_name):
if not function_exists(func_name):
sys.stderr.write("func_name %s not found.\n\n" % func_name)
sys.exit(-1)
def function_exists(func_name):
return bool(local("which %s" % func_name, capture=True))
def setup_front_end(project):
with lcd(project.front_end_dir):
local("npm install brunch")
local("node_modules/brunch/bin/brunch new static --skeleton https://github.com/fredkingham/brunch-with-cantaloupe.git")
@task
def django_project_setup(name, init_git=True):
with prefix("source %s" % VIRTUAL_ENV_WRAPPER):
with prefix("workon %s" % name):
project = Project(name)
local("django-admin.py startproject --template=https://github.com/fredkingham/django-project-skeleton/archive/master.zip %s" % name)
local("mv %s %s" % (name, project.project_dir))
setup_front_end(project)
with lcd(project.project_dir):
local("pip freeze > requirements.txt")
if init_git:
local("git init")
@task
def init_git(name):
project = Project(name)
with lcd(project.project_dir):
local("pip freeze > requirements.txt")
local("git init")
@task
def virtual_env_setup(name):
with prefix("source %s" % VIRTUAL_ENV_WRAPPER):
with settings(warn_only=True):
virtual_envs = local("workon", capture=True).splitlines()
if name in virtual_envs:
raise Exception("virtual env %s already exists" % name)
with prefix("source %s" % VIRTUAL_ENV_WRAPPER):
local("mkvirtualenv --no-site-packages %s" % name)
@task
def install_common_components(requirements_in=None):
if requirements_in:
local("pip install -r %s" % requirements_in)
else:
local("pip install ipython")
local("pip install django")
local("pip install ipdb")
local("pip install mock")
@task
def create(name, init_git=True, empty=False, requirements_in=False):
require_variable("VIRTUALENVWRAPPER_HOOK_DIR")
require_function("pip")
require_function("python")
require_function("git")
require_function("npm")
virtual_env_setup(name)
if not empty:
install_common_components(name)
django_project_setup(name, init_git)
@task
def remove(name):
with prefix("source %s" % VIRTUAL_ENV_WRAPPER):
with settings(warn_only=True):
local("rmvirtualenv %s" % name)
project = Project(name)
local("rm -rf %s" % project.project_dir)