瀏覽代碼

Fixed #28566 -- Added path matching to collectstatic ignore patterns.

Daniel Wiesmann 6 年之前
父節點
當前提交
8f75d21a2e

+ 1 - 1
django/contrib/staticfiles/management/commands/collectstatic.py

@@ -80,7 +80,7 @@ class Command(BaseCommand):
         ignore_patterns = options['ignore_patterns']
         if options['use_default_ignore_patterns']:
             ignore_patterns += apps.get_app_config('staticfiles').ignore_patterns
-        self.ignore_patterns = list(set(ignore_patterns))
+        self.ignore_patterns = list(set(os.path.normpath(p) for p in ignore_patterns))
         self.post_process = options['post_process']
 
     def collect(self):

+ 4 - 0
django/contrib/staticfiles/utils.py

@@ -22,10 +22,14 @@ def get_files(storage, ignore_patterns=None, location=''):
         ignore_patterns = []
     directories, files = storage.listdir(location)
     for fn in files:
+        # Match only the basename.
         if matches_patterns(fn, ignore_patterns):
             continue
         if location:
             fn = os.path.join(location, fn)
+            # Match the full file path.
+            if matches_patterns(fn, ignore_patterns):
+                continue
         yield fn
     for dir in directories:
         if matches_patterns(dir, ignore_patterns):

+ 7 - 2
docs/ref/contrib/staticfiles.txt

@@ -94,8 +94,13 @@ Some commonly used options are:
 
 .. django-admin-option:: --ignore PATTERN, -i PATTERN
 
-    Ignore files or directories matching this glob-style pattern. Use multiple
-    times to ignore more.
+    Ignore files, directories, or paths matching this glob-style pattern. Use
+    multiple times to ignore more. When specifying a path, always use forward
+    slashes, even on Windows.
+
+    .. versionchanged:: 2.2
+
+        Path matching was added.
 
 .. django-admin-option:: --dry-run, -n
 

+ 2 - 1
docs/releases/2.2.txt

@@ -105,7 +105,8 @@ Minor features
 :mod:`django.contrib.staticfiles`
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-* ...
+* Added path matching to the :option:`collectstatic --ignore` option so that
+  patterns like ``/vendor/*.js`` can be used.
 
 :mod:`django.contrib.syndication`
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ 1 - 1
tests/staticfiles_tests/apps/staticfiles_config.py

@@ -2,4 +2,4 @@ from django.contrib.staticfiles.apps import StaticFilesConfig
 
 
 class IgnorePatternsAppConfig(StaticFilesConfig):
-    ignore_patterns = ['*.css']
+    ignore_patterns = ['*.css', '*/vendor/*.js']

+ 0 - 0
tests/staticfiles_tests/apps/test/static/test/vendor/module.js


+ 3 - 2
tests/staticfiles_tests/test_management.py

@@ -319,11 +319,12 @@ class TestCollectionExcludeNoDefaultIgnore(TestDefaults, CollectionTestCase):
 class TestCollectionCustomIgnorePatterns(CollectionTestCase):
     def test_custom_ignore_patterns(self):
         """
-        A custom ignore_patterns list, ['*.css'] in this case, can be specified
-        in an AppConfig definition.
+        A custom ignore_patterns list, ['*.css', '*/vendor/*.js'] in this case,
+        can be specified in an AppConfig definition.
         """
         self.assertFileNotFound('test/nonascii.css')
         self.assertFileContains('test/.hidden', 'should be ignored')
+        self.assertFileNotFound(os.path.join('test', 'vendor', 'module.js'))
 
 
 class TestCollectionDryRun(TestNoFilesCreated, CollectionTestCase):