11#! /bin/bash
22
3+ retcode=0
4+ ping_url=" https://api.rosette.com/rest/v1"
5+
6+ # ------------------ Functions ----------------------------------------------------
37# Gets called when the user doesn't provide any args
48function HELP {
5- echo -e " \nusage: source_file.py --key API_KEY [--url ALT_URL]"
9+ echo -e " \nusage: --key API_KEY [--FILENAME filename] [--url ALT_URL]"
610 echo " API_KEY - Rosette API key (required)"
711 echo " FILENAME - Python source file (optional)"
812 echo " ALT_URL - Alternate service URL (optional)"
@@ -12,6 +16,58 @@ function HELP {
1216 exit 1
1317}
1418
19+ # Checks if Rosette API key is valid
20+ function checkAPI {
21+ match=$( curl " ${ping_url} /ping" -H " X-RosetteAPI-Key: ${API_KEY} " | grep -o " forbidden" )
22+ if [ ! -z $match ]; then
23+ echo -e " \nInvalid Rosette API Key"
24+ exit 1
25+ fi
26+ }
27+
28+ function cleanURL() {
29+ # strip the trailing slash off of the alt_url if necessary
30+ if [ ! -z " ${ALT_URL} " ]; then
31+ case ${ALT_URL} in
32+ * /) ALT_URL=${ALT_URL::- 1}
33+ echo " Slash detected"
34+ ;;
35+ esac
36+ ping_url=${ALT_URL}
37+ fi
38+ }
39+
40+ function validateURL() {
41+ match=$( curl " ${ping_url} /ping" -H " X-RosetteAPI-Key: ${API_KEY} " | grep -o " Rosette API" )
42+ if [ " ${match} " = " " ]; then
43+ echo -e " \n${ping_url} server not responding\n"
44+ exit 1
45+ fi
46+ }
47+
48+ function runExample() {
49+ echo -e " \n---------- ${1} start -------------"
50+ result=" "
51+ if [ -z ${ALT_URL} ]; then
52+ result=" $( python ${1} --key ${API_KEY} 2>&1 ) "
53+ else
54+ result=" $( python ${1} --key ${API_KEY} --url ${ALT_URL} 2>&1 ) "
55+ fi
56+ echo " ${result} "
57+ echo -e " \n---------- ${1} end -------------"
58+ if [[ " ${result} " == * " Exception" * ]]; then
59+ echo " Exception found"
60+ retcode=1
61+ elif [[ " $result " == * " processingFailure" * ]]; then
62+ retcode=1
63+ elif [[ " $result " == * " AttributeError" * ]]; then
64+ retcode=1
65+ elif [[ " $result " == * " ImportError" * ]]; then
66+ retcode=1
67+ fi
68+ }
69+ # ------------------ Functions End ------------------------------------------------
70+
1571# Gets API_KEY, FILENAME and ALT_URL if present
1672while getopts " :API_KEY:FILENAME:ALT_URL:GIT_USERNAME:VERSION" arg; do
1773 case " ${arg} " in
@@ -37,33 +93,11 @@ while getopts ":API_KEY:FILENAME:ALT_URL:GIT_USERNAME:VERSION" arg; do
3793 ;;
3894 esac
3995done
40- ping_url=" https://api.rosette.com/rest/v1"
4196
42- # strip the trailing slash off of the alt_url if necessary
43- if [ ! -z " ${ALT_URL} " ]; then
44- case ${ALT_URL} in
45- * /) ALT_URL=${ALT_URL::- 1}
46- echo " Slash detected"
47- ;;
48- esac
49- ping_url=${ALT_URL}
50- fi
97+ cleanURL
5198
52- # Checks for valid url
53- match=$( curl " ${ping_url} /ping" -H " X-RosetteAPI-Key: ${API_KEY} " | grep -o " Rosette API" )
54- if [ " ${match} " = " " ]; then
55- echo -e " \n${ping_url} server not responding\n"
56- exit 1
57- fi
99+ validateURL
58100
59- # Checks if Rosette API key is valid
60- function checkAPI {
61- match=$( curl " ${ping_url} /ping" -H " X-RosetteAPI-Key: ${API_KEY} " | grep -o " forbidden" )
62- if [ ! -z $match ]; then
63- echo -e " \nInvalid Rosette API Key"
64- exit 1
65- fi
66- }
67101
68102# Copy the mounted content in /source to current WORKDIR
69103cp -r -n /source/* .
@@ -75,15 +109,11 @@ if [ ! -z ${API_KEY} ]; then
75109 python /python-dev/setup.py install
76110 cd /python-dev/examples
77111 if [ ! -z ${FILENAME} ]; then
78- if [ ! -z ${ALT_URL} ]; then
79- python ${FILENAME} --key ${API_KEY} --url ${ALT_URL}
80- else
81- python ${FILENAME} --key ${API_KEY}
82- fi
112+ runExample ${FILENAME}
83113 elif [ ! -z ${ALT_URL} ]; then
84- find -maxdepth 1 -name ' *.py' -print -exec python {} --key ${API_KEY} --url ${ALT_URL} \;
85- else
86- find -maxdepth 1 -name ' *.py ' -print -exec python {} --key ${API_KEY} \;
114+ for file in * .py; do
115+ runExample ${file}
116+ done
87117 fi
88118else
89119 HELP
@@ -109,3 +139,5 @@ if [ ! -z ${GIT_USERNAME} ] && [ ! -z ${VERSION} ]; then
109139 git commit -a -m " publish python apidocs ${VERSION} "
110140 git push
111141fi
142+
143+ exit ${retcode}
0 commit comments