forked from pycontw/pycon.tw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
130 lines (97 loc) · 3.4 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
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import sys
from fabric.api import cd, run, sudo, lcd, local
from fabric.api import task
from fabric.api import env
env.forward_agent = True
PROJECT_DIR = os.environ.get('PROJECT_DIR', '')
PROJECT_NAME = os.environ.get('PROJECT_NAME', '')
DJANGO_DIR = PROJECT_DIR + '/' + PROJECT_NAME
VIRTUALENV_NAME = os.environ.get('VIRTUALENV_NAME', '')
SUPERVISOR_NAME = os.environ.get('SUPERVISOR_NAME', '')
def upgrade_system():
sudo('apt-get update -y')
def install_requirements():
with cd(PROJECT_DIR):
run('~/.virtualenvs/{0}/bin/pip install -r '
'requirements/production.txt'.format(VIRTUALENV_NAME))
def migrate_db():
with cd(DJANGO_DIR):
run('source {0}/env.sh && ~/.virtualenvs/{1}/bin/python '
'manage.py migrate'.format(PROJECT_DIR, VIRTUALENV_NAME))
def pull_repo():
with cd(PROJECT_DIR):
run('git pull origin master')
def restart_services():
sudo('supervisorctl restart {0}'.format(SUPERVISOR_NAME))
sudo('service nginx restart')
def collectstatic():
with cd(DJANGO_DIR):
run('source {0}/env.sh && ~/.virtualenvs/{1}/bin/python '
'manage.py collectstatic --noinput -c'.format(
PROJECT_DIR, VIRTUALENV_NAME,
))
def compile_translations():
with cd(DJANGO_DIR):
# run('~/.virtualenvs/{0}/bin/tx pull'.format(VIRTUALENV_NAME))
run('~/.virtualenvs/{0}/bin/python manage.py compilemessages'.format(
VIRTUALENV_NAME,
))
def clean_local_untracked_translation():
# Fix local git changes when only .po files are committed
# but .mo files are not.
with cd(PROJECT_DIR):
run('git checkout -- src/locale/')
@task
def deploy():
clean_local_untracked_translation()
pull_repo()
install_requirements()
collectstatic()
migrate_db()
compile_translations()
restart_services()
def write_transifex_config():
"""Used to setup Travis for Transifex push.
"""
transifexrc_path = os.path.expanduser('~/.transifexrc')
if os.path.exists(transifexrc_path):
return
with open(transifexrc_path, 'w') as f:
f.write((
'[https://www.transifex.com]\n'
'hostname = https://www.transifex.com\n'
'password = {password}\n'
'token = \n'
'username = pycontw\n'
).format(password=os.environ['TRANSIFEX_PASSWORD']))
@task
def pull_tx():
with lcd(os.path.join(os.path.dirname(__file__), 'src')):
local("ls locale/*/LC_MESSAGES/django.* | "
"grep '^locale/[^_]' | "
"xargs rm")
local('tx pull -a')
local('python manage.py compilemessages -x _src')
@task
def push_tx():
with lcd(os.path.join(os.path.dirname(__file__), 'src')):
local('python manage.py makemessages -a')
local('tx push -s')
@task
def travis_push_transifex():
if os.getenv('TRAVIS_PULL_REQUEST') != 'false':
print('Build triggered by a pull request. Transifex push skipped.',
file=sys.stderr)
return
current_branch = os.getenv('TRAVIS_BRANCH')
target_branch = 'master'
if current_branch != target_branch:
print('Branch {cur} is not {target}. Transifex push skipped.'.format(
cur=current_branch, target=target_branch,
), file=sys.stderr)
return
write_transifex_config()
push_tx()