summaryrefslogtreecommitdiff
path: root/django/contrib/admin/bin/compress.py
blob: 69660284eb75b9cfa33791a4266c760e5177cb18 (plain)
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
#!/usr/bin/env python
import argparse
import subprocess
import sys
from pathlib import Path

try:
    import closure
except ImportError:
    closure_compiler = None
else:
    closure_compiler = closure.get_jar_filename()

js_path = Path(__file__).parent.parent / 'static' / 'admin' / 'js'


def main():
    description = """With no file paths given this script will automatically
compress all jQuery-based files of the admin app. Requires the Google Closure
Compiler library and Java version 6 or later."""
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument('file', nargs='*')
    parser.add_argument(
        "-c", dest="compiler", default="~/bin/compiler.jar",
        help="path to Closure Compiler jar file",
    )
    parser.add_argument("-v", "--verbose", action="store_true", dest="verbose")
    parser.add_argument("-q", "--quiet", action="store_false", dest="verbose")
    options = parser.parse_args()

    compiler = Path(closure_compiler or options.compiler).expanduser()
    if not compiler.exists():
        sys.exit(
            "Google Closure compiler jar file %s not found. Please use the -c "
            "option to specify the path." % compiler
        )

    if not options.file:
        if options.verbose:
            sys.stdout.write("No filenames given; defaulting to admin scripts\n")
        files = [
            js_path / f
            for f in ["actions.js", "collapse.js", "inlines.js", "prepopulate.js"]
        ]
    else:
        files = [Path(f) for f in options.file]

    for file_path in files:
        to_compress = file_path.expanduser()
        if to_compress.exists():
            to_compress_min = to_compress.with_suffix('.min.js')
            cmd = "java -jar %s --js %s --js_output_file %s" % (compiler, to_compress, to_compress_min)
            if options.verbose:
                sys.stdout.write("Running: %s\n" % cmd)
            subprocess.call(cmd.split())
        else:
            sys.stdout.write("File %s not found. Sure it exists?\n" % to_compress)


if __name__ == '__main__':
    main()