Example #1
0
def bake_js(source_dir="js5", dest_file="script5.js"):
	create_baked_directory()
	fn = os.path.join(os.path.dirname(__file__), "..", "static", "baked", str(get_build_number()), dest_file)
	if not os.path.exists(fn):
		js_content = ""
		for sfn in get_js_file_list(source_dir):
			jsfile = open(os.path.join(os.path.dirname(__file__), "..", sfn))
			js_content += minify(jsfile.read()) + "\n"
			jsfile.close()

		o = open(fn, "w")
		o.write(minify(js_content, mangle=True, mangle_toplevel=False))
		o.close()
Example #2
0
def check_js_equal(expected=None, actual=None):
    """Minify the JavaScript and compare it"""
    with suppress_stderr():
        print('Minifying expected JS')
        js0 = slimit.minify(expected)
        print('Minifying actual JS')
        js1 = slimit.minify(actual)
    if js0 == js1:
        return True
    print('Expected')
    print(js0)
    print('Actual')
    print(js1)
    return False
Example #3
0
def bake_js(source_dir="js4", dest_file="script4.js"):
    create_baked_directory()
    fn = os.path.join(os.path.dirname(__file__), "..", "static", "baked",
                      str(get_build_number()), dest_file)
    if not os.path.exists(fn):
        js_content = ""
        for sfn in get_js_file_list(source_dir):
            jsfile = open(os.path.join(os.path.dirname(__file__), "..", sfn))
            js_content += minify(jsfile.read()) + "\n"
            jsfile.close()

        o = open(fn, "w")
        o.write(minify(js_content, mangle=True, mangle_toplevel=False))
        o.close()
Example #4
0
    def _minify(self, data, type, paths=[]):
        sep = ''

        # figure out how to minify something
        if type == 'javascript':
            sep = ';'

            # use envoy to run the custom command if it's supplied
            custom = self.config.get('js_minifier')
            if custom is not None:
                minify = lambda x: envoy.run(custom, data=x).std_out

            # otherwise use slimit
            else:
                options = self.config.get('js_minifier_options',
                                          {'mangle': True})

                minify = lambda x: slimit.minify(x, **options)

        elif type == 'css':
            # only one option for css right now
            minify = cssmin.cssmin

        def real_minify(path, contents):
            if '.min' in path:
                return contents

            return minify(contents)

        minified = sep.join([
            real_minify(path, contents) for path, contents in zip(paths, data)
        ])

        return minified
Example #5
0
File: setup.py Project: gryf/kiroku
    def run(self):
        """Execute command"""
        if not slimit:
            print("You need `slimit' module to minify JavaScript files")
            return

        for root, _, files in os.walk("kiroku/data/js"):
            for fname in files:
                if ".min." in fname:
                    continue

                fname = os.path.join(root, fname)
                minified = None
                new_name, ext = os.path.splitext(fname)
                new_name = os.path.join(new_name + ".min" + ext)

                with open(fname) as fobj:
                    minified = slimit.minify(fobj.read(), mangle=True)

                if minified:
                    with open(new_name, "w") as fobj:
                        fobj.write(minified)
                    # append minified file path without leading 'kiroku/'
                    new_name = new_name[7:]
                    self.distribution.package_data['kiroku'].append(new_name)
def minicite(js_names, do_wrap, do_minify):
    '''Concatenate, wrap, and minify project JavaScript files.'''
    
    # Read code
    js_files = [open(js_name).read() for js_name in js_names]
    
    # Concatenate code
    js = '\n\n'.join(js_files)

    # Optionally wrap code
    # Note: Wrapper file must contain %s for interpolation
    if do_wrap:

        # Read wrapper
        with open('js-util/wrapper.js') as wrap_file:
            wrap = wrap_file.read()
        
        # Wrap code
        js = wrap % js

    # Optionally minify code
    if do_minify:
        js = slimit.minify(js, mangle=True)
    
    # Return code
    return js
Example #7
0
def minicite(js_names, do_wrap, do_minify):
    '''Concatenate, wrap, and minify project JavaScript files.'''

    # Read code
    js_files = [open(js_name).read() for js_name in js_names]

    # Concatenate code
    js = '\n\n'.join(js_files)

    # Optionally wrap code
    # Note: Wrapper file must contain %s for interpolation
    if do_wrap:

        # Read wrapper
        with open('js-util/wrapper.js') as wrap_file:
            wrap = wrap_file.read()

        # Wrap code
        js = wrap % js

    # Optionally minify code
    if do_minify:
        js = slimit.minify(js, mangle=True)

    # Return code
    return js
Example #8
0
def handle_javascript(js_data, mangle=True, mangle_toplevel=True):
    """
    压缩混淆js
    :param js_data: js字符串
    :return:
    """
    return minify(js_data, mangle=mangle, mangle_toplevel=mangle_toplevel)
Example #9
0
    def _minify(self, data, type, paths=[]):
        sep = ''

        # figure out how to minify something
        if type == 'javascript':
            sep = ';'

            # use envoy to run the custom command if it's supplied
            custom = self.config.get('js_minifier')
            if custom is not None:
                minify = lambda x: envoy.run(custom, data=x).std_out

            # otherwise use slimit
            else:
                options = self.config.get(
                  'js_minifier_options', {'mangle': True}
                )

                minify = lambda x: slimit.minify(x, **options)

        elif type == 'css':
            # only one option for css right now
            minify = cssmin.cssmin

        def real_minify(path, contents):
            if '.min' in path:
                return contents

            return minify(contents)

        minified = sep.join(
          [real_minify(path, contents) for path, contents in zip(paths, data)]
        )

        return minified
Example #10
0
def urls_js(request=None):
    js_var_name = getattr(settings, 'JS_REVERSE_JS_VAR_NAME', JS_VAR_NAME)
    if not re.match(r'^[$A-Z_][\dA-Z_$]*$', js_var_name.upper()):
        raise ImproperlyConfigured(
            'JS_REVERSE_JS_VAR_NAME setting "%s" is not a valid javascript identifier.' % (js_var_name))

    minfiy = getattr(settings, 'JS_REVERSE_JS_MINIFY', JS_MINIFY)
    if not isinstance(minfiy, bool):
        raise ImproperlyConfigured(
            'JS_REVERSE_JS_MINIFY setting "%s" is not a valid. Needs to be set to True or False.' % (minfiy))

    default_urlresolver = urlresolvers.get_resolver(getattr(request, 'urlconf', None))
    response_body = loader.render_to_string('django_js_reverse/urls_js.tpl', {
        'urls': list(prepare_url_list(default_urlresolver)),
        'url_prefix': urlresolvers.get_script_prefix(),
        'js_var_name': js_var_name
    })

    if minfiy:
        response_body = minify(response_body, mangle=True, mangle_toplevel=False)

    if not request:
        return response_body
    else:
        return HttpResponse(response_body, **{content_type_keyword_name: 'application/javascript'})
Example #11
0
def compile_js(in_path, out_path):
    compiled_file = ""
    try:
        with open(in_path, 'r') as f:
            data = f.read()
            splitted_data = data.split('\n')
            for i in range(len(splitted_data)):
                try:
                    if splitted_data[i].split("!!@!!")[1] == "DELETEONCOMPILE":
                        splitted_data[i] = ""
                    elif splitted_data[i].split(
                            "!!@!!")[1] == "UNCOMMENTONCOMPILE":
                        splitted_data[i] = splitted_data[i][2:]
                except IndexError:
                    pass
            data = ""
            for line in splitted_data:
                data += line + "\n"
            compiled_file = minify(data, mangle=False, mangle_toplevel=True)
    except Exception as e:
        print(e)
        print("Failed to read file " + in_path)
        exit()
    try:
        with open(out_path, "w") as f:
            f.write(compiled_file)
    except Exception:
        print("Failed to write file " + out_path)
        exit()
Example #12
0
    def compressJs(self, s):
        """ Compress JS string. """
        jscompiler = config.get('global', 'jscompiler')
        if jscompiler == 'internal':
            from slimit import minify
            s = minify(s, mangle=False)

        else:
            tmp = open('/tmp/wctmp', 'w')
            tmp.write(s)
            tmp.close()

            cmd = jscompiler % {
                'input': '/tmp/wctmp',
                'output': '/tmp/wctmpout'
            }
            proc = subprocess.Popen([cmd],
                                    shell=True,
                                    stdin=subprocess.PIPE,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            for line in proc.stdout.readlines():
                sys.stdout.write(line)
            for line in proc.stderr.readlines():
                sys.stdout.write(line)

            proc.wait()
            if proc.returncode != 0:
                exit(1)

            tmp = open('/tmp/wctmpout', 'r')
            s = tmp.read().strip()
            tmp.close()

        return s
Example #13
0
 def minify_string(self, string, outfile=None, *, path=None):
     from slimit import minify
     result = minify(string, mangle=True)
     if outfile:
         open(outfile, 'w').write(result)
     else:
         return result
Example #14
0
def wrap_mini(paths, basename='pdp', debug=True):
    '''
    :param paths: list of paths to JavaScript files that are to be minified
    :type paths: list of strings
    :param basename: a prefix to prepend to the minified filename. The
                     minified filename will be {basename}-min-{pdp_version}.js
    :type basename: string
    :param debug: If set to True, no minification takes place and the input
                  `paths` are returned as is.
    :type debug: bool
    :returns: list of strings -- the strings is a filesystem path to the
              minified version of the file. If debug is set to True, the return
              value will be equal to the input `paths`.
    '''
    if debug:
        return paths
    else:
        d = resource_filename('pdp', 'static')
        version = get_distribution('pdp').version
        s = ''
        for path in paths:
            with open(os.path.join(d, path), 'r') as f:
                s += f.read()
        smin = minify(s, mangle=True, mangle_toplevel=False)
        outname = '{basename}-min-{version}.js'.format(**locals())
        outpath = os.path.join(d, outname)
        with open(outpath, 'w') as f:
            f.write(smin)
        return [outname]
Example #15
0
    def _build_javascript(self):
        js_name = self._config['js_name'] + '.js'
        source = os.path.join(self._cwd, 'src', js_name)
        dest = os.path.join(self._config['build_path'], js_name)

        if not os.path.exists(source):
            source = os.path.join(self._cwd, 'src', 'application.js')
            if not os.path.exists(source):
                return

        try:
            self._js_string = self._concat_javascript(source)
        except:
            raise

        if os.path.exists(dest):
            try:
                os.remove(dest)
            except:
                raise RemoveFileError('Could not delete the existing javascript application file.')

        try:
            f = open(dest, 'w+')
        except:
            raise FileNotWritableError('Could not write the javascript file.')

        if self._config['minify_js']:
            self._js_string = minify(self._js_string, mangle=True, mangle_toplevel=True)

        f.write(self._js_string)
        f.close()
Example #16
0
    def build(cls, basepath, bundle_key, ext):
        bundle = {}

        # Iterate over files in bundle; determine last modified time and
        # assemble content
        last_mtime = 0
        contents = ""
        for path in ASSET_MANIFEST[bundle_key]:
            path = os.path.join(os.path.abspath(basepath),
                path[len('/static/'):])
            last_mtime = max(last_mtime, os.stat(path)[stat.ST_MTIME])
            contents += open(path, "rb").read() + "\n"

        if ext == "js":
            bundle["contents"] = slimit.minify(contents, mangle=True,
                mangle_toplevel=True)
        elif ext == "css":
            bundle["contents"] = cssmin.cssmin(contents)
        else:
            assert False

        bundle["sha1"] = hashlib.sha1(bundle["contents"]).hexdigest()
        bundle["last_modified"] = datetime.datetime.fromtimestamp(last_mtime)
        bundle["mime_type"] = "text/javascript" if ext == "js" else "text/css"

        StaticBuild._bundles[bundle_key] = bundle
Example #17
0
 def compressJs(self, s):
     """ Compress JS string. """
     jscompiler = config.get('global', 'jscompiler')
     if jscompiler == 'internal':
         from slimit import minify
         s = minify(s, mangle=False)
         
     else:
         tmp = open('/tmp/wctmp', 'w')
         tmp.write(s)
         tmp.close()
         
         cmd = jscompiler % {'input':'/tmp/wctmp', 'output':'/tmp/wctmpout'}
         proc = subprocess.Popen([cmd], 
             shell=True, 
             stdin=subprocess.PIPE, 
             stdout=subprocess.PIPE, 
             stderr=subprocess.PIPE)
         for line in proc.stdout.readlines():
             sys.stdout.write(line)
         for line in proc.stderr.readlines():
             sys.stdout.write(line)
         
         proc.wait()
         if proc.returncode != 0:
             exit(1)
             
         tmp = open('/tmp/wctmpout', 'r')
         s = tmp.read().strip()
         tmp.close()
         
     return s
Example #18
0
    def minify_scripts():
        try:
            import slimit
            for fn in load_static_file_paths('ui/*.js'):
                if 'packages' in fn or not os.access(os.path.dirname(fn), os.W_OK):
                    return
                if fn.endswith('.min.js'):
                    continue
                min_fn = fn.replace('.js', '.min.js')
                if os.path.exists(min_fn) and os.path.getmtime(fn) <= os.path.getmtime(min_fn):
                    continue
                with codecs.open(fn, encoding='utf-8') as inf:
                    content = inf.read()
                    minified = slimit.minify(content, mangle=True, mangle_toplevel=True)
                    with codecs.open(min_fn, 'w', encoding='utf-8') as outf:
                        outf.write(minified)

            import csscompressor
            for fn in load_static_file_paths('ui/*.css'):
                if 'packages' in fn or not os.access(os.path.dirname(fn), os.W_OK):
                    return
                if fn.endswith('.min.css'):
                    continue
                min_fn = fn.replace('.css', '.min.css')
                if os.path.exists(min_fn) and os.path.getmtime(fn) <= os.path.getmtime(min_fn):
                    continue
                with codecs.open(fn, encoding='utf-8') as inf:
                    content = inf.read()
                    minified = csscompressor.compress(content)
                    with codecs.open(min_fn, 'w', encoding='utf-8') as outf:
                        outf.write(minified)
        except ImportError:
            pass
Example #19
0
def build_asset( destdir, asset ):

	target = os.path.join( destdir, assets.assets[ asset ][ 'output' ] )

	destination = open( target, 'wb' )

	for jsname in assets.assets[ asset ]['order']:

		isMax = False

		try:
			fname = assets.assets[ asset ][ 'min_files' ][ jsname ]
		except KeyError, ke:
			fname = assets.assets[ asset ][ 'max_files' ][ jsname ]
			isMax = True


		newname = os.path.join( os.path.dirname( os.path.realpath( __file__ ) ), 'static', fname )

		f = open( newname, 'rb')
		if isMax is True:
			print 'minifying and concatenating ' + fname
			destination.write(
				minify( f.read(), mangle = True, mangle_toplevel = False )
			)
		else:
			print 'concatenating ' + fname
			destination.write( f.read() )

		f.close()
Example #20
0
    def run(self):
        log.debug("[%s.%s] Compressing JS." %
                  (__name__, self.__class__.__name__))

        JSFULL_DIR = get_path(
            [BASEDIR, 'tribus', 'data', 'static', 'js', 'full'])
        JSMIN_DIR = get_path(
            [BASEDIR, 'tribus', 'data', 'static', 'js', 'min'])

        try:
            os.makedirs(JSMIN_DIR)
        except Exception as e:
            print e

        for JS_FILE in find_files(path=JSFULL_DIR, pattern='*.js'):

            JSMIN_FILE = get_path([JSMIN_DIR, os.path.basename(JS_FILE)])

            try:

                with open(JSMIN_FILE, 'w') as _file:
                    _file.write(slimit.minify(open(JS_FILE).read()))
                    _file.close()

            except Exception as e:
                print e

            log.debug("[%s.%s] %s > %s." %
                      (__name__, self.__class__.__name__, JS_FILE, JSMIN_FILE))
Example #21
0
    def GET(self, path):
        import mimetypes
        import stat
        import hashlib
        abspath = os.path.join(settings.STATIC_DIR, path)
        stat_result = os.stat(abspath)
        modified = datetime.datetime.fromtimestamp(stat_result[stat.ST_MTIME])
        web.header("Last-Modified",
                   modified.strftime('%a, %d %b %Y %H:%M:%S GMT'))

        mime_type, encoding = mimetypes.guess_type(abspath)
        if mime_type:
            web.header("Content-Type", mime_type)

        # 缓存10年
        cache_time = 86400 * 365 * 10
        web.header(
            "Expires",
            datetime.datetime.now() + datetime.timedelta(seconds=cache_time))
        web.header("Cache-Control", "max-age=%s" % cache_time)

        ims_value = web.ctx.env.get("HTTP_IF_MODIFIED_SINCE")
        if ims_value is not None:
            # ie的ims值不标准,所以导致不能正常产生缓存,这里解决
            # IE的是Sat, 02 Feb 2013 14:44:34 GMT; length=4285
            # 标准的为Sat, 02 Feb 2013 14:44:34 GMT
            stupid = ims_value.find(';')
            if stupid != -1:
                ims_value = ims_value[:stupid]

            since = datetime.datetime.strptime(ims_value,
                                               '%a, %d %b %Y %H:%M:%S %Z')
            if since >= modified:
                # 如果是调试模式,那么强制加载所有非第三方js文件
                if not (settings.DEBUG and abspath.endswith('.js')
                        and '3rd' not in abspath):
                    raise web.notmodified()

        with open(abspath, "rb") as f:
            data = f.read()
            hasher = hashlib.sha1()
            hasher.update(data)
            web.header("Etag", '"%s"' % hasher.hexdigest())
            # 合并js文件[第三方库不压缩]
            if abspath.endswith('.js') and not '3rd' in abspath:
                libs = re.findall(r'(.*?@import "([^"]+\.js)".*?)', data)
                for line, lib in libs:
                    lib = os.path.join(settings.STATIC_DIR, lib)
                    data = data.replace(line, file(lib).read())

                # mangle: 局部变量压缩
                # mangle_toplevel: 整个文件压缩[即函数名也压缩]
                #data = slimit.minify(data, mangle=True)
                if not settings.DEBUG:
                    import slimit
                    data = slimit.minify(data,
                                         mangle=True,
                                         mangle_toplevel=True)
            return data
Example #22
0
def minify_file(file_debug, file_minified):
    if closure_cmd:
        return minify_closure(file_debug, file_minified)
    elif minify:
        with open(file_minified, 'w') as file_out:
            with open(file_debug, 'r') as file_in:
                file_out.write(minify(file_in.read()))
                return True
Example #23
0
def minify_file(file_debug, file_minified):
    try:
        return minify_closure(file_debug, file_minified)
    except NameError:
        with open(file_minified, 'w') as file_out:
            with open(file_debug, 'r') as file_in:
                file_out.write(minify(file_in.read()))
                return True
def get_project_js(project_dir, compress):
	"""Returns the project's custom script, possibly minified, if it exists."""
	try:
		with open(os.path.join(project_dir, "project.js"), "r") as f:
			js = f.read()
			return js if not compress else minify(js)
	except:
		return ""
Example #25
0
def minify_file(file_debug, file_minified):
    try:
        return minify_closure(file_debug, file_minified)
    except NameError:
        with open(file_minified, 'w') as file_out:
            with open(file_debug, 'r') as file_in:
                file_out.write(minify(file_in.read()))
                return True
def new_campaign():
  if request.method == 'GET':
    return render_template('/campaign/new_campaign.html')

  if request.method == 'POST':
    try:
      file  = request.files['kernel']
      kernel_src      = minify(file.read())
    except SyntaxError:
      flash('Invalid kernel file syntax.', 'danger');
      return render_template('/campaign/new_campaign.html')

    file = request.files['static_dataset']
    static_dataset_src = file.read()

    try:
      file = request.files['dataset_generator']
      dataset_gen_src = minify(file.read())
    except SyntaxError:
      flash('Invalid dataset generator file syntax.', 'danger');
      return render_template('/campaign/new_campaign.html')

    campaign = {}
    campaign['user_id'] = current_user_id()
    campaign['kernel']  = kernel_src
    campaign['title']   = request.form['campaign_title']
    campaign['static_dataset'] = static_dataset_src
    campaign['dataset_gen'] = dataset_gen_src

    try:
      new_campaign = CampaignController.add(campaign)
    except CampaignController.ValidationError as e:
      flash(e.message, 'danger')
      return render_template('/campaign/new_campaign.html')

    # Generating the datasets may return an error.
    # If it does, we want to show this to the user
    returncode, error = generate_datasets(new_campaign)

    if (returncode == 0):
      return redirect(url_for('campaign_routes.all_campaigns'), code=303)
    else:
      CampaignController.real_delete(new_campaign.id)

      flash(error, 'danger')
      return render_template('/campaign/new_campaign.html')
Example #27
0
    def _compress(self):
        output = []

        for src in self.includes:
            with open('www/%s' % src) as f:
                output.append(minify(f.read()))

        return '\n'.join(output)
Example #28
0
def run():
	minified = ""
	for currentFile in files:
		minified += minify(open('../static/js/' + currentFile).read(), mangle=True, mangle_toplevel=True)
		minified += '\n'
	minFile = open('../static/js/main.js', 'w')
	minFile.write(minified)
	minFile.close()
Example #29
0
    def _compress(self):
        output = []

        for src in self.includes:
            with codecs.open(self._get_path(src), encoding='utf-8') as f:
                output.append(minify(f.read()))

        return '\n'.join(output)
    def _compress(self):
        output = []

        for src in self.includes:
            with codecs.open(self._get_path(src), encoding='utf-8') as f:
                output.append(minify(f.read()))

        return '\n'.join(output)
Example #31
0
def do_slimit(input_file, output_file, mangle):
    """
    Minify js using slimit
    """
    with open(output_file, mode='wb') as out:
        out.write(
            minify(open(input_file, mode='rb').read(), mangle=mangle)
        )
Example #32
0
def final_coffee_compile(filename):
  ccc = compile_coffeescript(filename)
  f = open('../debug/application.js', 'w')
  f.write(ccc)
  f.close()
  mccc = minify(ccc, mangle=True, mangle_toplevel=True)
  f = open('../release/application.js', 'w')
  f.write(mccc)
  f.close()
Example #33
0
    def _compress(self):
        output = []
        src_paths = []

        for src in self.includes:
            try:
                with codecs.open('www/%s' % src, encoding='utf-8') as f:
                    print '- compressing %s' % src
                    src_paths.append('www/%s' % src)
                    output.append(minify(f.read()))
            except IOError, e:
                if src.startswith('node_modules'):
                    with codecs.open(src, encoding='utf-8') as f:
                        print '- compressing %s' % src
                        src_paths.append(src)
                        output.append(minify(f.read()))
                else:
                    raise e
Example #34
0
def js(content, comment='', encoding=None):
    if comment:
        comment = '/* %s */\n' % comment
    else:
        comment = ''
    if encoding and type(content) is not unicode:
        content = unicode(content, encoding)
    return comment + slimit.minify(
        content, mangle=False, mangle_toplevel=False)
Example #35
0
def process_js(scripts):
	aggregated_scripts = ''

	for script in scripts:
		script_path = normpath(join(PROJECT_ROOT, script))
		with open(script_path, 'r') as script_file:
			aggregated_scripts += unicode(script_file.read(), 'utf-8')

	return minify(aggregated_scripts, mangle=True, mangle_toplevel=True)
Example #36
0
def JS2C(source, debug, target):
    ids = []
    modules = []
    # Locate the macros file name.
    consts = []
    macros = []
    for s in source:
        if 'macros.py' == (os.path.split(str(s))[1]):
            (consts, macros) = ReadMacros(ReadLines(str(s)))
        else:
            modules.append(s)

    debug_js_dir = ""
    if not debug:
        debug_js_dir = os.path.join(os.path.split(str(target))[0], 'js')
        if not os.path.isdir(debug_js_dir):
            os.makedirs(debug_js_dir)

    module_offset = 0
    all_sources = []
    for module in modules:
        filename = str(module)
        lines = ReadFile(filename)
        lines = ExpandConstants(lines, consts)
        lines = ExpandMacros(lines, macros)
        Validate(lines, filename)
        minified = minify(lines, mangle=True)
        if not debug:
            lines = minified
        id = os.path.split(filename)[1]
        raw_length = len(lines)
        ids.append((id, raw_length, module_offset))
        all_sources.append(lines)
        module_offset += raw_length
        if not debug:
            debug_js = open(os.path.join(debug_js_dir, id), 'w')
            debug_js.write(lines)
            debug_js.close()

    sources_data = ToCAsciiArray("".join(all_sources))

    # Build source code lines
    source_lines = []
    for (id, raw_length, module_offset) in ids:
        source_lines.append(SOURCE_DECLARATION % {
            'name': id,
            'offset': module_offset,
            'raw_length': raw_length,
        })

    # Emit result
    output = open(str(target), "w")
    output.write(HEADER_TEMPLATE % {
        'sources_data': sources_data,
        'source_lines': "".join(source_lines)
    })
    output.close()
Example #37
0
    def GET(self, path):
        import mimetypes
        import stat
        import hashlib
        abspath = os.path.join(settings.STATIC_DIR, path)
        stat_result = os.stat(abspath)
        modified = datetime.datetime.fromtimestamp(stat_result[stat.ST_MTIME])
        web.header(
            "Last-Modified", modified.strftime('%a, %d %b %Y %H:%M:%S GMT'))

        mime_type, encoding = mimetypes.guess_type(abspath)
        if mime_type:
            web.header("Content-Type", mime_type)

        # 缓存10年
        cache_time = 86400 * 365 * 10
        web.header("Expires", datetime.datetime.now() +
                   datetime.timedelta(seconds=cache_time))
        web.header("Cache-Control", "max-age=%s" % cache_time)

        ims_value = web.ctx.env.get("HTTP_IF_MODIFIED_SINCE")
        if ims_value is not None:
            # ie的ims值不标准,所以导致不能正常产生缓存,这里解决
            # IE的是Sat, 02 Feb 2013 14:44:34 GMT; length=4285
            # 标准的为Sat, 02 Feb 2013 14:44:34 GMT
            stupid = ims_value.find(';')
            if stupid != -1:
                ims_value = ims_value[:stupid]

            since = datetime.datetime.strptime(
                ims_value, '%a, %d %b %Y %H:%M:%S %Z')
            if since >= modified:
                # 如果是调试模式,那么强制加载所有非第三方js文件
                if not (settings.DEBUG and
                        abspath.endswith('.js') and '3rd' not in abspath):
                    raise web.notmodified()

        with open(abspath, "rb") as f:
            data = f.read()
            hasher = hashlib.sha1()
            hasher.update(data)
            web.header("Etag", '"%s"' % hasher.hexdigest())
            # 合并js文件[第三方库不压缩]
            if abspath.endswith('.js') and not '3rd' in abspath:
                libs = re.findall(r'(.*?@import "([^"]+\.js)".*?)', data)
                for line, lib in libs:
                    lib = os.path.join(settings.STATIC_DIR, lib)
                    data = data.replace(line, file(lib).read())

                # mangle: 局部变量压缩
                # mangle_toplevel: 整个文件压缩[即函数名也压缩]
                #data = slimit.minify(data, mangle=True)
                if not settings.DEBUG:
                    import slimit
                    data = slimit.minify(
                        data, mangle=True, mangle_toplevel=True)
            return data
Example #38
0
    def minify(self, content):
        try:
            minify = self._minify
        except AttributeError:
            from slimit import minify
            self._minify = minify
            minify = self._minify

        return minify(content)
Example #39
0
 def _test_minify(self, content, sets):
     if 'js_slim' in sets['options']:
         if not SLIM_OK:
             self.output('Slimit (https://github.com/rspivak/slimit) not available, not minifying!', 0)
             return content
         mangle = 'js_mangle' in sets['options']
         mangle_toplevel = 'js_mangle_toplevel' in sets['options']
         content = minify(content, mangle = mangle, mangle_toplevel = mangle_toplevel)
         self.output('content minified, mange = %r, mangle_toplevel = %r' % (mangle, mangle_toplevel), colourv = 3)
     return content
Example #40
0
    def process_file(self, file_obj):
        content = minify(file_obj.str,
                         mangle=self.mangle,
                         mangle_toplevel=self.mangle_toplevel)

        self.target.delete(file_obj.current_name)

        file_obj.str = content
        file_obj.add_suffix('.min', -2)
        yield file_obj
Example #41
0
def compress(conf,inputs,outputs):
	if inputs.has_key("filename") and inputs["filename"]["value"]!="NULL":
		filenames=inputs["filename"]["value"].split(",")
		inputs["file"]["value"]=""
			
		for i in range(0,len(filenames)):
			if inputs["type"]["value"]=="js" and inputs["filename"]["value"].count("css")==0:
				if filenames[i].count("min")==0 and filenames[i].count("ckeditor")==0 and filenames[i].count("compressed")==0:
					try:
						from slimit import minify
						inputs["file"]["value"]+=minify(open(conf["main"]["mmPath"]+"/"+inputs["type"]["value"].lower()+"/"+filenames[i]).read(), mangle=False, mangle_toplevel=True)
					except:
						try:
							from slimit import minify
							inputs["file"]["value"]+=minify(open(conf["main"]["publicationPath"]+"/"+filenames[i]).read(), mangle=False, mangle_toplevel=False)
						except Exception,e:
							try:
								inputs["file"]["value"]+="/* Failed to parse "+str(e)+"*/"+open(conf["main"]["publicationPath"]+"/"+inputs["type"]["value"].lower()+"/"+filenames[i]).read()+"/*Failed to parse*/\n"
							except:
								try:
									inputs["file"]["value"]+="/* Failed to parse */"+open(conf["main"]["mmPath"]+"/"+inputs["type"]["value"].lower()+"/"+filenames[i]).read()+"/*Failed to parse*/\n"
								except:
									pass
				else:
					try:
						inputs["file"]["value"]+=open(conf["main"]["mmPath"]+"/"+inputs["type"]["value"].lower()+"/"+filenames[i]).read()+"\n"
					except:
						try:
							inputs["file"]["value"]+=open(conf["main"]["publicationPath"]+"/"+filenames[i]).read()+"\n"
						except:
							pass

			else:
				from cssmin import cssmin
				try:
					inputs["file"]["value"]+=open(conf["main"]["mmPath"]+"/"+inputs["type"]["value"].lower()+"/"+filenames[i]).read()+"\n"
				except:
					try:
						inputs["file"]["value"]+=open(conf["main"]["publicationPath"]+"/"+filenames[i]).read()+"\n"
					except Exception,e:
						print >> sys.stderr,e
						pass
Example #42
0
def slimit(input_file):
    """
    :type   input_file: File  

    :returns: File 
    """
    
    content_bytes = input_file.read()
    output_bytes  = slimit_mod.minify(content_bytes, mangle=True, mangle_toplevel=False)
    
    return ContentFile(output_bytes)
Example #43
0
def compress_js(path):
    """Compress javascript files in `path` with Slimit."""
    import slimit
    files = glob.iglob(os.path.join(path, '*.js'))

    for js in files:
        with open(js, 'r+') as source:
            compressed = slimit.minify(source.read(), mangle=True, mangle_toplevel=True)
            source.seek(0)
            source.write(compressed)
            source.truncate()
Example #44
0
def compress(src_js, des_js):
    # 压缩 JS 文件
    # https://pypi.python.org/pypi/slimit
    return True
    print "***压缩JS中*** ... ..."
    target = src_js
    print ":: 目标: %s" % (target)
    uncompress = open(target, "r").read()
    jsmin = slimit.minify(uncompress, mangle=True, mangle_toplevel=True)
    print jsmin
    open(target, "w").write(jsmin)
Example #45
0
def render_js(js):
	try:
		out=cache.get(js)
		if out is None:
			fd=open(pkg_resources.resource_filename('vishwin_http.views', 'js/' + js), encoding='UTF-8')
			out=minify(fd.read(), mangle=True, mangle_toplevel=True)
			fd.close()
			cache.set(js, out)
		return Response(response=out, mimetype='text/javascript')
	except OSError:
		abort(404)
    def _minify(self, infile, outfile=None):
        if outfile is None:
            outfile = infile
        logging.info("Minifiing {0}".format(infile))
        f = open(infile, 'r')
        content = f.read()
        f.close()

        f = open(outfile, 'w')
        f.write(minify(content, mangle=False, mangle_toplevel=False))
        f.close()
Example #47
0
def convert_str(code, wrap=False):
    out = []
    minified = minify(code)

    for i in range(len(minified) - 1):
        out.append(symbols[minified[i]] + "+")  # All
    out.append(symbols[minified[-1]])  # Last char
    out = "".join(out)

    if wrap:
        out = "".join(["eval(", out, ");"])

    return out
Example #48
0
def BuildJS():
    """Build js libraries."""
    # First, minify and copy over vr.js.
    with open("js/vr.js", "r") as f:
        js = f.read()
    with open("../gh-pages/js/vr.min.js", "w") as f:
        f.write(slimit.minify(js))
    print "Wrote ../gh-pages/js/vr.min.js"

    # Copy the other javascript libraries.
    for fname in ["three.min.js", "material.min.js", "dialog-polyfill.min.js"]:
        shutil.copy("js/" + fname, "../gh-pages/js/" + fname)
        print "Wrote ../gh-pages/js/" + fname
Example #49
0
def main():
    js_root = os.path.join(os.path.dirname(DIR), 'js')
    src = os.path.join(js_root, 'threeio.js')
    dst = os.path.join(js_root, 'threeio.min.js')

    print('Minifying %s > %s' % (src, dst))

    with open(src) as fs:
        data = fs.read()

    minified = slimit.minify(data, mangle=True, mangle_toplevel=False)
    with open(dst, 'w') as fs:
        fs.write(minified)
Example #50
0
def render_js(js):
    try:
        out = cache.get(js)
        if out is None:
            fd = open(pkg_resources.resource_filename('wahgwan_http',
                                                      'js/' + js),
                      encoding='UTF-8')
            out = minify(fd.read(), mangle=True, mangle_toplevel=True)
            fd.close()
            cache.set(js, out)
        return Response(response=out, mimetype='text/javascript')
    except OSError:
        abort(404)
Example #51
0
def minify_text(filepath, file_type):
    print "in minify_text"
    url = os.path.realpath('.')
    print "url: " + url
    print(SITE_URL + filepath)
    print requests.get(SITE_URL + filepath)
    text = requests.get(SITE_URL + filepath).content
    #print text
    if file_type is "js":
        minified = minify(text)
    elif file_type is "css":
        minified = compress(text)
    return minified
Example #52
0
def slimit(input_file):
    """
    :type   input_file: File  

    :returns: File 
    """

    content_bytes = input_file.read()
    output_bytes = slimit_mod.minify(content_bytes,
                                     mangle=True,
                                     mangle_toplevel=False)

    return ContentFile(output_bytes)
Example #53
0
def urls_js(request=None):
    js_var_name = getattr(settings, 'JS_REVERSE_JS_VAR_NAME', JS_VAR_NAME)
    if not JS_IDENTIFIER_RE.match(js_var_name.upper()):
        raise ImproperlyConfigured(
            'JS_REVERSE_JS_VAR_NAME setting "%s" is not a valid javascript identifier.'
            % (js_var_name))

    js_global_object_name = getattr(settings,
                                    'JS_REVERSE_JS_GLOBAL_OBJECT_NAME',
                                    JS_GLOBAL_OBJECT_NAME)
    if not JS_IDENTIFIER_RE.match(js_global_object_name.upper()):
        raise ImproperlyConfigured(
            'JS_REVERSE_JS_GLOBAL_OBJECT_NAME setting "%s" is not a valid javascript identifier.'
            % (js_global_object_name))

    minfiy = getattr(settings, 'JS_REVERSE_JS_MINIFY', JS_MINIFY)
    if not isinstance(minfiy, bool):
        raise ImproperlyConfigured(
            'JS_REVERSE_JS_MINIFY setting "%s" is not a valid. Needs to be set to True or False.'
            % (minfiy))

    script_prefix_via_config = getattr(settings, 'JS_REVERSE_SCRIPT_PREFIX',
                                       None)
    if script_prefix_via_config:
        script_prefix = script_prefix_via_config
        if not script_prefix.endswith('/'):
            script_prefix = '{0}/'.format(script_prefix)
    else:
        script_prefix = urlresolvers.get_script_prefix()

    default_urlresolver = urlresolvers.get_resolver(
        getattr(request, 'urlconf', None))
    response_body = loader.render_to_string(
        'django_js_reverse/urls_js.tpl', {
            'urls': sorted(list(prepare_url_list(default_urlresolver))),
            'url_prefix': script_prefix,
            'js_var_name': js_var_name,
            'js_global_object_name': js_global_object_name,
        })

    if minfiy:
        response_body = minify(response_body,
                               mangle=True,
                               mangle_toplevel=False)

    if not request:
        return response_body
    else:
        return HttpResponse(response_body,
                            **{'content_type': 'application/javascript'})
Example #54
0
def exportMinFileJS(listFiles, outputDir):
    
    for i in range(len(listFiles)):
        files = open( listFiles[i]['path'], 'r')
        outPutFiles = open(outputDir/listFiles[i]['basename'], 'w')
        for linea in files.readlines():
            if not(linea.find('//')):
                outPutFiles.write(linea)
            else:
                files.seek(0)
                break
        
        outPutFiles.write(minify(files.read(), mangle=False, mangle_toplevel=False))
        outPutFiles.close()
Example #55
0
def minify_stuff():
    print "in /minify!"
    dat = request.json
    print dat
    minifiedjs = ""
    minifiedcss = ""
    if 'js' in dat:
        minifiedjs = minify(dat['js'])
    if 'css' in dat:
        minifiedcss = compress(dat['css'])
    toRet = {"js": minifiedjs, "css": minifiedcss}
    print 'RETURN IN MINIFY: '
    print toRet
    return jsonify(result=toRet)
Example #56
0
    def _compress(self):
        output = []
        src_paths = []

        for src in self.includes:
            src_paths.append('www/%s' % src)

            with open('www/%s' % src) as f:
                print '- compressing %s' % src
                output.append(minify(f.read()))

        context = make_context()
        context['paths'] = src_paths

        return '\n'.join(output)