1+ #! /usr/bin/env bash
2+
3+ # Executes the Jupyter Notebook given with the command line option --jupyterNotebook and creates a report directory for the results (ipynb, md, pdf)..
4+
5+ # Requires executeJupyterNotebook.sh, cleanupAfterReportGeneration.sh
6+
7+ # Overrideable Constants (defaults also defined in sub scripts)
8+ REPORTS_DIRECTORY=${REPORTS_DIRECTORY:- " reports" }
9+
10+ # Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
11+ set -o errexit -o pipefail
12+
13+ # Function to display script usage
14+ usage () {
15+ echo " Usage: $0 --jupyterNotebook nameOfTheJupyterNotebook [--reportName nameOfTheReportsDirectory]"
16+ echo " Example: $0 --jupyterNotebook ArtifactDependencies.ipynb"
17+ exit 1
18+ }
19+
20+ camel_to_kebab_case_file_name () {
21+ basename " ${1% .* } " | sed -r ' s/([a-z0-9])([A-Z])/\1-\2/g' | tr ' [:upper:]' ' [:lower:]'
22+ }
23+
24+ # Default values
25+ reportName=" "
26+ jupyterNotebook=" "
27+
28+ # Parse command line arguments
29+ while [[ $# -gt 0 ]]; do
30+ commandLineOption=" ${1} "
31+ case ${commandLineOption} in
32+ --jupyterNotebook)
33+ jupyterNotebook=" ${2} "
34+ shift
35+ ;;
36+ --reportName)
37+ reportName=" ${2} "
38+ shift
39+ ;;
40+
41+ * )
42+ echo " executeJupyterNotebookReports: Error: Unknown option: ${commandLineOption} "
43+ usage
44+ ;;
45+ esac
46+ shift
47+ done
48+
49+ if [[ -z ${jupyterNotebook} ]]; then
50+ echo " ${USAGE} "
51+ exit 1
52+ fi
53+
54+ if [[ -z ${reportName} ]]; then
55+ reportName=$( camel_to_kebab_case_file_name " ${jupyterNotebook} " )
56+ echo " executeJupyterNotebookReports: reportName defaults to ${reportName} "
57+ fi
58+
59+ # # Get this "scripts" directory if not already set
60+ # Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
61+ # CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
62+ # This way non-standard tools like readlink aren't needed.
63+ SCRIPTS_DIR=${SCRIPTS_DIR:- $( CDPATH=. cd -- " $( dirname -- " ${BASH_SOURCE[0]} " ) " && pwd -P )} # Repository directory containing the shell scripts
64+ echo " executeJupyterNotebookReports: SCRIPTS_DIR=${SCRIPTS_DIR} "
65+
66+ # Get the "scripts" directory by taking the path of this script and going one directory up.
67+ REPORTS_SCRIPT_DIR=${REPORTS_SCRIPT_DIR:- " ${SCRIPTS_DIR} /reports" } # Repository directory containing the report scripts
68+ echo " executeJupyterNotebookReports: REPORTS_SCRIPT_DIR=${REPORTS_SCRIPT_DIR} "
69+
70+ # Get the "jupyter" directory by taking the path of this script and going two directory up and then to "jupyter".
71+ JUPYTER_NOTEBOOK_DIRECTORY=${JUPYTER_NOTEBOOK_DIRECTORY:- " ${SCRIPTS_DIR} /../jupyter" } # Repository directory containing the Jupyter Notebooks
72+ echo " executeJupyterNotebookReports: JUPYTER_NOTEBOOK_DIRECTORY=${JUPYTER_NOTEBOOK_DIRECTORY} "
73+
74+ # Create report directory
75+ FULL_REPORT_DIRECTORY=" ${REPORTS_DIRECTORY} /${reportName} "
76+ mkdir -p " ${FULL_REPORT_DIRECTORY} "
77+
78+ # Execute and convert the given Jupyter Notebook within the given reports directory
79+ (cd " ${FULL_REPORT_DIRECTORY} " && exec " ${SCRIPTS_DIR} /executeJupyterNotebook.sh" " ${JUPYTER_NOTEBOOK_DIRECTORY} /${jupyterNotebook} " )
80+
81+ # Clean-up after report generation. Empty reports will be deleted.
82+ source " ${SCRIPTS_DIR} /cleanupAfterReportGeneration.sh" " ${FULL_REPORT_DIRECTORY} "
0 commit comments