-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompression.py
executable file
·124 lines (105 loc) · 4.57 KB
/
compression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
import os
import os.path
from optparse import OptionParser
from configobj import ConfigObj
#path to compressor
YUI_COMPRESSOR = os.path.relpath('./yuicompressor-2.4.2.jar')
# path to base directory of resources
resources = os.path.relpath('./resources')
def combine(in_files, out_file):
open_out_file = open(out_file, 'w')
for in_file in in_files:
open_in_file = open(in_file)
data = open_in_file.read()
open_in_file.close()
open_out_file.write(data)
print ' + %s' % in_file
print 'Combined: %s' % out_file
open_out_file.close()
def compress(in_file, out_file, compressor=YUI_COMPRESSOR, temp_file='debug_compress.temp', in_type='js', verbose=False):
open_temp = open(temp_file, 'w')
open_in_file = open(in_file, 'r')
data = open_in_file.read()
open_in_file.close()
open_temp.write(data)
open_temp.close()
options = ['-o "%s"' % out_file,
'--type %s' % in_type]
if verbose:
options.append('-v')
if compressor == YUI_COMPRESSOR:
os.system('java -jar "%s" %s "%s"' % (YUI_COMPRESSOR,
' '.join(options),
temp_file))
original_size = os.path.getsize(temp_file)
new_size = os.path.getsize(out_file)
print ' + %s' % in_file
print 'Combined and compressed: %s' % out_file
print 'Original: %.2f kB' % (original_size / 1024.0)
print 'Compressed: %.2f kB' % (new_size / 1024.0)
print 'Reduction: %.1f%%' % (float(original_size - new_size) / original_size * 100)
def run_scripts(do_compress=True, verbose=False):
for file_set in config['scripts']:
print 'Combining JavaScript...'
combine(file_set['files-to-combine'],
file_set['out-file-combined-path'])
if do_compress:
print 'Compressing JavaScript...'
compress(file_set['out-file-combined-path'],
file_set['out-file-combined-compressed-path'],
verbose=verbose)
def run_styles(config, do_compress=True, verbose=False):
for file_set in config['stylesheets']:
print 'Combining CSS...'
combine(file_set['files-to-combine'],
file_set['out-file-combined_path'])
if do_compress:
print 'Compressing CSS...'
compress(file_set['out-file-combined_path'],
file_set['out-file-combined-compressed-path'],
in_type='css', verbose=verbose)
def parse_config(filename):
config = ConfigObj(filename)
config.walk(lower_config_keys, call_on_sections=True)
config.walk(make_paths)
return config
def lower_config_keys(section, key):
new_key = key.lower()
section.rename(key, new_key)
def make_paths(section, key):
val = section[key]
section[key] = os.path.join(*val)
def run(args, options):
config_options = parse_config(args[0])
if (options.scripts and options.minify) or \
(not options.scripts and not options.stylesheets and options.minify):
run_scripts(config_options, verbose=options.verbose)
elif (not options.scripts and not options.stylesheets) or \
(options.scripts and not options.stylesheets and options.combine):
run_scripts(config_options, False)
if (options.stylesheets and options.minify) or \
(not options.scripts and not options.stylesheets and options.minify):
run_styles(config_options, verbose=options.verbose)
elif (not options.scripts and not options.stylesheets) or \
(not options.scripts and options.stylesheets and options.combine):
run_styles(config_options, False)
def main():
usage = 'usage: %prog [options] config_file'
parser = OptionParser(usage=usage)
parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
default=False, help='Add verbosity to compressor')
parser.add_option('-m', '--minify', action='store_true', dest='minify',
default=False, help='Combine then minify files')
parser.add_option('-c', '--combine', action='store_true', dest='combine',
default=False, help='Only combine files')
parser.add_option('-s', '--stylesheets', action='store_true', dest='stylesheets',
default=False, help='Do action to stylesheets')
parser.add_option('-j', '--scripts', action='store_true', dest='scripts',
default=False, help='Do action to scripts')
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error('config_file is missing')
run(args, options)
if __name__ == '__main__':
main()