2014-07-10 13:56:38 +00:00
|
|
|
#!/usr/bin/env python
|
2014-09-22 21:15:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 Calum Lind <calumlind@gmail.com>
|
|
|
|
# Copyright (C) 2010 Damien Churchill <damoxc@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
|
|
|
|
# the additional special exception to link portions of this program with the OpenSSL library.
|
|
|
|
# See LICENSE for more details.
|
|
|
|
#
|
|
|
|
|
2015-08-22 13:23:30 +00:00
|
|
|
"""Minifies the WebUI JS files.
|
2015-08-13 23:03:16 +00:00
|
|
|
|
|
|
|
Usage: python minify_web_js.py deluge/ui/web/js/deluge-all
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2017-02-12 11:26:31 +00:00
|
|
|
from __future__ import print_function, unicode_literals
|
2015-10-23 23:58:14 +00:00
|
|
|
|
2014-07-10 13:56:38 +00:00
|
|
|
import fileinput
|
|
|
|
import fnmatch
|
2014-09-03 23:27:11 +00:00
|
|
|
import os
|
2016-11-27 21:36:10 +00:00
|
|
|
import subprocess
|
2014-09-03 23:27:11 +00:00
|
|
|
import sys
|
2021-12-13 21:10:42 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from shutil import which
|
|
|
|
except ImportError:
|
|
|
|
# PY2 Fallback
|
|
|
|
from distutils.spawn import find_executable as which
|
2014-09-03 23:27:11 +00:00
|
|
|
|
2018-10-16 10:30:06 +00:00
|
|
|
closure_cmd = None
|
|
|
|
for cmd in ['closure-compiler', 'closure']:
|
2021-12-13 21:10:42 +00:00
|
|
|
if which(cmd):
|
2018-10-16 10:30:06 +00:00
|
|
|
closure_cmd = cmd
|
|
|
|
break
|
2015-08-22 13:23:30 +00:00
|
|
|
|
2018-10-16 10:30:06 +00:00
|
|
|
|
|
|
|
def minify_closure(file_in, file_out):
|
2015-08-22 13:23:30 +00:00
|
|
|
try:
|
2018-10-16 10:30:06 +00:00
|
|
|
subprocess.check_call(
|
|
|
|
[
|
|
|
|
closure_cmd,
|
|
|
|
'--warning_level',
|
|
|
|
'QUIET',
|
|
|
|
'--language_in=ECMASCRIPT5',
|
|
|
|
'--js',
|
|
|
|
file_in,
|
|
|
|
'--js_output_file',
|
|
|
|
file_out,
|
|
|
|
]
|
|
|
|
)
|
2015-08-22 13:23:30 +00:00
|
|
|
return True
|
2018-10-16 10:30:06 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return False
|
2015-08-22 13:23:30 +00:00
|
|
|
|
2016-11-16 22:18:18 +00:00
|
|
|
|
2021-12-14 22:28:43 +00:00
|
|
|
# Closure outputs smallest files but java-based command, can use rJSmin
|
2018-10-16 10:30:06 +00:00
|
|
|
# as a python-only fallback.
|
|
|
|
#
|
2021-12-14 22:28:43 +00:00
|
|
|
# deluge-all.js: Closure 131K, rJSmin: 148K
|
2018-10-16 10:30:06 +00:00
|
|
|
#
|
|
|
|
if not closure_cmd:
|
|
|
|
try:
|
2021-12-14 22:28:43 +00:00
|
|
|
from rjsmin import jsmin as minify
|
2018-10-16 10:30:06 +00:00
|
|
|
except ImportError:
|
|
|
|
print('Warning: No minifying command found.')
|
|
|
|
minify = None
|
2015-08-22 13:23:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def source_files_list(source_dir):
|
2015-09-04 13:55:47 +00:00
|
|
|
scripts = []
|
2015-08-22 13:23:30 +00:00
|
|
|
for root, dirnames, filenames in os.walk(source_dir):
|
|
|
|
dirnames.sort(reverse=True)
|
2015-09-04 13:55:47 +00:00
|
|
|
files = fnmatch.filter(filenames, '*.js')
|
|
|
|
files.sort()
|
2015-08-22 13:23:30 +00:00
|
|
|
|
|
|
|
order_file = os.path.join(root, '.order')
|
|
|
|
if os.path.isfile(order_file):
|
|
|
|
with open(order_file, 'r') as _file:
|
|
|
|
for line in _file:
|
2015-09-04 13:55:47 +00:00
|
|
|
if line.startswith('+ '):
|
|
|
|
order_filename = line.split()[1]
|
|
|
|
files.pop(files.index(order_filename))
|
|
|
|
files.insert(0, order_filename)
|
2015-08-22 13:23:30 +00:00
|
|
|
|
|
|
|
# Ensure root directory files are bottom of list.
|
|
|
|
if dirnames:
|
2015-09-04 13:55:47 +00:00
|
|
|
scripts.extend([os.path.join(root, f) for f in files])
|
2015-08-22 13:23:30 +00:00
|
|
|
else:
|
2015-09-04 13:55:47 +00:00
|
|
|
for filename in reversed(files):
|
|
|
|
scripts.insert(0, os.path.join(root, filename))
|
|
|
|
return scripts
|
2014-07-10 13:56:38 +00:00
|
|
|
|
|
|
|
|
2015-08-22 13:23:30 +00:00
|
|
|
def concat_src_files(file_list, fileout_path):
|
|
|
|
with open(fileout_path, 'w') as file_out:
|
|
|
|
file_in = fileinput.input(file_list)
|
|
|
|
file_out.writelines(file_in)
|
|
|
|
|
|
|
|
|
|
|
|
def minify_file(file_debug, file_minified):
|
2018-10-16 10:30:06 +00:00
|
|
|
if closure_cmd:
|
2016-05-19 13:12:14 +00:00
|
|
|
return minify_closure(file_debug, file_minified)
|
2018-10-16 10:30:06 +00:00
|
|
|
elif minify:
|
2015-08-25 15:18:02 +00:00
|
|
|
with open(file_minified, 'w') as file_out:
|
|
|
|
with open(file_debug, 'r') as file_in:
|
|
|
|
file_out.write(minify(file_in.read()))
|
2016-05-19 13:12:14 +00:00
|
|
|
return True
|
2015-08-22 13:23:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def minify_js_dir(source_dir):
|
|
|
|
build_name = os.path.basename(source_dir)
|
|
|
|
build_dir = os.path.dirname(source_dir)
|
|
|
|
file_debug_js = os.path.join(build_dir, build_name + '-debug.js')
|
|
|
|
file_minified_js = os.path.join(build_dir, build_name + '.js')
|
|
|
|
source_files = source_files_list(source_dir)
|
|
|
|
|
|
|
|
if not source_files:
|
2015-08-25 12:20:14 +00:00
|
|
|
print('No js files found, skipping %s' % source_dir)
|
2015-08-22 13:23:30 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
concat_src_files(source_files, file_debug_js)
|
2016-11-27 21:36:10 +00:00
|
|
|
print('Minifying %s' % source_dir)
|
|
|
|
if not minify_file(file_debug_js, file_minified_js):
|
2018-10-16 10:30:06 +00:00
|
|
|
print('Warning: Failed minifying files %s, debug only' % source_dir)
|
2021-12-14 22:28:43 +00:00
|
|
|
if os.path.isfile(file_minified_js):
|
|
|
|
os.remove(file_minified_js)
|
2015-08-22 13:23:30 +00:00
|
|
|
|
2016-11-16 22:18:18 +00:00
|
|
|
|
2015-08-22 13:23:30 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) != 2:
|
2018-10-02 14:39:51 +00:00
|
|
|
JS_SOURCE_DIRS = [
|
|
|
|
'deluge/ui/web/js/deluge-all',
|
|
|
|
'deluge/ui/web/js/extjs/ext-extensions',
|
|
|
|
]
|
2015-08-22 13:23:30 +00:00
|
|
|
else:
|
|
|
|
JS_SOURCE_DIRS = [os.path.abspath(sys.argv[1])]
|
2014-07-10 13:56:38 +00:00
|
|
|
|
2015-10-23 23:58:14 +00:00
|
|
|
for js_source_dir in JS_SOURCE_DIRS:
|
|
|
|
minify_js_dir(js_source_dir)
|