-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
161 lines (139 loc) · 5.39 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from fabric.api import env, local, lcd
from fabric.colors import red, green
from fabric.decorators import task, runs_once
from fabric.utils import abort
import datetime
import fileinput
import importlib
import os
import random
import sys
import time
PROJ_ROOT = os.path.dirname(env.real_fabfile)
env.project_name = 'xmlfacets'
@task
def setup():
"""
Set up a local development environment
This command must be run with Fabric installed globally (not inside a
virtual environment)
"""
if os.getenv('VIRTUAL_ENV') or hasattr(sys, 'real_prefix'):
abort(red('Deactivate any virtual environments before continuing.'))
make_settings()
make_virtual_env()
symlink_packages()
collect_static()
print ('\nDevelopment environment successfully created.\n' +
'Create a Postgres database, enter its information into ' +
'xmlfacets/settings_local.py, and run `fab sync_database` to finish.')
@task
def test():
"Run the test suite locally."
with lcd(PROJ_ROOT):
local('./bin/python manage.py test main admin_custom' % env)
@task
def sync_database():
"Sync db, make cache tables"
with lcd(PROJ_ROOT):
local('./bin/python manage.py syncdb')
@task
def runserver():
"Run the development server"
with lcd(PROJ_ROOT):
local('./bin/python manage.py runserver')
@task
def runfcgi():
"Run the fastcgi server (xpoli/jdf hack)"
outlog = os.getenv("XMLFACETS_OUTLOG", "/tmp/fcgi_out.log")
errlog = os.getenv("XMLFACETS_ERRLOG", "/tmp/fcgi_err.log")
pidfile = os.getenv("XMLFACETS_PID", "/tmp/cen.pid")
fcgiport = int(os.getenv("XMLFACETS_FCGI_PORT", 3044))
with lcd(PROJ_ROOT):
local('./bin/python manage.py runfcgi host=127.0.0.1 port=%(fcgiport)d pidfile=%(pidfile)s outlog="%(outlog)s" errlog="%(errlog)s"' %
dict(outlog=outlog, errlog=errlog, pidfile=pidfile, fcgiport=fcgiport)
)
@task
@runs_once
def make_settings():
"""
Generate a local settings file.
Without any arguments, this file will go in xmlfacets/settings_local.py.
If the function is passed an argument that defines env.hosts, this file will
be placed in the deploy directory with the name settings-{host}.py
"""
to_create = (['deploy/settings-{}.py'.format(host) for host in env.hosts]
or ['xmlfacets/settings_local.py'])
for settings_file in to_create:
secret_key = generate_secret_key()
with lcd(PROJ_ROOT):
local('if [ ! -f {0} ]; then cp {1} {0}; fi'.format(
settings_file, 'xmlfacets/example-settings_local.py'))
for line in fileinput.input(settings_file, inplace=True):
print line.replace("SECRET_KEY = ''",
"SECRET_KEY = '{}'".format(secret_key)),
@task
def watch_static():
"""
Collect static files as they are modified.
Reacts to changes of *.css, *.js, and *.less files.
"""
try:
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
except ImportError:
abort(red('Install Watchdog python package to watch filesystem files.'))
EXTS = ['.js', '.css', '.less']
class ChangeHandler(FileSystemEventHandler):
def __init__(self, *args, **kwargs):
super(ChangeHandler, self).__init__(*args, **kwargs)
self.last_collected = datetime.datetime.now()
def on_any_event(self, event):
if event.is_directory:
return
if os.path.splitext(event.src_path)[-1].lower() in EXTS:
now = datetime.datetime.now()
if (datetime.datetime.now() - self.last_collected).total_seconds() < 1:
return
local('./bin/python manage.py collectstatic --noinput')
sys.stdout.write('\n')
self.last_collected = datetime.datetime.now()
event_handler = ChangeHandler()
observer = Observer()
observer.schedule(
event_handler, os.path.join(PROJ_ROOT, 'xmlfacets'), recursive=True)
observer.start()
print green('\nWatching *.js, *.css, and *.less files for changes.\n')
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
def make_virtual_env():
"Make a virtual environment for local dev use"
with lcd(PROJ_ROOT):
local('virtualenv .')
local('./bin/pip install -r requirements.txt')
def symlink_packages():
"Symlink python packages not installed with pip"
missing = []
requirements = (req.rstrip().replace('# symlink: ', '')
for req in open('requirements.txt', 'r')
if req.startswith('# symlink: '))
for req in requirements:
try:
module = importlib.import_module(req)
except ImportError:
missing.append(req)
continue
with lcd(os.path.join(PROJ_ROOT, 'lib', 'python2.7', 'site-packages')):
local('ln -f -s {}'.format(os.path.dirname(module.__file__)))
if missing:
abort('Missing python packages: {}'.format(', '.join(missing)))
def collect_static():
with lcd(PROJ_ROOT):
local('./bin/python manage.py collectstatic --noinput -v0')
def generate_secret_key():
SECRET_CHARS = 'abcdefghijklmnopqrstuvwxyz1234567890-=!@#$$%^&&*()_+'
return ''.join([random.choice(SECRET_CHARS) for i in range(50)])