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 os
import warnings import warnings
import tempfile
from django.conf import settings from django.conf import settings
from compress.filter_base import FilterBase from compress.filter_base import FilterBase
from compress.utils import write_tmpfile, read_tmpfile
BINARY = getattr(settings, 'CSSTIDY_BINARY', 'csstidy') BINARY = getattr(settings, 'CSSTIDY_BINARY', 'csstidy')
ARGUMENTS = getattr(settings, 'CSSTIDY_ARGUMENTS', '--template=highest') ARGUMENTS = getattr(settings, 'CSSTIDY_ARGUMENTS', '--template=highest')
...@@ -12,17 +13,20 @@ warnings.simplefilter('ignore', RuntimeWarning) ...@@ -12,17 +13,20 @@ warnings.simplefilter('ignore', RuntimeWarning)
class CSSTidyFilter(FilterBase): class CSSTidyFilter(FilterBase):
def filter_css(self, css): def filter_css(self, css):
tmp_filename = write_tmpfile(css) tmp_file = tempfile.NamedTemporaryFile(mode='w+b')
tmp_file.write(css)
output_filename = os.tmpnam() output_file = tempfile.NamedTemporaryFile(mode='w+b')
command = '%s %s %s %s' % (BINARY, tmp_filename, ARGUMENTS, output_filename) command = '%s %s %s %s' % (BINARY, tmp_file.name, ARGUMENTS, output_file.name)
output = os.popen(command).read() command_output = os.popen(command).read()
if self.verbose: filtered_css = output_file.read()
print output output_file.close()
tmp_file.close()
os.unlink(tmp_filename) if self.verbose:
print command_output
return read_tmpfile(output_filename) return filtered_css
\ No newline at end of file
import os import os
import re import re
import tempfile
from django.conf import settings as django_settings from django.conf import settings as django_settings
from django.utils.http import urlquote from django.utils.http import urlquote
...@@ -62,27 +63,6 @@ def media_root(filename): ...@@ -62,27 +63,6 @@ def media_root(filename):
def media_url(url): def media_url(url):
return django_settings.MEDIA_URL + urlquote(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=''): def concat(filenames, separator=''):
""" """
Concatenate the files from the list of the ``filenames``, ouput separated with ``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