forked from inventree/InvenTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
440 lines (322 loc) · 9.67 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# -*- coding: utf-8 -*-
from shutil import copyfile
import os
import json
import sys
try:
from invoke import ctask as task
except:
from invoke import task
def apps():
"""
Returns a list of installed apps
"""
return [
'barcode',
'build',
'common',
'company',
'label',
'order',
'part',
'report',
'stock',
'InvenTree',
'users',
]
def localDir():
"""
Returns the directory of *THIS* file.
Used to ensure that the various scripts always run
in the correct directory.
"""
return os.path.dirname(os.path.abspath(__file__))
def managePyDir():
"""
Returns the directory of the manage.py file
"""
return os.path.join(localDir(), 'InvenTree')
def managePyPath():
"""
Return the path of the manage.py file
"""
return os.path.join(managePyDir(), 'manage.py')
def manage(c, cmd, pty=False):
"""
Runs a given command against django's "manage.py" script.
Args:
c - Command line context
cmd - django command to run
"""
c.run('cd "{path}" && python3 manage.py {cmd}'.format(
path=managePyDir(),
cmd=cmd
), pty=pty)
@task
def install(c):
"""
Installs required python packages
"""
# Install required Python packages with PIP
c.run('pip3 install -U -r requirements.txt')
# If a config.yaml file does not exist, copy from the template!
CONFIG_FILE = os.path.join(localDir(), 'InvenTree', 'config.yaml')
CONFIG_TEMPLATE_FILE = os.path.join(localDir(), 'InvenTree', 'config_template.yaml')
if not os.path.exists(CONFIG_FILE):
print("Config file 'config.yaml' does not exist - copying from template.")
copyfile(CONFIG_TEMPLATE_FILE, CONFIG_FILE)
@task
def shell(c):
"""
Open a python shell with access to the InvenTree database models.
"""
manage(c, 'shell', pty=True)
@task
def worker(c):
"""
Run the InvenTree background worker process
"""
manage(c, 'qcluster', pty=True)
@task
def superuser(c):
"""
Create a superuser (admin) account for the database.
"""
manage(c, 'createsuperuser', pty=True)
@task
def check(c):
"""
Check validity of django codebase
"""
manage(c, "check")
@task
def wait(c):
"""
Wait until the database connection is ready
"""
manage(c, "wait_for_db")
@task
def rebuild(c):
"""
Rebuild database models with MPTT structures
"""
manage(c, "rebuild_models")
@task
def migrate(c):
"""
Performs database migrations.
This is a critical step if the database schema have been altered!
"""
print("Running InvenTree database migrations...")
print("========================================")
manage(c, "makemigrations")
manage(c, "migrate")
manage(c, "migrate --run-syncdb")
manage(c, "check")
print("========================================")
print("InvenTree database migrations completed!")
@task
def static(c):
"""
Copies required static files to the STATIC_ROOT directory,
as per Django requirements.
"""
manage(c, "prerender")
manage(c, "collectstatic --no-input")
@task(pre=[install, migrate, static])
def update(c):
"""
Update InvenTree installation.
This command should be invoked after source code has been updated,
e.g. downloading new code from GitHub.
The following tasks are performed, in order:
- install
- migrate
- static
"""
pass
@task(post=[static])
def translate(c):
"""
Regenerate translation files.
Run this command after added new translatable strings,
or after adding translations for existing strings.
"""
# Translate applicable .py / .html / .js files
manage(c, "makemessages --all -e py,html,js --no-wrap")
manage(c, "compilemessages")
path = os.path.join('InvenTree', 'script', 'translation_stats.py')
c.run(f'python {path}')
@task
def style(c):
"""
Run PEP style checks against InvenTree sourcecode
"""
print("Running PEP style checks...")
c.run('flake8 InvenTree')
@task
def test(c, database=None):
"""
Run unit-tests for InvenTree codebase.
"""
# Run sanity check on the django install
manage(c, 'check')
# Run coverage tests
manage(c, 'test', pty=True)
@task
def coverage(c):
"""
Run code-coverage of the InvenTree codebase,
using the 'coverage' code-analysis tools.
Generates a code coverage report (available in the htmlcov directory)
"""
# Run sanity check on the django install
manage(c, 'check')
# Run coverage tests
c.run('coverage run {manage} test {apps}'.format(
manage=managePyPath(),
apps=' '.join(apps())
))
# Generate coverage report
c.run('coverage html')
def content_excludes():
"""
Returns a list of content types to exclude from import/export
"""
excludes = [
"contenttypes",
"sessions.session",
"auth.permission",
"authtoken.token",
"error_report.error",
"admin.logentry",
"django_q.schedule",
"django_q.task",
"django_q.ormq",
"users.owner",
"exchange.rate",
"exchange.exchangebackend",
]
output = ""
for e in excludes:
output += f"--exclude {e} "
return output
@task(help={'filename': "Output filename (default = 'data.json')"})
def export_records(c, filename='data.json'):
"""
Export all database records to a file
"""
# Get an absolute path to the file
if not os.path.isabs(filename):
filename = os.path.join(localDir(), filename)
filename = os.path.abspath(filename)
print(f"Exporting database records to file '{filename}'")
if os.path.exists(filename):
response = input("Warning: file already exists. Do you want to overwrite? [y/N]: ")
response = str(response).strip().lower()
if response not in ['y', 'yes']:
print("Cancelled export operation")
sys.exit(1)
tmpfile = f"{filename}.tmp"
cmd = f"dumpdata --indent 2 --output '{tmpfile}' {content_excludes()}"
# Dump data to temporary file
manage(c, cmd, pty=True)
print("Running data post-processing step...")
# Post-process the file, to remove any "permissions" specified for a user or group
with open(tmpfile, "r") as f_in:
data = json.loads(f_in.read())
for entry in data:
if "model" in entry:
# Clear out any permissions specified for a group
if entry["model"] == "auth.group":
entry["fields"]["permissions"] = []
# Clear out any permissions specified for a user
if entry["model"] == "auth.user":
entry["fields"]["user_permissions"] = []
# Write the processed data to file
with open(filename, "w") as f_out:
f_out.write(json.dumps(data, indent=2))
print("Data export completed")
@task(help={'filename': 'Input filename'}, post=[rebuild])
def import_records(c, filename='data.json'):
"""
Import database records from a file
"""
# Get an absolute path to the supplied filename
if not os.path.isabs(filename):
filename = os.path.join(localDir(), filename)
if not os.path.exists(filename):
print(f"Error: File '{filename}' does not exist")
sys.exit(1)
print(f"Importing database records from '{filename}'")
# Pre-process the data, to remove any "permissions" specified for a user or group
tmpfile = f"{filename}.tmp.json"
with open(filename, "r") as f_in:
data = json.loads(f_in.read())
for entry in data:
if "model" in entry:
# Clear out any permissions specified for a group
if entry["model"] == "auth.group":
entry["fields"]["permissions"] = []
# Clear out any permissions specified for a user
if entry["model"] == "auth.user":
entry["fields"]["user_permissions"] = []
# Write the processed data to the tmp file
with open(tmpfile, "w") as f_out:
f_out.write(json.dumps(data, indent=2))
cmd = f"loaddata '{tmpfile}' -i {content_excludes()}"
manage(c, cmd, pty=True)
print("Data import completed")
@task
def delete_data(c, force=False):
"""
Delete all database records!
Warning: This will REALLY delete all records in the database!!
"""
if force:
manage(c, 'flush --noinput')
else:
manage(c, 'flush')
@task(post=[rebuild])
def import_fixtures(c):
"""
Import fixture data into the database.
This command imports all existing test fixture data into the database.
Warning:
- Intended for testing / development only!
- Running this command may overwrite existing database data!!
- Don't say you were not warned...
"""
fixtures = [
# Build model
'build',
# Common models
'settings',
# Company model
'company',
'price_breaks',
'supplier_part',
# Order model
'order',
# Part model
'bom',
'category',
'params',
'part',
'test_templates',
# Stock model
'location',
'stock_tests',
'stock',
# Users
'users'
]
command = 'loaddata ' + ' '.join(fixtures)
manage(c, command, pty=True)
@task(help={'address': 'Server address:port (default=127.0.0.1:8000)'})
def server(c, address="127.0.0.1:8000"):
"""
Launch a (deveopment) server using Django's in-built webserver.
Note: This is *not* sufficient for a production installation.
"""
manage(c, "runserver {address}".format(address=address), pty=True)