From 384526010648684e1e210a531b83e09370ceb8fb Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Tue, 9 Jul 2024 01:51:52 +0200 Subject: [PATCH] Add cleanup task (#7581) * [FR] Add invoke task to remove compiled files Fixes #7559 * add optional clear step before install ensuring clean updates * add pre-install * Update preinstall.sh * Update functions.sh * Update preinstall.sh * add a generic run helper to ensure commands run from top directory * use generic run for other helpers --- .pkgr.yml | 1 + contrib/packager.io/preinstall.sh | 15 +++++++ tasks.py | 66 +++++++++++++++++++++---------- 3 files changed, 61 insertions(+), 21 deletions(-) create mode 100755 contrib/packager.io/preinstall.sh diff --git a/.pkgr.yml b/.pkgr.yml index 88f27034a5..4ddf0a86f5 100644 --- a/.pkgr.yml +++ b/.pkgr.yml @@ -14,6 +14,7 @@ env: - INVENTREE_BACKUP_DIR=/opt/inventree/backup - INVENTREE_PLUGIN_FILE=/opt/inventree/plugins.txt - INVENTREE_CONFIG_FILE=/opt/inventree/config.yaml +before_install: contrib/packager.io/preinstall.sh after_install: contrib/packager.io/postinstall.sh before: - contrib/packager.io/before.sh diff --git a/contrib/packager.io/preinstall.sh b/contrib/packager.io/preinstall.sh new file mode 100755 index 0000000000..ba9ebc0d5d --- /dev/null +++ b/contrib/packager.io/preinstall.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# +# packager.io preinstall script +# +PATH=${APP_HOME}/env/bin:${APP_HOME}/:/sbin:/bin:/usr/sbin:/usr/bin: + +# Envs that should be passed to setup commands +export SETUP_ENVS=PATH,APP_HOME,INVENTREE_MEDIA_ROOT,INVENTREE_STATIC_ROOT,INVENTREE_BACKUP_DIR,INVENTREE_PLUGINS_ENABLED,INVENTREE_PLUGIN_FILE,INVENTREE_CONFIG_FILE,INVENTREE_SECRET_KEY_FILE,INVENTREE_DB_ENGINE,INVENTREE_DB_NAME,INVENTREE_DB_USER,INVENTREE_DB_PASSWORD,INVENTREE_DB_HOST,INVENTREE_DB_PORT,INVENTREE_ADMIN_USER,INVENTREE_ADMIN_EMAIL,INVENTREE_ADMIN_PASSWORD,SETUP_NGINX_FILE,SETUP_ADMIN_PASSWORD_FILE,SETUP_NO_CALLS,SETUP_DEBUG,SETUP_EXTRA_PIP,SETUP_PYTHON + +if test -f "${APP_HOME}/env/bin/pip"; then + echo "# Clearing precompiled files" + sudo -u ${APP_USER} --preserve-env=$SETUP_ENVS bash -c "cd ${APP_HOME} && invoke clear-generated" +else + echo "# No python environment found - skipping" +fi diff --git a/tasks.py b/tasks.py index d5a502f935..7f2042aa04 100644 --- a/tasks.py +++ b/tasks.py @@ -136,6 +136,20 @@ def managePyPath(): return managePyDir().joinpath('manage.py') +def run(c, cmd, path: Path = None, pty=False, env=None): + """Runs a given command a given path. + + Args: + c: Command line context. + cmd: Command to run. + path: Path to run the command in. + pty (bool, optional): Run an interactive session. Defaults to False. + """ + env = env or {} + path = path or localDir() + c.run(f'cd "{path}" && {cmd}', pty=pty, env=env) + + def manage(c, cmd, pty: bool = False, env=None): """Runs a given command against django's "manage.py" script. @@ -145,24 +159,18 @@ def manage(c, cmd, pty: bool = False, env=None): pty (bool, optional): Run an interactive session. Defaults to False. env (dict, optional): Environment variables to pass to the command. Defaults to None. """ - env = env or {} - c.run( - 'cd "{path}" && python3 manage.py {cmd}'.format(path=managePyDir(), cmd=cmd), - pty=pty, - env=env, - ) + run(c, f'python3 manage.py {cmd}', managePyDir(), pty, env) -def yarn(c, cmd, pty: bool = False): +def yarn(c, cmd): """Runs a given command against the yarn package manager. Args: c: Command line context. cmd: Yarn command to run. - pty (bool, optional): Run an interactive session. Defaults to False. """ path = localDir().joinpath('src', 'frontend') - c.run(f'cd "{path}" && {cmd}', pty=pty) + run(c, cmd, path, False) def node_available(versions: bool = False, bypass_yarn: bool = False): @@ -230,10 +238,10 @@ def plugins(c, uv=False): # Install the plugins if not uv: - c.run(f"pip3 install --disable-pip-version-check -U -r '{plugin_file}'") + run(c, f"pip3 install --disable-pip-version-check -U -r '{plugin_file}'") else: c.run('pip3 install --no-cache-dir --disable-pip-version-check uv') - c.run(f"uv pip install -r '{plugin_file}'") + run(c, f"uv pip install -r '{plugin_file}'") # Collect plugin static files manage(c, 'collectplugins') @@ -254,22 +262,24 @@ def install(c, uv=False): c.run( 'pip3 install --no-cache-dir --disable-pip-version-check -U pip setuptools' ) - c.run( - f'pip3 install --no-cache-dir --disable-pip-version-check -U --require-hashes -r {INSTALL_FILE}' + run( + c, + f'pip3 install --no-cache-dir --disable-pip-version-check -U --require-hashes -r {INSTALL_FILE}', ) else: c.run( 'pip3 install --no-cache-dir --disable-pip-version-check -U uv setuptools' ) - c.run(f'uv pip install -U --require-hashes -r {INSTALL_FILE}') + run(c, f'uv pip install -U --require-hashes -r {INSTALL_FILE}') # Run plugins install plugins(c, uv=uv) # Compile license information lic_path = managePyDir().joinpath('InvenTree', 'licenses.txt') - c.run( - f'pip-licenses --format=json --with-license-file --no-license-path > {lic_path}' + run( + c, + f'pip-licenses --format=json --with-license-file --no-license-path > {lic_path}', ) @@ -279,14 +289,14 @@ def setup_dev(c, tests=False): print("Installing required python packages from 'src/backend/requirements-dev.txt'") # Install required Python packages with PIP - c.run('pip3 install -U --require-hashes -r src/backend/requirements-dev.txt') + run(c, 'pip3 install -U --require-hashes -r src/backend/requirements-dev.txt') # Install pre-commit hook print('Installing pre-commit for checks before git commits...') - c.run('pre-commit install') + run(c, 'pre-commit install') # Update all the hooks - c.run('pre-commit autoupdate') + run(c, 'pre-commit autoupdate') print('pre-commit set up is done...') # Set up test-data if flag is set @@ -1351,6 +1361,20 @@ def docs_server(c, address='localhost:8080', compile_schema=False): if compile_schema: # Build the schema docs first schema(c, ignore_warnings=True, overwrite=True, filename='docs/schema.yml') - c.run('python docs/extract_schema.py docs/schema.yml') + run(c, 'python docs/extract_schema.py docs/schema.yml') - c.run(f'mkdocs serve -a {address} -f docs/mkdocs.yml') + run(c, f'mkdocs serve -a {address} -f docs/mkdocs.yml') + + +@task +def clear_generated(c): + """Clear generated files from `inv update`.""" + # pyc/pyo files + run(c, 'find . -name "*.pyc" -exec rm -f {} +') + run(c, 'find . -name "*.pyo" -exec rm -f {} +') + # cache folders + run(c, 'find . -name "__pycache__" -exec rm -rf {} +') + + # Generated translations + run(c, 'find . -name "django.mo" -exec rm -f {} +') + run(c, 'find . -name "messages.mo" -exec rm -f {} +')