Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 54 additions & 17 deletions pyjs/contrib/pyjscompressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import shutil
import subprocess
import sys
import tempfile
from tempfile import NamedTemporaryFile as _NamedTemporaryFile

compiler_path=os.path.join(os.path.dirname(__file__),'compiler.jar')

Expand Down Expand Up @@ -68,9 +68,10 @@ def compile(js_file, js_output_file, html_file=''):
if debug:
args.append('--formatting=pretty_print')

error = subprocess.call(args=args,
process = subprocess.Popen(args=args,
stdout=open(os.devnull, 'w'),
stderr=subprocess.STDOUT)
output, error = process.communicate()

if error:
raise Exception(' '.join([
Expand All @@ -79,7 +80,7 @@ def compile(js_file, js_output_file, html_file=''):


def compress_css(css_file):
css_output_file = tempfile.NamedTemporaryFile()
css_output_file = NamedTemporaryFile()
f = open(css_file)
css = f.read()
css = re.sub(r"\s+([!{};:>+\(\)\],])", r"\1", css)
Expand All @@ -92,13 +93,13 @@ def compress_css(css_file):


def compress_js(js_file):
js_output_file = tempfile.NamedTemporaryFile()
js_output_file = NamedTemporaryFile()
compile(js_file, js_output_file.name)
return finish_compressors(js_output_file.name, js_file)


def compress_html(html_file):
html_output_file = tempfile.NamedTemporaryFile()
html_output_file = NamedTemporaryFile()

f = open(html_file)
html = f.read()
Expand All @@ -123,10 +124,10 @@ def script_repl(matchobj):

js_output_files = []
for script in scripts:
js_file = tempfile.NamedTemporaryFile()
js_file = NamedTemporaryFile()
js_file.write(script)
js_file.flush()
js_output_file = tempfile.NamedTemporaryFile()
js_output_file = NamedTemporaryFile()
js_output_files.append(js_output_file)
compile(js_file.name, js_output_file.name, html_file)

Expand All @@ -148,7 +149,7 @@ def finish_compressors(new_path, old_path):

def compress(path):
try:
ext = os.path.splitext(path)[1]
ext = os.path.splitext(path)[-1]
if ext == '.css':
return compress_css(path)
elif ext == '.js':
Expand Down Expand Up @@ -204,7 +205,7 @@ def getsmallpath(path, max_chars):
return smallpath


def compress_all(path):
def compress_all(path, exts):
# Print headers for progress output
print('%45s %s' % ('Files', 'Compression'))

Expand All @@ -220,7 +221,13 @@ def compress_all(path):
files_to_compress = []
for root, dirs, files in os.walk(path):
for file in files:
files_to_compress.append(os.path.join(root, file))
ext = '.' + file.split('.')[-1]
print ext
if exts:
if ext in exts:# only allow matching extensions
files_to_compress.append(os.path.join(root, file))
else:
files_to_compress.append(os.path.join(root, file))

if num_procs > 1:
proc_pool = multiprocessing.Pool(num_procs)
Expand Down Expand Up @@ -275,17 +282,19 @@ def compress_all(path):
(p_size / 1024., n_size / 1024.)
print('%s %s' % (sizes.ljust(51), "%4.1f%%" % compression))

def dopyjscompressor(directory, c_path=compiler_path, n_procs=0, opts='SIMPLE_OPTIMIZATIONS', dbg=False):
def do_pyjscompressor(directory, c_path=compiler_path, n_procs=0, opts='SIMPLE_OPTIMIZATIONS', dbg=False, allow = None):
global compiler_path
global num_procs
global debug
global optimizations
global temp_files

compiler_path=c_path
num_procs = n_procs
optimizations = opts
debug = dbg

temp_files = list()

if not compiler_path:
# Not specified on command line
# Try environment
Expand Down Expand Up @@ -315,16 +324,37 @@ def dopyjscompressor(directory, c_path=compiler_path, n_procs=0, opts='SIMPLE_OP
except NotImplementedError:
print("Could not determine CPU Count. Using One process")
num_procs = 1


if allow:
[x.strip() for x in allow.split(',')]

print("Running %d processes" % num_procs)

try:
compress_all(directory)
compress_all(directory, allow)
except KeyboardInterrupt:
print('')
print('Compression Aborted')


# writing to a tempFile has different behavior on windows and linux
def NamedTemporaryFile():
global temp_files
if sys.platform == "linux" or sys.platform == "linux2":
OPT_DELETE = True
elif sys.platform == "darwin":
OPT_DELETE= True
elif sys.platform == "win32":
OPT_DELETE = False
tempFile = _NamedTemporaryFile(delete = OPT_DELETE)
temp_files.append(tempFile.name)
return tempFile

def do_cleanup():
for temp_file in temp_files:
if os.path.exists(temp_file):
print 'remove %s'%temp_file
os.remove(temp_file)

def main():
try:
import argparse
Expand Down Expand Up @@ -352,6 +382,9 @@ def main():
parser.add_argument('-o', metavar='OPTIMIZATION_LEVEL', default='SIMPLE_OPTIMIZATIONS',
type=str, dest='optimizations',
help='Closure Compiler Optimization levels')
parser.add_argument('-a', metavar='allow', default='.html,.js,.css', type=str,
dest='allow',
help='List of allowed file extensions (include periods separate by comma: e.g., .html,.css) ')
options = parser.parse_args()
directory = options.directory
else:
Expand All @@ -368,12 +401,16 @@ def main():
parser.add_option('-o', metavar='OPTIMIZATION_LEVEL', default='SIMPLE_OPTIMIZATION',
type=str, dest='optimizations',
help='Closure Compiler Optimization levels')
parser.add_option('-a', metavar='allow', default=0, type=str,
dest='allow',
help='List of allowed file extensions (include periods separate by comma: e.g., .html,.css) ')
options, args = parser.parse_args()
if len(args) != 1:
parser.error('Please specify the directory to compress')
directory = args[0]
dopyjscompressor(directory, options.compiler, options.num_procs, options.optimizations, options.debug)

do_pyjscompressor(directory, options.compiler, options.num_procs, options.optimizations, options.debug, options.allow)
do_cleanup()

if __name__ == '__main__':
main()