-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcreate-model
More file actions
executable file
·177 lines (150 loc) · 4.43 KB
/
Copy pathcreate-model
File metadata and controls
executable file
·177 lines (150 loc) · 4.43 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
# Script of Model Creation Template-based for Data Science
#
# Main usage: ./create-model new-project-name
#
# Authors: Manoel Vilela <manoel.neto@neoway.com.br>
# Vitor Hugo <vitor.deluca@neoway.com.br>
readlinkf () {
# This function is a replacement for "readlink -f"
# which does not work in iOS (Mac).
TARGET_FILE=$1
cd "$(dirname "$TARGET_FILE")" || exit 1
TARGET_FILE="$(basename "$TARGET_FILE")"
while [ -L "$TARGET_FILE" ]
do
TARGET_FILE="$(readlink "$TARGET_FILE")"
cd "$(dirname "$TARGET_FILE")" || exit 1
TARGET_FILE="$(basename "$TARGET_FILE")"
done
echo "$(pwd -P)"/"$TARGET_FILE"
}
PROGRAM=$(basename "$0")
SCRIPT_PATH=$(readlinkf "$0")
TEMPLATE_PROJECT_PATH=$(dirname "$SCRIPT_PATH")
TEMPLATE_FOLDER="$TEMPLATE_PROJECT_PATH/template/"
AUTHOR=$(git config --global user.name)
EMAIL=$(git config --global user.email)
PLACEHOLDER_REGEX="<@[a-zA-Z]+>"
# Info function variable
info () {
printf "\e[0;32m[+]\e[0m "
echo "$@"
}
attention () {
printf "\e[0;36m[!]\e[0m "
echo "$@"
}
usage () {
echo "usage: $PROGRAM [-dihu] new-project-name"
echo
echo "PARAMETERS"
echo " -h, --help Show this help and exit"
echo " -d, --debug Debug mode for development"
echo " -i, --install Install this script into ~/.local/bin as symlink"
echo " -u, --update Update the template"
exit 1
}
# For debugging purposes
search_placeholders () {
grep -R -E "$PLACEHOLDER_REGEX" "$TEMPLATE_FOLDER"
}
# type: str -> str
# Translate invalid Python identifiers common characters
# from project names, like hyphens and spaces to underscore.
# Ex.: composite-indicator -> composite_indicator
normalize_name () {
local name="$1"
echo "$name" | tr '-' '_' | tr ' ' '_'
}
debug() {
info "DEBUG MODE!"
echo "Variables"
echo "AUTHOR=$AUTHOR"
echo "EMAIL=$EMAIL"
echo "project_path=$project_path"
echo "TEMPLATE_PROJECT_PATH=$TEMPLATE_PROJECT_PATH"
echo "TEMPLATE_FOLDER=$TEMPLATE_FOLDER"
info "NORMALIZE PROJECT NAME"
echo "$project_name -> $model_name"
info "SEARCH PLACEHOLDERS"
search_placeholders
exit 0
}
install () {
mkdir -p ~/.local/bin
info INSTALLING SCRIPT INTO ~/.local/bin
ln -s -f -v "$SCRIPT_PATH" ~/.local/bin/
exit 0
}
update() {
info UPDATE PROJECT TEMPLATE BASE
cd "$TEMPLATE_PROJECT_PATH" || exit 1
git pull
exit 0
}
search_replace () {
local search
local replace
search="$1"
replace="$2"
info "$search -> $replace"
# shellcheck disable=SC2038
find . -type f | xargs sed -i.bak "s/$search/$replace/g"
}
argparsing () {
# if first argument is as parameter, don't parse it
if [[ "$1" != "-"* ]]; then
project_path=$(readlinkf "$(dirname "$1")")
project_name=$(basename "$1")
model_name=$(normalize_name "$project_name")
fi
# default to help usage if no param is passed
if [[ -z "$1" ]]; then
usage
fi
# vanilla argument parsing
for arg in "$@"; do
if [[ $arg == -h || $arg == --help ]]; then
usage
elif [[ $arg == -d || $arg == --debug ]]; then
debug "$@"
elif [[ $arg == -i || $arg == --install ]]; then
install
elif [[ $arg == -u || $arg == --update ]]; then
update
fi;
done
}
main () {
argparsing "$@"
new_project=$project_path/$project_name
info "COPYING BASE TEMPLATE TO: $new_project"
mkdir -p "$project_path"
cp -R "$TEMPLATE_FOLDER" "$new_project"
cd "$new_project" || exit 1
info "RENAME MODEL DIRECTORY"
info "RENAME PLACEHOLDERS"
search_replace "<@model>" "$model_name"
search_replace "<@project>" "$project_name"
search_replace "<@author>" "$AUTHOR"
search_replace "<@email>" "$EMAIL"
# Exceptions
sed -i.bak "s/model_name/$model_name/g" setup.py docs/hack/docs.sh
sed -i.bak "s/project/$project_name/g" Makefile
sed -i.bak "s/model_name/$model_name/g" \
model_name/main.py \
model_name/config.py \
Makefile \
Dockerfile
# Delete backup files
find . -name "*.bak" -type f -delete
mv model_name "$model_name"
info "INITIALIZING GIT REPOSITORY"
mkdir -p workspace/{data,models}
git init
attention "CREATE NEW GIT PROJECT AT SOME GIT HOSTING SERVICE"
attention "EVAL: git remote add origin <remote repository address>"
info "DONE!"
}
main "$@"