def handle(self, *args, **options): self.stdout.write("Bundling...\n") dev_mode = bool(options.get('dev')) _bundle_versions = {} set_bundle_versions(_bundle_versions) if options.get('parallel'): self.stdout.write("Writing bundles in parallel\n") pool = Pool() results = pool.map(do_make_bundle, [ (bundle, '_' if dev_mode else None) for bundle in get_bundles() ]) pool.close() pool.join() for bundle_name, hash_version in results: _bundle_versions[bundle_name] = hash_version else: for bundle in get_bundles(): self.stdout.write("Writing bundle: %s\n" % bundle.name) _, hash_version = do_make_bundle((bundle, '_' if dev_mode else None)) # Build bundle versions as we're going along in case they're used in templated bundles _bundle_versions[bundle.name] = hash_version self.stdout.write("\t%s\n" % bundle.get_version()) version_info = '\n'.join([' "%s": "%s",' % version for version in _bundle_versions.iteritems()]) with open(bundles_settings.BUNDLES_VERSION_FILE, 'wb') as bundles_versions: bundles_versions.write("""\ #!/usr/bin/env python BUNDLES_VERSIONS = { %s } """ % version_info) for single_file_input, single_file_output in bundles_settings.BUNDLES_SINGLE_FILES: self.stdout.write("Writing: %s\n" % single_file_output) file_type = os.path.splitext(single_file_input)[1][1:] processors = processor_library.get_default_preprocessors_for(file_type) + processor_library.get_default_postprocessors_for(file_type) with open(single_file_output, 'wb') as output_file: for chunk in processor_pipeline(processors, FileChunkGenerator(open(single_file_input, 'rb'))): output_file.write(chunk) self.stdout.write("Done.\n")
def handle(self, *args, **options): bundle_versions = get_bundle_versions() for bundle in get_bundles(): hash_version = bundle_versions[bundle.name] bundle_path = bundle.get_path(hash_version) self.stdout.write("Removing bundle: %s\n" % bundle_path) try: os.remove(bundle_path) except: self.stderr.write("Could not remove bundle: %s\n" % bundle_path) if bundle.uglify_command: try: if bundle.source_map_file_root and bundle.source_map_url_root: os.remove('%s.%s.%s.map' % (os.path.join(bundle.source_map_file_root, bundle.bundle_filename), hash_version, bundle.bundle_type)) else: os.remove('%s.map' % bundle_path) except: self.stderr.write("Could not remove bundle source map: %s.map\n" % bundle_path) self.stdout.write("Removing bundles version file: %s\n" % bundles_settings.BUNDLES_VERSION_FILE) os.remove(bundles_settings.BUNDLES_VERSION_FILE) for _, single_file_output in bundles_settings.BUNDLES_SINGLE_FILES: self.stdout.write("Removing: %s\n" % single_file_output) try: os.remove(single_file_output) except: self.stderr.write("Could not remove single file: %s\n" % single_file_output) self.stdout.write("Done.\n")
def _render_bundle(bundle_name, debug=False): """ Renders the HTML for a bundle in place - one HTML tag or many depending on settings.USE_BUNDLES """ try: bundle = get_bundles()[bundle_name] except KeyError: raise ImproperlyConfigured("Bundle '%s' is not defined" % bundle_name) if debug and not bundle.create_debug: raise ImproperlyConfigured("Bundle '%s' does not have a debug bundle" % bundle_name) if bundle.use_bundle: return _render_file( bundle.bundle_type, bundle.get_debug_url() if debug else bundle.get_url(), attrs=({ 'media': bundle.media } if bundle.media else {})) # Render files individually bundle_files = [] for bundle_file in bundle.files: bundle_files.append( _render_file(bundle_file.file_type, bundle_file.file_url, attrs=({ 'media': bundle_file.media } if bundle.media else {}))) return '\n'.join(bundle_files)
def get_file(path): global file_cache if not file_cache: for bundle in get_bundles(): for bundle_file in bundle.files: file_cache[os.path.realpath(bundle_file.file_path)] = { 'bundle_file': bundle_file, 'cache': None } if path in file_cache: if not file_cache[path]['cache']: mimetype, encoding = mimetypes.guess_type(path) mimetype = mimetype or 'application/octet-stream' # TODO: less files need to change the way they are rendered in the template print "Generating", path file_cache[path]['cache'] = { 'contents': reduce(concat, (chunk for chunk in processor_pipeline(file_cache[path]['bundle_file'].processors, FileChunkGenerator(open(file_cache[path]['bundle_file'].file_path, 'rb'))))), 'mimetype': mimetype, } return file_cache[path]['cache'] return None
def get_file(path): global file_cache if not file_cache: for bundle in get_bundles(): for bundle_file in bundle.files: file_cache[os.path.realpath(bundle_file.file_path)] = { 'bundle_file': bundle_file, 'cache': None } if path in file_cache: if not file_cache[path]['cache']: mimetype, encoding = mimetypes.guess_type(path) mimetype = mimetype or 'application/octet-stream' # TODO: less files need to change the way they are rendered in the template print "Generating", path file_cache[path]['cache'] = { 'contents': reduce(concat, (chunk for chunk in processor_pipeline(file_cache[path]['bundle_file'].processors, FileChunkGenerator(open(file_cache[path]['bundle_file'].file_path, 'rb'))))), 'mimetype': mimetype, } return file_cache[path]['cache'] return None
def get_context(self): """ Override this to change context """ return { 'settings': get_safe_settings(), 'bundles': get_bundles(), }
def get_context(self): """ Override this to change context """ return { 'settings': get_safe_settings(), 'bundles': get_bundles(), }
def handle(self, *args, **options): bundle_versions = get_bundle_versions() for bundle in get_bundles(): hash_version = bundle_versions[bundle.name] bundle_path = '%s.%s.%s' % (os.path.join( bundle.bundle_file_root, bundle.bundle_filename), hash_version, bundle.bundle_type) self.stdout.write("Removing bundle: %s\n" % bundle_path) try: os.remove(bundle_path) except: self.stderr.write("Could not remove bundle: %s\n" % bundle_path) if bundle.uglify_command: try: if bundle.source_map_file_root and bundle.source_map_url_root: os.remove('%s.%s.%s.map' % (os.path.join(bundle.source_map_file_root, bundle.bundle_filename), hash_version, bundle.bundle_type)) else: os.remove('%s.map' % bundle_path) except: self.stderr.write( "Could not remove bundle source map: %s.map\n" % bundle_path) if bundle.create_debug: debug_hash_version = bundle_versions['debug:' + bundle.name] bundle_debug_path = '%s.debug.%s.%s' % (os.path.join( bundle.bundle_file_root, bundle.bundle_filename), debug_hash_version, bundle.bundle_type) try: os.remove(bundle_debug_path) except: self.stderr.write("Could not remove debug bundle: %s\n" % bundle_debug_path) self.stdout.write("Removing bundles version file: %s\n" % bundles_settings.BUNDLES_VERSION_FILE) os.remove(bundles_settings.BUNDLES_VERSION_FILE) for _, single_file_output in bundles_settings.BUNDLES_SINGLE_FILES: self.stdout.write("Removing: %s\n" % single_file_output) try: os.remove(single_file_output) except: self.stderr.write("Could not remove single file: %s\n" % single_file_output) self.stdout.write("Done.\n")
def handle(self, target_directory, *args, **options): try: os.mkdir(target_directory) except OSError: pass for bundle in get_bundles(): manifest_filename = os.path.join(target_directory, bundle.name) + '.manifest' with open(manifest_filename, 'w') as manifest: manifest.write("\n".join(f.file_path for f in bundle.files))
def handle(self, *args, **options): self.stdout.write("Bundling...\n") dev_mode = bool(options.get('dev')) _bundle_versions = {} set_bundle_versions(_bundle_versions) for bundle in get_bundles(): self.stdout.write("Writing bundle: %s\n" % bundle.name) if bundle.uglify_command: hash_version = make_uglify_bundle(bundle, fixed_version='_' if dev_mode else None) else: hash_version = make_bundle(bundle, fixed_version='_' if dev_mode else None) # Build bundle versions as we're going along in case they're used in templated bundles _bundle_versions[bundle.name] = hash_version if bundle.create_debug: if bundle.uglify_command: _bundle_versions['debug:' + bundle.name] = make_uglify_bundle(bundle, debug=True) else: _bundle_versions['debug:' + bundle.name] = make_bundle(bundle, debug=True) self.stdout.write("\t%s\n" % bundle.get_version()) version_info = '\n'.join([' "%s": "%s",' % version for version in _bundle_versions.iteritems()]) with open(bundles_settings.BUNDLES_VERSION_FILE, 'wb') as bundles_versions: bundles_versions.write("""\ #!/usr/bin/env python BUNDLES_VERSIONS = { %s } """ % version_info) for single_file_input, single_file_output in bundles_settings.BUNDLES_SINGLE_FILES: self.stdout.write("Writing: %s\n" % single_file_output) file_type = os.path.splitext(single_file_input)[1][1:] processors = processor_library.get_default_preprocessors_for(file_type) + processor_library.get_default_postprocessors_for(file_type) with open(single_file_output, 'wb') as output_file: for chunk in processor_pipeline(processors, FileChunkGenerator(open(single_file_input, 'rb'))): output_file.write(chunk) self.stdout.write("Done.\n")
def _render_bundle(bundle_name): """ Renders the HTML for a bundle in place - one HTML tag or many depending on settings.USE_BUNDLES """ try: bundle = get_bundles()[bundle_name] except KeyError: raise ImproperlyConfigured("Bundle '%s' is not defined" % bundle_name) if bundle.use_bundle: return _render_file(bundle.bundle_type, bundle.get_url(), attrs=({'media':bundle.media} if bundle.media else {})) # Render files individually bundle_files = [] for bundle_file in bundle.files: if bundle_file.precompile_in_debug: bundle_files.append(_render_file(bundle_file.bundle_type, bundle_file.precompile_url, attrs=({'media':bundle_file.media} if bundle.media else {}))) else: bundle_files.append(_render_file(bundle_file.file_type, bundle_file.file_url, attrs=({'media':bundle_file.media} if bundle.media else {}))) return '\n'.join(bundle_files)
def handle(self, target_directory, *args, **options): try: os.mkdir(target_directory) except OSError: pass for bundle in get_bundles(): if options.get('bundle_type') and bundle.bundle_type != options.get('bundle_type'): continue manifest_filename = os.path.join(target_directory, bundle.name) + '.manifest' with open(manifest_filename, 'w') as manifest: for bundle_file in bundle.files: if bundle_file.processors: # The file has a preprocessor. This means in its current state it may not be a valid file # and thus not suitable for inclusion in the manifest. Do any appropriate preprocessing and # write out an appropriate version output_pipeline = processor_pipeline(bundle_file.processors, FileChunkGenerator(open(bundle_file.file_path, 'rb'))) output_file_name = os.path.realpath(os.path.join(target_directory, '%s-%s.%s' % (str(uuid.uuid4())[-8:], os.path.split(bundle_file.file_path)[1], bundle.bundle_type))) with open(output_file_name, 'wb') as output_file: for chunk in output_pipeline: output_file.write(chunk) manifest.write(output_file_name + "\n") else: manifest.write(bundle_file.file_path + "\n")
def handle(self, *args, **options): self.show_successes = not bool(options.get('failures_only')) watch = bool(options.get('watch')) file_pattern = options.get('pattern') if watch: try: import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler import curses except ImportError: raise CommandError( 'watchdog is required for this (pip install watchdog') self.errored_files = {} self.log_lines = [] watching = {} def check_and_lint_file(src): # TODO: don't repeatedly lint the same file for watchdir in watching: if watchdir in src: for bundle in watching[watchdir]: if src in bundle: result, error_message = self.lint_file( bundle.bundle_type, src, iter_input=processor_pipeline( bundle[src].processors, FileChunkGenerator(open(src, 'rb')))) self.log_watch_result( src, result, error_message=error_message) break class FileEventHandler(FileSystemEventHandler): def on_created(self, event): if not event.is_directory: check_and_lint_file(event.src_path) def on_modified(self, event): if not event.is_directory: check_and_lint_file(event.src_path) # TODO: watchdog dirsnapshot line 97 patched (otherwise it doesn't work with PyCharm) # #if stat_info.st_ino == ref_stat_info.st_ino and stat_info.st_mtime != ref_stat_info.st_mtime: # if stat_info.st_mtime != ref_stat_info.st_mtime: event_handler = FileEventHandler() observer = Observer() curses.setupterm() self.drawscreen() for bundle in get_bundles(): if bundle.files_root not in watching: watching[bundle.files_root] = set() observer.schedule(event_handler, path=bundle.files_root, recursive=True) watching[bundle.files_root].add(bundle) observer.start() try: while True: time.sleep(10) except KeyboardInterrupt: observer.stop() observer.join() return files_linted = set() failures = 0 def file_checked(success, error_message, file_path): files_linted.add(file_path) if success: if self.show_successes: self.stdout.write( self.style.HTTP_SUCCESS('OK\t\t%s\n' % file_path)) return 0 else: self.stdout.write( self.style.HTTP_SERVER_ERROR('FAIL\t\t%s\n' % file_path)) self.stdout.write(self.style.HTTP_SERVER_ERROR(error_message)) return 1 for bundle in get_bundles(): for bundle_file in bundle.files: if file_pattern and file_pattern not in bundle_file.file_path: continue if bundle_file.file_path in files_linted: continue # Check the file exists, even for non-linted files if not os.path.exists(bundle_file.file_path): self.stdout.write( self.style.HTTP_SERVER_ERROR('FAIL\t\t%s\n' % bundle_file.file_path)) self.stdout.write( self.style.HTTP_SERVER_ERROR( 'File does not exist (referenced from %s)\n' % bundle.name)) failures += 1 continue if not bundle_file.lint: continue success, error_message = self.lint_file( bundle.bundle_type, bundle_file.file_path, iter_input=processor_pipeline( bundle_file.processors, FileChunkGenerator(open(bundle_file.file_path, 'rb')))) failures += file_checked(success, error_message, bundle_file.file_path) for single_file_path, _ in bundles_settings.BUNDLES_SINGLE_FILES: success, error_message = self.lint_file( os.path.splitext(single_file_path)[1][1:], single_file_path) failures += file_checked(success, error_message, single_file_path) if failures: raise CommandError('%s FILE%s FAILED' % (failures, 'S' if failures > 1 else '')) else: self.stdout.write(self.style.HTTP_REDIRECT('\nALL FILES PASSED\n'))
def do_get_bundles(): """ Assigns the bundle definitions to a context variable """ return get_bundles()
def do_get_bundles(): """ Assigns the bundle definitions to a context variable """ return get_bundles()
def handle(self, *args, **options): try: import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler import curses except ImportError: raise CommandError('watchdog is required for this (pip install watchdog') self.errored_files = {} self.log_lines = [] watching = {} def check_and_lint_file(src): """ Called when a watched file changes """ full_path = os.path.realpath(src) for watchdir in watching: if watchdir in src: for bundle in watching[watchdir]: if full_path in bundle: self.precompile(full_path, bundle) self.lint_file(full_path, bundle) return # file could appear in multiple bundles class FileEventHandler(FileSystemEventHandler): def on_created(self, event): if not event.is_directory: check_and_lint_file(event.src_path) def on_modified(self, event): if not event.is_directory: check_and_lint_file(event.src_path) event_handler = FileEventHandler() observer = Observer() curses.setupterm() self.drawscreen() # First time we start, precompile everything initial_run = set() for bundle in get_bundles(): # Note: watchdog seems to only work with relative paths watch_path = os.path.relpath(bundle.files_root) if watch_path not in watching: watching[watch_path] = set() observer.schedule(event_handler, path=watch_path, recursive=True) watching[watch_path].add(bundle) for bundle_file in bundle.files: if bundle_file.file_path in initial_run: continue initial_run.add(bundle_file.file_path) self.precompile(bundle_file.file_path, bundle) observer.start() try: while True: time.sleep(10) except KeyboardInterrupt: observer.stop() observer.join()
def handle(self, *args, **options): show_successes = not bool(options.get('failures_only')) file_pattern = options.get('pattern') failures = 0 files_added = set() files_to_lint = [] for bundle in get_bundles(): for bundle_file in bundle.files: if file_pattern and file_pattern not in bundle_file.file_path: continue # Check the file exists, even for non-linted files if not os.path.exists(bundle_file.file_path): self.stdout.write(self.style.HTTP_SERVER_ERROR('FAIL\t\t%s\n' % bundle_file.file_path)) self.stdout.write(self.style.HTTP_SERVER_ERROR('File does not exist (referenced from %s)\n' % bundle.name)) failures += 1 continue if not bundle_file.lint or bundle_file.file_path in files_added: continue files_added.add(bundle_file.file_path) files_to_lint.append(( bundle.bundle_type, bundle_file.file_path, bundle_file.processors, )) def handle_result(success, error_message, file_path): if success: if show_successes: self.stdout.write(self.style.HTTP_SUCCESS('OK\t\t%s\n' % file_path)) return 0 else: self.stdout.write(self.style.HTTP_SERVER_ERROR('FAIL\t\t%s\n' % file_path)) self.stdout.write(self.style.HTTP_SERVER_ERROR(error_message)) return 1 if options.get('parallel'): pool = Pool() results = pool.map(do_lint_file, files_to_lint) pool.close() pool.join() for success, error_message, file_path in results: failures += handle_result(success, error_message, file_path) else: for bundle_type, file_path, processors in files_to_lint: success, error_message, _ = do_lint_file((bundle_type, file_path, processors)) failures += handle_result(success, error_message, file_path) for single_file_path, _ in bundles_settings.BUNDLES_SINGLE_FILES: success, error_message = lint_file(os.path.splitext(single_file_path)[1][1:], single_file_path) failures += handle_result(success, error_message, single_file_path) if failures: raise CommandError('%s FILE%s FAILED' % (failures, 'S' if failures > 1 else '')) else: self.stdout.write(self.style.HTTP_REDIRECT('\nALL FILES PASSED\n'))
def handle(self, *args, **options): self.show_successes = not bool(options.get('failures_only')) watch = bool(options.get('watch')) file_pattern = options.get('pattern') if watch: try: import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler import curses except ImportError: raise CommandError('watchdog is required for this (pip install watchdog') self.errored_files = {} self.log_lines = [] watching = {} def check_and_lint_file(src): # TODO: don't repeatedly lint the same file for watchdir in watching: if watchdir in src: for bundle in watching[watchdir]: if src in bundle: result, error_message = self.lint_file(bundle.bundle_type, src, iter_input=processor_pipeline(bundle[src].processors, FileChunkGenerator(open(src, 'rb')))) self.log_watch_result(src, result, error_message=error_message) break class FileEventHandler(FileSystemEventHandler): def on_created(self, event): if not event.is_directory: check_and_lint_file(event.src_path) def on_modified(self, event): if not event.is_directory: check_and_lint_file(event.src_path) # TODO: watchdog dirsnapshot line 97 patched (otherwise it doesn't work with PyCharm) # #if stat_info.st_ino == ref_stat_info.st_ino and stat_info.st_mtime != ref_stat_info.st_mtime: # if stat_info.st_mtime != ref_stat_info.st_mtime: event_handler = FileEventHandler() observer = Observer() curses.setupterm() self.drawscreen() for bundle in get_bundles(): if bundle.files_root not in watching: watching[bundle.files_root] = set() observer.schedule(event_handler, path=bundle.files_root, recursive=True) watching[bundle.files_root].add(bundle) observer.start() try: while True: time.sleep(10) except KeyboardInterrupt: observer.stop() observer.join() return files_linted = set() failures = 0 def file_checked(success, error_message, file_path): files_linted.add(file_path) if success: if self.show_successes: self.stdout.write(self.style.HTTP_SUCCESS('OK\t\t%s\n' % file_path)) return 0 else: self.stdout.write(self.style.HTTP_SERVER_ERROR('FAIL\t\t%s\n' % file_path)) self.stdout.write(self.style.HTTP_SERVER_ERROR(error_message)) return 1 for bundle in get_bundles(): for bundle_file in bundle.files: if file_pattern and file_pattern not in bundle_file.file_path: continue if bundle_file.file_path in files_linted: continue # Check the file exists, even for non-linted files if not os.path.exists(bundle_file.file_path): self.stdout.write(self.style.HTTP_SERVER_ERROR('FAIL\t\t%s\n' % bundle_file.file_path)) self.stdout.write(self.style.HTTP_SERVER_ERROR('File does not exist (referenced from %s)\n' % bundle.name)) failures += 1 continue if not bundle_file.lint: continue success, error_message = self.lint_file(bundle.bundle_type, bundle_file.file_path, iter_input=processor_pipeline(bundle_file.processors, FileChunkGenerator(open(bundle_file.file_path, 'rb')))) failures += file_checked(success, error_message, bundle_file.file_path) for single_file_path, _ in bundles_settings.BUNDLES_SINGLE_FILES: success, error_message = self.lint_file(os.path.splitext(single_file_path)[1][1:], single_file_path) failures += file_checked(success, error_message, single_file_path) if failures: raise CommandError('%s FILE%s FAILED' % (failures, 'S' if failures > 1 else '')) else: self.stdout.write(self.style.HTTP_REDIRECT('\nALL FILES PASSED\n'))