Refs #23919 -- Updated contrib.admin's compress.py to use pathlib.

This commit is contained in:
Tom Forbes 2017-07-21 13:32:50 +01:00 committed by Tim Graham
parent 3159ad4df6
commit 462f1589cd
1 changed files with 12 additions and 14 deletions

View File

@ -1,17 +1,17 @@
#!/usr/bin/env python #!/usr/bin/env python
import argparse import argparse
import os
import subprocess import subprocess
import sys import sys
from pathlib import Path
try: try:
import closure import closure
except ImportError: except ImportError:
closure_compiler = None closure_compiler = None
else: else:
closure_compiler = os.path.join(os.path.dirname(closure.__file__), 'closure.jar') closure_compiler = closure.get_jar_filename()
js_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'static', 'admin', 'js') js_path = Path(__file__).parent.parent / 'static' / 'admin' / 'js'
def main(): def main():
@ -28,8 +28,8 @@ Compiler library and Java version 6 or later."""
parser.add_argument("-q", "--quiet", action="store_false", dest="verbose") parser.add_argument("-q", "--quiet", action="store_false", dest="verbose")
options = parser.parse_args() options = parser.parse_args()
compiler = closure_compiler if closure_compiler else os.path.expanduser(options.compiler) compiler = Path(closure_compiler if closure_compiler else options.compiler).expanduser()
if not os.path.exists(compiler): if not compiler.exists():
sys.exit( sys.exit(
"Google Closure compiler jar file %s not found. Please use the -c " "Google Closure compiler jar file %s not found. Please use the -c "
"option to specify the path." % compiler "option to specify the path." % compiler
@ -39,18 +39,16 @@ Compiler library and Java version 6 or later."""
if options.verbose: if options.verbose:
sys.stdout.write("No filenames given; defaulting to admin scripts\n") sys.stdout.write("No filenames given; defaulting to admin scripts\n")
files = [ files = [
os.path.join(js_path, f) for f in js_path / f
["actions.js", "collapse.js", "inlines.js", "prepopulate.js"] for f in ["actions.js", "collapse.js", "inlines.js", "prepopulate.js"]
] ]
else: else:
files = options.file files = [Path(f) for f in options.file]
for file_name in files: for file_path in files:
if not file_name.endswith(".js"): to_compress = file_path.expanduser()
file_name = file_name + ".js" if to_compress.exists():
to_compress = os.path.expanduser(file_name) to_compress_min = to_compress.with_suffix('.min.js')
if os.path.exists(to_compress):
to_compress_min = "%s.min.js" % "".join(file_name.rsplit(".js"))
cmd = "java -jar %s --js %s --js_output_file %s" % (compiler, to_compress, to_compress_min) cmd = "java -jar %s --js %s --js_output_file %s" % (compiler, to_compress, to_compress_min)
if options.verbose: if options.verbose:
sys.stdout.write("Running: %s\n" % cmd) sys.stdout.write("Running: %s\n" % cmd)