Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def load_json(string):
return json.loads(jsmin.jsmin(string))
# upload ui html to target
ssh.copy(html_local, html_target)
# upload assets to the dataset
webresources_local = opj(webui_local, 'assets')
webresources_remote = opj(path, WEB_HTML_DIR)
ssh('mkdir -p {}'.format(sh_quote(webresources_remote)))
ssh.copy(webresources_local, webresources_remote, recursive=True)
# minimize and upload js assets
for js_file in glob(opj(webresources_local, 'js', '*.js')):
with open(js_file) as asset:
try:
from jsmin import jsmin
# jsmin = lambda x: x # no minimization
minified = jsmin(asset.read()) # minify asset
except ImportError:
lgr.warning(
"Will not minify web interface javascript, no jsmin available")
minified = asset.read() # no minify available
with make_tempfile(content=minified) as tempf: # write minified to tempfile
js_name = js_file.split('/')[-1]
ssh.copy(tempf, opj(webresources_remote, 'assets', 'js', js_name)) # and upload js
# explicitly make web+metadata dir of dataset world-readable, if shared set to 'all'
mode = None
if shared in (True, 'true', 'all', 'world', 'everybody'):
mode = 'a+rX'
elif shared == 'group':
mode = 'g+rX'
elif str(shared).startswith('0'):
mode = shared
if config['hashfilenames'] == True:
filename['raw'] = '{0}-{1}.{2}'.format(bundlename, md5, 'js')
filename['min'] = '{0}-{1}.{2}'.format(bundlename, md5, 'min.js')
filename['gz'] = '{0}-{1}.{2}'.format(bundlename, md5, 'min.js.gz')
else:
filename['raw'] = '{0}.{1}'.format(bundlename, 'js')
filename['min'] = '{0}.{1}'.format(bundlename, 'min.js')
filename['gz'] = '{0}.{1}'.format(bundlename, 'min.js.gz')
bundleinfo['javascript']['output'] = filename
#Write the bundle file raw data
writedata(configfile, config, config['output'], filename['raw'], rawdata)
#Minify
mindata = jsmin.jsmin(rawdata)
writedata(configfile, config, config['output'], filename['min'], mindata)
#Gzip
gzdata = zlib.compress(mindata)
writedata(configfile, config, config['output'], filename['gz'], gzdata)
#Save the sizes
bundleinfo['javascript']['size'] = {
'raw':len(rawdata),
'min':len(mindata),
'gz':len(gzdata)
}
#Now CSS (same as above, but a little different)
if bundledata['includes']['css']:
logging.info('-Processing CSS')
def __minify_data(self, data):
return jsmin.jsmin(str(data))
def CompressScript(lines, do_jsmin):
# If we're not expecting this code to be user visible, we can run it through
# a more aggressive minifier.
if do_jsmin:
return jsmin.jsmin(lines)
# Remove stuff from the source that we don't want to appear when
# people print the source code using Function.prototype.toString().
# Note that we could easily compress the scripts mode but don't
# since we want it to remain readable.
#lines = re.sub('//.*\n', '\n', lines) # end-of-line comments
#lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments.
#lines = re.sub('\s+\n+', '\n', lines) # trailing whitespace
return lines
import os
import re
import shutil
from csscompressor import compress as cssmin
from jsmin import jsmin
from .common import GrablibError, ProcessBase, logger
MINIFY_LOOKUP = [
(r'.js$', jsmin),
(r'.css$', cssmin),
]
class MinifyLibs(ProcessBase):
"""
minify and concatenate js and css
"""
def __init__(self, minify_info, **kwargs):
"""
initialize MinifyLibs.
:param minify_info: dict of: files to generate => list of regexes of files to generate it from
:param sites: dict of names of sites to simplify similar urls, see examples.
"""
super(MinifyLibs, self).__init__(**kwargs)
try:
# packer or packer-io?
binary = 'packer-io'
if shutil.which(binary) == None:
binary = 'packer'
if shutil.which(binary) == None:
print("packer not found. Install it: "
"https://www.packer.io/docs/install/index.html")
return 254
# run packer with relevant config minified
configfile = os.path.join(DIRS.user_config_dir, 'config.js')
with open(configfile, 'r') as config:
f = create_cachefd('packer_var_file.json')
f.write(jsmin(config.read()))
f.close()
flags = ['-var-file={}'.format(f.name)]
packer_cache_dir = os.getenv('PACKER_CACHE_DIR', DIRS.user_cache_dir)
special_env = {'PACKER_CACHE_DIR': packer_cache_dir}
special_env['TMPDIR'] = DIRS.user_cache_dir
if DEBUG:
special_env['PACKER_LOG'] = '1'
flags.append('-on-error=abort')
if args.force:
flags.append('-force')
cmd = [binary, 'build']
cmd.extend(flags)
Raises:
:py:class:`TypeError` - if you pass more than one of the abovementioned args.
"""
Option = namedtuple("Option", ["text", "value"])
ALL_OPTIONS = jsmin('''\
var result_arr = [];
var opt_elements = arguments[0].options;
for(var i = 0; i < opt_elements.length; i++){
var option = opt_elements[i];
result_arr.push([option.innerHTML, option.getAttribute("value")]);
}
return result_arr;
''')
SELECTED_OPTIONS = jsmin('return arguments[0].selectedOptions;')
SELECTED_OPTIONS_TEXT = jsmin('''\
var result_arr = [];
var opt_elements = arguments[0].selectedOptions;
for(var i = 0; i < opt_elements.length; i++){
result_arr.push(opt_elements[i].innerHTML);
}
return result_arr;
''')
SELECTED_OPTIONS_VALUE = jsmin('''\
var result_arr = [];
var opt_elements = arguments[0].selectedOptions;
for(var i = 0; i < opt_elements.length; i++){
result_arr.push(opt_elements[i].getAttribute("value"));
}
return result_arr;
info("Download from http://dl.google.com/closure-compiler/compiler-latest.zip")
try:
import closure_ws
use_compressor = "closure_ws"
info("Using Closure via Web Service - limited to files < 1Mb!")
except ImportError:
info("No closure_ws")
if use_compressor == "closure":
if not warnings:
closure.extra_params = "--warning_level QUIET"
minimize = closure.minimize
elif use_compressor == "closure_ws":
minimize = closure_ws.minimize
elif use_compressor == "jsmin":
minimize = jsmin.jsmin
return minimize
def render(self, context):
return jsmin(self.nodelist.render(context))