Commit ff32f083 by andreas.pelme

Use tempfile instead of os.tmpnam. Patch from Remco Wendt. Thanks!


git-svn-id: https://django-compress.googlecode.com/svn/trunk@67 98d35234-f74b-0410-9e22-51d878bdf110
parent dceb9f2c
import os
import warnings
import tempfile
from django.conf import settings
from compress.filter_base import FilterBase
from compress.utils import write_tmpfile, read_tmpfile
BINARY = getattr(settings, 'CSSTIDY_BINARY', 'csstidy')
ARGUMENTS = getattr(settings, 'CSSTIDY_ARGUMENTS', '--template=highest')
......@@ -12,17 +13,20 @@ warnings.simplefilter('ignore', RuntimeWarning)
class CSSTidyFilter(FilterBase):
def filter_css(self, css):
tmp_filename = write_tmpfile(css)
output_filename = os.tmpnam()
command = '%s %s %s %s' % (BINARY, tmp_filename, ARGUMENTS, output_filename)
output = os.popen(command).read()
if self.verbose:
print output
tmp_file = tempfile.NamedTemporaryFile(mode='w+b')
tmp_file.write(css)
os.unlink(tmp_filename)
output_file = tempfile.NamedTemporaryFile(mode='w+b')
command = '%s %s %s %s' % (BINARY, tmp_file.name, ARGUMENTS, output_file.name)
command_output = os.popen(command).read()
filtered_css = output_file.read()
output_file.close()
tmp_file.close()
if self.verbose:
print command_output
return read_tmpfile(output_filename)
\ No newline at end of file
return filtered_css
import os
import re
import tempfile
from django.conf import settings as django_settings
from django.utils.http import urlquote
......@@ -62,27 +63,6 @@ def media_root(filename):
def media_url(url):
return django_settings.MEDIA_URL + urlquote(url)
def write_tmpfile(content):
try:
filename = os.tmpnam()
except RuntimeWarning:
pass
fd = open(filename, 'wb+')
fd.write(content)
fd.close()
return filename
def read_tmpfile(filename, delete=True):
fd = open(filename, 'rb')
r = fd.read()
fd.close()
if delete:
os.unlink(filename)
return r
def concat(filenames, separator=''):
"""
Concatenate the files from the list of the ``filenames``, ouput separated with ``separator``.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment