def make_function_from_file(self, path, file): fp = os.path.splitext(path) basename = fp[0].replace(" ", "_").replace("/", "_").replace("-", "_").replace(".", "_").replace("+", "_") ext = fp[1][1:] filetype = "" contents = "" if ext == "html": filetype = "page" elif ext == "css": filetype = "style" elif ext == "js": filetype = "script" file_contents = open(os.path.expanduser(file)).read() # minimize javascript, css files if ext == "js": file_contents = jspacker.jsmin(file_contents) self.compile_js(file_contents) elif ext == "css": packer = CSSPacker(file_contents) file_contents = packer.pack() data = str(file_contents).encode("hex") method = 'dataWithHexString(@"%s")' % data return {"method": method, "path": path}
def make_function_from_file(self,path,file): fp = os.path.splitext(path) basename = fp[0].replace(' ','_').replace('/','_').replace('-','_').replace('.','_').replace('+','_') ext = fp[1][1:] filetype = '' contents = '' if ext=='html': filetype = 'page' elif ext=='css': filetype = 'style' elif ext=='js': filetype = 'script' file_contents = open(os.path.expanduser(file)).read() # minimize javascript, css files if ext == 'js': file_contents = jspacker.jsmin(file_contents) self.compile_js(file_contents) elif ext == 'css': packer = CSSPacker(file_contents) file_contents = packer.pack() data = str(file_contents).encode("hex") method = "dataWithHexString(@\"%s\")" % data return {'method':method,'path':path}
def make_function_from_file(self,path,file): file_contents = open(os.path.expanduser(file)).read() if self.deploytype == 'production' or self.deploytype == 'commonjs': file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace('Titanium.','Ti.') self.compile_js(file_contents) # TODO: employ advanced encoding with titanium_prep if self.deploytype == 'commonjs': data = str(file_contents).encode("hex") method = "dataWithHexString(@\"%s\")" % data else: tfile = tempfile.NamedTemporaryFile(mode="r+b", delete=False) tfilename = tfile.name tfile.write(file_contents) tfile.close() template_dir = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename)) titanium_prep = os.path.abspath(os.path.join(template_dir,'titanium_prep')) data = os.popen("\"%s\" \"%s\" \"%s\"" % (titanium_prep, tfilename, self.appid)).read() os.remove(tfilename) data = data.translate(None, '\r\n') method = "@\"%s\"" % data return {'method':method,'path':path}
def make_function_from_file(self,file): fp = os.path.splitext(file) ext = fp[1][1:] thefile = os.path.expanduser(file) file_contents = open(thefile).read() save_off = False parse_modules = True # minimize javascript, css files if ext == 'js': file_contents = jspacker.jsmin(file_contents) save_off = True elif ext == 'css': packer = CSSPacker(file_contents) file_contents = packer.pack() save_off = True parse_modules = False if save_off: of = open(thefile,'w') of.write(file_contents) of.close() print "[DEBUG] compressing: %s" % thefile if parse_modules: # determine which modules this file is using self.extract_modules(file_contents)
def make_function_from_file(self, path, file): file_contents = open(os.path.expanduser(file)).read() if self.deploytype == 'production' or self.deploytype == 'commonjs': file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace('Titanium.', 'Ti.') self.compile_js(file_contents) # TODO: employ advanced encoding with titanium_prep if self.deploytype == 'commonjs': data = str(file_contents).encode("hex") method = "dataWithHexString(@\"%s\")" % data else: tfile = tempfile.NamedTemporaryFile(mode="r+b", delete=False) tfilename = tfile.name tfile.write(file_contents) tfile.close() template_dir = os.path.abspath( os.path.dirname(sys._getframe(0).f_code.co_filename)) titanium_prep = os.path.abspath( os.path.join(template_dir, 'titanium_prep')) data = os.popen("\"%s\" \"%s\" \"%s\"" % (titanium_prep, tfilename, self.appid)).read() os.remove(tfilename) data = data.translate(None, '\r\n') method = "@\"%s\"" % data return {'method': method, 'path': path}
def make_function_from_file(cls, path, file, instance=None): file_contents = open(os.path.expanduser(file)).read() file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace('Titanium.', 'Ti.') if instance: instance.compile_js(file_contents) data = str(file_contents).encode("hex") method = "dataWithHexString(@\"%s\")" % data return {'method': method, 'path': path}
def make_function_from_file(cls,path,file,instance=None): file_contents = open(os.path.expanduser(file)).read() file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace('Titanium.','Ti.') if instance: instance.compile_js(file_contents) data = str(file_contents).encode("hex") method = "dataWithHexString(@\"%s\")" % data return {'method':method,'path':path}
def load_api(self,file, api=""): file_contents = codecs.open(file, 'r', 'utf-8').read() if not self.debug and file.find('.js') != -1: return jspacker.jsmin(file_contents) elif file.find('.css') != -1: # need to replace urls to add directory prefix into path return re.sub(r'(url\s*\([\'"]?)', r'\1' + os.path.split(api)[0] + '/', file_contents) else: return file_contents
def make_function_from_file(cls, file, instance=None): f = os.path.expanduser(file) file_contents = codecs.open(f, 'r', 'utf-8').read() if not instance or not instance.debug: file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace('Titanium.', 'Ti.') if instance: file_contents = instance.compile_js(file_contents, f) return file_contents
def make_function_from_file(cls,file,instance=None): f = os.path.expanduser(file) file_contents = codecs.open(f, 'r', 'utf-8').read() if not instance or not instance.debug: file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace('Titanium.','Ti.') if instance: file_contents = instance.compile_js(file_contents, f) return file_contents
def compile_js_asset_file(self, path, file): file_contents = open(os.path.expanduser(file)).read() if self.deploytype == 'production' or self.deploytype == 'commonjs': file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace('Titanium.', 'Ti.') self.compile_js(file_contents) path = os.path.join(self.assets_dir, path) dir = os.path.dirname(path) if not os.path.exists(dir): os.makedirs(dir) tfile = open(path, 'w+') tfile.write(file_contents) tfile.close()
def compile_js_asset_file(self,path,file): file_contents = open(os.path.expanduser(file)).read() if self.deploytype == 'production' or self.deploytype == 'commonjs': file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace('Titanium.','Ti.') self.compile_js(file_contents) path = os.path.join(self.assets_dir,path) dir = os.path.dirname(path) if not os.path.exists(dir): os.makedirs(dir) tfile = open(path,'w+') tfile.write(file_contents) tfile.close()
def make_function_from_file(cls, path, file, instance): file_contents = open(os.path.expanduser(file)).read() file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace("Titanium.", "Ti.") instance.compile_js(file_contents) tfile = tempfile.NamedTemporaryFile(mode="r+b", delete=False) tfilename = tfile.name tfile.write(file_contents) tfile.close() template_dir = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename)) titanium_prep = os.path.abspath(os.path.join(template_dir, "titanium_prep")) data = os.popen('"%s" "%s" "%s"' % (titanium_prep, tfilename, instance.appid)).read() os.remove(tfilename) data = data.translate(None, "\r\n") method = '@"%s"' % data return {"method": method, "path": path}
def make_function_from_file(cls,path,file,instance): file_contents = open(os.path.expanduser(file)).read() if instance.deploytype == 'production': file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace('Titanium.','Ti.') instance.compile_js(file_contents) tfile = tempfile.NamedTemporaryFile(mode="r+b", delete=False) tfilename = tfile.name tfile.write(file_contents) tfile.close() template_dir = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename)) titanium_prep = os.path.abspath(os.path.join(template_dir,'titanium_prep')) data = os.popen("\"%s\" \"%s\" \"%s\"" % (titanium_prep, tfilename, instance.appid)).read() os.remove(tfilename) data = data.translate(None, '\r\n') method = "@\"%s\"" % data return {'method':method,'path':path}
def make_function_from_file(cls, path, file, instance): file_contents = open(os.path.expanduser(file)).read() if instance.deploytype == 'production': file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace('Titanium.', 'Ti.') instance.compile_js(file_contents) tfile = tempfile.NamedTemporaryFile(mode="r+b", delete=False) tfilename = tfile.name tfile.write(file_contents) tfile.close() template_dir = os.path.abspath( os.path.dirname(sys._getframe(0).f_code.co_filename)) titanium_prep = os.path.abspath( os.path.join(template_dir, 'titanium_prep')) data = os.popen("\"%s\" \"%s\" \"%s\"" % (titanium_prep, tfilename, instance.appid)).read() os.remove(tfilename) data = data.translate(None, '\r\n') method = "@\"%s\"" % data return {'method': method, 'path': path}
def extract_file_details(f): #print "Processing... %s" % f file_contents = open(f).read() file_contents = jspacker.jsmin(file_contents) extract_modules(file_contents,'Titanium') extract_modules(file_contents,'Ti')
def jspack(c): return jspacker.jsmin(c)
def jspack(c): return jspacker.jsmin(c) def csspack(c): return CSSPacker(c).pack()
def make_function_from_file(self,path,file): fp = os.path.splitext(path) basename = fp[0].replace(' ','_').replace('/','_').replace('-','_').replace('.','_').replace('+','_') ext = fp[1][1:] url = 'app://%s/%s' % (self.appid,path) filetype = '' contents = '' if ext=='html': filetype = 'page' elif ext=='css': filetype = 'style' elif ext=='js': filetype = 'script' methodname = "%sNamed%s%s" % (filetype,basename[0:1].upper(),basename[1:]) method_define = "- (NSData*) %s;" % methodname seed = random.randint(1,9) key = "%s%d%s" % (self.appid,seed,methodname) file_contents = open(os.path.expanduser(file)).read() _file_contents = file_contents # minimize javascript, css files if ext == 'js': file_contents = jspacker.jsmin(file_contents) elif ext == 'css': packer = CSSPacker(file_contents) file_contents = packer.pack() # determine which modules this file is using self.extract_modules(file_contents) if self.debug and ext == 'js': file_contents = """ try { %s } catch(__ex__) { if (typeof __ex__ == 'string') { var msg = __ex__ __ex__ = {line:3,sourceURL:'%s',message:msg}; } var _sur = __ex__.sourceURL; if (_sur) { _sur = _sur.substring(%d); } Titanium.API.reportUnhandledException(__ex__.line-3,_sur,__ex__.message); } """ % (_file_contents,url.encode("utf-8"),len('app://%s/'%self.appid)) if self.encrypt: out = subprocess.Popen([self.encryptor,file,key], stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0] data = str(out).strip() method = """ %s { NSString *k1 = @"%s"; int seed = %d; NSString *k2 = @"%s"; NSData *d = AES128DecryptWithKey(dataWithHexString(@"%s"), [NSString stringWithFormat:@"%%@%%d%%@",k1,seed,k2]); if ([d length] == 0) return nil; return decode64(d); } """ % (method_define,self.appid,seed,methodname,data) else: sys.stdout.flush() data = str(file_contents).encode("hex") method = """ %s { NSData *d = dataWithHexString(@"%s"); if ([d length] == 0) return nil; return d; } """ % (method_define,data) return {'name':methodname,'method':method,'define':method_define,'url':url,'path':path}
def add_compiled_resources(source, target): print "[DEBUG] copy resources from %s to %s" % (source, target) compiled_targets = {} for root, dirs, files in os.walk(source, True, None, True): for name in ignoreDirs: if name in dirs: dirs.remove(name) # don't visit ignored directories for file in files: if file in ignoreFiles: continue prefix = root[len(source):] from_ = to_unicode_or_not(os.path.join(root, file)) to_ = os.path.expanduser(from_.replace(source, target, 1)) to_directory = os.path.expanduser(os.path.split(to_)[0]) if not os.path.exists(to_directory): os.makedirs(to_directory) fp = os.path.splitext(file) ext = fp[1] if ext == '.jss': continue if len(fp) > 1 and ext in ['.html', '.js', '.css']: path = prefix + os.sep + file path = path[1:] entry = {'path': path, 'from': from_, 'to': to_} if compiled_targets.has_key(ext): compiled_targets[ext].append(entry) else: compiled_targets[ext] = [entry] if not write_routing: # only copy if different filesize or doesn't exist if not os.path.exists(to_) or os.path.getsize( from_) != os.path.getsize(to_): print "[DEBUG] copying: %s to %s" % (from_, to_) shutil.copyfile(from_, to_) if compiled_targets.has_key('.html'): compiled = self.process_html_files(compiled_targets, source) if len(compiled) > 0: for c in compiled: from_ = c['from'] to_ = c['to'] path = c['path'] print "[DEBUG] copying: %s to %s" % (from_, to_) file_contents = open(from_).read() file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace( 'Titanium.', 'Ti.') to = open(to_, 'w') to.write(file_contents) to.close() for ext in ('.css', '.html'): if compiled_targets.has_key(ext): for css_file in compiled_targets[ext]: from_ = css_file['from'] to_ = css_file['to'] print "[DEBUG] copying: %s to %s" % (from_, to_) if path.endswith('.css'): file_contents = open(from_).read() packer = CSSPacker(file_contents) file_contents = packer.pack() to = open(to_, 'w') to.write(file_contents) to.close() else: shutil.copyfile(from_, to_) if compiled_targets.has_key('.js'): for js_file in compiled_targets['.js']: path = js_file['path'] from_ = js_file['from'] compile_js_file(path, from_)
def add_compiled_resources(source,target): print "[DEBUG] copy resources from %s to %s" % (source,target) compiled_targets = {} for root, dirs, files in os.walk(source): for name in ignoreDirs: if name in dirs: dirs.remove(name) # don't visit ignored directories for file in files: if file in ignoreFiles: continue prefix = root[len(source):] from_ = os.path.join(root, file) to_ = os.path.expanduser(from_.replace(source, target, 1)) to_directory = os.path.expanduser(os.path.split(to_)[0]) if not os.path.exists(to_directory): os.makedirs(to_directory) fp = os.path.splitext(file) ext = fp[1] if ext == '.jss': continue if len(fp)>1 and write_routing and ext in ['.html','.js','.css']: path = prefix + os.sep + file path = path[1:] entry = {'path':path,'from':from_,'to':to_} if compiled_targets.has_key(ext): compiled_targets[ext].append(entry) else: compiled_targets[ext]=[entry] else: # only copy if different filesize or doesn't exist if not os.path.exists(to_) or os.path.getsize(from_)!=os.path.getsize(to_): print "[DEBUG] copying: %s to %s" % (from_,to_) shutil.copyfile(from_, to_) if compiled_targets.has_key('.html'): compiled = self.process_html_files(compiled_targets,source) if len(compiled) > 0: for c in compiled: from_ = c['from'] to_ = c['to'] path = c['path'] print "[DEBUG] copying: %s to %s" % (from_,to_) file_contents = open(from_).read() file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace('Titanium.','Ti.') to = open(to_,'w') to.write(file_contents) to.close() for ext in ('.css','.html'): if compiled_targets.has_key(ext): for css_file in compiled_targets[ext]: from_ = css_file['from'] to_ = css_file['to'] print "[DEBUG] copying: %s to %s" % (from_,to_) if path.endswith('.css'): file_contents = open(from_).read() packer = CSSPacker(file_contents) file_contents = packer.pack() to = open(to_,'w') to.write(file_contents) to.close() else: shutil.copyfile(from_, to_) if compiled_targets.has_key('.js'): for js_file in compiled_targets['.js']: path = js_file['path'] from_ = js_file['from'] to_ = js_file['to'] print "[DEBUG] compiling: %s" % from_ metadata = Compiler.make_function_from_file(path,from_,self) method = metadata['method'] eq = path.replace('.','_') impf.write(' [map setObject:%s forKey:@"%s"];\n' % (method,eq))
def add_compiled_resources(source, target): print "[DEBUG] copy resources from %s to %s" % (source, target) compiled_targets = {} for root, dirs, files in os.walk(source, True, None, True): for name in ignoreDirs: if name in dirs: dirs.remove(name) # don't visit ignored directories for file in files: if file in ignoreFiles: continue prefix = root[len(source) :] from_ = to_unicode_or_not(os.path.join(root, file)) to_ = os.path.expanduser(from_.replace(source, target, 1)) to_directory = os.path.expanduser(os.path.split(to_)[0]) if not os.path.exists(to_directory): os.makedirs(to_directory) fp = os.path.splitext(file) ext = fp[1] if ext == ".jss": continue if len(fp) > 1 and write_routing and ext in [".html", ".js", ".css"]: path = prefix + os.sep + file path = path[1:] entry = {"path": path, "from": from_, "to": to_} if compiled_targets.has_key(ext): compiled_targets[ext].append(entry) else: compiled_targets[ext] = [entry] else: # only copy if different filesize or doesn't exist if not os.path.exists(to_) or os.path.getsize(from_) != os.path.getsize(to_): print "[DEBUG] copying: %s to %s" % (from_, to_) shutil.copyfile(from_, to_) if compiled_targets.has_key(".html"): compiled = self.process_html_files(compiled_targets, source) if len(compiled) > 0: for c in compiled: from_ = c["from"] to_ = c["to"] path = c["path"] print "[DEBUG] copying: %s to %s" % (from_, to_) file_contents = open(from_).read() file_contents = jspacker.jsmin(file_contents) file_contents = file_contents.replace("Titanium.", "Ti.") to = open(to_, "w") to.write(file_contents) to.close() for ext in (".css", ".html"): if compiled_targets.has_key(ext): for css_file in compiled_targets[ext]: from_ = css_file["from"] to_ = css_file["to"] print "[DEBUG] copying: %s to %s" % (from_, to_) if path.endswith(".css"): file_contents = open(from_).read() packer = CSSPacker(file_contents) file_contents = packer.pack() to = open(to_, "w") to.write(file_contents) to.close() else: shutil.copyfile(from_, to_) if compiled_targets.has_key(".js"): for js_file in compiled_targets[".js"]: path = js_file["path"] from_ = js_file["from"] compile_js_file(path, from_)