-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdp.sh
executable file
·125 lines (107 loc) · 4.22 KB
/
mdp.sh
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
# MDP: A motif detector and predictor.
#
# Copyright (c) 2018 Grzegorz Stepien
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# For more details, see './LICENSE.md'
# (where '.' represents this program's root directory).
# Script expects the path to a JSON driver file as input. Starts a Rserve instance,
# executes command './transceiver_framework/transceiver_framework.sh <path_to_json_driver_file> "mdp"'.
# After the latter's execution finishes, this script terminates the previously started
# Rserve instance.
# Helper method for error printing
error_msg()
{
TITLE="Unsuccessful execution of this script via: \"$0 $CONSOLE_PARAMS\""
"$PRINTF" "ERROR: $TITLE\n\tMessage: $1\n"
}
CONSOLE_PARAMS=$@
# Always execute the shutdown hook when exiting
shutdown_hook()
{
if [ -n "$ROOT_FOLDER" ]; then
# Shutdown Rserve server
"$NOHUP" "$RSCRIPT" "${ROOT_FOLDER}/mdp/mdp/src/main/r/rserve_terminator.R" $PORT >> "${DRIVER_CONFIG_FOLDER}/logs/R_out.log"
fi
if [ -n "$DRIVER_CONFIG_FOLDER" ]; then
# Delete temp folder
"$RM" -r -f "${DRIVER_CONFIG_FOLDER}/tmp"
fi
}
trap shutdown_hook EXIT
# Helper method for determining a free TCP port on the local machine
get_free_tcp_port()
{
read PORT_LOWER PORT_UPPER < /proc/sys/net/ipv4/ip_local_port_range
PORT=$("$COMM" -23 \
<("$SEQ" $PORT_LOWER $PORT_UPPER | "$SORT") \
<("$SS" -ant | "$AWK" 'NR>=2{print $5}' | "$REV" | "$CUT" -d ':' -f 1 | "$REV" | "$SORT" -u) | \
"$SHUF" | "$HEAD" -n 1)
if [ -z "$PORT" ]; then
error_msg "No free TCP port could be found."
exit 1
fi
}
# Check tool availability
SHUF=$(which shuf)
SS=$(which ss)
GREP=$(which grep)
DIRNAME=$(which dirname)
READLINK=$(which readlink)
SHELL=$(which bash)
if [ -z "$SHELL" ]; then
SHELL=$(which sh)
fi
MKDIR=$(which mkdir)
RM=$(which rm)
RSCRIPT=$(which Rscript)
SED=$(which sed)
HEAD=$(which head)
PRINTF=$(which printf)
NOHUP=$(which nohup)
COMM=$(which comm)
SEQ=$(which seq)
AWK=$(which awk)
REV=$(which rev)
CUT=$(which cut)
SORT=$(which sort)
if [ -z "$SHUF" -o -z "$SS" -o -z "$GREP" -o -z "$DIRNAME" -o -z "$READLINK" -o \
-z "$SHELL" -o -z "$MKDIR" -o -z "$RM" -o -z "$RSCRIPT" -o -z "$SED" -o \
-z "$HEAD" -o -z "$PRINTF" -o -z "$NOHUP" -o -z "$COMM" -o -z "$SEQ" -o \
-z "$AWK" -o -z "$REV" -o -z "$CUT" -o -z "$SORT" ]; then
error_msg "At least one required tool is missing. See \"Check tool availability\" paragraph of \"$0\" for more details."
exit 1
fi
# Folder containing this script. Should be the child of transceiver framework's root folder - adapt this if you move this script somewhere else!
ROOT_FOLDER=$("$DIRNAME" "$0")
ROOT_FOLDER=$("$READLINK" -e "$ROOT_FOLDER/..")
# Folder containing the JSON driver file
DRIVER_CONFIG_FOLDER=$("$DIRNAME" "$1")
DRIVER_CONFIG_FOLDER=$("$READLINK" -e "$DRIVER_CONFIG_FOLDER")
# Transceiver framework execution script
TF_SCRIPT="${ROOT_FOLDER}/transceiver_framework/transceiver_framework.sh"
# Get free port and store it in a temp folder inside the folder containing the provided JSON driver file
get_free_tcp_port
"$MKDIR" -p "${DRIVER_CONFIG_FOLDER}/tmp"
"$PRINTF" "$PORT" > "${DRIVER_CONFIG_FOLDER}/tmp/port"
# Start Rserve server and log R related output to 'R_out.log'
# in a newly created 'logs' folder in the folder containing the JSON driver file.
"$MKDIR" -p "${DRIVER_CONFIG_FOLDER}/logs"
"$NOHUP" "$RSCRIPT" "${ROOT_FOLDER}/mdp/mdp/src/main/r/rserve_starter.R" $PORT > "${DRIVER_CONFIG_FOLDER}/logs/R_out.log"
# Composed commands to execute
RUN_COMMAND="\"$SHELL\" \"$TF_SCRIPT\" \"$1\" \"mdp\""
# Execute
eval "$RUN_COMMAND"