def _processCommand( command, text, path, cache=True, tmpsuffix="tmp", tmpprefix="paml_", resolveData=None, allowEmpty=False, cwd=None): timestamp = has_changed = data = None cache, is_same, data, cache_key = cacheGet( text, path, cache) if (not is_same) or (not cache): if not path or os.path.isdir(path): temp_created = True fd, path = tempfile.mkstemp(suffix=tmpsuffix,prefix=tmpprefix) os.write(fd, engine.ensure_bytes(text)) os.close(fd) command = command[:-1] + [path] else: temp_created = False # FIXME: Honestly, I have so many problems with popen it's unbelievable. # I sometimes get sugar to freeze without any reason. I'm keeping the # following snipped for reference of what not to do. # --- # cmd = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE, # stderr=subprocess.PIPE, cwd=cwd) # data = cmd.stdout.read() # error = cmd.stderr.read() # print (" data", repr(data)) # print (" error", repr(error)) # print ("waiting...") # cmd.wait() # --- # Here the `shell` means single-line comman,d p = subprocess.Popen(" ".join(command), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, cwd=cwd) data, error = p.communicate() # DEBUG: # If we have a resolveData attribute, we use it to resolve/correct the # data if temp_created: os.unlink(path) if not data and resolveData: data = resolveData() if not data and not allowEmpty: raise Exception(error or u"No data processing `{0}`".format(u" ".join(command))) if not temp_created: # We don't cache temp files. Temp files are only created when # we don't have a path. if cache is SIG_CACHE: cache.set(path,cache_key,data) assert cache.has(path, cache_key) elif cache is MEMORY_CACHE: cache.set(cache_key,data) assert cache.has(cache_key) == data assert data is not None, "paml.web._processCommand: None returned by {0}".format(command) return engine.ensure_unicode(data)
def processSugar( text, path, request=None, cache=True, includeSource=False ): text = engine.ensure_unicode(text) if os.path.isdir(path or "."): parent_path = path or "." else: parent_path = os.path.dirname(os.path.abspath(path or ".")) sugar2 = None # try: # import sugar2 # except ImportError, e: # sugar2 = None # pass if sugar2: # If Sugar2 is available, we'll use it command = sugar2.SugarCommand("sugar2") arguments = [ "-cljs", "--cache", "--include-source" if includeSource else "" "-L" + parent_path, "-L" + os.path.join(parent_path, "lib", "sjs"), path ] return command.runAsString (arguments), "text/javascript" else: # We create a temp dir to cd to, because sugar's DParser # creates temp files in the current dir. temp_output = None if not path: temp_output = tempfile.mktemp() path = temp_output with open(temp_output, "w") as f: f.write(text.encode("utf-8")) temp_path = tempfile.mkdtemp() norm_path = lambda _:os.path.relpath(_, temp_path) if not os.path.exists(temp_path): os.mkdir(temp_path) # Otherwise we fallback to the regular Sugar, which has to be # run through popen (so it's slower) command = [ getCommands()["sugar"], "-cSljs" if includeSource else "-cljs", "-L" + norm_path(parent_path), "-L" + norm_path(os.path.join(parent_path, "lib", "sjs")), norm_path(path) ] res = _processCommand(command, text, path, cache, cwd=temp_path), "text/javascript" # We clean up the temp dir if os.path.exists(temp_path): os.rmdir(temp_path) if temp_output and os.path.exists(temp_output): os.unlink(temp_output) return res
def cacheGet( text, path, cache ): if cache: if path: cache = SIG_CACHE timestamp = SignatureCache.mtime(path) is_same, data = cache.get(path,timestamp) return cache, is_same, data, timestamp else: text = engine.ensure_unicode(text) sig = hashlib.sha256(bytes(u" ".join(command) + text)).hexdigest() cache = MEMORY_CACHE is_same = cache.has(sig) data = cache.get(sig) return cache, is_same, data, sig else: return cache, False, None, None