Commits

Jan-Jaap Driessen committed 0d6d14c

Added a fanstatic console script for listing libraries and compiling
the resources. Also supports `forced` compiling of resources.

  • Participants
  • Parent commits a91a4c5
  • Branches compilers-n-compressors

Comments (0)

Files changed (2)

   Base functionality of these compilers is shared in a baseclass. 
   Compilers do not know about Resources or Library, only about filenames.
 
+- Added a fanstatic console script for listing libraries and compiling
+  the resources. Also supports `forced` compiling of resources.
 
 0.11.1 (2011-04-13)
 ===================

File fanstatic/scripts.py

+import logging
+from operator import attrgetter
+from argparse import ArgumentParser
+
+from fanstatic import get_library_registry
+
+
+def compile(zest=None, libraries=None, force=False):
+    """
+    This console script compiles all known resources. 
+
+    Can be used as a zest.releaser entry point. 
+
+    http://pypi.python.org/pypi/zest.releaser/#entrypoints-documentation
+    """
+    if libraries is None:
+        # We are only interested in the libraries that have been installed in
+        # development mode.
+        libraries = get_library_registry().devmode()
+
+    for library in libraries:
+        to_compile = []
+        for resource in sorted(library.known_resources.values(), key=attrgetter('source')):
+            if resource.compiler and (force or resource.should_compile()):
+                to_compile.append(resource)
+
+        if not to_compile:
+            logging.info("nothing to compile in '%s'" % library.name)
+            continue
+        logging.info("compiling %s resource%s in '%s'" % (
+            len(to_compile), 's' if len(to_compile) > 1 else '', library.name))
+        for resource in to_compile:
+            logging.info("compiling %s ..." % resource.source)
+            resource.cook(force=force)
+            logging.info('...done')
+
+
+def main():
+    parser = ArgumentParser(description='fanstatic functionality')
+    parser.add_argument('--verbose', action='store_true', help='be more verbose')
+    parser.add_argument('--compile', action='store_true',
+        help='compile the resources in the libraries')
+    parser.add_argument('--force', action='store_true', 
+        help='force compilation of resources')
+    parser.add_argument('libraries', nargs='*',
+        help='one or more library names to process')
+
+    args = parser.parse_args()
+    logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO,
+                        format='%(message)s')
+
+    registry = get_library_registry()
+    if args.libraries:
+        libraries = []
+        for lib in args.libraries:
+            libraries.append(registry[lib])
+    else:
+        libraries = registry.values()
+
+    for library in libraries:
+        logging.info('''library '%s' @ %s contains:''' % (library.name, library.path))
+        for resource in sorted(library.known_resources.values(), key=attrgetter('source')):
+            logging.info('- %s' % resource.source)
+
+        if args.compile:
+            # Delegate to compile function.
+            compile(libraries=[library], force=args.force)
+
+