tools/update-prod-static: Avoid shelling out for mkdir, cp, mv, rm.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg
2018-07-18 17:50:16 -04:00
committed by Tim Abbott
parent 19bdf54f33
commit 0bfcd2294e

View File

@@ -5,6 +5,7 @@
import os import os
import argparse import argparse
import shutil
import sys import sys
# We need settings so we can figure out where the prod-static directory is. # We need settings so we can figure out where the prod-static directory is.
@@ -30,7 +31,7 @@ prev_deploy = args.prev_deploy
os.chdir(settings.DEPLOY_ROOT) os.chdir(settings.DEPLOY_ROOT)
# Redirect child processes' output to a log file (most recent run only). # Redirect child processes' output to a log file (most recent run only).
run(["mkdir", "-p", "var/log"]) os.makedirs("var/log", exist_ok=True)
fp = open('var/log/update-prod-static.log', 'w') fp = open('var/log/update-prod-static.log', 'w')
# Install node packages # Install node packages
@@ -56,12 +57,11 @@ run(['./tools/minify-js'] + (['--prev-deploy', prev_deploy] if prev_deploy else
stdout=fp, stderr=fp) stdout=fp, stderr=fp)
# Copy the KaTeX files outside node_modules # Copy the KaTeX files outside node_modules
run(['mkdir', '-p', os.path.join(settings.STATIC_ROOT, 'node_modules/katex/dist/')], os.makedirs(os.path.join(settings.STATIC_ROOT, 'node_modules/katex/dist/'),
stdout=fp, stderr=fp) exist_ok=True)
run(['cp', 'node_modules/katex/dist/katex.css', shutil.copy('node_modules/katex/dist/katex.css',
os.path.join(settings.STATIC_ROOT, 'node_modules/katex/dist/')], os.path.join(settings.STATIC_ROOT, 'node_modules/katex/dist/'))
stdout=fp, stderr=fp)
run(['cp', '-R', 'node_modules/katex/dist/fonts', run(['cp', '-R', 'node_modules/katex/dist/fonts',
os.path.join(settings.STATIC_ROOT, 'node_modules/katex/dist/fonts')], os.path.join(settings.STATIC_ROOT, 'node_modules/katex/dist/fonts')],
@@ -76,7 +76,8 @@ CSS_FILES = [
# Copy CSS files in node_modules to prod-static/serve # Copy CSS files in node_modules to prod-static/serve
for css_file in CSS_FILES: for css_file in CSS_FILES:
run(['cp', '--parents', css_file, settings.STATIC_ROOT]) os.makedirs(os.path.join(settings.STATIC_ROOT, os.path.dirname(css_file)), exist_ok=True)
shutil.copyfile(css_file, os.path.join(settings.STATIC_ROOT, css_file))
# Collect the files that we're going to serve; this creates prod-static/serve. # Collect the files that we're going to serve; this creates prod-static/serve.
run(['./manage.py', 'collectstatic', '--no-default-ignore', run(['./manage.py', 'collectstatic', '--no-default-ignore',
@@ -85,21 +86,20 @@ run(['./manage.py', 'collectstatic', '--no-default-ignore',
if not settings.PRODUCTION: if not settings.PRODUCTION:
# When building a release tarball, we need to move staticfiles.json # When building a release tarball, we need to move staticfiles.json
run(['mv', 'prod-static/serve/staticfiles.json', 'staticfiles.json'], shutil.move('prod-static/serve/staticfiles.json', 'staticfiles.json')
stdout=fp, stderr=fp)
# Compile translation strings to generate `.mo` files. # Compile translation strings to generate `.mo` files.
run(['./manage.py', 'compilemessages'], stdout=fp, stderr=fp) run(['./manage.py', 'compilemessages'], stdout=fp, stderr=fp)
# Move the source maps out of the serve/ directory and into their # Move the source maps out of the serve/ directory and into their
# proper place. # proper place.
run(['rm', '-rf', 'prod-static/source-map'], stdout=fp, stderr=fp) if os.path.exists('prod-static/source-map'):
shutil.rmtree('prod-static/source-map')
# Needed if PRODUCTION # Needed if PRODUCTION
run(['mkdir', '-p', 'prod-static'], stdout=fp, stderr=fp) os.makedirs('prod-static', exist_ok=True)
run(['mv', os.path.join(settings.STATIC_ROOT, 'source-map'), 'prod-static/source-map'], shutil.move(os.path.join(settings.STATIC_ROOT, 'source-map'), 'prod-static/source-map')
stdout=fp, stderr=fp)
# Move language_options.json to the production release # Move language_options.json to the production release
run(['cp', '-aT', 'static/locale', os.path.join(settings.STATIC_ROOT, 'locale')], run(['cp', '-aT', 'static/locale', os.path.join(settings.STATIC_ROOT, 'locale')],