def handle(self, *specs, **options): preprocess_only = options['preprocess_only'] js = [] css = [] if not specs: # Default to compressing every spec js.extend(js_spec.SPEC.keys()) css.extend(css_spec.SPEC.keys()) else: # Figure out what goes where for s in specs: if s in js_spec.SPEC: js.append(s) if s in css_spec.SPEC: css.append(s) # Remove any dupes js, css = list(uniquified(js)), list(uniquified(css)) # ...and any undesired keys unwanted = ('__extension__', ) for i in unwanted: for m in (js, css): try: m.remove(i) except ValueError: pass # Get the compressed media file for each spec (the compressor takes care # of glob expansion etc) js = [(k, js_compressor.compress_spec(js_spec.SPEC[k], preprocess_only=preprocess_only)) for k in js] css = [(k, css_compressor.compress_spec(css_spec.SPEC[k], preprocess_only=preprocess_only)) for k in css] js = { 'compressed': js, 'compressor': js_compressor, 'spec': js_spec.SPEC, } css = { 'compressed': css, 'compressor': css_compressor, 'spec': css_spec.SPEC, } for media in (js, css): for key, compressed in media['compressed']: # Figure out the location for each compressed file path = media['compressor'].get_compressed_path(media['spec'], key) path = os.path.join(ROOT_PATH, path) # If a file already exists here, delete it if os.path.exists(path): os.remove(path) # Write the compressed data to the file f = open(path, 'w') f.write(compressed) f.close()
def compress_spec(self, spec, **kwargs): """Compresses all files in the passed spec, returning the result.""" files = self.get_spec_files(spec=spec) result = [] for f in uniquified(files): result.append(self.compress_file(os.path.join(ROOT_PATH, f), **kwargs)) return '\n'.join(result)
def get_file_list(self, *keys): compiled = [] for key in keys: value = self.spec[key] compiled.append(self.process_patterns(*value)) compiled.append(key) compiled = uniquified(flattened(compiled)) commit = settings.VCS_COMMIT_IDENTIFIER return [u'%s/c-%s-%s.%s' % (self.extension, p, commit, self.extension) for p in compiled]
def handle(self, *specs, **options): js = [] css = [] if not specs: # Default to compressing every spec js = js_spec.SPEC.keys() css = css_spec.SPEC.keys() else: # Figure out what goes where for s in specs: if s in js_spec.SPEC: js.append(s) if s in css_spec.SPEC: css.append(s) # Remove any dupes js, css = list(uniquified(js)), list(uniquified(css)) # ...and any undesired keys unwanted = ('__extension__', ) for i in unwanted: for m in (js, css): try: m.remove(i) except ValueError: pass js_files = {} css_files = {} # Create a mapping from every file to the specs that it is a part of for spec in js: files = js_compressor.get_spec_files(js_spec.SPEC[spec], include_indirect=True) for f in files: f = os.path.join(settings.MEDIA_ROOT, f) assert os.path.exists(f), f if f in js_files: js_files[f].append(spec) else: js_files[f] = [spec, ] for spec in css: files = css_compressor.get_spec_files(css_spec.SPEC[spec], include_indirect=True) for f in files: f = os.path.join(settings.MEDIA_ROOT, f) assert os.path.exists(f), f if f in css_files: css_files[f].append(spec) else: css_files[f] = [spec, ] monitored_files = js_files monitored_files.update(css_files) if int(options.get('verbosity', 1)) > 1: print '! Monitoring:' for f in monitored_files: print ' %s' % f # Now we have a list of files to monitor and a mapping back to the specs # they belong to, we can start monitoring for changes. # Since I use VMWare as a dev environment, can't use inotify (HGFS # doesn't emit the events :-) # First, get the last modification times of all the files file_timestamps = {} for path in monitored_files: file_timestamps[path] = os.path.getmtime(path) # Now, loop and watch for changes try: print(':') while 1: modified_specs = set() for path, old_timestamp in file_timestamps.items(): new_timestamp = os.path.getmtime(path) if not new_timestamp == old_timestamp: print(' Modification of %s detected.' % path) for spec in monitored_files[path]: modified_specs.add(spec) file_timestamps[path] = new_timestamp if modified_specs: print(' -> Recompiling specs %s...' % (', '.join(modified_specs))) try: call_command('compressmedia', preprocess_only=True, *modified_specs) except CalledProcessError: print(' Error processing. Ignored.') else: print(' Done!') print(':') time.sleep(1.5) except KeyboardInterrupt: print('Bye.') sys.exit(0)
def get_file_list(self, *keys): patterns = flattened([self.spec[key] for key in keys]) compiled = self.process_patterns(*patterns) return uniquified(self.process_globs(compiled))
def process_patterns(self, *patterns): """Processes all of the specified patterns using process_pattern.""" return uniquified(flattened([self.process_pattern(s) for s in patterns]))