def _run_compressor(self, input_file, output_file): """Run the YUI compressor command on the input file in order to produce the output file""" yuicompressor_jar = yuicompressor.get_jar_filename() command_args = ['java', '-jar', yuicompressor_jar, '--type', self.minification_type, '-o' , output_file] # add additional compressor options for attr in self.compressor_options: value = getattr(self, attr, None) if value: option = '--' + attr.replace('_', '-') command_args.append(option) if attr not in self.boolean_options: command_args.append(str(value)) # add the input file command_args.append(input_file) process = subprocess.Popen(command_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output, _ = process.communicate() retcode = process.poll() if retcode != 0: cmd = ' '.join(command_args) msg = 'Cannot run command "%s": %s' % (cmd, output) raise DistutilsExecError(msg)
def compress(sources, output, files_type): """ Compress input files into single minimized output file. Args: sources: List of files to compress. output: Output filename. files_type: "css" or "js". """ handle, combined_filename = tempfile.mkstemp() os.close(handle) try: with open(combined_filename, mode='wb') as out: for input_file in sources: out.write(open(input_file, mode='rb').read()) yuicompressor_jar = yuicompressor.get_jar_filename() args = ['java', '-jar', yuicompressor_jar, '--type', files_type, '-o', output, combined_filename] process = subprocess.Popen(args=args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output, _ = process.communicate() retcode = process.poll() if retcode != 0: cmd = ' '.join(args) msg = 'Cannot run command "%s": %s' % (cmd, output) raise EnvironmentError(msg) finally: os.remove(combined_filename)
def copy_project(args): print("check environment......") print("check yuicompressor-2.4.8") try: import yuicompressor jar_path = yuicompressor.get_jar_filename() except ImportError: jar_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'yuicompressor-2.4.8.jar') if not os.path.isfile(jar_path): print('You need to install yuicompressor jar or python lib first.') sys.exit() print("yuicompressor is OK") print("check ng-annotate") command_min = ['npm', '-g', 'list', '|', 'grep', 'ng-annotate'] try: op = subprocess.Popen(command_min, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) temp_str = op.communicate()[0] if temp_str == '' or 'ng-annotate@' not in temp_str: print('You need to install npm and ng-annotate first') sys.exit() except: print('You need to install npm and ng-annotate first') sys.exit() print("ng-annotate is OK") print("start to duplicate project") if len(args) == 0: answer = raw_input('Do you want to deal with current path?(Y/N):') if answer.lower().startswith("y"): root = os.getcwd() clean_deployment = os.path.join(root, 'clean_deployment') else: sys.exit() elif len(args) == 1: if not os.path.isdir(args[0]): print('Please give a directory path.') sys.exit() else: root = args[0] clean_deployment = os.path.join(args[0], 'clean_deployment') else: print("You can't give tow or more args.") sys.exit() if os.path.isdir(clean_deployment): answer = raw_input('Do you want to recreate clean_deployment directory?(Y/N):') if answer.lower().startswith("y"): print('Remove old clean_deployment directory!') shutil.rmtree(clean_deployment) else: sys.exit() print('Copy project and create clean_deployment directory...') shutil.copytree(root, clean_deployment, ignore=shutil.ignore_patterns(*IGNORE_DIRECTORY)) print('finished copy.') return clean_deployment, jar_path
def main(args): # check ngmin try: command_args = ['ngmin', '--version'] op = subprocess.Popen(command_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd='/') output = op.communicate()[0] if command_args[0] in output: print("You need install ngmin before using.") sys.exit() else: print("ngmin version: %s." % output) except subprocess.CalledProcessError as e: print("Call Error: %s" % e.message) sys.exit() #check yuicompressor try: import yuicompressor jar_path = yuicompressor.get_jar_filename() except ImportError: jar_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'yuicompressor-2.4.8.jar') if not os.path.isfile(jar_path): print('You need yuicompressor jar or python lib.') sys.exit() print('System check over, start to deal with proejct......') if len(args) == 0: answer = raw_input('Do you want to deal with current path?(Y/N):') if answer.lower().startswith("y"): root = os.getcwd() clean_mobile = os.path.join(root, 'clean_mobile') else: sys.exit() elif len(args) == 1: if not os.path.isdir(args[0]): print('Please give a directory path.') sys.exit() else: root = args[0] clean_mobile = os.path.join(args[0], 'clean_mobile') else: print("You can't give tow or more args.") sys.exit() if os.path.isdir(clean_mobile): answer = raw_input('Do you want to recreate clean_mobile directory?(Y/N):') if answer.lower().startswith("y"): print('Remove old clean_mobile directory!') shutil.rmtree(clean_mobile) else: sys.exit() print('Copy project and create clean_mobile directory...') shutil.copytree(root, clean_mobile, ignore=shutil.ignore_patterns(*IGNORE_DIRECTORY)) print('finished copy.') return clean_mobile, jar_path
def test_many_pass_compress(self): import yuicompressor self.c.yuicompressor = yuicompressor.get_jar_filename() self.c.compresslevel = 1 self.c.run() assert(not os.path.isdir(TMP_DIR)) assert(os.path.isdir(OUT_DIR)) assert(os.path.isfile( '/'.join([ OUT_DIR, 'resources', 'tw2.forms', 'static', 'edit-undo.png' ]) ))
def _compress(self, format, input_files, output_file): import yuicompressor import tempfile handle, merged_filename = tempfile.mkstemp(prefix='minify') os.close(handle) try: self._merge_files(input_files, merged_filename) os.system('java -jar %s --type %s -o %s --charset utf-8 %s' % (yuicompressor.get_jar_filename(), format, output_file, merged_filename)) finally: os.remove(merged_filename)
def test_many_pass_compress(self): self.skipTest("Skipping until we don't require tw2.forms for testing.") self.skipTest("Skipping until we don't require yuicompressor for testing.") import yuicompressor self.c.yuicompressor = yuicompressor.get_jar_filename() self.c.compresslevel = 1 self.c.run() assert(not os.path.isdir(TMP_DIR)) assert(os.path.isdir(OUT_DIR)) assert(os.path.isfile( '/'.join([ OUT_DIR, 'resources', 'tw2.forms', 'static', 'edit-undo.png' ]) ))
def test_many_pass_compress(self): if HAS_SKIP: self.skipTest( "Skipping until we don't require tw2.forms for testing.") self.skipTest( "Skipping until we don't require yuicompressor for testing.") else: # Just pretend like we passed... :/ return import yuicompressor self.c.yuicompressor = yuicompressor.get_jar_filename() self.c.compresslevel = 1 self.c.run() assert (not os.path.isdir(TMP_DIR)) assert (os.path.isdir(OUT_DIR)) assert (os.path.isfile('/'.join( [OUT_DIR, 'resources', 'tw2.forms', 'static', 'edit-undo.png'])))
def setup(self): super(YUIBase, self).setup() try: self.jar = self.get_config('YUI_COMPRESSOR_PATH', what='YUI Compressor') except EnvironmentError: try: import yuicompressor self.jar = yuicompressor.get_jar_filename() except ImportError: raise EnvironmentError( "\nYUI Compressor jar can't be found." "\nPlease either install the yuicompressor package:" "\n\n pip install yuicompressor\n" "\nor provide a YUI_COMPRESSOR_PATH setting or an " "environment variable with the full path to the " "YUI compressor jar.")
def test_one_pass(self): if HAS_SKIP: self.skipTest("Skipping until we don't require tw2.forms for testing.") self.skipTest("Skipping until we don't require yuicompressor for testing.") else: # Just pretend like we passed... :/ return import yuicompressor self.c.yuicompressor = yuicompressor.get_jar_filename() self.c.compresslevel = 1 self.c.onepass = True self.c.run() assert(not os.path.isdir(TMP_DIR)) assert(os.path.isdir(OUT_DIR)) assert(os.path.isfile( '/'.join([ OUT_DIR, 'resources', 'tw2.forms', 'static', 'edit-undo.png' ]) ))
def setup(self): try: self.jar = self.get_config('YUI_COMPRESSOR_PATH', what='YUI Compressor') except EnvironmentError: try: import yuicompressor self.jar = yuicompressor.get_jar_filename() except ImportError: raise EnvironmentError( "\nYUI Compressor jar can't be found." "\nPlease either install the yuicompressor package:" "\n\n pip install yuicompressor\n" "\nor provide a YUI_COMPRESSOR_PATH setting or an " "environment variable with the full path to the " "YUI compressor jar." ) self.java_setup()
def install_html5(install_dir="www", minifier="uglifyjs", gzip=True, brotli=True, verbose=False): if minifier: print("minifying html5 client to '%s' using %s" % (install_dir, minifier)) else: print("copying html5 client to '%s'" % (install_dir, )) try: from xpra.src_info import REVISION, LOCAL_MODIFICATIONS except ImportError: try: from add_build_info import get_svn_props svn_props = get_svn_props(False) REVISION = int(svn_props.get("REVISION", 0)) LOCAL_MODIFICATIONS = int(svn_props.get("LOCAL_MODIFICATIONS", 0)) except (ImportError, ValueError): print("WARNING: source information is missing") print(" this build should not be used") REVISION = 0 LOCAL_MODIFICATIONS = 0 #those are used to replace the file we ship in source form #with one that is maintained by the distribution: symlinks = { "jquery.js" : [ "/usr/share/javascript/jquery/jquery.js", "/usr/share/javascript/jquery/latest/jquery.js", "/usr/share/javascript/jquery/3/jquery.js", ], "jquery-ui.js" : [ "/usr/share/javascript/jquery-ui/jquery-ui.js", "/usr/share/javascript/jquery-ui/latest/jquery-ui.js", "/usr/share/javascript/jquery-ui/3/jquery-ui.js", ], } for k,files in glob_recurse("html5").items(): if k!="": k = os.sep+k for fname in files: if fname.endswith(".tmp"): continue src = os.path.join(os.getcwd(), fname) parts = fname.split(os.path.sep) if parts[0]=="html5": fname = os.path.join(*parts[1:]) if install_dir==".": install_dir = os.getcwd() dst = os.path.join(install_dir, fname) if os.path.exists(dst): os.unlink(dst) #try to find an existing installed library and symlink it: symlink_options = symlinks.get(os.path.basename(fname), []) if install_symlink(symlink_options, dst): #we've created a symlink, skip minification and compression continue ddir = os.path.split(dst)[0] if ddir and not os.path.exists(ddir): os.makedirs(ddir, 0o755) ftype = os.path.splitext(fname)[1].lstrip(".") bname = os.path.basename(src) fsrc = src if ftype=="js" or fname.endswith("index.html"): #save to a temporary file after replacing strings: with open(src, mode='br') as f: odata = f.read().decode("latin1") data = odata if bname=="Utilities.js": print("adding revision info to %s" % (bname,)) if REVISION: data = data.replace('REVISION : "0",', 'REVISION : "%i",' % REVISION) if LOCAL_MODIFICATIONS: data = data.replace('LOCAL_MODIFICATIONS : "0",', 'LOCAL_MODIFICATIONS : "%i",' % LOCAL_MODIFICATIONS) for regexp, replacewith in { r"^\s*for\s*\(\s*let\s+" : "for(var ", r"^\s*let\s+" : "var ", r"^\s*for\s*\(\s*const\s+" : "for(var ", r"^\s*const\s+" : "var ", }.items(): p = re.compile(regexp) newdata = [] for line in data.splitlines(): newdata.append(p.sub(replacewith, line)) data = "\n".join(newdata) if data!=odata: fsrc = src+".tmp" with open(fsrc, "wb") as f: f.write(data.encode("latin1")) os.chmod(fsrc, 0o644) if minifier and ftype=="js": if minifier=="uglifyjs": minify_cmd = ["uglifyjs", fsrc, "-o", dst, "--compress", ] else: assert minifier=="yuicompressor" import yuicompressor #@UnresolvedImport jar = yuicompressor.get_jar_filename() java_cmd = os.environ.get("JAVA", "java") minify_cmd = [java_cmd, "-jar", jar, fsrc, "--nomunge", "--line-break", "400", "--type", ftype, "-o", dst, ] r = get_status_output(minify_cmd)[0] if r!=0: raise Exception("Error: failed to minify '%s', command %s returned error %i" % ( bname, minify_cmd, r)) os.chmod(dst, 0o644) print("minified %s" % (fname, )) else: print("copied %s" % (fname,)) shutil.copyfile(fsrc, dst) os.chmod(dst, 0o644) if fsrc!=src: os.unlink(fsrc) if ftype not in ("png", ): if gzip: gzip_dst = "%s.gz" % dst if os.path.exists(gzip_dst): os.unlink(gzip_dst) cmd = ["gzip", "-f", "-n", "-9", "-k", dst] get_status_output(cmd) if os.path.exists(gzip_dst): os.chmod(gzip_dst, 0o644) if brotli: br_dst = "%s.br" % dst if os.path.exists(br_dst): os.unlink(br_dst) #find brotli on $PATH paths = os.environ.get("PATH", "").split(os.pathsep) if os.name=="posix": #not always present, #but brotli is often installed there (install from source): paths.append("/usr/local/bin") for x in paths: br = os.path.join(x, "brotli") if sys.platform.startswith("win"): br += ".exe" if not os.path.exists(br): continue cmd = [br, "-k", dst] code, out, err = get_status_output(cmd) if code!=0: print("brotli error code=%i on %s" % (code, cmd)) if out: print("stdout=%s" % out) if err: print("stderr=%s" % err) elif os.path.exists(br_dst): os.chmod(br_dst, 0o644) break else: print("Warning: brotli did not create '%s'" % br_dst) if os.name=="posix": try: from xpra.platform.paths import get_desktop_background_paths except ImportError as e: print("cannot locate desktop background: %s" % (e,)) else: paths = get_desktop_background_paths() print("desktop background paths: %s" % (paths,)) if paths: extra_symlinks = {"background.png" : paths} for f, symlink_options in extra_symlinks.items(): dst = os.path.join(install_dir, f) install_symlink(symlink_options, dst)
def install_html5(install_dir="www", minifier="uglifyjs", gzip=True, brotli=True): if minifier not in ("", None, "copy"): print("minifying html5 client to '%s' using %s" % (install_dir, minifier)) else: print("copying html5 client to '%s'" % (install_dir, )) brotli_cmd = None brotli_version = None if brotli: #find brotli on $PATH paths = os.environ.get("PATH", "").split(os.pathsep) if os.name=="posix": #not always present, #but brotli is often installed there (install from source): paths.append("/usr/local/bin") for x in paths: br = os.path.join(x, "brotli") if sys.platform.startswith("win"): br += ".exe" if os.path.exists(br): proc = Popen([br, "--version"], stdout=PIPE, stderr=PIPE) stdout = proc.communicate()[0] if proc.wait()==0: brotli_version = stdout.strip(b"\n\r").decode() brotli_cmd = br break print("brotli_cmd=%s" % (brotli_cmd)) if brotli_version: print(" %s" % (brotli_version)) #those are used to replace the file we ship in source form #with one that is maintained by the distribution: symlinks = { "jquery.js" : [ "/usr/share/javascript/jquery/jquery.js", "/usr/share/javascript/jquery/latest/jquery.js", "/usr/share/javascript/jquery/3/jquery.js", ], "jquery-ui.js" : [ "/usr/share/javascript/jquery-ui/jquery-ui.js", "/usr/share/javascript/jquery-ui/latest/jquery-ui.js", "/usr/share/javascript/jquery-ui/3/jquery-ui.js", ], "materialicons-regular.ttf" : [ "/usr/share/fonts/truetype/material-design-icons-iconfont/MaterialIcons-Regular.ttf", ], "materialicons-regular.woff" : [ "/usr/share/fonts/woff/material-design-icons-iconfont/MaterialIcons-Regular.woff", ], "materialicons-regular.woff2" : [ "/usr/share/fonts/woff/material-design-icons-iconfont/MaterialIcons-Regular.woff2", ], } for k,files in glob_recurse("html5").items(): if k!="": k = os.sep+k for fname in files: if fname.endswith(".tmp"): continue src = os.path.join(os.getcwd(), fname) parts = fname.split(os.path.sep) if parts[0]=="html5": fname = os.path.join(*parts[1:]) if install_dir==".": install_dir = os.getcwd() dst = os.path.join(install_dir, fname) if os.path.exists(dst): os.unlink(dst) #try to find an existing installed library and symlink it: symlink_options = symlinks.get(os.path.basename(fname), []) if install_symlink(symlink_options, dst): #we've created a symlink, skip minification and compression continue ddir = os.path.split(dst)[0] if ddir and not os.path.exists(ddir): os.makedirs(ddir, 0o755) ftype = os.path.splitext(fname)[1].lstrip(".") bname = os.path.basename(src) fsrc = src if ftype=="js" or fname.endswith("index.html"): #save to a temporary file after replacing strings: with io.open(src, mode='r', encoding='utf8') as f: odata = f.read() data = odata for regexp, replacewith in { r"^\s*for\s*\(\s*let\s+" : "for(var ", r"^\s*let\s+" : "var ", r"^\s*for\s*\(\s*const\s+" : "for(var ", r"^\s*const\s+" : "var ", }.items(): p = re.compile(regexp) newdata = [] for line in data.splitlines(): newdata.append(p.sub(replacewith, line)) data = "\n".join(newdata) if data!=odata: fsrc = src+".tmp" with io.open(fsrc, "w", encoding='utf8') as f: f.write(data) os.chmod(fsrc, 0o644) if minifier not in ("", None, "copy") and ftype=="js": if minifier=="uglifyjs": minify_cmd = ["uglifyjs", fsrc, "-o", dst, "--compress", ] else: assert minifier=="yuicompressor" try: import yuicompressor # @UnresolvedImport jar = yuicompressor.get_jar_filename() java_cmd = os.environ.get("JAVA", "java") minify_cmd = [java_cmd, "-jar", jar] except Exception: minify_cmd = ["yuicompressor"] minify_cmd += [ fsrc, "--nomunge", "--line-break", "400", "--type", ftype, "-o", dst, ] r = get_status_output(minify_cmd)[0] if r!=0: print("Error: failed to minify '%s', command %s returned error %i" % ( bname, minify_cmd, r)) shutil.copyfile(fsrc, dst) os.chmod(dst, 0o644) print("minified %s" % (fname, )) else: print("copied %s" % (fname,)) shutil.copyfile(fsrc, dst) os.chmod(dst, 0o644) if fsrc!=src: os.unlink(fsrc) if ftype not in ("png", ): if gzip: gzip_dst = "%s.gz" % dst if os.path.exists(gzip_dst): os.unlink(gzip_dst) cmd = ["gzip", "-f", "-n", "-9", "-k", dst] get_status_output(cmd) if os.path.exists(gzip_dst): os.chmod(gzip_dst, 0o644) if brotli and brotli_cmd: br_dst = "%s.br" % dst if os.path.exists(br_dst): os.unlink(br_dst) if brotli_version and brotli_version>="1": cmd = [brotli_cmd, "-k", dst] else: cmd = [brotli_cmd, "--input", dst, "--output", br_dst] code, out, err = get_status_output(cmd) if code!=0: print("brotli error code=%i on %s" % (code, cmd)) if out: print("stdout=%s" % out) if err: print("stderr=%s" % err) elif os.path.exists(br_dst): os.chmod(br_dst, 0o644) else: print("Warning: brotli did not create '%s'" % br_dst) if os.name=="posix": paths = [ "/usr/share/backgrounds/images/default.png", "/usr/share/backgrounds/images/*default*.png", "/usr/share/backgrounds/*default*png", "/usr/share/backgrounds/gnome/adwaita*.jpg", #Debian Stretch "/usr/share/backgrounds/images/*jpg", #CentOS 7 ] if paths: extra_symlinks = {"background.png" : paths} for f, symlink_options in extra_symlinks.items(): dst = os.path.join(install_dir, f) if install_symlink(symlink_options, dst): break
def install_html5(install_dir="www", minifier="uglifyjs", gzip=True, brotli=True, verbose=False, extra_symlinks={}): if minifier: print("minifying html5 client to '%s' using %s" % (install_dir, minifier)) else: print("copying html5 client to '%s'" % (install_dir, )) #those are used to replace the file we ship in source form #with one that is maintained by the distribution: symlinks = { "jquery.js" : [ "/usr/share/javascript/jquery/jquery.js", "/usr/share/javascript/jquery/3/jquery.js", ], "jquery-ui.js" : [ "/usr/share/javascript/jquery-ui/jquery-ui.js", "/usr/share/javascript/jquery-ui/3/jquery-ui.js", ], } for k,files in glob_recurse("html5").items(): if (k!=""): k = os.sep+k for f in files: src = os.path.join(os.getcwd(), f) parts = f.split(os.path.sep) if parts[-1] in ("AUTHORS", "LICENSE"): continue if parts[0]=="html5": f = os.path.join(*parts[1:]) if install_dir==".": install_dir = os.getcwd() dst = os.path.join(install_dir, f) if os.path.exists(dst): os.unlink(dst) #try to find an existing installed library and symlink it: symlink_options = symlinks.get(os.path.basename(f), []) if install_symlink(symlink_options, dst): #we've created a symlink, skip minification and compression continue ddir = os.path.split(dst)[0] if ddir and not os.path.exists(ddir): os.makedirs(ddir, 0o755) ftype = os.path.splitext(f)[1].lstrip(".") if minifier and ftype=="js": if minifier=="uglifyjs": minify_cmd = ["uglifyjs", "--screw-ie8", src, "-o", dst, "--compress", ] else: assert minifier=="yuicompressor" import yuicompressor #@UnresolvedImport jar = yuicompressor.get_jar_filename() java_cmd = os.environ.get("JAVA", "java") minify_cmd = [java_cmd, "-jar", jar, src, "--nomunge", "--line-break", "400", "--type", ftype, "-o", dst, ] r = get_status_output(minify_cmd)[0] if r!=0: print("Error: failed to minify '%s', command returned error %i" % (f, r)) if verbose: print(" command: %s" % (minify_cmd,)) else: print("minified %s" % (f, )) else: r = -1 if r!=0: shutil.copyfile(src, dst) os.chmod(dst, 0o644) if ftype not in ("png", ): if gzip: gzip_dst = "%s.gz" % dst if os.path.exists(gzip_dst): os.unlink(gzip_dst) cmd = ["gzip", "-f", "-n", "-9", "-k", dst] get_status_output(cmd) if os.path.exists(gzip_dst): os.chmod(gzip_dst, 0o644) if brotli: br_dst = "%s.br" % dst if os.path.exists(br_dst): os.unlink(br_dst) #find brotli on $PATH paths = os.environ.get("PATH", "").split(os.pathsep) if os.name=="posix": #not always present, #but brotli is often installed there (install from source): paths.append("/usr/local/bin") for x in paths: br = os.path.join(x, "brotli") cmd = [br, "-k", dst] if os.path.exists(br): break code, out, err = get_status_output(cmd) if code!=0: print("brotli error code=%i on %s" % (code, cmd)) if out: print("stdout=%s" % out) if err: print("stderr=%s" % err) elif os.path.exists(br_dst): os.chmod(br_dst, 0o644) else: print("Warning: brotli did not create '%s'" % br_dst) if os.name=="posix": #point the background.png to a local background image: background_options = [ "/usr/share/backgrounds/images/default.png", "/usr/share/backgrounds/images/*default*.png", "/usr/share/backgrounds/*default*png", "/usr/share/backgrounds/gnome/adwaita*.jpg", #Debian Stretch "/usr/share/backgrounds/images/*jpg", #CentOS 7 ] extra_symlinks = {"background.png" : background_options} for f, symlink_options in extra_symlinks.items(): dst = os.path.join(install_dir, f) install_symlink(symlink_options, dst)