mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	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
This commit is contained in:
		| @@ -14,6 +14,7 @@ env: | |||||||
|   - INVENTREE_BACKUP_DIR=/opt/inventree/backup |   - INVENTREE_BACKUP_DIR=/opt/inventree/backup | ||||||
|   - INVENTREE_PLUGIN_FILE=/opt/inventree/plugins.txt |   - INVENTREE_PLUGIN_FILE=/opt/inventree/plugins.txt | ||||||
|   - INVENTREE_CONFIG_FILE=/opt/inventree/config.yaml |   - INVENTREE_CONFIG_FILE=/opt/inventree/config.yaml | ||||||
|  | before_install: contrib/packager.io/preinstall.sh | ||||||
| after_install: contrib/packager.io/postinstall.sh | after_install: contrib/packager.io/postinstall.sh | ||||||
| before: | before: | ||||||
|   - contrib/packager.io/before.sh |   - contrib/packager.io/before.sh | ||||||
|   | |||||||
							
								
								
									
										15
									
								
								contrib/packager.io/preinstall.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										15
									
								
								contrib/packager.io/preinstall.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -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 | ||||||
							
								
								
									
										66
									
								
								tasks.py
									
									
									
									
									
								
							
							
						
						
									
										66
									
								
								tasks.py
									
									
									
									
									
								
							| @@ -136,6 +136,20 @@ def managePyPath(): | |||||||
|     return managePyDir().joinpath('manage.py') |     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): | def manage(c, cmd, pty: bool = False, env=None): | ||||||
|     """Runs a given command against django's "manage.py" script. |     """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. |         pty (bool, optional): Run an interactive session. Defaults to False. | ||||||
|         env (dict, optional): Environment variables to pass to the command. Defaults to None. |         env (dict, optional): Environment variables to pass to the command. Defaults to None. | ||||||
|     """ |     """ | ||||||
|     env = env or {} |     run(c, f'python3 manage.py {cmd}', managePyDir(), pty, env) | ||||||
|     c.run( |  | ||||||
|         'cd "{path}" && python3 manage.py {cmd}'.format(path=managePyDir(), cmd=cmd), |  | ||||||
|         pty=pty, |  | ||||||
|         env=env, |  | ||||||
|     ) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def yarn(c, cmd, pty: bool = False): | def yarn(c, cmd): | ||||||
|     """Runs a given command against the yarn package manager. |     """Runs a given command against the yarn package manager. | ||||||
|  |  | ||||||
|     Args: |     Args: | ||||||
|         c: Command line context. |         c: Command line context. | ||||||
|         cmd: Yarn command to run. |         cmd: Yarn command to run. | ||||||
|         pty (bool, optional): Run an interactive session. Defaults to False. |  | ||||||
|     """ |     """ | ||||||
|     path = localDir().joinpath('src', 'frontend') |     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): | def node_available(versions: bool = False, bypass_yarn: bool = False): | ||||||
| @@ -230,10 +238,10 @@ def plugins(c, uv=False): | |||||||
|  |  | ||||||
|     # Install the plugins |     # Install the plugins | ||||||
|     if not uv: |     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: |     else: | ||||||
|         c.run('pip3 install --no-cache-dir --disable-pip-version-check uv') |         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 |     # Collect plugin static files | ||||||
|     manage(c, 'collectplugins') |     manage(c, 'collectplugins') | ||||||
| @@ -254,22 +262,24 @@ def install(c, uv=False): | |||||||
|         c.run( |         c.run( | ||||||
|             'pip3 install --no-cache-dir --disable-pip-version-check -U pip setuptools' |             'pip3 install --no-cache-dir --disable-pip-version-check -U pip setuptools' | ||||||
|         ) |         ) | ||||||
|         c.run( |         run( | ||||||
|             f'pip3 install --no-cache-dir --disable-pip-version-check -U --require-hashes -r {INSTALL_FILE}' |             c, | ||||||
|  |             f'pip3 install --no-cache-dir --disable-pip-version-check -U --require-hashes -r {INSTALL_FILE}', | ||||||
|         ) |         ) | ||||||
|     else: |     else: | ||||||
|         c.run( |         c.run( | ||||||
|             'pip3 install --no-cache-dir --disable-pip-version-check -U uv setuptools' |             '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 |     # Run plugins install | ||||||
|     plugins(c, uv=uv) |     plugins(c, uv=uv) | ||||||
|  |  | ||||||
|     # Compile license information |     # Compile license information | ||||||
|     lic_path = managePyDir().joinpath('InvenTree', 'licenses.txt') |     lic_path = managePyDir().joinpath('InvenTree', 'licenses.txt') | ||||||
|     c.run( |     run( | ||||||
|         f'pip-licenses --format=json --with-license-file --no-license-path > {lic_path}' |         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'") |     print("Installing required python packages from 'src/backend/requirements-dev.txt'") | ||||||
|  |  | ||||||
|     # Install required Python packages with PIP |     # 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 |     # 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') |     run(c, 'pre-commit install') | ||||||
|  |  | ||||||
|     # Update all the hooks |     # Update all the hooks | ||||||
|     c.run('pre-commit autoupdate') |     run(c, '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 | ||||||
| @@ -1351,6 +1361,20 @@ def docs_server(c, address='localhost:8080', compile_schema=False): | |||||||
|     if compile_schema: |     if compile_schema: | ||||||
|         # Build the schema docs first |         # Build the schema docs first | ||||||
|         schema(c, ignore_warnings=True, overwrite=True, filename='docs/schema.yml') |         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 {} +') | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user