Esempio n. 1
0
 def render(self, context):
     p = JavaScriptPacker()
     script = self.nodelist.render(context).strip()
     #ugly test on empty script, no reason to pack such small scripts
     if 1 or settings.DEBUG == False or len(script) < 20:
         return self.get_tags_around(script)
     packed = p.pack(script, compaction=False, encoding=62, fastDecode=True)
     return self.get_tags_around(packed)
Esempio n. 2
0
    def build(self,
              input_files,
              output_file,
              packaging_module,
              include_version=True,
              src_prefix_path=""):
        """
    Build distributable version of x3dom
    
    @param src_prefix_path: Optional path that is used as prefix for all source files
    @type src_prefix_path: String
    """

        print "output file:", output_file
        print "input_files:", input_files

        version_out = ""

        if include_version == True:
            # find the VERSION file
            if os.path.isfile("VERSION"):
                version_file_name = "VERSION"
            elif os.path.isfile("src/VERSION"):
                version_file_name = "src/VERSION"
            else:
                print "FATAL: Cannot find any VERSION file"
                sys.exit(0)

            # parse file & generate version.js
            version_out = self.generate_version_file(version_file_name)

            # Add the version.js to the list of input files
            input_files.append((version_out, [version_out]))

        concatenated_file = ""
        in_len = 0
        out_len = 0

        # Merging files
        print "Packing Files"
        for (_, files) in input_files:
            for f in files:
                if f == version_out:
                    concatenated_file = self._mergeFile(concatenated_file, f)
                else:
                    concatenated_file = self._mergeFile(
                        concatenated_file,
                        self._prefixFilePath(f, src_prefix_path))
                """       
              #Single file?
              if filename[-3:] == ".js":
                  #Merge directly
                  concatenated_file = self._mergeFile(concatenated_file, filename)
              #Otherwise (folder)
              else:
                  #Open all files in folder and merge individually
                  print "Folder: ", filename
                  node_files = [f for f in os.listdir(filename) if isfile(join(filename,f)) and f[-3:]==".js"]
                  print ";".join(node_files)
                  for node_file in node_files:
                      concatenated_file = self._mergeFile(concatenated_file, join(filename,node_file))
            """

        print ""

        outpath = os.path.dirname(os.path.abspath(output_file))

        if not os.access(outpath, os.F_OK):
            print "Create Dir ", outpath
            os.mkdir(outpath)

        # Packaging
        print "Packaging"
        print self.VERSION_STRING
        print "  Algo    " + packaging_module
        print "  Output  " + os.path.abspath(output_file)

        # JSMIN
        if packaging_module == "jsmin":
            # Minifiy the concatenated files
            out_stream = StringIO()
            jsm = jsmin.JavascriptMinify()
            jsm.minify(StringIO(concatenated_file), out_stream)

            out_len = len(out_stream.getvalue())

            # Write the minified output file
            outfile = open(output_file, 'w')
            outfile.write(self.VERSION_STRING)
            outfile.write(out_stream.getvalue())
            outfile.close()

        # JSPACKER
        elif packaging_module == "jspacker":
            p = JavaScriptPacker()

            result = p.pack(concatenated_file,
                            compaction=True,
                            encoding=62,
                            fastDecode=False)
            out_len = len(result)
            outfile = open(output_file, 'w')
            outfile.write(self.VERSION_STRING)
            outfile.write(result)
            outfile.close()

        # ClosureCompiler
        elif packaging_module == "closure":

            # collect files
            files = []
            for (_, filesForComponent) in input_files:
                for f in filesForComponent:
                    if f == version_out:
                        files += ["--js=" + f]
                    else:
                        files += [
                            "--js=" + self._prefixFilePath(f, src_prefix_path)
                        ]
                    #concatenated_file = self._mergeFile(concatenated_file, _prefixFilePath(f, src_prefix_path))

            Popen([
                "java", "-jar", "tools/compiler.jar", "--js_output_file=" +
                output_file, "--summary_detail_level=3",
                "--warning_level=VERBOSE"
            ] + files)
            # Popen(["java", "-jar", "tools/compiler.jar", "--js_output_file=" + output_file] + files)

        # NONE
        elif packaging_module == 'none':
            outfile = open(output_file, 'w')
            outfile.write(self.VERSION_STRING)
            outfile.write(concatenated_file)
            outfile.close()

        # Output some stats
        in_len = len(concatenated_file)
        ratio = float(out_len) / float(in_len)
        print "  Packed  %s -> %s" % (in_len, out_len)
        print "  Ratio   %s" % (ratio)
Esempio n. 3
0
 def output(self, _in, out, **kw):
     out.write(JavaScriptPacker().pack(_in.read(),
                                       compaction=False,
                                       encoding=62,
                                       fastDecode=True))
Esempio n. 4
0
                  
    if options.algo == "jsmin":
        # Minifiy the concatenated files
        out_stream = StringIO()  
        jsm = jsmin.JavascriptMinify()
        jsm.minify(StringIO(concatenated_file), out_stream)   
                        
        out_len = len(out_stream.getvalue())
        
        # Write the minified output file
        outfile = open(options.outfile, 'w')
        outfile.write("/** X3DOM Runtime, http://www.x3dom.org/ %s - %s */" 
      					% (version, svn_revision) )
        outfile.write(out_stream.getvalue())
        outfile.close()        
    elif options.algo == "jspacker":
        p = JavaScriptPacker()
        
        result = p.pack(concatenated_file, compaction=True, encoding=62, 
                        fastDecode=False)
        out_len = len(result)
        outfile = open(options.outfile, 'w')
        outfile.write(result)
        outfile.close()
        
    # Output some stats
    in_len = len(concatenated_file)    
    ratio = float(out_len) / float(in_len);
    print "packed: %s to %s, ratio is %s" % (in_len, out_len, ratio)
    
Esempio n. 5
0
  def build(self, input_files, output_file, packaging_module, include_version=True):
    
    if include_version == True:
        # find the VERSION file
        if os.path.isfile("VERSION"):
            version_file_name = "VERSION"
        elif os.path.isfile("src/VERSION"):
            version_file_name = "src/VERSION"
        else:
          print "FATAL: Cannot find any VERSION file"
          sys.exit(0)
    
        # parse file & generate version.js
        version_out = self.generate_version_file(version_file_name);
    
        # Add the version.js to the list of input files
        input_files.append(version_out)

    concatenated_file = ""
    in_len = 0
    out_len = 0
    
    # Merging files
    print "Packing Files"
    for filename in input_files:
      try:
        print "  " + os.path.abspath(filename)
        f = open(filename, 'r')
        concatenated_file += f.read()
        f.close()
      except:
        print "Could not open input file '%s'. Skipping" % filename    
      concatenated_file += "\n"
    print ""
     
    outpath = os.path.dirname(os.path.abspath(output_file))
    
    if not os.access(outpath, os.F_OK):
      print "Create Dir ", outpath
      os.mkdir(outpath)
    
    # Packaging
    print "Packaging"
    print self.VERSION_STRING
    print "  Algo    " + packaging_module
    print "  Output  " + os.path.abspath(output_file)
    
    # JSMIN
    if packaging_module == "jsmin":
      # Minifiy the concatenated files
      out_stream = StringIO()  
      jsm = jsmin.JavascriptMinify()
      jsm.minify(StringIO(concatenated_file), out_stream)   
                      
      out_len = len(out_stream.getvalue())
      
      # Write the minified output file
      outfile = open(output_file, 'w')
      outfile.write(self.VERSION_STRING)
      outfile.write(out_stream.getvalue())
      outfile.close()
    
    # JSPACKER
    elif packaging_module == "jspacker":
      p = JavaScriptPacker()
      
      result = p.pack(concatenated_file, compaction=True, encoding=62, fastDecode=False)
      out_len = len(result)
      outfile = open(output_file, 'w')
      outfile.write(self.VERSION_STRING)
      outfile.write(result)
      outfile.close()
    
    # ClosureCompiler
    elif packaging_module == "closure":
    
      # collect files
      files = []
      for filename in input_files:
        files += ["--js=" + filename]
        
      Popen(["java", "-jar", "tools/compiler.jar", "--js_output_file=" + output_file, "--summary_detail_level=3", "--warning_level=VERBOSE"] + files)
      #Popen(["java", "-jar", "tools/compiler.jar", "--js_output_file=" + output_file] + files)
    
    # NONE
    elif packaging_module == 'none':
      outfile = open(output_file, 'w')
      outfile.write(self.VERSION_STRING)
      outfile.write(concatenated_file)
      outfile.close()

    # Output some stats
    in_len = len(concatenated_file)    
    ratio = float(out_len) / float(in_len);
    print "  Packed  %s -> %s" % (in_len, out_len)
    print "  Ratio   %s" % (ratio)
Esempio n. 6
0
  def build(self, input_files, output_file, packaging_module, include_version=True, src_prefix_path=""):
    """
    Build distributable version of x3dom
    
    @param src_prefix_path: Optional path that is used as prefix for all source files
    @type src_prefix_path: String
    """
    
    print "output file:", output_file
    print "input_files:", input_files
    
    version_out = ""
    
    if include_version == True:
        # find the VERSION file
        if os.path.isfile("VERSION"):
            version_file_name = "VERSION"
        elif os.path.isfile("src/VERSION"):
            version_file_name = "src/VERSION"
        else:
            print "FATAL: Cannot find any VERSION file"
            sys.exit(0)
    
        # parse file & generate version.js
        version_out = self.generate_version_file(version_file_name);
    
        # Add the version.js to the list of input files
        input_files.append((version_out, [version_out]))

    concatenated_file = ""
    in_len = 0
    out_len = 0
    
    # Merging files
    print "Packing Files"
    for (_, files) in input_files:
        for f in files:
            if f == version_out:
                concatenated_file = self._mergeFile(concatenated_file, f)
            else:
                concatenated_file = self._mergeFile(concatenated_file, self._prefixFilePath(f, src_prefix_path))
            """       
              #Single file?
              if filename[-3:] == ".js":
                  #Merge directly
                  concatenated_file = self._mergeFile(concatenated_file, filename)
              #Otherwise (folder)
              else:
                  #Open all files in folder and merge individually
                  print "Folder: ", filename
                  node_files = [f for f in os.listdir(filename) if isfile(join(filename,f)) and f[-3:]==".js"]
                  print ";".join(node_files)
                  for node_file in node_files:
                      concatenated_file = self._mergeFile(concatenated_file, join(filename,node_file))
            """
    
    print ""
    
    outpath = os.path.dirname(os.path.abspath(output_file))
    
    if not os.access(outpath, os.F_OK):
      print "Create Dir ", outpath
      os.mkdir(outpath)
    
    # Packaging
    print "Packaging"
    print self.VERSION_STRING
    print "  Algo    " + packaging_module
    print "  Output  " + os.path.abspath(output_file)
    
    # JSMIN
    if packaging_module == "jsmin":
      # Minifiy the concatenated files
      out_stream = StringIO()  
      jsm = jsmin.JavascriptMinify()
      jsm.minify(StringIO(concatenated_file), out_stream)   
                      
      out_len = len(out_stream.getvalue())
      
      # Write the minified output file
      outfile = open(output_file, 'w')
      outfile.write(self.VERSION_STRING)
      outfile.write(out_stream.getvalue())
      outfile.close()
    
    # JSPACKER
    elif packaging_module == "jspacker":
      p = JavaScriptPacker()
      
      result = p.pack(concatenated_file, compaction=True, encoding=62, fastDecode=False)
      out_len = len(result)
      outfile = open(output_file, 'w')
      outfile.write(self.VERSION_STRING)
      outfile.write(result)
      outfile.close()
    
    # ClosureCompiler
    elif packaging_module == "closure":
    
      # collect files
      files = []
      for (_, filesForComponent) in input_files:
          for f in filesForComponent:
              if f == version_out:
                  files += ["--js=" + f]
              else:
                  files += ["--js=" + self._prefixFilePath(f, src_prefix_path)]
              #concatenated_file = self._mergeFile(concatenated_file, _prefixFilePath(f, src_prefix_path))

        
        
      Popen(["java", "-jar", "tools/compiler.jar", "--js_output_file=" + output_file, "--summary_detail_level=3", "--warning_level=VERBOSE"] + files)
      # Popen(["java", "-jar", "tools/compiler.jar", "--js_output_file=" + output_file] + files)
    
    # NONE
    elif packaging_module == 'none':
      outfile = open(output_file, 'w')
      outfile.write(self.VERSION_STRING)
      outfile.write(concatenated_file)
      outfile.close()

    # Output some stats
    in_len = len(concatenated_file)    
    ratio = float(out_len) / float(in_len);
    print "  Packed  %s -> %s" % (in_len, out_len)
    print "  Ratio   %s" % (ratio)
Esempio n. 7
0
def build(parser, options, args):

    if len(args) == 0:
        print parser.print_help()
        print "- No input files specified. Exiting -"
        sys.exit(0)
    
    if not options.outfile:
        print parser.print_help()
        print "- Please specify an output file using the -o options. Exiting. -"
        sys.exit(0)
    
    # Create the version.js and fill in the svn revision    
    # Read the version from the VERSION file
    in_src = False;
    if os.path.isfile("VERSION"):
        version_file = open("VERSION", "r")
    else:
        version_file = open("src/VERSION", "r")
        in_src = True;
        
    version = version_file.read()
    # Make sure to only use the version string without '\n' etc. 
    version = version.split()[0]
    print "Version  '", version, "'"
    version_file.close()
    # Add the version.js to the list of input files
    args.append("version.js")
    # Extract the svn revision 
    try:
        git_revision = Popen(["git", "log", "-1", "--pretty=format:%H"], stdout=PIPE).communicate()[0]
        git_date = Popen(["git", "log", "-1", "--pretty=format:%ad"], stdout=PIPE).communicate()[0]
    except:
        git_revision = 0
        git_date = 0
    print "Revision '", git_revision, "'"
    print "Date     '", git_date, "'"
    
    # Write the version and revision to file
    version_file_name = 'version.js'
    if in_src:
        version_file_name = 'src/version.js'
    version_file = open(version_file_name, "w")
    version_file.write(VERSION_TEMPLATE % (version, git_revision, git_date))
    version_file.close()            
    
    concatenated_file = ""
    in_len = 0
    out_len = 0
    
    for filename in args:
        try:
            f = open(filename, 'r')
            concatenated_file += f.read()
            f.close()
        except:
            print "Could not open input file '%s'. Skipping" % filename    
        concatenated_file += "\n"
     
    outpath = os.path.dirname(os.path.abspath(options.outfile))
    
    if not os.access(outpath, os.F_OK):
        print "Create Dir: ", outpath
        os.mkdir(outpath)
                  
    print "Using", options.algo
                  
    if options.algo == "jsmin":
        # Minifiy the concatenated files
        out_stream = StringIO()  
        jsm = jsmin.JavascriptMinify()
        jsm.minify(StringIO(concatenated_file), out_stream)   
                        
        out_len = len(out_stream.getvalue())
        
        # Write the minified output file
        outfile = open(options.outfile, 'w')
        outfile.write("/** X3DOM Runtime, http://www.x3dom.org/ %s - %s - %s */" % (version, git_revision, git_date) )
        outfile.write(out_stream.getvalue())
        outfile.close()
    elif options.algo == "jspacker":
        p = JavaScriptPacker()
        
        result = p.pack(concatenated_file, compaction=True, encoding=62, 
                        fastDecode=False)
        out_len = len(result)
        outfile = open(options.outfile, 'w')
        outfile.write(result)
        outfile.close()
        
    # Output some stats
    in_len = len(concatenated_file)    
    ratio = float(out_len) / float(in_len);
    print "packed: %s to %s, ratio is %s" % (in_len, out_len, ratio)