Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.3.X] Fixed #17737 -- Stopped the collectstatic management command …

…from copying the wrong file in repeated runs. Thanks, pigletto.

Backport from trunk (r17612).

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.3.X@17613 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 523d6167d6075eb3b9cb3aef71feb940ea0be5a3 1 parent dad3e55
Jannis Leidel authored March 01, 2012
10  django/contrib/staticfiles/management/commands/collectstatic.py
@@ -76,6 +76,7 @@ def handle_noargs(self, **options):
76 76
             if confirm != 'yes':
77 77
                 raise CommandError("Collecting static files cancelled.")
78 78
 
  79
+        processed_files = []
79 80
         for finder in finders.get_finders():
80 81
             for path, storage in finder.list(ignore_patterns):
81 82
                 # Prefix the relative path if the source storage contains it
@@ -83,10 +84,13 @@ def handle_noargs(self, **options):
83 84
                     prefixed_path = os.path.join(storage.prefix, path)
84 85
                 else:
85 86
                     prefixed_path = path
  87
+                if prefixed_path in processed_files:
  88
+                    continue
86 89
                 if symlink:
87 90
                     self.link_file(path, prefixed_path, storage, **options)
88 91
                 else:
89 92
                     self.copy_file(path, prefixed_path, storage, **options)
  93
+                processed_files.append(prefixed_path)
90 94
 
91 95
         actual_count = len(self.copied_files) + len(self.symlinked_files)
92 96
         unmodified_count = len(self.unmodified_files)
@@ -196,9 +200,7 @@ def copy_file(self, path, prefixed_path, source_storage, **options):
196 200
                     os.makedirs(os.path.dirname(full_path))
197 201
                 except OSError:
198 202
                     pass
199  
-                shutil.copy2(source_path, full_path)
200  
-            else:
201  
-                source_file = source_storage.open(path)
202  
-                self.storage.save(prefixed_path, source_file)
  203
+            source_file = source_storage.open(path)
  204
+            self.storage.save(prefixed_path, source_file)
203 205
         if not prefixed_path in self.copied_files:
204 206
             self.copied_files.append(prefixed_path)
52  tests/regressiontests/staticfiles_tests/tests.py
@@ -245,6 +245,58 @@ def test_no_common_ignore_patterns(self):
245 245
         self.assertFileContains('test/CVS', 'should be ignored')
246 246
 
247 247
 
  248
+class TestCollectionFilesOverride(BuildStaticTestCase):
  249
+    """
  250
+    Test overriding duplicated files by ``collectstatic`` management command.
  251
+    Check for proper handling of apps order in INSTALLED_APPS even if file
  252
+    modification dates are in different order:
  253
+
  254
+        'regressiontests.staticfiles_tests.apps.test',
  255
+        'regressiontests.staticfiles_tests.apps.no_label',
  256
+    """
  257
+    def setUp(self):
  258
+        self.orig_path = os.path.join(TEST_ROOT, 'apps', 'no_label', 'static', 'file2.txt')
  259
+        # get modification and access times for no_label/static/file2.txt
  260
+        self.orig_mtime = os.path.getmtime(self.orig_path)
  261
+        self.orig_atime = os.path.getatime(self.orig_path)
  262
+
  263
+        # prepare duplicate of file2.txt from no_label app
  264
+        # this file will have modification time older than no_label/static/file2.txt
  265
+        # anyway it should be taken to STATIC_ROOT because 'test' app is before
  266
+        # 'no_label' app in INSTALLED_APPS
  267
+        self.testfile_path = os.path.join(TEST_ROOT, 'apps', 'test', 'static', 'file2.txt')
  268
+        f = open(self.testfile_path, 'w+')
  269
+        f.write('duplicate of file2.txt')
  270
+        f.close()
  271
+        os.utime(self.testfile_path, (self.orig_atime - 1, self.orig_mtime - 1))
  272
+        super(TestCollectionFilesOverride, self).setUp()
  273
+
  274
+    def tearDown(self):
  275
+        if os.path.exists(self.testfile_path):
  276
+            os.unlink(self.testfile_path)
  277
+        # set back original modification time
  278
+        os.utime(self.orig_path, (self.orig_atime, self.orig_mtime))
  279
+
  280
+    def test_override(self):
  281
+        self.assertFileContains('file2.txt', 'duplicate of file2.txt')
  282
+
  283
+        # run collectstatic again
  284
+        self.run_collectstatic()
  285
+
  286
+        self.assertFileContains('file2.txt', 'duplicate of file2.txt')
  287
+
  288
+        # and now change modification time of no_label/static/file2.txt
  289
+        # test app is first in INSTALLED_APPS so file2.txt should remain unmodified
  290
+        mtime = os.path.getmtime(self.testfile_path)
  291
+        atime = os.path.getatime(self.testfile_path)
  292
+        os.utime(self.orig_path, (mtime + 1, atime + 1))
  293
+
  294
+        # run collectstatic again
  295
+        self.run_collectstatic()
  296
+
  297
+        self.assertFileContains('file2.txt', 'duplicate of file2.txt')
  298
+
  299
+
248 300
 class TestNoFilesCreated(object):
249 301
 
250 302
     def test_no_files_created(self):

0 notes on commit 523d616

Please sign in to comment.
Something went wrong with that request. Please try again.