Commits

Daniel LaMotte committed e356889

haml scss: adding compilescss and compilehaml for deployment

Comments (0)

Files changed (4)

haml/management/__init__.py

Empty file added.

haml/management/commands/__init__.py

Empty file added.

haml/management/commands/compilehaml.py

+from django.conf import settings
+from django.core.management.base import CommandError, NoArgsCommand
+
+from haml.compiler import Compiler
+
+import errno
+import os
+
+# TODO enhance to work with app_directories.Loader?
+
+class Command(NoArgsCommand):
+    help = 'Compile all .haml files in TEMPLATE_DIRS to HAML_COMPILED'
+
+    def handle_noargs(self, **options):
+        try:
+            haml_path = settings.HAML_COMPILED
+        except AttributeError:
+            raise CommandError(
+                'You must specify a HAML_COMPILED path in settings'
+            )
+
+        for template_dir in settings.TEMPLATE_DIRS:
+            # trick to add proper trailing slash if necessary
+            l = len(os.path.join(template_dir + '/', ''))
+
+            for dirpath, dirnames, filenames in os.walk(template_dir):
+                for fn in filenames:
+                    if fn.lower().endswith('.haml'):
+                        print os.path.join(dirpath[l:], fn)
+
+                        newfn = os.path.join(haml_path, dirpath[l:], fn)
+                        target_dir = os.path.dirname(newfn)
+                        leftover = []
+                        while True:
+                            try:
+                                os.mkdir(target_dir)
+                            except OSError as e:
+                                if e.errno == errno.ENOENT:
+                                    leftover.insert(0, target_dir)
+                                    target_dir = os.path.dirname(target_dir)
+
+                                elif e.errno == errno.EEXIST:
+                                    break
+
+                                else:
+                                    raise
+                            else:
+                                break
+
+                        for d in leftover:
+                            os.mkdir(d)
+
+                        Compiler(
+                            open(os.path.join(dirpath, fn)),
+                            open(newfn, 'w')
+                        ).compile()

scss_integ/management/commands/compilescss.py

+from django.conf import settings
+from django.core.management.base import NoArgsCommand
+
+from scss import Scss
+
+import os
+
+class Command(NoArgsCommand):
+    help = 'Compile all .scss files in STATIC_ROOT after collectstatic'
+
+    def handle_noargs(self, **options):
+        for dirpath, dirnames, filenames in os.walk(settings.STATIC_ROOT):
+            for fn in filenames:
+                if fn.lower().endswith('.scss'):
+                    print os.path.join(dirpath, fn)
+                    newfn = fn[:-5] + '.css'
+                    open(os.path.join(dirpath, newfn), 'w').write(
+                        Scss().compile(
+                            open(os.path.join(dirpath, fn)).read()
+                        )
+                    )