collectstatic 2.9 KB
Newer Older
1 2
#!/usr/bin/env bash

Kenneth Reitz's avatar
Kenneth Reitz committed
3 4 5 6 7 8 9 10 11 12
# Django Collectstatic runner. If you have Django installed, collectstatic will
# automatically be executed as part of the build process. If collectstatic
# fails, your build fails.

# This functionality will only activate if Django is in requirements.txt.

# Runtime arguments:
#   - $DISABLE_COLLECTSTATIC: disables this functionality.
#   - $DEBUG_COLLECTSTATIC: upon failure, print out environment variables.

Kenneth Reitz's avatar
Kenneth Reitz committed
13
# shellcheck source=bin/utils
14
source "$BIN_DIR/utils"
15

Kenneth Reitz's avatar
Kenneth Reitz committed
16
# Location of 'manage.py', if it exists.
17
MANAGE_FILE=$(find . -maxdepth 3 -type f -name 'manage.py' -printf '%d\t%P\n' | sort -nk1 | cut -f2 | head -1)
18
MANAGE_FILE=${MANAGE_FILE:-fakepath}
19

Kenneth Reitz's avatar
Kenneth Reitz committed
20
# Legacy file-based support for $DISABLE_COLLECTSTATIC
21
[ -f .heroku/collectstatic_disabled ] && DISABLE_COLLECTSTATIC=1
Kenneth Reitz's avatar
Kenneth Reitz committed
22

23
# Ensure that Django is explicitly specified in requirements.txt
Kenneth Reitz's avatar
Kenneth Reitz committed
24
sp-grep -s django && DJANGO_INSTALLED=1
25

Kenneth Reitz's avatar
v100  
Kenneth Reitz committed
26

27
if [ ! "$DISABLE_COLLECTSTATIC" ] && [ -f "$MANAGE_FILE" ] && [ "$DJANGO_INSTALLED" ]; then
28 29
    set +e

Kenneth Reitz's avatar
Kenneth Reitz committed
30
    puts-step "$ python $MANAGE_FILE collectstatic --noinput"
31

Kenneth Reitz's avatar
Kenneth Reitz committed
32
    # Run collectstatic, cleanup some of the noisy output.
David Zülke's avatar
David Zülke committed
33
    PYTHONPATH=${PYTHONPATH:-.}
34
    export PYTHONPATH
Kenneth Reitz's avatar
Kenneth Reitz committed
35 36 37

    # Create a temporary file for collecting the collectstaic logs.
    COLLECTSTATIC_LOG=$(mktemp)
David Zülke's avatar
David Zülke committed
38

Kenneth Reitz's avatar
Kenneth Reitz committed
39
    python "$MANAGE_FILE" collectstatic --noinput --traceback 2>&1 | tee "$COLLECTSTATIC_LOG" | sed '/^Post-processed/d;/^Copying/d;/^$/d' | indent
Kenneth Reitz's avatar
Kenneth Reitz committed
40 41 42
    COLLECTSTATIC_STATUS="${PIPESTATUS[0]}"

    set -e
43 44

    # Display a warning if collectstatic failed.
Kenneth Reitz's avatar
Kenneth Reitz committed
45
    [ "$COLLECTSTATIC_STATUS" -ne 0 ] && {
46

Kenneth Reitz's avatar
Kenneth Reitz committed
47 48 49
        if grep -q 'SyntaxError' "$COLLECTSTATIC_LOG"; then
            mcount "failure.collectstatic.syntax-error"

Kenneth Reitz's avatar
Kenneth Reitz committed
50
        elif grep -q 'ImproperlyConfigured' "$COLLECTSTATIC_LOG"; then
Kenneth Reitz's avatar
Kenneth Reitz committed
51 52
            mcount "failure.collectstatic.improper-configuration"

Kenneth Reitz's avatar
Kenneth Reitz committed
53
        elif grep -q 'The CSS file' "$COLLECTSTATIC_LOG"; then
Kenneth Reitz's avatar
Kenneth Reitz committed
54 55
            mcount "failure.collectstatic.fancy-references"

Kenneth Reitz's avatar
Kenneth Reitz committed
56
        elif grep -q 'OSError' "$COLLECTSTATIC_LOG"; then
Kenneth Reitz's avatar
Kenneth Reitz committed
57
            mcount "failure.collectstatic.missing-file"
Kenneth Reitz's avatar
Kenneth Reitz committed
58 59 60

        else
            mcount "failure.collectstatic.other"
Kenneth Reitz's avatar
Kenneth Reitz committed
61 62
        fi

63 64 65 66 67 68 69 70
        echo
        echo " !     Error while running '$ python $MANAGE_FILE collectstatic --noinput'."
        echo "       See traceback above for details."
        echo
        echo "       You may need to update application code to resolve this error."
        echo "       Or, you can disable collectstatic for this application:"
        echo
        echo "          $ heroku config:set DISABLE_COLLECTSTATIC=1"
Kenneth Reitz's avatar
Kenneth Reitz committed
71
        echo
72
        echo "       https://devcenter.heroku.com/articles/django-assets"
Kenneth Reitz's avatar
Kenneth Reitz committed
73 74 75 76 77 78

        # Additionally, dump out the environment, if debug mode is on.
        if [ "$DEBUG_COLLECTSTATIC" ]; then
            echo
            echo "****** Collectstatic environment variables:"
            echo
Kenneth Reitz's avatar
Kenneth Reitz committed
79
            env | indent
Kenneth Reitz's avatar
Kenneth Reitz committed
80 81 82
        fi

        # Abort the build.
83 84 85
        exit 1
    }

86 87
    echo
fi