Annotation of kupuMPIWG/tools/compress.py, revision 1.1.1.1
1.1 dwinter 1: #!/usr/bin/env python
2:
3: """Remove comments, newlines and redundant whitespace from JavaScript code
4:
5: This reads all paths that were passed in as arguments on the command-line
6: and removes everything that is ignored by JavaScript. This makes that
7: the source isn't readable anymore (which I personally consider bad),
8: but also that less bytes have to be served by the server, scripts are
9: loaded faster and also that they're executed faster.
10:
11: WARNING: This script converts files in place! Original files will be
12: overwritten. Do *not* run this on a development version of your code,
13: since you won't be able to get them back into the original state. This
14: should be ran only by system administrators if they want to speed up
15: their setups.
16: """
17:
18: import sys, re
19:
20: one_line_comment = re.compile(r'^\s*//.*$', re.M)
21: trailing_comment = re.compile(r'//(\w|\s)*$', re.M)
22: multi_line_comment = re.compile(r'^\s*/\*.*?\*/', re.M | re.S)
23: whitespace_after_separator = re.compile(r';\s*', re.M | re.S)
24: whitespace_after_opening_bracket = re.compile(r'{\s*', re.M | re.S)
25: starting_whitespace = re.compile(r'^\s*', re.M | re.S)
26:
27: def strip(data):
28: """Processes the data, removing comments and unecessary whitespace."""
29: data = one_line_comment.sub('', data)
30: data = trailing_comment.sub('', data)
31: data = multi_line_comment.sub('', data)
32: data = whitespace_after_separator.sub(';', data)
33: data = whitespace_after_opening_bracket.sub('{', data)
34: data = starting_whitespace.sub('', data)
35: return data.strip()
36:
37: for file in sys.argv[1:]:
38: data = open(file).read()
39: data = strip(data)
40: open(file, 'w').write(data)
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>