Skip to content

Instantly share code, notes, and snippets.

@cmccandless
Last active November 12, 2018 15:30
Show Gist options
  • Save cmccandless/a1d3a440b44ebadcc21cf77e67439aaa to your computer and use it in GitHub Desktop.
Save cmccandless/a1d3a440b44ebadcc21cf77e67439aaa to your computer and use it in GitHub Desktop.
wazuh-docs requirements file test
#!/usr/bin/env python3.6
import os
msg = {
# 'passed': '✔',
'failed': '🗙',
'passed': ':heavy_check_mark:',
'failed': ':x:',
'timed_out': '**T***'
}
BODY = """# Requirements.txt Status
## Contents
```
{}
```
## Make Target Build Status
{}
\**Timed out after 3 minutes*
"""
def create_table(statuses, pythons):
lines = []
lines.append('| ' + ' | '.join(['Target', *(p.title() for p in pythons)]) + ' |')
lines.append('|:--- |:' + ':|:'.join(['---'] * len(pythons)) + ':|')
for target, status in sorted(statuses.items()):
row = '| {} | '.format(target)
row += ' | '.join(map(msg.get, map(status.get, pythons)))
row += ' |'
lines.append(row)
return '\n'.join(lines)
if __name__ == '__main__':
# echo "# Requirements.txt Status"
# echo
# echo "## Contents"
# echo
# echo '```'
# cat requirements.txt
# echo '```'
# echo
# echo "## Make Target Build Status"
# echo
# python3.6 ./mkstatustable.py
# echo
# echo *\*Timed out after 3 minutes*
with open('requirements.txt') as f:
requirements = f.read()
statuses = {}
log_dir = 'logs'
pythons = os.listdir('venv')
for python in pythons:
logfile = os.path.join(log_dir, python, 'target_results.log')
with open(logfile) as f:
target_statuses = [
line.split(' ')
for line in f.read().split('\n')
if line
]
for target, status in target_statuses:
if target not in statuses:
statuses[target] = {}
statuses[target][python] = status
table = create_table(statuses, sorted(pythons))
print(BODY.format(requirements, table))

Requirements.txt Status

Contents

Sphinx==1.6.5
sphinx-rtd-theme==0.2.4
sphinxcontrib-images==0.7.0

Make Target Build Status

Target Python2.7 Python3.5 Python3.6
applehelp
changes ✔️ ✔️ ✔️
coverage
devhelp ✔️ ✔️ ✔️
dirhtml ✔️ ✔️ ✔️
doctest
epub
gettext ✔️ ✔️ ✔️
html ✔️ ✔️ ✔️
htmlhelp ✔️ ✔️ ✔️
info
json ✔️ ✔️ ✔️
latex ✔️ ✔️ ✔️
latexpdf
latexpdfja
linkcheck T* T* T*
man ✔️ ✔️ ✔️
pickle ✔️ ✔️ ✔️
pseudoxml ✔️ ✔️ ✔️
qthelp
singlehtml ✔️ ✔️ ✔️
texinfo ✔️ ✔️ ✔️
text
xml ✔️ ✔️ ✔️

*Timed out after 3 minutes

Sphinx==1.6.5
sphinx-rtd-theme==0.2.4
sphinxcontrib-images==0.7.0
#!/bin/bash
# wazuh-docs-build.sh
# Author: Corey McCandless <crm1994@gmail.com>
# License: MIT
# Description: This script will initialize a virtual
# environment, install build dependencies,
# and build every make target listed in `make help`.
#
# Output is logged to build.log
# Each make target has a separate $TARGET.log
#
# usage: wazuh-docs-build.sh [python] [timeout] [target]
# arguments:
# python path to python binary (default: python)
# timeout make target timeout (default: 0s)
# target make target (default: all)
#
# examples:
# ./wazuh-docs-build.sh
# ./wazuh-docs-build.sh python2.7 60s
# ./wazuh-docs-build.sh python3 1m
# ./wazuh-docs-build.sh python3 1m html
PYTHON=${1:-python}
TIMEOUT=${2:-0s}
MAKE_TARGET=${3:-all}
BUILD_BASE=build
BUILDDIR="${BUILD_BASE}/${PYTHON}"
LOG_BASE=logs
LOG_DIR="${LOG_BASE}/${PYTHON}"
LOG_TARGET_RESULTS="${LOG_DIR}/target_results.log"
EXIT_ON_FAIL=0
TEMPFILE=/tmp/$$.tmp
echo '0' > $TEMPFILE
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
CURSOR_HOME='\033[999D'
ERASE_TO_END='\033[K'
if [ -d ${LOG_DIR} ]; then
rm -rf ${LOG_DIR}
fi
mkdir -p "${LOG_DIR}"
LOG="${LOG_DIR}/build.log"
cleanup()
{
unlink $TEMPFILE
}
abort()
{
if [ $EXIT_ON_FAIL -eq 1 ]; then
cleanup
echo "ABORTED" >> $LOG
exit ${1:-1}
fi
}
trap 'kill -INT -$pid; exit 1' INT
TIMEOUTS=0
do_make()
{
TARGET=$1
TARGET_LOG="${LOG_DIR}/${TARGET}.log"
printf "Building $TARGET..."
timeout $TIMEOUT make $TARGET BUILDDIR=$BUILDDIR &> ${TARGET_LOG} &
pid=$!
wait $pid
ret=$?
printf "${CURSOR_HOME}${ERASE_TO_END}"
case "$ret" in
2)
printf "${RED}[ FAILED ]${NC} ${TARGET}; see ${TARGET_LOG} for more information\n"
echo "[ FAILED ] ${TARGET}; see ${TARGET_LOG} for more information" >> $LOG
echo "$TARGET failed" >> $LOG_TARGET_RESULTS
abort $ret
;;
124)
printf "${RED}[ TIMEOUT]${NC} ${TARGET}\n"
echo "[ TIMEOUT] ${TARGET}" >> $LOG
echo "$TARGET timed_out" >> $LOG_TARGET_RESULTS
abort $ret
echo $(($(cat $TEMPFILE) + 1)) > $TEMPFILE
;;
*)
printf "${GREEN}[ PASSED ]${NC} ${TARGET}\n"
echo "[ PASSED ] ${TARGET}" >> $LOG
echo "$TARGET passed" >> $LOG_TARGET_RESULTS
;;
esac
return 0
}
VENV_BASE="venv"
mkdir -p "$VENV_BASE"
VENV="$VENV_BASE/$PYTHON"
if [ -d "$VENV" ]; then
rm -rf "$VENV"
fi
echo "Creating virtualenv in $VENV..." | tee $LOG
virtualenv "$VENV" --python=${PYTHON} >> $LOG
echo >> $LOG
echo "Activating virtualenv..." | tee -a $LOG
source "$VENV/bin/activate" >> $LOG
echo >> $LOG
if [ ! -f requirements.txt ]; then
echo "Generating requirements.txt..." | tee $LOG
echo "sphinx==1.6.1" > requirements.txt
echo "sphinx-rtd-theme==0.2.4" >> requirements.txt
echo "sphinxcontrib-images==0.7.0" >> requirements.txt
fi
echo "Installing requirements..." | tee -a $LOG
pip -vvv install -r requirements.txt >> $LOG
echo "Installed Packages:"
pip freeze
echo >> $LOG
mkdir -p "$BUILD_BASE"
if [ "$MAKE_TARGET" != "all" ]; then
do_make $MAKE_TARGET
else
make help | tail -n +2 | cut -d' ' -f3 | while read TARGET; do
do_make $TARGET;
done
TIMEOUTS=$(cat $TEMPFILE)
if [ $TIMEOUTS -gt 1 ]; then
echo "${TIMEOUTS} targets timed out. Consider raising timeout value."
fi
fi
cleanup
echo "Done" >> $LOG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment