From d35df764cbbbcf1259e2e0c6165f030bae47ae09 Mon Sep 17 00:00:00 2001 From: Kyle McCormick Date: Tue, 18 Apr 2023 12:36:24 -0400 Subject: [PATCH] build: move collectstatic ignore patterns into configuration Django provides the `collectstatic` management command, which collects static assets into the STATIC_ROOT so that they can be served by some system external to Django (like nginx or caddy), as is usually desired in production environments. edx-platform contains several types of files that we don't want to be collected into the STATIC_ROOT. Previously, these files had to be supplied to the manageme nt command using the `--ignore` option: ./manage.py lms collectstatic --ignore geoip --ignore sass ...etc ./manage.py cms collectstatic --ignore geoip --ignore sass ...etc This yields a long, hard-to-remember command. Paver wrapped the command in its big `paver update_assets` task, but that task also builds assets, which is totally overkill when you're just trying to collect them. Fortunately, `collectstatic`'s default ignore patterns can be configured by defining a custom AppConfig class. We define such an config (`EdxPlatformStaticFilesConfig`) for LMS and CMS. Now, devs can collect LMS and CMS assets with just: ./manage.py lms collectstatic ./manage.py cms collectstatic Further reading on collecstatic and --ignore: https://docs.djangoproject.com/en/3.2/ref/contrib/staticfiles/#customizing-the-i gnored-pattern-list Further reading on eschewing Paver: https://github.com/openedx/edx-platform/blob/master/docs/decisions/0017-reimplem ent-asset-processing.rst Closes: https://github.com/openedx/edx-platform/issues/31658 --- cms/envs/common.py | 6 ++++- lms/envs/common.py | 5 ++++- openedx/core/lib/staticfiles_config.py | 31 ++++++++++++++++++++++++++ pavelib/assets.py | 23 +------------------ 4 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 openedx/core/lib/staticfiles_config.py diff --git a/cms/envs/common.py b/cms/envs/common.py index 6c7321718613..7593ec8468c5 100644 --- a/cms/envs/common.py +++ b/cms/envs/common.py @@ -1565,8 +1565,12 @@ 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', - 'django.contrib.staticfiles', + + # Tweaked version of django.contrib.staticfiles + 'openedx.core.lib.staticfiles_config.EdxPlatformStaticFilesConfig', + 'django_celery_results', + 'method_override', # Common Initialization diff --git a/lms/envs/common.py b/lms/envs/common.py index bac83c7860f6..fb11cfcf0ab4 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -2955,7 +2955,10 @@ def _make_locale_paths(settings): # pylint: disable=missing-function-docstring 'django.contrib.redirects', 'django.contrib.sessions', 'django.contrib.sites', - 'django.contrib.staticfiles', + + # Tweaked version of django.contrib.staticfiles + 'openedx.core.lib.staticfiles_config.EdxPlatformStaticFilesConfig', + 'django_celery_results', # Common Initialization diff --git a/openedx/core/lib/staticfiles_config.py b/openedx/core/lib/staticfiles_config.py new file mode 100644 index 000000000000..8168a09cc192 --- /dev/null +++ b/openedx/core/lib/staticfiles_config.py @@ -0,0 +1,31 @@ +""" +Configuration utilities for static assets. Shared by LMS and CMS. +""" +from django.contrib.staticfiles.apps import StaticFilesConfig + + +class EdxPlatformStaticFilesConfig(StaticFilesConfig): + """ + A thin wrapper around the standard django.contrib.staticfiles app + which adds the proper file & folder ignore patterns for edx-platform + static asset collection. + + This allows devs & operators to run: + ./manage.py [lms|cms] collectstatic + + instead of: + ./manage.py [lms|cms] collectstatic --ignore geoip --ignore sass ... etc. + """ + ignore_patterns = StaticFilesConfig.ignore_patterns + [ + + "geoip", # Geo-IP data, only accessed in Python + "sass", # We compile these out, don't need the source files in staticfiles + "xmodule_js", # Symlink for tests. + + # Karma test related files: + "fixtures", + "karma_*.js", + "spec", + "spec_helpers", + "spec-helpers", + ] diff --git a/pavelib/assets.py b/pavelib/assets.py index ad933de19596..003c76487585 100644 --- a/pavelib/assets.py +++ b/pavelib/assets.py @@ -692,30 +692,9 @@ def collect_assets(systems, settings, **kwargs): `settings` is the Django settings module to use. `**kwargs` include arguments for using a log directory for collectstatic output. Defaults to /dev/null. """ - ignore_patterns = [ - # Karma test related files... - "fixtures", - "karma_*.js", - "spec", - "spec_helpers", - "spec-helpers", - "xmodule_js", # symlink for tests - - # Geo-IP data, only accessed in Python - "geoip", - - # We compile these out, don't need the source files in staticfiles - "sass", - ] - - ignore_args = " ".join( - f'--ignore "{pattern}"' for pattern in ignore_patterns - ) - for sys in systems: collectstatic_stdout_str = _collect_assets_cmd(sys, **kwargs) - sh(django_cmd(sys, settings, "collectstatic {ignore_args} --noinput {logfile_str}".format( - ignore_args=ignore_args, + sh(django_cmd(sys, settings, "collectstatic --noinput {logfile_str}".format( logfile_str=collectstatic_stdout_str ))) print(f"\t\tFinished collecting {sys} assets.")