Minor cleanup of minify js script

This commit is contained in:
Calum Lind 2015-08-14 00:03:16 +01:00
parent 4196912966
commit 6020809462

View File

@ -9,6 +9,12 @@
# See LICENSE for more details. # See LICENSE for more details.
# #
"""Minifies the Webui JS files.
Usage: python minify_web_js.py deluge/ui/web/js/deluge-all
"""
import fileinput import fileinput
import fnmatch import fnmatch
import os import os
@ -16,57 +22,53 @@ import sys
from slimit import minify from slimit import minify
"""Minifies the Webui JS files
Usage: python minify_web_js.py deluge/ui/web/js/deluge-all
"""
if len(sys.argv) != 2: if len(sys.argv) != 2:
print 'Specify a source js directory... e.g. deluge/ui/web/js/deluge-all' print 'Specify a source js directory, e.g. deluge/ui/web/js/deluge-all'
sys.exit(1) sys.exit(1)
SOURCE_DIR = os.path.abspath(sys.argv[1]) SOURCE_DIR = os.path.abspath(sys.argv[1])
BUILD_NAME = os.path.basename(SOURCE_DIR) BUILD_NAME = os.path.basename(SOURCE_DIR)
BUILD_DIR = os.path.dirname(SOURCE_DIR) BUILD_DIR = os.path.dirname(SOURCE_DIR)
SRC_FILE_LIST = [] SRC_FILE_LIST = []
for root, dirnames, filenames in os.walk(SOURCE_DIR): for root, dirnames, filenames in os.walk(SOURCE_DIR):
dirnames.sort(reverse=True) dirnames.sort(reverse=True)
files = fnmatch.filter(filenames, "*.js") filenames_js = fnmatch.filter(filenames, '*.js')
files.sort() filenames_js.sort()
order_file = os.path.join(root, '.order') order_file = os.path.join(root, '.order')
if os.path.isfile(order_file): if os.path.isfile(order_file):
with open(order_file, 'r') as f: with open(order_file, 'r') as _file:
for line in f: for line in _file:
line = line.strip() line = line.strip()
if not line or line[0] == '#': if not line or line[0] == '#':
continue continue
pos, filename = line.split() order_pos, order_filename = line.split()
files.pop(files.index(filename)) filenames_js.pop(filenames_js.index(order_filename))
if pos == '+': if order_pos == '+':
files.insert(0, filename) filenames_js.insert(0, order_filename)
if not dirnames: # Ensure root directory files are bottom of list.
for fnames_ordered in reversed(files): if dirnames:
SRC_FILE_LIST.insert(0, os.path.join(root, fnames_ordered)) for filename in filenames_js:
SRC_FILE_LIST.append(os.path.join(root, filename))
else: else:
for fnames_ordered in files: for filename in reversed(filenames_js):
SRC_FILE_LIST.append(os.path.join(root, fnames_ordered)) SRC_FILE_LIST.insert(0, os.path.join(root, filename))
if not SRC_FILE_LIST: if not SRC_FILE_LIST:
print 'No js files found' print 'No js files found'
sys.exit(1) sys.exit(1)
print 'Minifying %s' % BUILD_NAME print 'Minifying %s ...' % BUILD_NAME
# generate the single file, unminified version # Create the unminified debug file.
file_dbg_js = os.path.join(BUILD_DIR, BUILD_NAME + '-debug.js') file_debug_js = os.path.join(BUILD_DIR, BUILD_NAME + '-debug.js')
with open(file_dbg_js, 'w') as _file: with open(file_debug_js, 'w') as _file:
input_lines = fileinput.input(SRC_FILE_LIST) input_lines = fileinput.input(SRC_FILE_LIST)
_file.writelines(input_lines) _file.writelines(input_lines)
# generate the minified version file_minified_js = os.path.join(BUILD_DIR, BUILD_NAME + '.js')
fileout_js = os.path.join(BUILD_DIR, BUILD_NAME + '.js') with open(file_minified_js, 'w') as file_out:
with open(fileout_js, 'w') as out_file: with open(file_debug_js, 'r') as file_in:
with open(file_dbg_js, 'r') as in_file: file_out.write(minify(file_in.read()))
out_file.write(minify(in_file.read()))