peterbe / django-static

Template tags for better serving static files from templates in Django
BSD 2-Clause "Simplified" License
193 stars 28 forks source link

Bug in copying of images in CSS #3

Closed saturdayplace closed 14 years ago

saturdayplace commented 14 years ago

Everything works like a charm, except that images listed in my CSS files aren't copied correctly. In my DJANGO_STATIC_SAVE_PREFIX directory, all the images are showing up as 1KB files, but they're corrupt. I tracked this down to line 509 of django_static.py - I don't think binary files can be copied in Python as you have currently. Below is a diff that fixed things for my machine.

diff

--- a/django_static.py  Fri May 07 11:23:34 2010 -0600
+++ b/django_static.py  Fri May 07 11:25:09 2010 -0600
@@ -3,6 +3,7 @@
 import re
 import sys
 import stat
+import shutil
 from glob import glob
 from collections import defaultdict
 from cStringIO import StringIO
@@ -506,7 +507,7 @@
     else:
         # straight copy
         #print "** STORING COPY:", new_filepath
-        open(new_filepath, 'w').write(open(filepath).read())
+        shutil.filecopy(filepath, new_filepath)

     return wrap_up(DJANGO_STATIC_NAME_PREFIX + new_filename)

Oh, and this probably only matters on Windows as I'm guessing these would be symlinked in Linux. I'm not sure if this diff is formatted correctly, as this is my first time. Let me know if I can help any futher.

peterbe commented 14 years ago

You're right. But it's shutil.copyfile not shutil.filecopy. All tests pass.

saturdayplace commented 14 years ago

Oops. I had another version with copyfile that was working. Sorry about that.