Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Divided the logic up, added comments, and a new test.

  • Loading branch information...
commit 7033f55326f327f11595ae748b07e9de3c2cde98 1 parent b892557
@jMyles jMyles authored
View
33 coldbrew/__init__.py
@@ -0,0 +1,33 @@
+from coldbrew.cache import get_cache_key, get_hexdigest, get_hashed_mtime
+import subprocess
+import shlex
+from django.conf import settings
+import os
+
+def get_string_from_path(source_file_path):
+ source_file = open(source_file_path)
+ coffeescript_string = source_file.read()
+ source_file.close()
+ return coffeescript_string
+
+def compile(coffeescript_string):
+
+ args = shlex.split("%s -c -s -p" % settings.COFFEESCRIPT_EXECUTABLE, posix=settings.POSIX_COMPATIBLE)
+ try:
+ p = subprocess.Popen(args, stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ except OSError:
+ raise RuntimeError('CoffeeScript Executable not found. Is it installed in your OS?')
+
+ out, errors = p.communicate(coffeescript_string.encode("utf-8"))
+
+ if errors:
+ logger.error(errors)
+
+ if settings.COLDBREW_FAIL_LOUD:
+ raise ColdBrewCompileError('Compiling %s \n\n %s' % (full_path, errors))
+ else:
+ return errors
+
+ if out:
+ return out.decode("utf-8")
View
98 coldbrew/templatetags/coldbrew.py
@@ -1,15 +1,12 @@
from ..cache import get_cache_key, get_hexdigest, get_hashed_mtime
-from ..settings import COFFEESCRIPT_EXECUTABLE, COFFEESCRIPT_USE_CACHE, \
- COFFEESCRIPT_CACHE_TIMEOUT, COFFEESCRIPT_OUTPUT_DIR, POSIX_COMPATIBLE, \
- COFFEESCRIPT_LOCATION
from ..exceptions import ColdBrewCompileError
+from .. import compile, get_string_from_path
from django.conf import settings
from django.core.cache import cache
from django.template.base import Library, Node
import logging
import os
-import shlex
-import subprocess
+
logger = logging.getLogger("coffeescript")
@@ -20,29 +17,17 @@ class InlineCoffeescriptNode(Node):
def __init__(self, nodelist):
self.nodelist = nodelist
-
- def compile(self, source):
- args = shlex.split("%s -c -s -p" % COFFEESCRIPT_EXECUTABLE, posix=POSIX_COMPATIBLE)
-
- p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
- out, errors = p.communicate(source.encode("utf-8"))
- if out:
- return out.decode("utf-8")
- elif errors:
- return errors.decode("utf-8")
-
- return u""
-
+
def render(self, context):
output = self.nodelist.render(context)
- if COFFEESCRIPT_USE_CACHE:
+ if settings.COFFEESCRIPT_USE_CACHE:
cache_key = get_cache_key(get_hexdigest(output))
cached = cache.get(cache_key, None)
if cached is not None:
return cached
- output = self.compile(output)
- cache.set(cache_key, output, COFFEESCRIPT_CACHE_TIMEOUT)
+ output = compile(output)
+ cache.set(cache_key, output, settings.COFFEESCRIPT_CACHE_TIMEOUT)
return output
else:
return self.compile(output)
@@ -56,53 +41,40 @@ def do_inlinecoffeescript(parser, token):
@register.simple_tag
-def coffeescript(path):
-
- full_path = os.path.join(COFFEESCRIPT_LOCATION, path)
- filename = os.path.split(path)[-1]
-
- output_directory = os.path.join(COFFEESCRIPT_LOCATION, COFFEESCRIPT_OUTPUT_DIR, os.path.dirname(path))
-
- hashed_mtime = get_hashed_mtime(full_path)
-
+def coffeescript(source_file_path):
+
+ filename = os.path.split(source_file_path)[-1]
if filename.endswith(".coffee"):
base_filename = filename[:-7]
else:
base_filename = filename
+ output_directory = os.path.join(settings.COFFEESCRIPT_LOCATION,
+ settings.COFFEESCRIPT_OUTPUT_DIR,
+ os.path.dirname(source_file_path))
+ full_path = os.path.join(settings.COFFEESCRIPT_LOCATION, source_file_path)
+ hashed_mtime = get_hashed_mtime(full_path)
output_path = os.path.join(output_directory, "%s-%s.js" % (base_filename, hashed_mtime))
- if not os.path.exists(output_path):
- source_file = open(full_path)
- source = source_file.read()
- source_file.close()
-
- args = shlex.split("%s -c -s -p" % COFFEESCRIPT_EXECUTABLE, posix=POSIX_COMPATIBLE)
- try:
- p = subprocess.Popen(args, stdin=subprocess.PIPE,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- except OSError:
- raise RuntimeError('CoffeeScript Executable not found. Is it installed in your OS?')
- out, errors = p.communicate(source)
- if out:
- if not os.path.exists(output_directory):
- os.makedirs(output_directory)
- compiled_file = open(output_path, "w+")
- compiled_file.write(out)
- compiled_file.close()
-
- # Remove old files
- compiled_filename = os.path.split(output_path)[-1]
- for filename in os.listdir(output_directory):
- if filename.startswith(base_filename) and filename != compiled_filename:
- os.remove(os.path.join(output_directory, filename))
- elif errors:
- logger.error(errors)
-
- if settings.COLDBREW_FAIL_LOUD:
- raise ColdBrewCompileError('Compiling %s \n\n %s' % (full_path, errors))
-
- return path
+ # If the file already exists, we're not going to even bother reading the input again.
+ if os.path.exists(output_path):
+ return output_path[len(settings.STATIC_ROOT):].replace(os.sep, '/').lstrip("/")
+
+
+ coffeescript_string = get_string_from_path(full_path)
+ compiled_javascript = compile(coffeescript_string)
+
+ if not os.path.exists(output_directory):
+ os.makedirs(output_directory)
+ compiled_file = open(output_path, "w+")
+ compiled_file.write(compiled_javascript)
+ compiled_file.close()
+
+ # Remove old files
+ compiled_filename = os.path.split(output_path)[-1]
+ for filename in os.listdir(output_directory):
+ if filename.startswith(base_filename) and filename != compiled_filename:
+ os.remove(os.path.join(output_directory, filename))
# If DEBUG is on, we want to see if a staticfiles directory is at the beginning
# of our output_path. If it is, we know to use that path instead of STATIC_ROOT.
@@ -111,5 +83,5 @@ def coffeescript(path):
if output_path.startswith(static_dir):
return output_path[len(static_dir):].replace(os.sep, '/').lstrip("/")
-
- return output_path[len(settings.STATIC_ROOT):].replace(os.sep, '/').lstrip("/")
+ relative_output_path = output_path[len(settings.STATIC_ROOT):].replace(os.sep, '/').lstrip("/")
+ return relative_output_path
View
1  coldbrew/tests/media/scripts/test-already-exists.coffee
@@ -0,0 +1 @@
+world = "hello"
View
12 coldbrew/tests/django_settings.py → coldbrew/tests/test_settings.py
@@ -2,13 +2,23 @@
import os
import sys
+
STATIC_ROOT = MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media')
+COFFEESCRIPT_LOCATION = STATIC_ROOT
+
INSTALLED_APPS = (
"coldbrew",
)
COFFEESCRIPT_MTIME_DELAY = 2
COFFEESCRIPT_OUTPUT_DIR = "COFFEESCRIPT_CACHE"
+DATABASES={
+ 'default': {
+ 'ENGINE': 'django.db.backends.sqlite3',
+ 'NAME':'notreal',
+ }
+ }
+
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
@@ -25,3 +35,5 @@
},
}
}
+
+from coldbrew.settings import *
View
48 coldbrew/tests/tests.py
@@ -1,6 +1,11 @@
-from unittest import TestCase
+from django.test import TestCase
+
+from django.test.utils import override_settings
+
from django.template.base import Template
from django.template.context import RequestContext
+from django.conf import settings
+
import os
import re
import time
@@ -11,11 +16,8 @@
class CoffeeScriptTestCase(TestCase):
def setUp(self):
- from django.conf import settings as django_settings
- self.django_settings = django_settings
-
- output_dir = os.path.join(self.django_settings.MEDIA_ROOT,
- self.django_settings.COFFEESCRIPT_OUTPUT_DIR)
+ output_dir = os.path.join(settings.MEDIA_ROOT,
+ settings.COFFEESCRIPT_OUTPUT_DIR)
# Remove the output directory if it exists to start from scratch
if os.path.exists(output_dir):
@@ -43,7 +45,7 @@ def test_external_coffeescript(self):
compiled_filename = template.render(RequestContext({})).strip()
self.assertTrue(bool(compiled_filename_re.match(compiled_filename)))
- compiled_path = os.path.join(self.django_settings.MEDIA_ROOT, compiled_filename)
+ compiled_path = os.path.join(settings.MEDIA_ROOT, compiled_filename)
compiled_content = open(compiled_path).read()
compiled = """(function() {\n
console.log("Hello, World!");\n
@@ -52,7 +54,7 @@ def test_external_coffeescript(self):
self.assertEquals(compiled_content, compiled)
# Change the modification time
- source_path = os.path.join(self.django_settings.MEDIA_ROOT, "scripts/test.coffee")
+ source_path = os.path.join(settings.MEDIA_ROOT, "scripts/test.coffee")
os.utime(source_path, None)
# The modification time is cached so the compiled file is not updated
@@ -61,7 +63,7 @@ def test_external_coffeescript(self):
self.assertEquals(compiled_filename, compiled_filename_2)
# Wait to invalidate the cached modification time
- time.sleep(self.django_settings.COFFEESCRIPT_MTIME_DELAY)
+ time.sleep(settings.COFFEESCRIPT_MTIME_DELAY)
# Now the file is re-compiled
compiled_filename_3 = template.render(RequestContext({})).strip()
@@ -70,6 +72,32 @@ def test_external_coffeescript(self):
# Check that we have only one compiled file, old files should be removed
- compiled_file_dir = os.path.dirname(os.path.join(self.django_settings.MEDIA_ROOT,
+ compiled_file_dir = os.path.dirname(os.path.join(settings.MEDIA_ROOT,
compiled_filename_3))
self.assertEquals(len(os.listdir(compiled_file_dir)), 1)
+
+ @override_settings(COFFEESCRIPT_OUTPUT_DIR="%s/compiled/" % settings.STATIC_ROOT)
+ def test_compiled_file_already_exists_file_is_not_written_again(self):
+ template = Template("""
+ {% load coldbrew %}
+ {% coffeescript "scripts/test-already-exists.coffee" %}
+ """)
+ # Render it once.
+ compiled_filename = template.render(RequestContext({})).strip()
+ first_access = os.path.getatime("%s/%s" % (settings.STATIC_ROOT, compiled_filename))
+ # ...and delete it.
+ os.remove("%s/%s" % (settings.STATIC_ROOT, compiled_filename))
+
+ # Now render it agian.
+ compiled_filename_again = template.render(RequestContext({})).strip()
+ second_access = os.path.getatime("%s/%s" % (settings.STATIC_ROOT, compiled_filename_again))
+
+ # The file will have been accessed again.
+ self.assertGreater(second_access, first_access)
+
+ # Render it a third time - this time the file will already exist.
+ compiled_filename_yet_again = template.render(RequestContext({})).strip()
+ third_access = os.path.getatime("%s/%s" % (settings.STATIC_ROOT, compiled_filename_yet_again))
+
+ # Since the file already existed, we won't have written again
+ self.assertEqual(third_access, second_access)
View
4 runtests.py
@@ -1,8 +1,10 @@
import os
+os.environ["DJANGO_SETTINGS_MODULE"] = "coldbrew.tests.test_settings"
+
from unittest import main
from coldbrew.tests.tests import CoffeeScriptTestCase
-os.environ["DJANGO_SETTINGS_MODULE"] = "coldbrew.tests.django_settings"
+
if __name__ == '__main__':
main()
Please sign in to comment.
Something went wrong with that request. Please try again.