-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtasks.py
182 lines (150 loc) · 5.19 KB
/
tasks.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
import re
from sys import platform
from invoke import task
# Workaround for homebrew installation of Python (https://bugs.python.org/issue22490)
import os
os.environ.pop("__PYVENV_LAUNCHER__", None)
ROOT = os.path.dirname(os.path.realpath(__file__))
# Account for platform differences:
# - *n*x supports `pty`, but windows does not,
# - the virtual environment dir structure differs between *n*x and windows,
# - *n*x uses "python3", but on windows it was kept "python".
if platform == "win32":
PLATFORM_ARG = dict(
env={
"PYTHONUNBUFFERED": "True"
}
)
VENV_BIN_NAME = "Scripts"
PYTHON = "python"
else:
PLATFORM_ARG = dict(pty=True)
VENV_BIN_NAME = "bin"
PYTHON = "python3"
VENV_BIN_PATH = os.path.join(".", "pulsevenv", VENV_BIN_NAME, "")
def create_env_file(env_file):
"""Create or update the .env file"""
with open(env_file, "r") as f:
env_vars = f.read()
# update the DATABASE_URL env
new_db_url = "DATABASE_URL=postgres://postgres@localhost:5432/pulse"
old_db_url = re.search("DATABASE_URL=.*", env_vars)
if old_db_url:
env_vars = env_vars.replace(old_db_url.group(0), new_db_url)
else:
env_vars = env_vars + "DATABASE_URL=postgres://postgres@localhost:5432/pulse\n"
# update the ALLOWED_HOSTS env
new_hosts = "ALLOWED_HOSTS=*"
old_hosts = re.search("ALLOWED_HOSTS=.*", env_vars)
if old_hosts:
env_vars = env_vars.replace(old_hosts.group(0), new_hosts)
else:
env_vars = env_vars + "ALLOWED_HOSTS=*\n"
# create the new env file
with open(".env", "w") as f:
f.write(env_vars)
def create_super_user(ctx):
preamble = "from django.contrib.auth import get_user_model;User = get_user_model();"
create = "User.objects.create_superuser('admin', '[email protected]', 'admin')"
manage(ctx, f'shell -c "{preamble} {create}"')
print("\nCreated superuser `admin` with email `[email protected]` and password `admin`.")
# Project setup and update
@task(aliases=["new-env"])
def setup(ctx):
"""Automate project's configuration and dependencies installation"""
with ctx.cd(ROOT):
if os.path.isfile(".env"):
print("* Updating your .env")
create_env_file(".env")
else:
print("* Creating a new .env")
create_env_file("sample.env")
# create virtualenv
print("* Creating a Python virtual environment")
if platform == "win32" and not os.path.isfile(VENV_BIN_PATH + "python.exe"):
ctx.run(f"{PYTHON} -m venv pulsevenv")
elif not os.path.isfile(VENV_BIN_PATH + "python3"):
ctx.run(f"{PYTHON} -m venv pulsevenv")
print("* Installing pip-tools")
ctx.run(VENV_BIN_PATH + "pip install pip-tools")
# install deps
print("* Installing Python dependencies")
pip_sync(ctx)
new_db(ctx)
@task(aliases=["catchup"])
def catch_up(ctx):
"""Install dependencies and apply migrations"""
print("Installing Python dependencies")
pip_sync(ctx)
print("Applying database migrations")
migrate(ctx)
@task
def new_db(ctx):
"""Create a new database with fake data"""
print("* Reset the database")
ctx.run("dropdb --if-exists -h localhost -U postgres pulse")
ctx.run("createdb -h localhost -U postgres pulse")
print("* Migrating database")
migrate(ctx)
print("* Creating fake data")
manage(ctx, "load_fake_data")
create_super_user(ctx)
print("* Done!\n"
"You can get a full list of inv commands with 'inv -l'\n"
"Start you server with 'inv runserver'\n"
)
# Django shorthands
@task
def manage(ctx, command):
"""Shorthand to manage.py. inv docker-manage \"[COMMAND] [ARG]\""""
with ctx.cd(ROOT):
ctx.run(VENV_BIN_PATH + f"python manage.py {command}", **PLATFORM_ARG)
@task
def runserver(ctx, arguments=""):
"""Start a web server"""
manage(ctx, f"runserver {arguments}")
@task
def migrate(ctx):
"""Updates database schema"""
manage(ctx, "migrate")
@task
def makemigrations(ctx):
"""Creates new migration(s) for apps"""
manage(ctx, "makemigrations")
# Tests
@task
def test(ctx):
"""Run tests"""
print("Running flake8")
ctx.run(VENV_BIN_PATH + "python -m flake8 pulseapi", **PLATFORM_ARG)
print("Running tests")
manage(ctx, "test")
# Pip-tools
@task(aliases=["docker-pip-compile"])
def pip_compile(ctx, command):
"""Shorthand to pip-tools. inv pip-compile \"[COMMAND] [ARG]\""""
with ctx.cd(ROOT):
ctx.run(
VENV_BIN_PATH + f"pip-compile {command}",
**PLATFORM_ARG,
)
@task(aliases=["docker-pip-compile-lock"])
def pip_compile_lock(ctx):
"""Lock prod and dev dependencies"""
with ctx.cd(ROOT):
ctx.run(
VENV_BIN_PATH + "pip-compile",
**PLATFORM_ARG,
)
ctx.run(
VENV_BIN_PATH + "pip-compile dev-requirements.in",
**PLATFORM_ARG,
)
@task(aliases=["docker-pip-sync"])
def pip_sync(ctx):
"""Sync your python virtualenv"""
with ctx.cd(ROOT):
ctx.run(
VENV_BIN_PATH + "pip-sync requirements.txt dev-requirements.txt",
**PLATFORM_ARG,
)