Merge pull request #71 from radicallyopensecurity/better-fo-handling

simplified handling of multiple .fo files
This commit is contained in:
John Sinteur 2020-04-03 13:51:06 +02:00 committed by GitHub
commit e89483c107
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 44 deletions

View File

@ -16,7 +16,7 @@
# (at your option) any later version.
VERSION=0.11
VERSION=0.12
DOCBUILDER=/usr/local/bin/docbuilder.py
TEMPLOC=$(mktemp -d)
@ -33,24 +33,25 @@ BRANCH=master
# Set the default PDF name based on the target name
TARGETPDF="target/$TARGET-latest.pdf"
# Read optional arguments
# Reading positional parms is a bit ugly, shifting parms or getopt would be nicer
if [[ ! -z $3 ]]; then
if [[ ! $3 == -* ]]; then
NAMESPACE=$3
else
PARMS=$3
PARAMS=$3
fi
fi
if [[ ! -z $4 ]]; then
if [[ ! $3 == -* ]]; then
BRANCH=$4
else
PARMS="$PARMS $4"
PARAMS="$PARAMS $4"
fi
fi
if [[ $# -ge 5 ]]; then
shift 4
PARMS="$PARMS $@"
PARAMS="$PARAMS $@"
fi
trap cleanup EXIT QUIT
@ -100,14 +101,17 @@ build() {
fi
pushd source &>/dev/null
backwards_compatible
TARGETFO="target/${TARGET}.fo"
if ([[ $TARGET == "quote" ]] || [[ $TARGET == "offerte" ]]); then
TARGETPDF="target/quote_${REPO/off-/}.pdf"
elif [[ $TARGET == "report" ]]; then
TARGETPDF="target/report_${REPO/pen-/}.pdf"
TARGETHTML="target/report_${REPO/pen-/}.html"
TARGETMD="target/report_${REPO/pen-/}.md"
fi
$DOCBUILDER -c -i $TARGET.xml -o ../$TARGETPDF -x ../xslt/generate_$TARGET.xsl $PARMS
$DOCBUILDER -c -i $TARGET.xml -f ../$TARGETFO -o ../$TARGETPDF -x ../xslt/generate_$TARGET.xsl $PARAMS $NOPRINT
if [[ $? -ne 0 ]]; then
echo "[-] Sorry, failed to parse $TARGET. Use \`builder $TARGET $REPO $NAMESPACE $BRANCH -v\` for more information."
echo "[-] Sorry, failed to parse $TARGET. Use \`build $TARGET $REPO $NAMESPACE $BRANCH -v\` for more information."
exit 1
fi
popd &>/dev/null
@ -115,10 +119,23 @@ build() {
echo "[-] hmmm... failed to build PDF file (could not find $TARGETPDF)"
exit 1
fi
if [ $TARGET == "report" ]; then
java -jar /usr/local/bin/saxon/saxon9he.jar -s:source/$TARGET.xml -o:$TARGETHTML -xsl:xslt/generate_html_$TARGET.xsl -xi
if [ ! -f $TARGETHTML ]; then
echo "[-] Note: failed to build HTML file (could not find $TARGETHTML)"
else
pandoc $TARGETHTML -t markdown_strict -o $TARGETMD
if [ ! -f $TARGETMD ]; then
echo "[-] Note: failed to build markdown file (could not find $TARGETMD)"
fi
fi
fi
}
add_to_repo() {
git add $TARGETPDF
git add $TARGETHTML &>/dev/null
git add $TARGETMD &>/dev/null
git add target/waiver_?*.pdf &>/dev/null
git add target/execsummary.pdf &>/dev/null
git commit -q -m "$TARGETPDF proudly manufactured using ChatOps" &>/dev/null
@ -130,5 +147,14 @@ echo "builder v$VERSION - Rocking your world, one build at a time..."
clone_repo
build
add_to_repo
echo "[+] listo! Check out $GITWEB/$NAMESPACE/$REPO/raw/$BRANCH/$TARGETPDF"
echo " [+] Get PDF: $GITWEB/$NAMESPACE/$REPO/raw/$BRANCH/$TARGETPDF"
if [[ -f target/execsummary.pdf ]]; then
echo " [+] Get exec summary PDF: $GITWEB/$NAMESPACE/$REPO/raw/$BRANCH/target/execsummary.pdf"
fi
if [[ $TARGET == "report" && -f $TARGETHTML ]]; then
echo " [+] Download HTML: $GITWEB/$NAMESPACE/$REPO/raw/$BRANCH/$TARGETHTML"
fi
if [[ $TARGET == "report" && -f $TARGETMD ]]; then
echo " [+] Quick look (rendered MarkDown): $GITWEB/$NAMESPACE/$REPO/blob/$BRANCH/$TARGETMD"
fi
exit 0

View File

@ -233,43 +233,20 @@ def main():
format(options['output'], exception.strerror), result)
result = to_fo(options)
if result:
if OFFERTE in options['xslt']: # an offerte can generate multiple fo's
report_output = options['output']
verboseprint('generating separate waivers detected')
output_dir = os.path.dirname(options['output'])
fop_dir = os.path.dirname(options['fop'])
try:
for fop in [os.path.splitext(x)[0] for x in
os.listdir(fop_dir) if x.endswith('fo')]:
if WAIVER in fop:
options['output'] = output_dir + os.sep + fop + '.pdf'
else:
options['output'] = report_output
options['fop'] = fop_dir + os.sep + fop + '.fo'
result = to_pdf(options) and result
except OSError as exception:
print_exit('[-] ERR: {0}'.format(exception.strerror),
exception.errno)
if options['execsummary']: # we're generating a summary as well as a report
report_output = options['output']
verboseprint('generating additional executive summary')
output_dir = os.path.dirname(options['output'])
fop_dir = os.path.dirname(options['fop'])
try:
for fop in [os.path.splitext(x)[0] for x in
os.listdir(fop_dir) if x.endswith('fo')]:
if EXECSUMMARY in fop:
options['output'] = output_dir + os.sep + fop + '.pdf'
else:
options['output'] = report_output
options['fop'] = fop_dir + os.sep + fop + '.fo'
result = to_pdf(options) and result
except OSError as exception:
print_exit('[-] ERR: {0}'.format(exception.strerror),
exception.errno)
else:
result = to_pdf(options)
output_dir = os.path.dirname(options['output'])
fop_dir = os.path.dirname(options['fop'])
remaining_fo = [os.path.splitext(x)[0] for x in os.listdir(fop_dir) if x.endswith('fo') and not 'offerte' in x and not 'report' in x]
result = to_pdf(options)
if len(remaining_fo) > 0:
print('[+] Generating additional .fo files...')
try:
for fo in remaining_fo:
options['output'] = output_dir + os.sep + fo + '.pdf'
options['fop'] = fop_dir + os.sep + fo + '.fo'
result = to_pdf(options) and result
except OSError as exception:
print_exit('[-] ERR: {0}'.format(exception.strerror),
exception.errno)
else:
print_exit('[-] Unsuccessful (error {0})'.format(result), result)
sys.exit(not result)