2020-08-21 10:39:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
2021-04-25 01:29:07 +00:00
|
|
|
import json
|
2020-11-12 05:10:00 +00:00
|
|
|
import sys
|
2020-08-21 10:39:43 +00:00
|
|
|
|
2021-04-11 04:05:55 +00:00
|
|
|
try:
|
|
|
|
from invoke import ctask as task
|
|
|
|
except:
|
|
|
|
from invoke import task
|
|
|
|
|
2021-04-01 13:06:17 +00:00
|
|
|
|
2020-08-21 11:08:04 +00:00
|
|
|
def apps():
|
|
|
|
"""
|
|
|
|
Returns a list of installed apps
|
|
|
|
"""
|
|
|
|
|
|
|
|
return [
|
|
|
|
'barcode',
|
|
|
|
'build',
|
|
|
|
'common',
|
|
|
|
'company',
|
|
|
|
'label',
|
|
|
|
'order',
|
|
|
|
'part',
|
|
|
|
'report',
|
|
|
|
'stock',
|
2020-10-03 13:45:24 +00:00
|
|
|
'InvenTree',
|
|
|
|
'users',
|
2020-08-21 11:08:04 +00:00
|
|
|
]
|
|
|
|
|
2021-04-01 13:06:17 +00:00
|
|
|
|
2020-08-21 10:39:43 +00:00
|
|
|
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__))
|
|
|
|
|
2021-04-01 13:06:17 +00:00
|
|
|
|
2020-08-21 10:39:43 +00:00
|
|
|
def managePyDir():
|
|
|
|
"""
|
|
|
|
Returns the directory of the manage.py file
|
|
|
|
"""
|
|
|
|
|
|
|
|
return os.path.join(localDir(), 'InvenTree')
|
|
|
|
|
2021-04-01 13:06:17 +00:00
|
|
|
|
2020-08-21 10:39:43 +00:00
|
|
|
def managePyPath():
|
|
|
|
"""
|
|
|
|
Return the path of the manage.py file
|
|
|
|
"""
|
|
|
|
|
2020-08-21 11:08:04 +00:00
|
|
|
return os.path.join(managePyDir(), 'manage.py')
|
2020-08-21 10:39:43 +00:00
|
|
|
|
2021-04-01 13:06:17 +00:00
|
|
|
|
2020-08-28 19:38:16 +00:00
|
|
|
def manage(c, cmd, pty=False):
|
2020-08-21 10:39:43 +00:00
|
|
|
"""
|
|
|
|
Runs a given command against django's "manage.py" script.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
c - Command line context
|
|
|
|
cmd - django command to run
|
|
|
|
"""
|
|
|
|
|
2021-08-17 23:52:27 +00:00
|
|
|
result = c.run('cd "{path}" && python3 manage.py {cmd}'.format(
|
2020-08-21 10:39:43 +00:00
|
|
|
path=managePyDir(),
|
|
|
|
cmd=cmd
|
2020-08-28 19:38:16 +00:00
|
|
|
), pty=pty)
|
2020-08-21 10:39:43 +00:00
|
|
|
|
2020-08-21 11:08:04 +00:00
|
|
|
|
2021-04-01 13:06:17 +00:00
|
|
|
@task
|
2020-08-21 11:08:04 +00:00
|
|
|
def install(c):
|
|
|
|
"""
|
2021-04-01 13:06:17 +00:00
|
|
|
Installs required python packages
|
2020-08-21 11:08:04 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Install required Python packages with PIP
|
2020-08-21 11:32:08 +00:00
|
|
|
c.run('pip3 install -U -r requirements.txt')
|
2020-08-21 11:08:04 +00:00
|
|
|
|
2020-11-24 10:18:00 +00:00
|
|
|
|
|
|
|
@task
|
|
|
|
def shell(c):
|
|
|
|
"""
|
|
|
|
Open a python shell with access to the InvenTree database models.
|
|
|
|
"""
|
|
|
|
|
|
|
|
manage(c, 'shell', pty=True)
|
|
|
|
|
|
|
|
|
2020-08-21 11:24:02 +00:00
|
|
|
@task
|
|
|
|
def superuser(c):
|
|
|
|
"""
|
|
|
|
Create a superuser (admin) account for the database.
|
|
|
|
"""
|
|
|
|
|
2020-08-28 19:38:16 +00:00
|
|
|
manage(c, 'createsuperuser', pty=True)
|
2020-08-21 11:24:02 +00:00
|
|
|
|
2021-08-17 23:52:27 +00:00
|
|
|
|
2020-09-17 12:44:17 +00:00
|
|
|
@task
|
|
|
|
def check(c):
|
|
|
|
"""
|
|
|
|
Check validity of django codebase
|
|
|
|
"""
|
|
|
|
|
|
|
|
manage(c, "check")
|
|
|
|
|
2021-08-17 23:52:27 +00:00
|
|
|
|
2021-04-10 12:35:10 +00:00
|
|
|
@task
|
|
|
|
def wait(c):
|
|
|
|
"""
|
|
|
|
Wait until the database connection is ready
|
|
|
|
"""
|
|
|
|
|
2021-08-17 23:52:27 +00:00
|
|
|
return manage(c, "wait_for_db")
|
|
|
|
|
|
|
|
|
|
|
|
@task(pre=[wait])
|
|
|
|
def worker(c):
|
|
|
|
"""
|
|
|
|
Run the InvenTree background worker process
|
|
|
|
"""
|
|
|
|
|
|
|
|
manage(c, 'qcluster', pty=True)
|
|
|
|
|
2021-04-10 12:35:10 +00:00
|
|
|
|
2021-06-18 11:53:15 +00:00
|
|
|
@task
|
2021-10-04 21:05:26 +00:00
|
|
|
def rebuild_models(c):
|
2021-06-18 11:53:15 +00:00
|
|
|
"""
|
|
|
|
Rebuild database models with MPTT structures
|
|
|
|
"""
|
|
|
|
|
2021-10-04 21:05:26 +00:00
|
|
|
manage(c, "rebuild_models", pty=True)
|
2021-06-18 11:53:15 +00:00
|
|
|
|
2021-11-19 20:50:41 +00:00
|
|
|
|
2021-10-04 21:05:26 +00:00
|
|
|
@task
|
|
|
|
def rebuild_thumbnails(c):
|
|
|
|
"""
|
|
|
|
Rebuild missing image thumbnails
|
|
|
|
"""
|
|
|
|
|
|
|
|
manage(c, "rebuild_thumbnails", pty=True)
|
2021-08-17 23:52:27 +00:00
|
|
|
|
2021-11-19 20:50:41 +00:00
|
|
|
|
2021-07-31 23:06:17 +00:00
|
|
|
@task
|
|
|
|
def clean_settings(c):
|
|
|
|
"""
|
|
|
|
Clean the setting tables of old settings
|
|
|
|
"""
|
|
|
|
|
|
|
|
manage(c, "clean_settings")
|
|
|
|
|
2021-11-19 20:50:41 +00:00
|
|
|
|
2021-10-04 21:05:26 +00:00
|
|
|
@task(post=[rebuild_models, rebuild_thumbnails])
|
2020-08-21 10:39:43 +00:00
|
|
|
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")
|
2021-08-17 13:10:57 +00:00
|
|
|
manage(c, "migrate --noinput")
|
2020-08-21 10:39:43 +00:00
|
|
|
manage(c, "migrate --run-syncdb")
|
|
|
|
manage(c, "check")
|
|
|
|
|
|
|
|
print("========================================")
|
|
|
|
print("InvenTree database migrations completed!")
|
|
|
|
|
2020-08-21 11:08:04 +00:00
|
|
|
|
2020-08-21 10:39:43 +00:00
|
|
|
@task
|
2020-08-21 11:08:04 +00:00
|
|
|
def static(c):
|
|
|
|
"""
|
|
|
|
Copies required static files to the STATIC_ROOT directory,
|
|
|
|
as per Django requirements.
|
|
|
|
"""
|
|
|
|
|
2021-04-20 11:37:19 +00:00
|
|
|
manage(c, "prerender")
|
2021-04-01 13:40:47 +00:00
|
|
|
manage(c, "collectstatic --no-input")
|
2020-08-21 11:08:04 +00:00
|
|
|
|
|
|
|
|
2021-08-19 21:36:54 +00:00
|
|
|
@task
|
|
|
|
def translate_stats(c):
|
|
|
|
"""
|
|
|
|
Collect translation stats.
|
|
|
|
The file generated from this is needed for the UI.
|
|
|
|
"""
|
|
|
|
|
|
|
|
path = os.path.join('InvenTree', 'script', 'translation_stats.py')
|
|
|
|
c.run(f'python3 {path}')
|
|
|
|
|
|
|
|
|
|
|
|
@task(post=[translate_stats, static])
|
2020-08-21 11:13:28 +00:00
|
|
|
def translate(c):
|
|
|
|
"""
|
|
|
|
Regenerate translation files.
|
|
|
|
|
|
|
|
Run this command after added new translatable strings,
|
|
|
|
or after adding translations for existing strings.
|
|
|
|
"""
|
|
|
|
|
2020-10-24 11:13:40 +00:00
|
|
|
# Translate applicable .py / .html / .js files
|
2021-05-05 07:34:35 +00:00
|
|
|
manage(c, "makemessages --all -e py,html,js --no-wrap")
|
2020-08-21 11:13:28 +00:00
|
|
|
manage(c, "compilemessages")
|
|
|
|
|
2021-08-17 08:22:07 +00:00
|
|
|
|
2021-08-19 21:37:38 +00:00
|
|
|
@task(pre=[install, migrate, translate_stats, static, clean_settings])
|
2021-08-17 08:22:07 +00:00
|
|
|
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
|
2021-08-19 21:36:54 +00:00
|
|
|
- translate_stats
|
2021-08-19 21:37:38 +00:00
|
|
|
- static
|
2021-08-17 08:22:07 +00:00
|
|
|
- clean_settings
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-08-21 11:12:05 +00:00
|
|
|
@task
|
|
|
|
def style(c):
|
|
|
|
"""
|
|
|
|
Run PEP style checks against InvenTree sourcecode
|
|
|
|
"""
|
|
|
|
|
|
|
|
print("Running PEP style checks...")
|
|
|
|
c.run('flake8 InvenTree')
|
|
|
|
|
2021-08-17 08:22:07 +00:00
|
|
|
|
2020-09-02 09:47:07 +00:00
|
|
|
@task
|
2020-09-01 11:01:38 +00:00
|
|
|
def test(c, database=None):
|
2020-08-21 11:10:14 +00:00
|
|
|
"""
|
|
|
|
Run unit-tests for InvenTree codebase.
|
|
|
|
"""
|
|
|
|
# Run sanity check on the django install
|
|
|
|
manage(c, 'check')
|
|
|
|
|
|
|
|
# Run coverage tests
|
2020-09-02 09:47:07 +00:00
|
|
|
manage(c, 'test', pty=True)
|
2020-08-21 11:10:14 +00:00
|
|
|
|
2021-08-17 08:22:07 +00:00
|
|
|
|
2020-08-21 11:08:04 +00:00
|
|
|
@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
|
2020-08-21 11:17:38 +00:00
|
|
|
c.run('coverage html')
|
|
|
|
|
2021-04-25 00:13:31 +00:00
|
|
|
|
|
|
|
def content_excludes():
|
|
|
|
"""
|
|
|
|
Returns a list of content types to exclude from import/export
|
|
|
|
"""
|
|
|
|
|
|
|
|
excludes = [
|
|
|
|
"contenttypes",
|
2021-04-25 00:52:47 +00:00
|
|
|
"sessions.session",
|
2021-04-25 00:13:31 +00:00
|
|
|
"auth.permission",
|
2021-06-20 06:36:39 +00:00
|
|
|
"authtoken.token",
|
2021-04-25 00:13:31 +00:00
|
|
|
"error_report.error",
|
|
|
|
"admin.logentry",
|
|
|
|
"django_q.schedule",
|
|
|
|
"django_q.task",
|
|
|
|
"django_q.ormq",
|
2021-04-27 15:10:46 +00:00
|
|
|
"users.owner",
|
2021-06-20 06:13:07 +00:00
|
|
|
"exchange.rate",
|
|
|
|
"exchange.exchangebackend",
|
2021-11-03 14:31:26 +00:00
|
|
|
"common.notificationentry",
|
2021-04-25 00:13:31 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
output = ""
|
|
|
|
|
|
|
|
for e in excludes:
|
|
|
|
output += f"--exclude {e} "
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2020-11-12 02:31:27 +00:00
|
|
|
@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)
|
2021-11-19 20:50:41 +00:00
|
|
|
filename = os.path.abspath(filename)
|
2020-11-12 02:31:27 +00:00
|
|
|
|
|
|
|
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")
|
2020-11-12 05:10:00 +00:00
|
|
|
sys.exit(1)
|
2020-11-12 02:31:27 +00:00
|
|
|
|
2021-04-25 01:29:07 +00:00
|
|
|
tmpfile = f"{filename}.tmp"
|
2020-11-12 02:31:27 +00:00
|
|
|
|
2021-06-13 18:08:42 +00:00
|
|
|
cmd = f"dumpdata --indent 2 --output '{tmpfile}' {content_excludes()}"
|
2021-04-25 01:29:07 +00:00
|
|
|
|
|
|
|
# Dump data to temporary file
|
2020-11-12 05:10:00 +00:00
|
|
|
manage(c, cmd, pty=True)
|
2020-11-12 02:31:27 +00:00
|
|
|
|
2021-04-25 01:41:48 +00:00
|
|
|
print("Running data post-processing step...")
|
|
|
|
|
2021-04-25 02:07:58 +00:00
|
|
|
# Post-process the file, to remove any "permissions" specified for a user or group
|
2021-04-25 01:29:07 +00:00
|
|
|
with open(tmpfile, "r") as f_in:
|
|
|
|
data = json.loads(f_in.read())
|
|
|
|
|
|
|
|
for entry in data:
|
|
|
|
if "model" in entry:
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2021-04-25 01:29:07 +00:00
|
|
|
# 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))
|
|
|
|
|
2021-04-25 02:07:58 +00:00
|
|
|
print("Data export completed")
|
|
|
|
|
2021-04-25 01:29:07 +00:00
|
|
|
|
2021-10-04 21:05:26 +00:00
|
|
|
@task(help={'filename': 'Input filename'}, post=[rebuild_models, rebuild_thumbnails])
|
2020-11-12 02:31:27 +00:00
|
|
|
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")
|
2020-11-12 05:10:00 +00:00
|
|
|
sys.exit(1)
|
2020-11-12 02:31:27 +00:00
|
|
|
|
|
|
|
print(f"Importing database records from '{filename}'")
|
|
|
|
|
2021-04-25 02:07:58 +00:00
|
|
|
# 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:
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2021-04-25 02:07:58 +00:00
|
|
|
# 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))
|
|
|
|
|
2021-06-13 18:08:42 +00:00
|
|
|
cmd = f"loaddata '{tmpfile}' -i {content_excludes()}"
|
2020-11-12 02:31:27 +00:00
|
|
|
|
|
|
|
manage(c, cmd, pty=True)
|
|
|
|
|
2021-04-25 02:07:58 +00:00
|
|
|
print("Data import completed")
|
|
|
|
|
2021-06-21 00:23:53 +00:00
|
|
|
|
|
|
|
@task
|
|
|
|
def delete_data(c, force=False):
|
|
|
|
"""
|
|
|
|
Delete all database records!
|
|
|
|
|
|
|
|
Warning: This will REALLY delete all records in the database!!
|
|
|
|
"""
|
|
|
|
|
|
|
|
if force:
|
2021-06-21 00:38:50 +00:00
|
|
|
manage(c, 'flush --noinput')
|
2021-06-21 00:23:53 +00:00
|
|
|
else:
|
|
|
|
manage(c, 'flush')
|
|
|
|
|
|
|
|
|
2021-10-04 21:05:26 +00:00
|
|
|
@task(post=[rebuild_models, rebuild_thumbnails])
|
2020-11-12 02:31:27 +00:00
|
|
|
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',
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2020-11-12 03:48:57 +00:00
|
|
|
# Common models
|
|
|
|
'settings',
|
|
|
|
|
2020-11-12 02:31:27 +00:00
|
|
|
# Company model
|
|
|
|
'company',
|
|
|
|
'price_breaks',
|
|
|
|
'supplier_part',
|
|
|
|
|
|
|
|
# Order model
|
|
|
|
'order',
|
|
|
|
|
|
|
|
# Part model
|
|
|
|
'bom',
|
|
|
|
'category',
|
|
|
|
'params',
|
|
|
|
'part',
|
|
|
|
'test_templates',
|
|
|
|
|
|
|
|
# Stock model
|
|
|
|
'location',
|
|
|
|
'stock_tests',
|
|
|
|
'stock',
|
2021-04-25 01:41:48 +00:00
|
|
|
|
|
|
|
# Users
|
|
|
|
'users'
|
2020-11-12 02:31:27 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
command = 'loaddata ' + ' '.join(fixtures)
|
|
|
|
|
|
|
|
manage(c, command, pty=True)
|
|
|
|
|
2021-02-10 15:55:04 +00:00
|
|
|
|
2020-08-21 11:51:22 +00:00
|
|
|
@task(help={'address': 'Server address:port (default=127.0.0.1:8000)'})
|
2020-08-21 11:49:03 +00:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2020-09-28 11:26:40 +00:00
|
|
|
manage(c, "runserver {address}".format(address=address), pty=True)
|
2021-08-28 10:59:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def render_js_files(c):
|
|
|
|
"""
|
|
|
|
Render templated javascript files (used for static testing).
|
|
|
|
"""
|
|
|
|
|
|
|
|
manage(c, "test InvenTree.ci_render_js")
|