chriswarbo-net: b628d6e94f8c0097d14c609b45904ced6d7f1a16

     1: #!/usr/bin/env bash
     2: set -e
     3: 
     4: if PROCESS=$(pgrep ".*8666.*" | grep python)
     5: then
     6:     echo -e "Server already running, aborting\\n$PROCESS" 1>&2
     7:     exit 1
     8: fi
     9: 
    10: function ignoreInSource {
    11:     # Things we don't expect to be reachable; mostly redirects from old URLs
    12:     grep -v "./activecode"          |
    13:     grep -v "./archive.html"        |
    14:     grep -v "./arduino"             |
    15:     grep -v "./blog.html"           |
    16:     grep -v "./blog/index.html"     |
    17:     grep -v "./data_custom"         |
    18:     grep -v "./essays"              |
    19:     grep -v "./index.php"           |
    20:     grep -v "./maze"                |
    21:     grep -v "./nixos"               |
    22:     grep -v "./optimisation"        |
    23:     grep -v "./plumb"               |
    24:     grep -v "./posts"               |
    25:     grep -v "./powerplay"           |
    26:     grep -v "./procedural"          |
    27:     grep -v "./projects.html"       |
    28:     grep -v "./projects/index.html" |
    29:     grep -v "./projects/repos"      |
    30:     grep -v "./redirect.html"       |
    31:     grep -v "./repos"               |
    32:     grep -v "./turtleview"
    33: 
    34: }
    35: 
    36: function cleanup {
    37:     echo "all_pages_reachable exiting, killing server (PID $SERVER_PID)" 1>&2
    38:     kill "$SERVER_PID" || true
    39:     sleep 3
    40:     kill -9 "$SERVER_PID" || true
    41: }
    42: 
    43: # shellcheck disable=SC2154
    44: pushd "$rendered" > /dev/null
    45: 
    46:     SOURCE_CONTENT=$(find . | ignoreInSource | sort)
    47: 
    48:     darkhttpd "$PWD" --port 8666 > /dev/null &
    49:     SERVER_PID="$!"
    50:     sleep 2
    51: 
    52:     # Kill the server when we exit
    53:     trap cleanup EXIT
    54: 
    55: popd > /dev/null
    56: 
    57: # Crawl the temporary site. Unfinished pages are intentionally unreachable
    58: # from index.html, but they should still be reachable from unfinished.html.
    59: # Exclude /git since it's added in during deployment, and repos/ since the
    60: # content may not be under our control.
    61: OUTPUT=$(wget --page-requisites --mirror --no-parent --content-on-error \
    62:               -X "/git,/projects/repos"              \
    63:               "http://localhost:8666"                                   \
    64:               "http://localhost:8666/unfinished.html" 2>&1) || {
    65:     echo "FIXME: wget exited with error code" 1>&2
    66: }
    67: 
    68: [[ -d "localhost:8666" ]] || {
    69:     echo "$OUTPUT" 1>&2
    70:     echo "Didn't find 'localhost:8666' in downloaded content" 1>&2
    71:     exit 1
    72: }
    73: 
    74: # Compare the content we've downloaded to that in $rendered
    75: 
    76: pushd "localhost:8666" > /dev/null
    77: 
    78:     DEST_CONTENT=$(find . | sort)
    79: 
    80: popd > /dev/null
    81: 
    82: # We could use diff, but meh
    83: ERR=0
    84: while read -r SOURCE_FILE
    85: do
    86:     if ! echo "$DEST_CONTENT" | grep -Fx "$SOURCE_FILE" > /dev/null
    87:     then
    88:         if echo "$SOURCE_FILE" | grep "^./js/" > /dev/null
    89:         then
    90:             echo "Skipping unreachable Javascript file '$SOURCE_FILE'"
    91:             continue
    92:         fi
    93: 
    94:         echo "Rendered file '$SOURCE_FILE' wasn't reached" 1>&2
    95:         ERR=1
    96:     fi
    97: done < <(echo "$SOURCE_CONTENT")
    98: 
    99: while read -r DEST_FILE
   100: do
   101:     echo "$SOURCE_CONTENT" | grep -Fx "$DEST_FILE" > /dev/null || {
   102:         echo "Reached unexpected file '$DEST_FILE'" 1>&2
   103:         ERR=1
   104:     }
   105: done < <(echo "$SOURCE_CONTENT")
   106: 
   107: [[ "$ERR" -eq 0 ]] || {
   108:     echo "wget output follows: " 1>&2
   109:     echo "$OUTPUT" 1>&2
   110:     echo
   111:     echo "End wget output (look before it to see the specific error)" 1>&2
   112: }
   113: 
   114: exit "$ERR"

Generated by git2html.