-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckPyImport
More file actions
executable file
·42 lines (39 loc) · 1.18 KB
/
checkPyImport
File metadata and controls
executable file
·42 lines (39 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env bash
#USAGE: checkPyImport packagename
#
#EXAMPLES:
# #Should return Success and an exit code of 0
# checkPyImport os
#
# #Should return Failure, exit code 1 -- "Douglas Adams" != "Monty Python"
# checkPyImport slartibartfast
#
# #In a script where you don't want the stdout, do something like:
# checkPyImport slartibartfast &> /dev/null
# if [ "$?" = "0" ] ; then
# printf "%s\n" "I prefer Douglas Adams too."
# else
# printf "%s\n" "No point calling that Python script"
# fi
#
#DESCRIPTION:
# Starts python and imports the requested package before exiting.
# A message is printed to standard out and the exit code is set
# based on success or failure of the import.
#
# Author: Stuart A. Knock
# Originally Written: 2015-11-25
# https://github.com/stuart-knock/bash-tools
#
#TODO: Add some extra checks and messages, maybe add a verbosity flag...
#local pathtopython=$(which python)
#which python &> /dev/null ; test "$?" = "0" || printf "%s\n" "Can't find python..."
printf "Testing if python can import %s: " "$1"
python -c "import $1 ; exit()" &> /dev/null
if [ "$?" = "0" ] ; then
printf "%s\n" "Success"
exit 0
else
printf "%s\n" "Failure"
exit 1
fi