2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-04 14:28:48 +00:00

Backport changes to tasks.py (#6256)

- Fixes ongoing issues with import/export
This commit is contained in:
Oliver 2024-01-16 21:47:41 +11:00 committed by GitHub
parent 2b0ef2bc61
commit 1abdb1fd46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

532
tasks.py
View File

@ -18,11 +18,10 @@ def checkPythonVersion():
If the python version is not sufficient, exits with a non-zero exit code. If the python version is not sufficient, exits with a non-zero exit code.
""" """
REQ_MAJOR = 3 REQ_MAJOR = 3
REQ_MINOR = 9 REQ_MINOR = 9
version = sys.version.split(" ")[0] version = sys.version.split(' ')[0]
valid = True valid = True
@ -33,8 +32,8 @@ def checkPythonVersion():
valid = False valid = False
if not valid: if not valid:
print(f"The installed python version ({version}) is not supported!") print(f'The installed python version ({version}) is not supported!')
print(f"InvenTree requires Python {REQ_MAJOR}.{REQ_MINOR} or above") print(f'InvenTree requires Python {REQ_MAJOR}.{REQ_MINOR} or above')
sys.exit(1) sys.exit(1)
@ -59,29 +58,57 @@ def apps():
] ]
def content_excludes(): def content_excludes(
"""Returns a list of content types to exclude from import/export.""" allow_auth: bool = True,
allow_tokens: bool = True,
allow_plugins: bool = True,
allow_sso: bool = True,
):
"""Returns a list of content types to exclude from import/export.
Arguments:
allow_auth (bool): Allow user/group information to be exported/imported
allow_tokens (bool): Allow tokens to be exported/importe
allow_plugins (bool): Allow plugin information to be exported/imported
allow_sso (bool): Allow SSO tokens to be exported/imported
"""
excludes = [ excludes = [
"contenttypes", 'contenttypes',
"auth.permission", 'auth.permission',
"users.apitoken", 'error_report.error',
"error_report.error", 'admin.logentry',
"admin.logentry", 'django_q.schedule',
"django_q.schedule", 'django_q.task',
"django_q.task", 'django_q.ormq',
"django_q.ormq", 'exchange.rate',
"users.owner", 'exchange.exchangebackend',
"exchange.rate", 'common.notificationentry',
"exchange.exchangebackend", 'common.notificationmessage',
"common.notificationentry", 'user_sessions.session',
"common.notificationmessage",
"user_sessions.session",
] ]
output = "" # Optionally exclude user auth data
if not allow_auth:
excludes.append('auth.group')
excludes.append('auth.user')
# Optionally exclude user token information
if not allow_tokens:
excludes.append('users.apitoken')
# Optionally exclude plugin information
if not allow_plugins:
excludes.append('plugin.pluginconfig')
excludes.append('plugin.pluginsetting')
# Optionally exclude SSO application information
if not allow_sso:
excludes.append('socialaccount.socialapp')
output = ''
for e in excludes: for e in excludes:
output += f"--exclude {e} " output += f'--exclude {e} '
return output return output
@ -113,10 +140,10 @@ def manage(c, cmd, pty: bool = False):
cmd: Django command to run. cmd: Django command to run.
pty (bool, optional): Run an interactive session. Defaults to False. pty (bool, optional): Run an interactive session. Defaults to False.
""" """
c.run('cd "{path}" && python3 manage.py {cmd}'.format( c.run(
path=managePyDir(), 'cd "{path}" && python3 manage.py {cmd}'.format(path=managePyDir(), cmd=cmd),
cmd=cmd pty=pty,
), pty=pty) )
def yarn(c, cmd, pty: bool = False): def yarn(c, cmd, pty: bool = False):
@ -133,6 +160,7 @@ def yarn(c, cmd, pty: bool = False):
def node_available(versions: bool = False, bypass_yarn: bool = False): def node_available(versions: bool = False, bypass_yarn: bool = False):
"""Checks if the frontend environment (ie node and yarn in bash) is available.""" """Checks if the frontend environment (ie node and yarn in bash) is available."""
def ret(val, val0=None, val1=None): def ret(val, val0=None, val1=None):
if versions: if versions:
return val, val0, val1 return val, val0, val1
@ -140,7 +168,10 @@ def node_available(versions: bool = False, bypass_yarn: bool = False):
def check(cmd): def check(cmd):
try: try:
return str(subprocess.check_output([cmd], stderr=subprocess.STDOUT, shell=True), encoding='utf-8').strip() return str(
subprocess.check_output([cmd], stderr=subprocess.STDOUT, shell=True),
encoding='utf-8',
).strip()
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
return None return None
except FileNotFoundError: except FileNotFoundError:
@ -154,7 +185,9 @@ def node_available(versions: bool = False, bypass_yarn: bool = False):
# Print a warning if node is available but yarn is not # Print a warning if node is available but yarn is not
if node_version and not yarn_passes: if node_version and not yarn_passes:
print('Node is available but yarn is not. Install yarn if you wish to build the frontend.') print(
'Node is available but yarn is not. Install yarn if you wish to build the frontend.'
)
# Return the result # Return the result
return ret(yarn_passes and node_version, node_version, yarn_version) return ret(yarn_passes and node_version, node_version, yarn_version)
@ -168,11 +201,13 @@ def check_file_existance(filename: str, overwrite: bool = False):
overwrite (bool, optional): Overwrite the file without asking. Defaults to False. overwrite (bool, optional): Overwrite the file without asking. Defaults to False.
""" """
if Path(filename).is_file() and overwrite is False: if Path(filename).is_file() and overwrite is False:
response = input("Warning: file already exists. Do you want to overwrite? [y/N]: ") response = input(
'Warning: file already exists. Do you want to overwrite? [y/N]: '
)
response = str(response).strip().lower() response = str(response).strip().lower()
if response not in ['y', 'yes']: if response not in ['y', 'yes']:
print("Cancelled export operation") print('Cancelled export operation')
sys.exit(1) sys.exit(1)
@ -198,7 +233,9 @@ def install(c):
# Install required Python packages with PIP # Install required Python packages with PIP
c.run('pip3 install --upgrade pip') c.run('pip3 install --upgrade pip')
c.run('pip3 install --upgrade setuptools') c.run('pip3 install --upgrade setuptools')
c.run('pip3 install --no-cache-dir --disable-pip-version-check -U -r requirements.txt') c.run(
'pip3 install --no-cache-dir --disable-pip-version-check -U -r requirements.txt'
)
@task(help={'tests': 'Set up test dataset at the end'}) @task(help={'tests': 'Set up test dataset at the end'})
@ -210,12 +247,12 @@ def setup_dev(c, tests=False):
c.run('pip3 install -U -r requirements-dev.txt') c.run('pip3 install -U -r requirements-dev.txt')
# Install pre-commit hook # Install pre-commit hook
print("Installing pre-commit for checks before git commits...") print('Installing pre-commit for checks before git commits...')
c.run('pre-commit install') c.run('pre-commit install')
# Update all the hooks # Update all the hooks
c.run('pre-commit autoupdate') c.run('pre-commit autoupdate')
print("pre-commit set up is done...") print('pre-commit set up is done...')
# Set up test-data if flag is set # Set up test-data if flag is set
if tests: if tests:
@ -232,19 +269,19 @@ def superuser(c):
@task @task
def rebuild_models(c): def rebuild_models(c):
"""Rebuild database models with MPTT structures.""" """Rebuild database models with MPTT structures."""
manage(c, "rebuild_models", pty=True) manage(c, 'rebuild_models', pty=True)
@task @task
def rebuild_thumbnails(c): def rebuild_thumbnails(c):
"""Rebuild missing image thumbnails.""" """Rebuild missing image thumbnails."""
manage(c, "rebuild_thumbnails", pty=True) manage(c, 'rebuild_thumbnails', pty=True)
@task @task
def clean_settings(c): def clean_settings(c):
"""Clean the setting tables of old settings.""" """Clean the setting tables of old settings."""
manage(c, "clean_settings") manage(c, 'clean_settings')
@task(help={'mail': "mail of the user who's MFA should be disabled"}) @task(help={'mail': "mail of the user who's MFA should be disabled"})
@ -253,20 +290,16 @@ def remove_mfa(c, mail=''):
if not mail: if not mail:
print('You must provide a users mail') print('You must provide a users mail')
manage(c, f"remove_mfa {mail}") manage(c, f'remove_mfa {mail}')
@task( @task(help={'frontend': 'Build the frontend'})
help={
'frontend': 'Build the frontend',
}
)
def static(c, frontend=False): def static(c, frontend=False):
"""Copies required static files to the STATIC_ROOT directory, as per Django requirements.""" """Copies required static files to the STATIC_ROOT directory, as per Django requirements."""
manage(c, "prerender") manage(c, 'prerender')
if frontend and node_available(): if frontend and node_available():
frontend_build(c) frontend_build(c)
manage(c, "collectstatic --no-input") manage(c, 'collectstatic --no-input')
@task @task
@ -280,48 +313,49 @@ def translate_stats(c):
try: try:
manage(c, 'compilemessages', pty=True) manage(c, 'compilemessages', pty=True)
except Exception: except Exception:
print("WARNING: Translation files could not be compiled:") print('WARNING: Translation files could not be compiled:')
path = Path('InvenTree', 'script', 'translation_stats.py') path = Path('InvenTree', 'script', 'translation_stats.py')
c.run(f'python3 {path}') c.run(f'python3 {path}')
@task(post=[translate_stats]) @task(post=[translate_stats])
def translate(c): def translate(c, ignore_static=False, no_frontend=False):
"""Rebuild translation source files. Advanced use only! """Rebuild translation source files. Advanced use only!
Note: This command should not be used on a local install, Note: This command should not be used on a local install,
it is performed as part of the InvenTree translation toolchain. it is performed as part of the InvenTree translation toolchain.
""" """
# Translate applicable .py / .html / .js / .tsx files # Translate applicable .py / .html / .js files
manage(c, "makemessages --all -e py,html,js --no-wrap") manage(c, 'makemessages --all -e py,html,js --no-wrap')
manage(c, "compilemessages") manage(c, 'compilemessages')
if node_available(): if not no_frontend and node_available():
frontend_install(c) frontend_install(c)
frontend_trans(c) frontend_trans(c)
frontend_build(c) frontend_build(c)
# Update static files # Update static files
static(c) if not ignore_static:
static(c)
@task @task
def backup(c): def backup(c):
"""Backup the database and media files.""" """Backup the database and media files."""
print("Backing up InvenTree database...") print('Backing up InvenTree database...')
manage(c, "dbbackup --noinput --clean --compress") manage(c, 'dbbackup --noinput --clean --compress')
print("Backing up InvenTree media files...") print('Backing up InvenTree media files...')
manage(c, "mediabackup --noinput --clean --compress") manage(c, 'mediabackup --noinput --clean --compress')
@task @task
def restore(c): def restore(c):
"""Restore the database and media files.""" """Restore the database and media files."""
print("Restoring InvenTree database...") print('Restoring InvenTree database...')
manage(c, "dbrestore --noinput --uncompress") manage(c, 'dbrestore --noinput --uncompress')
print("Restoring InvenTree media files...") print('Restoring InvenTree media files...')
manage(c, "mediarestore --noinput --uncompress") manage(c, 'mediarestore --noinput --uncompress')
@task(post=[rebuild_models, rebuild_thumbnails]) @task(post=[rebuild_models, rebuild_thumbnails])
@ -330,16 +364,16 @@ def migrate(c):
This is a critical step if the database schema have been altered! This is a critical step if the database schema have been altered!
""" """
print("Running InvenTree database migrations...") print('Running InvenTree database migrations...')
print("========================================") print('========================================')
manage(c, "makemigrations") manage(c, 'makemigrations')
manage(c, "migrate --noinput") manage(c, 'migrate --noinput')
manage(c, "migrate --run-syncdb") manage(c, 'migrate --run-syncdb')
manage(c, "check") manage(c, 'check')
print("========================================") print('========================================')
print("InvenTree database migrations completed!") print('InvenTree database migrations completed!')
@task( @task(
@ -347,8 +381,8 @@ def migrate(c):
help={ help={
'skip_backup': 'Skip database backup step (advanced users)', 'skip_backup': 'Skip database backup step (advanced users)',
'frontend': 'Force frontend compilation/download step (ignores INVENTREE_DOCKER)', 'frontend': 'Force frontend compilation/download step (ignores INVENTREE_DOCKER)',
'no_frontend': 'Skip frontend compilation/download step' 'no_frontend': 'Skip frontend compilation/download step',
} },
) )
def update(c, skip_backup=False, frontend: bool = False, no_frontend: bool = False): def update(c, skip_backup=False, frontend: bool = False, no_frontend: bool = False):
"""Update InvenTree installation. """Update InvenTree installation.
@ -390,13 +424,27 @@ def update(c, skip_backup=False, frontend: bool = False, no_frontend: bool = Fal
# Data tasks # Data tasks
@task(help={ @task(
'filename': "Output filename (default = 'data.json')", help={
'overwrite': "Overwrite existing files without asking first (default = off/False)", 'filename': "Output filename (default = 'data.json')",
'include_permissions': "Include user and group permissions in the output file (filename) (default = off/False)", 'overwrite': 'Overwrite existing files without asking first (default = False)',
'delete_temp': "Delete temporary files (containing permissions) at end of run. Note that this will delete temporary files from previous runs as well. (default = off/False)" 'include_permissions': 'Include user and group permissions in the output file (default = False)',
}) 'include_tokens': 'Include API tokens in the output file (default = False)',
def export_records(c, filename='data.json', overwrite=False, include_permissions=False, delete_temp=False): 'exclude_plugins': 'Exclude plugin data from the output file (default = False)',
'include_sso': 'Include SSO token data in the output file (default = False)',
'retain_temp': 'Retain temporary files (containing permissions) at end of process (default = False)',
}
)
def export_records(
c,
filename='data.json',
overwrite=False,
include_permissions=False,
include_tokens=False,
exclude_plugins=False,
include_sso=False,
retain_temp=False,
):
"""Export all database records to a file. """Export all database records to a file.
Write data to the file defined by filename. Write data to the file defined by filename.
@ -422,44 +470,58 @@ def export_records(c, filename='data.json', overwrite=False, include_permissions
check_file_existance(filename, overwrite) check_file_existance(filename, overwrite)
tmpfile = f"{filename}.tmp" tmpfile = f'{filename}.tmp'
cmd = f"dumpdata --indent 2 --output '{tmpfile}' {content_excludes()}" excludes = content_excludes(
allow_tokens=include_tokens,
allow_plugins=not exclude_plugins,
allow_sso=include_sso,
)
cmd = f"dumpdata --natural-foreign --indent 2 --output '{tmpfile}' {excludes}"
# Dump data to temporary file # Dump data to temporary file
manage(c, cmd, pty=True) manage(c, cmd, pty=True)
print("Running data post-processing step...") print('Running data post-processing step...')
# Post-process the file, to remove any "permissions" specified for a user or group # Post-process the file, to remove any "permissions" specified for a user or group
with open(tmpfile, "r") as f_in: with open(tmpfile, 'r') as f_in:
data = json.loads(f_in.read()) data = json.loads(f_in.read())
if include_permissions is False: if include_permissions is False:
for entry in data: for entry in data:
if "model" in entry: if 'model' in entry:
# Clear out any permissions specified for a group # Clear out any permissions specified for a group
if entry["model"] == "auth.group": if entry['model'] == 'auth.group':
entry["fields"]["permissions"] = [] entry['fields']['permissions'] = []
# Clear out any permissions specified for a user # Clear out any permissions specified for a user
if entry["model"] == "auth.user": if entry['model'] == 'auth.user':
entry["fields"]["user_permissions"] = [] entry['fields']['user_permissions'] = []
# Write the processed data to file # Write the processed data to file
with open(filename, "w") as f_out: with open(filename, 'w') as f_out:
f_out.write(json.dumps(data, indent=2)) f_out.write(json.dumps(data, indent=2))
print("Data export completed") print('Data export completed')
if delete_temp is True: if not retain_temp:
print("Removing temporary file") print('Removing temporary files')
os.remove(tmpfile) os.remove(tmpfile)
@task(help={'filename': 'Input filename', 'clear': 'Clear existing data before import'}, post=[rebuild_models, rebuild_thumbnails]) @task(
def import_records(c, filename='data.json', clear=False): help={
'filename': 'Input filename',
'clear': 'Clear existing data before import',
'retain_temp': 'Retain temporary files at end of process (default = False)',
},
post=[rebuild_models, rebuild_thumbnails],
)
def import_records(
c, filename='data.json', clear: bool = False, retain_temp: bool = False
):
"""Import database records from a file.""" """Import database records from a file."""
# Get an absolute path to the supplied filename # Get an absolute path to the supplied filename
if not os.path.isabs(filename): if not os.path.isabs(filename):
@ -474,32 +536,69 @@ def import_records(c, filename='data.json', clear=False):
print(f"Importing database records from '{filename}'") print(f"Importing database records from '{filename}'")
# Pre-process the data, to remove any "permissions" specified for a user or group # We need to load 'auth' data (users / groups) *first*
tmpfile = f"{filename}.tmp.json" # This is due to the users.owner model, which has a ContentType foreign key
authfile = f'{filename}.auth.json'
with open(filename, "r") as f_in: # Pre-process the data, to remove any "permissions" specified for a user or group
data = json.loads(f_in.read()) datafile = f'{filename}.data.json'
with open(filename, 'r') as f_in:
try:
data = json.loads(f_in.read())
except json.JSONDecodeError as exc:
print(f'Error: Failed to decode JSON file: {exc}')
sys.exit(1)
auth_data = []
load_data = []
for entry in data: for entry in data:
if "model" in entry: if 'model' in entry:
# Clear out any permissions specified for a group # Clear out any permissions specified for a group
if entry["model"] == "auth.group": if entry['model'] == 'auth.group':
entry["fields"]["permissions"] = [] entry['fields']['permissions'] = []
# Clear out any permissions specified for a user # Clear out any permissions specified for a user
if entry["model"] == "auth.user": if entry['model'] == 'auth.user':
entry["fields"]["user_permissions"] = [] entry['fields']['user_permissions'] = []
# Save auth data for later
if entry['model'].startswith('auth.'):
auth_data.append(entry)
else:
load_data.append(entry)
else:
print('Warning: Invalid entry in data file')
print(entry)
# Write the auth file data
with open(authfile, 'w') as f_out:
f_out.write(json.dumps(auth_data, indent=2))
# Write the processed data to the tmp file # Write the processed data to the tmp file
with open(tmpfile, "w") as f_out: with open(datafile, 'w') as f_out:
f_out.write(json.dumps(data, indent=2)) f_out.write(json.dumps(load_data, indent=2))
cmd = f"loaddata '{tmpfile}' -i {content_excludes()}" excludes = content_excludes(allow_auth=False)
# Import auth models first
print('Importing user auth data...')
cmd = f"loaddata '{authfile}'"
manage(c, cmd, pty=True)
# Import everything else next
print('Importing database records...')
cmd = f"loaddata '{datafile}' -i {excludes}"
manage(c, cmd, pty=True) manage(c, cmd, pty=True)
print("Data import completed") if not retain_temp:
print('Removing temporary files')
os.remove(datafile)
os.remove(authfile)
print('Data import completed')
@task @task
@ -508,7 +607,7 @@ def delete_data(c, force=False):
Warning: This will REALLY delete all records in the database!! Warning: This will REALLY delete all records in the database!!
""" """
print("Deleting all data from InvenTree database...") print('Deleting all data from InvenTree database...')
if force: if force:
manage(c, 'flush --noinput') manage(c, 'flush --noinput')
@ -530,32 +629,26 @@ def import_fixtures(c):
fixtures = [ fixtures = [
# Build model # Build model
'build', 'build',
# Common models # Common models
'settings', 'settings',
# Company model # Company model
'company', 'company',
'price_breaks', 'price_breaks',
'supplier_part', 'supplier_part',
# Order model # Order model
'order', 'order',
# Part model # Part model
'bom', 'bom',
'category', 'category',
'params', 'params',
'part', 'part',
'test_templates', 'test_templates',
# Stock model # Stock model
'location', 'location',
'stock_tests', 'stock_tests',
'stock', 'stock',
# Users # Users
'users' 'users',
] ]
command = 'loaddata ' + ' '.join(fixtures) command = 'loaddata ' + ' '.join(fixtures)
@ -567,16 +660,16 @@ def import_fixtures(c):
@task @task
def wait(c): def wait(c):
"""Wait until the database connection is ready.""" """Wait until the database connection is ready."""
return manage(c, "wait_for_db") return manage(c, 'wait_for_db')
@task(pre=[wait], help={'address': 'Server address:port (default=127.0.0.1:8000)'}) @task(pre=[wait], help={'address': 'Server address:port (default=127.0.0.1:8000)'})
def server(c, address="127.0.0.1:8000"): def server(c, address='127.0.0.1:8000'):
"""Launch a (development) server using Django's in-built webserver. """Launch a (development) server using Django's in-built webserver.
Note: This is *not* sufficient for a production installation. Note: This is *not* sufficient for a production installation.
""" """
manage(c, "runserver {address}".format(address=address), pty=True) manage(c, 'runserver {address}'.format(address=address), pty=True)
@task(pre=[wait]) @task(pre=[wait])
@ -589,7 +682,7 @@ def worker(c):
@task @task
def render_js_files(c): def render_js_files(c):
"""Render templated javascript files (used for static testing).""" """Render templated javascript files (used for static testing)."""
manage(c, "test InvenTree.ci_render_js") manage(c, 'test InvenTree.ci_render_js')
@task(post=[translate_stats, static, server]) @task(post=[translate_stats, static, server])
@ -607,40 +700,44 @@ def test_translations(c):
django.setup() django.setup()
# Add language # Add language
print("Add dummy language...") print('Add dummy language...')
print("========================================") print('========================================')
manage(c, "makemessages -e py,html,js --no-wrap -l xx") manage(c, 'makemessages -e py,html,js --no-wrap -l xx')
# change translation # change translation
print("Fill in dummy translations...") print('Fill in dummy translations...')
print("========================================") print('========================================')
file_path = pathlib.Path(settings.LOCALE_PATHS[0], 'xx', 'LC_MESSAGES', 'django.po') file_path = pathlib.Path(settings.LOCALE_PATHS[0], 'xx', 'LC_MESSAGES', 'django.po')
new_file_path = str(file_path) + '_new' new_file_path = str(file_path) + '_new'
# compile regex # compile regex
reg = re.compile( reg = re.compile(
r"[a-zA-Z0-9]{1}" + # match any single letter and number # noqa: W504 r'[a-zA-Z0-9]{1}' + # match any single letter and number # noqa: W504
r"(?![^{\(\<]*[}\)\>])" + # that is not inside curly brackets, brackets or a tag # noqa: W504 r'(?![^{\(\<]*[}\)\>])' + # that is not inside curly brackets, brackets or a tag # noqa: W504
r"(?<![^\%][^\(][)][a-z])" + # that is not a specially formatted variable with singles # noqa: W504 r'(?<![^\%][^\(][)][a-z])' + # that is not a specially formatted variable with singles # noqa: W504
r"(?![^\\][\n])" # that is not a newline r'(?![^\\][\n])' # that is not a newline
) )
last_string = '' last_string = ''
# loop through input file lines # loop through input file lines
with open(file_path, "rt") as file_org: with open(file_path, 'rt') as file_org:
with open(new_file_path, "wt") as file_new: with open(new_file_path, 'wt') as file_new:
for line in file_org: for line in file_org:
if line.startswith('msgstr "'): if line.startswith('msgstr "'):
# write output -> replace regex matches with x in the read in (multi)string # write output -> replace regex matches with x in the read in (multi)string
file_new.write(f'msgstr "{reg.sub("x", last_string[7:-2])}"\n') file_new.write(f'msgstr "{reg.sub("x", last_string[7:-2])}"\n')
last_string = "" # reset (multi)string last_string = '' # reset (multi)string
elif line.startswith('msgid "'): elif line.startswith('msgid "'):
last_string = last_string + line # a new translatable string starts -> start append last_string = (
last_string + line
) # a new translatable string starts -> start append
file_new.write(line) file_new.write(line)
else: else:
if last_string: if last_string:
last_string = last_string + line # a string is being read in -> continue appending last_string = (
last_string + line
) # a string is being read in -> continue appending
file_new.write(line) file_new.write(line)
# change out translation files # change out translation files
@ -648,9 +745,9 @@ def test_translations(c):
new_file_path.rename(file_path) new_file_path.rename(file_path)
# compile languages # compile languages
print("Compile languages ...") print('Compile languages ...')
print("========================================") print('========================================')
manage(c, "compilemessages") manage(c, 'compilemessages')
# reset cwd # reset cwd
os.chdir(base_path) os.chdir(base_path)
@ -668,7 +765,9 @@ def test_translations(c):
'coverage': 'Run code coverage analysis (requires coverage package)', 'coverage': 'Run code coverage analysis (requires coverage package)',
} }
) )
def test(c, disable_pty=False, runtest='', migrations=False, report=False, coverage=False): def test(
c, disable_pty=False, runtest='', migrations=False, report=False, coverage=False
):
"""Run unit-tests for InvenTree codebase. """Run unit-tests for InvenTree codebase.
To run only certain test, use the argument --runtest. To run only certain test, use the argument --runtest.
@ -713,7 +812,7 @@ def test(c, disable_pty=False, runtest='', migrations=False, report=False, cover
@task(help={'dev': 'Set up development environment at the end'}) @task(help={'dev': 'Set up development environment at the end'})
def setup_test(c, ignore_update=False, dev=False, path="inventree-demo-dataset"): def setup_test(c, ignore_update=False, dev=False, path='inventree-demo-dataset'):
"""Setup a testing environment.""" """Setup a testing environment."""
from InvenTree.InvenTree.config import get_media_dir from InvenTree.InvenTree.config import get_media_dir
@ -722,41 +821,43 @@ def setup_test(c, ignore_update=False, dev=False, path="inventree-demo-dataset")
# Remove old data directory # Remove old data directory
if os.path.exists(path): if os.path.exists(path):
print("Removing old data ...") print('Removing old data ...')
c.run(f'rm {path} -r') c.run(f'rm {path} -r')
# Get test data # Get test data
print("Cloning demo dataset ...") print('Cloning demo dataset ...')
c.run(f'git clone https://github.com/inventree/demo-dataset {path} -v --depth=1') c.run(f'git clone https://github.com/inventree/demo-dataset {path} -v --depth=1')
print("========================================") print('========================================')
# Make sure migrations are done - might have just deleted sqlite database # Make sure migrations are done - might have just deleted sqlite database
if not ignore_update: if not ignore_update:
migrate(c) migrate(c)
# Load data # Load data
print("Loading database records ...") print('Loading database records ...')
import_records(c, filename=f'{path}/inventree_data.json', clear=True) import_records(c, filename=f'{path}/inventree_data.json', clear=True)
# Copy media files # Copy media files
print("Copying media files ...") print('Copying media files ...')
src = Path(path).joinpath('media').resolve() src = Path(path).joinpath('media').resolve()
dst = get_media_dir() dst = get_media_dir()
shutil.copytree(src, dst, dirs_exist_ok=True) shutil.copytree(src, dst, dirs_exist_ok=True)
print("Done setting up test environment...") print('Done setting up test environment...')
print("========================================") print('========================================')
# Set up development setup if flag is set # Set up development setup if flag is set
if dev: if dev:
setup_dev(c) setup_dev(c)
@task(help={ @task(
'filename': "Output filename (default = 'schema.yml')", help={
'overwrite': "Overwrite existing files without asking first (default = off/False)", 'filename': "Output filename (default = 'schema.yml')",
}) 'overwrite': 'Overwrite existing files without asking first (default = off/False)',
}
)
def schema(c, filename='schema.yml', overwrite=False): def schema(c, filename='schema.yml', overwrite=False):
"""Export current API schema.""" """Export current API schema."""
check_file_existance(filename, overwrite) check_file_existance(filename, overwrite)
@ -773,7 +874,8 @@ def version(c):
# Gather frontend version information # Gather frontend version information
_, node, yarn = node_available(versions=True) _, node, yarn = node_available(versions=True)
print(f""" print(
f"""
InvenTree - inventree.org InvenTree - inventree.org
The Open-Source Inventory Management System\n The Open-Source Inventory Management System\n
@ -792,13 +894,16 @@ Node {node if node else 'N/A'}
Yarn {yarn if yarn else 'N/A'} Yarn {yarn if yarn else 'N/A'}
Commit hash:{InvenTreeVersion.inventreeCommitHash()} Commit hash:{InvenTreeVersion.inventreeCommitHash()}
Commit date:{InvenTreeVersion.inventreeCommitDate()}""") Commit date:{InvenTreeVersion.inventreeCommitDate()}"""
)
if len(sys.argv) == 1 and sys.argv[0].startswith('/opt/inventree/env/lib/python'): if len(sys.argv) == 1 and sys.argv[0].startswith('/opt/inventree/env/lib/python'):
print(""" print(
"""
You are probably running the package installer / single-line installer. Please mentioned that in any bug reports! You are probably running the package installer / single-line installer. Please mentioned that in any bug reports!
Use '--list' for a list of available commands Use '--list' for a list of available commands
Use '--help' for help on a specific command""") Use '--help' for help on a specific command"""
)
@task() @task()
@ -826,8 +931,8 @@ def frontend_install(c):
Args: Args:
c: Context variable c: Context variable
""" """
print("Installing frontend dependencies") print('Installing frontend dependencies')
yarn(c, "yarn install") yarn(c, 'yarn install')
@task @task
@ -837,9 +942,9 @@ def frontend_trans(c):
Args: Args:
c: Context variable c: Context variable
""" """
print("Compiling frontend translations") print('Compiling frontend translations')
yarn(c, "yarn run extract") yarn(c, 'yarn run extract')
yarn(c, "yarn run compile") yarn(c, 'yarn run compile')
@task @task
@ -849,8 +954,8 @@ def frontend_build(c):
Args: Args:
c: Context variable c: Context variable
""" """
print("Building frontend") print('Building frontend')
yarn(c, "yarn run build --emptyOutDir") yarn(c, 'yarn run build --emptyOutDir')
@task @task
@ -860,19 +965,29 @@ def frontend_dev(c):
Args: Args:
c: Context variable c: Context variable
""" """
print("Starting frontend development server") print('Starting frontend development server')
yarn(c, "yarn run dev") yarn(c, 'yarn run dev')
@task(help={ @task(
'ref': "git ref, default: current git ref", help={
'tag': "git tag to look for release", 'ref': 'git ref, default: current git ref',
'file': "destination to frontend-build.zip file", 'tag': 'git tag to look for release',
'repo': "GitHub repository, default: InvenTree/inventree", 'file': 'destination to frontend-build.zip file',
'extract': "Also extract and place at the correct destination, default: True", 'repo': 'GitHub repository, default: InvenTree/inventree',
'clean': "Delete old files from InvenTree/web/static/web first, default: True", 'extract': 'Also extract and place at the correct destination, default: True',
}) 'clean': 'Delete old files from InvenTree/web/static/web first, default: True',
def frontend_download(c, ref=None, tag=None, file=None, repo="InvenTree/inventree", extract=True, clean=True): }
)
def frontend_download(
c,
ref=None,
tag=None,
file=None,
repo='InvenTree/inventree',
extract=True,
clean=True,
):
"""Download a pre-build frontend from GitHub if you dont want to install nodejs on your machine. """Download a pre-build frontend from GitHub if you dont want to install nodejs on your machine.
There are 3 possibilities to install the frontend: There are 3 possibilities to install the frontend:
@ -894,7 +1009,7 @@ def frontend_download(c, ref=None, tag=None, file=None, repo="InvenTree/inventre
import requests import requests
# globals # globals
default_headers = {"Accept": "application/vnd.github.v3+json"} default_headers = {'Accept': 'application/vnd.github.v3+json'}
# helper functions # helper functions
def find_resource(resource, key, value): def find_resource(resource, key, value):
@ -908,30 +1023,34 @@ def frontend_download(c, ref=None, tag=None, file=None, repo="InvenTree/inventre
if not extract: if not extract:
return return
dest_path = Path(__file__).parent / "InvenTree/web/static/web" dest_path = Path(__file__).parent / 'InvenTree/web/static/web'
# if clean, delete static/web directory # if clean, delete static/web directory
if clean: if clean:
shutil.rmtree(dest_path, ignore_errors=True) shutil.rmtree(dest_path, ignore_errors=True)
os.makedirs(dest_path) os.makedirs(dest_path)
print(f"Cleaned directory: {dest_path}") print(f'Cleaned directory: {dest_path}')
# unzip build to static folder # unzip build to static folder
with ZipFile(file, "r") as zip_ref: with ZipFile(file, 'r') as zip_ref:
zip_ref.extractall(dest_path) zip_ref.extractall(dest_path)
print(f"Unzipped downloaded frontend build to: {dest_path}") print(f'Unzipped downloaded frontend build to: {dest_path}')
def handle_download(url): def handle_download(url):
# download frontend-build.zip to temporary file # download frontend-build.zip to temporary file
with requests.get(url, headers=default_headers, stream=True, allow_redirects=True) as response, NamedTemporaryFile(suffix=".zip") as dst: with requests.get(
url, headers=default_headers, stream=True, allow_redirects=True
) as response, NamedTemporaryFile(suffix='.zip') as dst:
response.raise_for_status() response.raise_for_status()
# auto decode the gzipped raw data # auto decode the gzipped raw data
response.raw.read = functools.partial(response.raw.read, decode_content=True) response.raw.read = functools.partial(
with open(dst.name, "wb") as f: response.raw.read, decode_content=True
)
with open(dst.name, 'wb') as f:
shutil.copyfileobj(response.raw, f) shutil.copyfileobj(response.raw, f)
print(f"Downloaded frontend build to temporary file: {dst.name}") print(f'Downloaded frontend build to temporary file: {dst.name}')
handle_extract(dst.name) handle_extract(dst.name)
@ -942,51 +1061,72 @@ def frontend_download(c, ref=None, tag=None, file=None, repo="InvenTree/inventre
# check arguments # check arguments
if ref is not None and tag is not None: if ref is not None and tag is not None:
print("[ERROR] Do not set ref and tag.") print('[ERROR] Do not set ref and tag.')
return return
if ref is None and tag is None: if ref is None and tag is None:
try: try:
ref = subprocess.check_output(["git", "rev-parse", "HEAD"], encoding="utf-8").strip() ref = subprocess.check_output(
['git', 'rev-parse', 'HEAD'], encoding='utf-8'
).strip()
except Exception: except Exception:
print("[ERROR] Cannot get current ref via 'git rev-parse HEAD'") print("[ERROR] Cannot get current ref via 'git rev-parse HEAD'")
return return
if ref is None and tag is None: if ref is None and tag is None:
print("[ERROR] Either ref or tag needs to be set.") print('[ERROR] Either ref or tag needs to be set.')
if tag: if tag:
tag = tag.lstrip("v") tag = tag.lstrip('v')
try: try:
handle_download(f"https://github.com/{repo}/releases/download/{tag}/frontend-build.zip") handle_download(
f'https://github.com/{repo}/releases/download/{tag}/frontend-build.zip'
)
except Exception as e: except Exception as e:
if not isinstance(e, requests.HTTPError): if not isinstance(e, requests.HTTPError):
raise e raise e
print(f"""[ERROR] An Error occurred. Unable to download frontend build, release or build does not exist, print(
f"""[ERROR] An Error occurred. Unable to download frontend build, release or build does not exist,
try downloading the frontend-build.zip yourself via: https://github.com/{repo}/releases try downloading the frontend-build.zip yourself via: https://github.com/{repo}/releases
Then try continuing by running: invoke frontend-download --file <path-to-downloaded-zip-file>""") Then try continuing by running: invoke frontend-download --file <path-to-downloaded-zip-file>"""
)
return return
if ref: if ref:
# get workflow run from all workflow runs on that particular ref # get workflow run from all workflow runs on that particular ref
workflow_runs = requests.get(f"https://api.github.com/repos/{repo}/actions/runs?head_sha={ref}", headers=default_headers).json() workflow_runs = requests.get(
f'https://api.github.com/repos/{repo}/actions/runs?head_sha={ref}',
headers=default_headers,
).json()
if not (qc_run := find_resource(workflow_runs["workflow_runs"], "name", "QC")): if not (qc_run := find_resource(workflow_runs['workflow_runs'], 'name', 'QC')):
print("[ERROR] Cannot find any workflow runs for current sha") print('[ERROR] Cannot find any workflow runs for current sha')
return return
print(f"Found workflow {qc_run['name']} (run {qc_run['run_number']}-{qc_run['run_attempt']})") print(
f"Found workflow {qc_run['name']} (run {qc_run['run_number']}-{qc_run['run_attempt']})"
)
# get frontend-build artifact from all artifacts available for this workflow run # get frontend-build artifact from all artifacts available for this workflow run
artifacts = requests.get(qc_run["artifacts_url"], headers=default_headers).json() artifacts = requests.get(
if not (frontend_artifact := find_resource(artifacts["artifacts"], "name", "frontend-build")): qc_run['artifacts_url'], headers=default_headers
print("[ERROR] Cannot find frontend-build.zip attachment for current sha") ).json()
if not (
frontend_artifact := find_resource(
artifacts['artifacts'], 'name', 'frontend-build'
)
):
print('[ERROR] Cannot find frontend-build.zip attachment for current sha')
return return
print(f"Found artifact {frontend_artifact['name']} with id {frontend_artifact['id']} ({frontend_artifact['size_in_bytes']/1e6:.2f}MB).") print(
f"Found artifact {frontend_artifact['name']} with id {frontend_artifact['id']} ({frontend_artifact['size_in_bytes']/1e6:.2f}MB)."
)
print(f""" print(
f"""
GitHub doesn't allow artifact downloads from anonymous users. Either download the following file GitHub doesn't allow artifact downloads from anonymous users. Either download the following file
via your signed in browser, or consider using a point release download via invoke frontend-download --tag <git-tag> via your signed in browser, or consider using a point release download via invoke frontend-download --tag <git-tag>
Download: https://github.com/{repo}/suites/{qc_run['check_suite_id']}/artifacts/{frontend_artifact['id']} manually and Download: https://github.com/{repo}/suites/{qc_run['check_suite_id']}/artifacts/{frontend_artifact['id']} manually and
continue by running: invoke frontend-download --file <path-to-downloaded-zip-file>""") continue by running: invoke frontend-download --file <path-to-downloaded-zip-file>"""
)