def compress(self, phase): if phase=="js": #work only with js compressor phase for file_name in compressor.get("coffee"): full_path_name = search_media(file_name) mtime = os.stat(full_path_name).st_mtime key = zlib.adler32(full_path_name) & 0xffffffff cache_file_name = "coffee_%s_%s.%s" % (key, int(mtime), "js") full_cache_name = os.path.join(COMPRESSOR_CACHE_ROOT, cache_file_name) if not os.path.isfile(full_cache_name): print "MAKE NEW COFFEE COMPILATTION" #delete old cache pattern = re.compile(r"^coffee_%s_(\d+).js$" % key) #check and delete old cached files for f in os.listdir(COMPRESSOR_CACHE_ROOT): if pattern.match(f): os.unlink(os.path.join(COMPRESSOR_CACHE_ROOT, f)) #compile coffee script output = getoutput("coffee -p %s > %s" % (full_path_name, full_cache_name)) if output.strip("\n")!="": raise CoffeeScriptCompilationException(output) #check compressor.replace("coffee", file_name, "js", COMPRESSOR_CACHE_PREFIX +"/" + cache_file_name)
def copy_to_media(self, file): full_filename = search_media(file) if full_filename.startswith(settings.MEDIA_ROOT): return settings.MEDIA_URL + full_filename[len(settings.MEDIA_ROOT)+1:] new_filename = "%s%s" % (zlib.adler32(full_filename) & 0xffffffff, file[file.rfind("."):]) shutil.copyfile(full_filename, os.path.join(COMPRESSOR_CACHE_ROOT, new_filename)) return COMPRESSOR_CACHE_URL + new_filename
def make_cache(self, medi_type): #check cache compress_files = [] for file in compressor.get(medi_type): if file in COMPRESSOR_ONE_FILE_IGNORE: compressor.content_add( self.html( medi_type, self.copy_to_media( file))) else: compress_files.append(search_media(file)) max_mtime = max(map(lambda file: os.stat(file).st_mtime, compress_files)) key = zlib.adler32("_".join(compress_files)) & 0xffffffff name = "%s_%s.%s" % (key, int(max_mtime), medi_type) cache_name = os.path.join(COMPRESSOR_CACHE_ROOT, name) cache_url = COMPRESSOR_CACHE_URL + name if not os.path.isfile(cache_name): #make file pattern = re.compile(r"^%s_(\d+).%s$" % (key, medi_type)) #check and delete old cached CSS style for f in os.listdir(COMPRESSOR_CACHE_ROOT): if pattern.match(f): os.unlink(os.path.join(COMPRESSOR_CACHE_ROOT, f)) content=[] for file in compress_files: try: f = codecs.open(file, "r", "utf-8") if medi_type=="js": content.append(FILE_MARKER + file) content.append(f.read()) f.close() except IOError, e: raise Exception("Can't find '%s'\n%s" % (file, e)) # data = self.make_obfuscation(medi_type, "\n".join(content)) error_happend = False f = open(cache_name, "w") try: f.write(data) except Exception, e: error_happend = True