This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
/
__init__.py
217 lines (181 loc) · 5.44 KB
/
__init__.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# -*- coding:utf-8 -*-
import logging
import os
from os.path import abspath, dirname, join
from os import environ
from fabric.api import *
from fabric.network import ssh
from fabric.operations import local
from fabric.operations import sudo
from fabric.utils import puts
from fabric.colors import green, red
from fabric.api import *
from fabric.context_managers import lcd
BASEDIR = dirname(__file__)
BACKENDDIR = abspath(join(abspath(BASEDIR), '../../'))
HOMEDIR = abspath(join(abspath(BASEDIR), '../../..'))
FRONTENDDIR = abspath(join(abspath(BASEDIR), '../../frontend'))
LOGGER = logging.getLogger(__name__)
requirements_file = abspath(join(abspath(BACKENDDIR), 'requirements.txt'))
# http://docs.fabfile.org/en/1.5/tutorial.html
#
project = "gastosabertos_website"
env.user = 'gastosabertos'
env.hosts = ['gastosabertos.org']
env.local = False
#env.key_filename = '~/.ssh/ga_id_rsa'
def freeze():
''' Creates static html files '''
if not env.local:
with prefix('. /home/ubuntu/virtualenvs/venv-system/bin/activate'):
local('python site.py build')
else:
local('python site.py build')
@task
def build():
''''Build markdown and templates'''
freeze()
@task
def run_local():
env.local = True
@task
def run_server():
local('python site.py')
def reset():
"""
Reset local debug env.
"""
local("rm -rf ./build")
local("mkdir ./build")
def setup():
"""
Setup virtual env.
"""
reset()
local("virtualenv env")
# local("workon py")
activate_this = "env/bin/activate_this.py"
execfile(activate_this, dict(__file__=activate_this))
# def get_venv():
# """ Get the current virtual environment name
# Bail out if we're not in one
# """
# try:
# return os.environ['VIRTUAL_ENV']
# except KeyError:
# print 'Not in a virtualenv'
# exit(1)
def get_pip():
""" Get an absolute path to the pip executable
for the current virtual environment
"""
return 'pip'
def check_for(what, unrecoverable_msg, installation_cmd=None):
def failure():
print unrecoverable_msg
exit()
print "%s" % local ("pwd")
try:
test_result = local("which %s" % what, capture=True)
return test_result
except:
if(installation_cmd):
print "Unable to find %s, will attempt installation (you might be asked for sudo password below)"
local(installation_cmd)
else:
failure()
try:
test_result = local(cmd, capture=True)
return test_result
except:
failure()
def get_node():
return check_for('node', 'You need to install Node.js to run the Require.js optimiser and the frontend tests')
def get_npm():
return check_for('npm', 'You need to install Node.js and npm to run gulp, less, require.js optimiser and the frontend tests')
def get_bower():
return check_for('./node_modules/bower/bin/bower', 'You need to install Bower to be able to install JS libs', 'sudo npm install -g bower')
def get_gulp():
return check_for('./node_modules/gulp/bin/gulp.js', 'You need to install Gulp to be able to compile front-end JS, stylesheets and run front-end tests', 'sudo npm install -g gulp-cli')
@task
def install_backend_deps():
""" Install python dependencies from requirements.txt file
"""
with lcd(BACKENDDIR):
cmd = '%(pip)s install -r %(requirements_file)s' % {
'pip': get_pip(),
'requirements_file': requirements_file
}
local(cmd)
# Install Pandoc
local("sudo apt-get install pandoc")
# Install Pyandoc
with lcd(HOMEDIR):
if not os.path.isdir(os.path.join(HOMEDIR, 'pyandoc')):
local("git clone git@github.com:kennethreitz/pyandoc.git")
with lcd("pyandoc"):
if not env.local:
with prefix('. /home/ubuntu/virtualenvs/venv-system/bin/activate'):
local("python setup.py install")
else:
local("python setup.py install")
@task
def install_frontend_deps():
""" install front-end dependencies using npm and bower
"""
with lcd(FRONTENDDIR):
cmd = '%(npm)s install' % {'npm': get_npm()}
local(cmd)
cmd = '%(bower)s install' % {'bower': get_bower()}
local(cmd)
@task
def install_deps():
install_backend_deps()
install_frontend_deps()
print 'Run `fab build_static` to get everything ready for development'
@task
def build_static():
with lcd(FRONTENDDIR):
cmd = '%(gulp)s build' % {'gulp': get_gulp()}
local(cmd)
@task
def build_all():
setup()
install_deps()
build_static()
build()
@task
def deploy():
"""
Deploy project to Gastos Abertos server
"""
project_dir = '/home/gastosabertos/gastos_abertos_website'
with cd(project_dir):
local('tar -cvzf build.tar.gz build')
run('cp -r build build-old')
put('build.tar.gz', '.')
run('tar -xvf build.tar.gz')
@task
def test():
"""
Run frontend tests
"""
with lcd(FRONTENDDIR):
cmd = '%(gulp)s test' % {'gulp': get_gulp()}
local(cmd)
@task
def tdd():
"""
Watch files changes to run frontend tests
"""
with lcd(FRONTENDDIR):
cmd = '%(gulp)s tdd' % {'gulp': get_gulp()}
local(cmd)
@task
def watch():
"""
Watch files changes to build
"""
with lcd(FRONTENDDIR):
cmd = '%(gulp)s watch' % {'gulp': get_gulp()}
local(cmd)