Newer
Older

Théo - Le Filament
committed
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/sh
ODOO_ROOT_DIR=${ODOO_ROOT_DIR:-'/opt/odoo'}
SAVE_COMMITS_DIR=${SAVE_COMMITS_DIR:-'/opt/odoo_commits'}
SAVE_COMMITS_FILENAME=${SAVE_COMMITS_FILENAME:-'custom_addons'}
TMP_REPO_PATH=${TEMP_REPO_PATH:-'/tmp/repo'}
CSV_FILE="${SAVE_COMMITS_DIR}/${SAVE_COMMITS_FILENAME}"
CSV_HEADERS='repo;ref;commit;module;dst'
# logging section: begin
STYLE_FGBLACK='\033[30m'
STYLE_FGRED='\033[31m'
STYLE_FGGREEN='\033[32m'
STYLE_FGYELLOW='\033[33m'
STYLE_FGBLUE='\033[34m'
STYLE_FGMAGENTA='\033[35m'
STYLE_FGCYAN='\033[36m'
STYLE_FGLIGHTGRAY='\033[37m'
STYLE_BGBLACK='\033[40m'
STYLE_BGRED='\033[41m'
STYLE_BGGREEN='\033[42m'
STYLE_BGYELLOW='\033[43m'
STYLE_BGBLUE='\033[44m'
STYLE_BGMAGENTA='\033[45m'
STYLE_BGCYAN='\033[46m'
STYLE_BGLIGHTGRAY='\033[47m'
STYLE_DEFAULT='\033[39m'
STYLE_BOLD='\033[1m'
STYLE_NORMAL='\033[0m'
print_info() {
msg="$1"
echo "${STYLE_FGBLUE}info: ${msg}${STYLE_DEFAULT}"
}
print_success() {
msg="$1"
print_info "${msg} ${STYLE_FGGREEN}success${STYLE_DEFAULT}"
}
print_failed() {
msg="$1"
print_info "${msg} ${STYLE_FGRED}failed${STYLE_DEFAULT}"
}
print_err() {
msg="$1"
exit_code="${2:-0}"
echo "${STYLE_BOLD}${STYLE_FGRED}err: ${msg}${STYLE_DEFAULT}${STYLE_NORMAL}"
[ "$exit_code" != "0" ] && exit_script "$exit_code"
}
exit_script() {
exit_code="${1:-0}"
exit "$exit_code"
}
# logging section: end
save_commit() {
repo="$1"
ref="$2"
commit="$3"
module="$4"
dst="$5"
if [ -f "$CSV_FILE" ]; then
if [ "$(head --lines=1 $CSV_FILE)" != "$CSV_HEADERS" ]; then
sed -i "1s/^/${CSV_HEADERS}\n/" "$CSV_FILE"
if [ "$?" -ne 0 ]; then
return 1
fi
fi
else
echo "$CSV_HEADERS" > "$CSV_FILE"
fi
echo "${repo};${ref};${commit};${module};${dst}" >> "$CSV_FILE"
if [ "$?" -ne 0 ]; then
return 1
fi
return 0
}
repo="$1"
shift
ref="$1"
shift
dst="$1"
shift
modules="$@"
# Cloning repo.
log_action="repo '$repo': cloning [no tree + branch]..."
print_info "$log_action"
git clone --branch "${ref}" --filter=tree:0 "${repo}" "${TMP_REPO_PATH}"
if [ "$?" -eq 0 ]; then
print_success "$log_action"
else
print_failed "$log_action"
log_action="repo '$repo': cloning [no tree]..."
print_info "$log_action"
git clone --filter=tree:0 "${repo}" "${TMP_REPO_PATH}"
if [ "$?" -eq 0 ]; then
print_success "$log_action"
else
print_failed "$log_action"
log_action="repo '$repo': cloning [branch]..."
print_info "$log_action"
git clone --single-branch --branch "${ref}" --depth 1 "${repo}" "${TMP_REPO_PATH}"
if [ "$?" -eq 0 ]; then
print_success "$log_action"
else
print_failed "$log_action"
log_action="repo '$repo': cloning [classic]..."
print_info "$log_action"
git clone "${repo}" "${TMP_REPO_PATH}"
if [ "$?" -eq 0 ]; then
print_success "$log_action"
else
print_failed "$log_action"
print_err "unable to clone repo '$repo'" 1
fi
fi
fi
fi
## Get module code and save informations about it in a CSV file.
# Checkout to ref.
log_action="repo '${repo}': checkout to '${ref}'..."
print_info "$log_action"
git -C "${TMP_REPO_PATH}" checkout "${ref}"
if [ "$?" -eq 0 ]; then
print_success "$log_action"
else
print_failed "$log_action"
print_err "unable to checkout to '${ref}' in repo '${repo}'" 2
fi
# Get commit number.
log_action="repo '${repo}': getting commit number..."
print_info "$log_action"
commit="$(git -C ${TMP_REPO_PATH} rev-parse HEAD)"
if [ "$?" -eq 0 ]; then
print_success "$log_action"
else
print_failed "$log_action"
print_err "unable to get commit number in repo '${repo}'" 3
fi
# Get selected modules from repo.
if [ -n "$modules" ]; then
print_info "repo '${repo}': repo with multiple modules detected, modules selected: [$(echo $modules | sed 's/ /, /g')]"
# Install each module.
for module in $modules; do
module_dst="${ODOO_ROOT_DIR}/${dst}/${module}"
if [ -d "$module_dst" ]; then
print_info "module '${module}': destination '${module_dst}' already exist removing..."
rm -r "$module_dst"
fi
log_action="module '${module}': moving module code to directory '${module_dst}'..."
print_info "$log_action"
mv "${TMP_REPO_PATH}/${module}" "${ODOO_ROOT_DIR}/${dst}/"
if [ "$?" -eq 0 ]; then
print_success "$log_action"
else
print_failed "$log_action"
print_err "unable to move module '${module}' from repo '${repo}' to directory '${module_dst}'" 5
fi
log_action="module '${module}': saving module informations..."
print_info "$log_action"
save_commit "$repo" "$ref" "$commit" "$module" "$dst"
if [ "$?" -eq 0 ]; then
print_success "$log_action"
else
print_failed "$log_action"
print_err "unable to save informations of module '${module}' in file '${CSV_FILE}'" 6
fi
done
# Get repo as a module.
else
module_dst="${ODOO_ROOT_DIR}/${dst}"
module="$(basename $dst)"
print_info "repo '${repo}': repo as a single module detected: '${module}'"
if [ -d "$module_dst" ]; then
print_info "module '${module}': destination '${module_dst}' already exist removing..."
rm -r "$module_dst"
fi
log_action="module '${module}': creating destination directory '${module_dst}'..."
print_info "$log_action"
mkdir -p "$module_dst"
if [ "$?" -eq 0 ]; then
print_success "$log_action"
else
print_failed "$log_action"
print_err "unable to create destination directory '${module_dst}' for '${module}' from repo '${repo}'" 4
fi
log_action="module '${module}': moving module code to directory '${module_dst}'..."
print_info "$log_action"
mv ${TMP_REPO_PATH}/* "$module_dst"
if [ "$?" -eq 0 ]; then
print_success "$log_action"
else
print_failed "$log_action"
print_err "unable to move code of module '${module}' from repo '${repo}' to directory '${module_dst}'" 5
fi
log_action="module '${module}': saving module informations..."
print_info "$log_action"
save_commit "$repo" "$ref" "$commit" "$module" "$dst"
if [ "$?" -eq 0 ]; then
print_success "$log_action"
else
print_failed "$log_action"
print_err "unable to save informations of module '${module}' in file '${CSV_FILE}'" 6
fi
fi
rm -r "${TMP_REPO_PATH}"