forked from OTRS/module-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMigratePackage.sh
executable file
·280 lines (234 loc) · 7.56 KB
/
MigratePackage.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
# --
# module-tools/MigratePackage.sh
# - script for migrating package to a certain OTRS release.
# Copyright (C) 2001-2012 OTRS AG, http://otrs.org/
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# 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 Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --
# TODO:
# * verbose/non-verbose mode
# * some error handling
# * clean up inside of tmp dir
# * maybe: force mode - if file versions are not matching
#
# version detection
VERSION=$(echo "\$Revision: 1.5 $" | awk '{print $2}');
# flag definition
DEBUG=
VERBOSE=
# needed params
MODULE=
SOURCE=
TARGET=
# create temp dir
TMP_BASE_DIR="/tmp"
PID=$$
TMP_DIR="${TMP_BASE_DIR}/otrs_auto_migrate_${PID}"
# package excludes
PKG_EXCLUDE_SOPM=" ! -name *.sopm "
PKG_EXCLUDE_DOC=" ! -path */doc/* "
PKG_EXCLUDE_CONF=" ! -path */Kernel/Config/Files/* "
# function for printing out usage and exit
function usage {
echo -e \
"usage: $0 [-h] [-d] [-v] [-m module path] [-s source path] [-t target path] \n" \
"\tVersion: $VERSION \n" \
"\t-h: prints this help dialogue \n" \
"\t-d: enable shell debug mode \n" \
"\t-v: enable verbose mode \n" \
"\t-m: /path/to/custom/package \n" \
"\t-s: /path/to/otrs - current OTRS version on which package is based on \n" \
"\t-t: /path/to/otrs - target OTRS version on which package should be migrated \n" \
>&2
exit 1
}
# function for creating temp dir
function create_temp {
TEMP_DIR=$1
# sanity check for temp dir
if [ -d "$TEMP_DIR" ]; then
echo "Temp directory $TEMP_DIR already exits already, exiting."
exit 1;
fi
echo -n "Creating temp directory '$TEMP_DIR': "
mkdir $TEMP_DIR
if [ "x$?" != "x0" ]; then
echo "failed, exiting!"
exit 1
fi
echo "done."
}
# function for clean up temp dir
function cleanup_temp {
TEMP_DIR=$1
if [ -d "$TEMP_DIR" ]; then
echo -n "Removing temp directory '$TEMP_DIR': "
rm -rf $TEMP_DIR
if [ "x$?" != "x0" ]; then
echo "failed!"
fi
echo "done."
fi
}
# function to create a diff from package file to framework file
function create_diff {
TEMP_DIR=$1
FW_FILE=$2
PKG_FILE=$3
# get base of package file
PATCH_FILE=$(basename $PKG_FILE)
# first perl part removes patching of Id and OldId
# second perl part removes VERSION patching
echo -n "- generating patch file - "
diff -Naur $FW_FILE $PKG_FILE \
| perl -le '$PatchContent = join("", <STDIN>); $PatchContent =~ s{ (?: @@ .*? @@\n .*? Id .*? [^@@]+ ) }{}xms; print $PatchContent' \
| perl -le '$PatchContent = join("", <STDIN>); $PatchContent =~ s{ (?: @@ .*? @@\n .*? VERSION \s+ = .*? [^@@]+ ) }{}xms; print $PatchContent' \
> ${TEMP_DIR}/${PATCH_FILE}.patch
if [ "x$?" != "x0" ]; then
echo -n "- patch file creation failed - "
return 1
fi
}
# function creates the package file
# patching and OldId injection
function create_pkg_file {
TEMP_DIR=$1
FW_FILE=$2
PKG_FILE=$3
# get base of package file
PKG_BASENAME=$(basename $PKG_FILE)
PATCH_FILE="${TEMP_DIR}/${PKG_BASENAME}.patch"
# copy framework file to temp dir
cp $FW_FILE ${TEMP_DIR}/${PKG_BASENAME}
# apply patch to file
patch -p0 ${TEMP_DIR}/${PKG_BASENAME} < $PATCH_FILE 1>/dev/null
if [ "x$?" != "x0" ]; then
echo -n "- patching failed - "
return 1
fi
# setting duplicating Id line to OldId
# just duplicating Id line due to CVS will overwrite Id with the proper value
cat ${TEMP_DIR}/${PKG_BASENAME} \
| perl -le '$FileContent = join("", <STDIN>); $FileContent =~ s{^(\# \s+ \$)Id(.*?)$}{$1Id$2\n$1OldId$2}xms; print $FileContent;' \
> ${TEMP_DIR}/${PKG_BASENAME}
}
# function to find framework file of package file
# and create patches and new package files
function handle_pkg_file {
PKG_FILE=$1
# get line with OldId
old_id=$(grep "^# \$OldId" $PKG_FILE)
if [ "x${old_id}" = "x" ]; then
echo -n "- no 'OldId' file markers - "
return 1
fi
# extract needed data
old_file_name=$(echo $old_id | sed -e 's/.*OldId: \([A-Za-z0-9.]*\),.*/\1/' )
if [ "x${old_file_name}" = "x" ]; then
echo -n "- unable to extract framework file name - "
return 1
fi
# get CVS OldId of file
old_file_id=$(echo $old_id | sed -e 's/.*,v \([0-9]*\(\.[0-9]*\)*\).*/\1/' )
if [ "x${old_file_id}" = "x" ]; then
echo -n "- unable to extract framework file Id - "
return 1
fi
# try find file source framework directory
source_fw_file=$(find $SOURCE -type f -name "$old_file_name")
if [ "x${source_fw_file}" = "x" ]; then
echo -n "- not a framework file - "
return 1
fi
# get line with Id in framework file
fw_id=$(grep "^# \$Id" $source_fw_file)
if [ "x${fw_id}" = "x" ]; then
echo -n "- no 'Id' file markers - "
return 1
fi
# get CVS Id of file
fw_file_id=$(echo $fw_id | sed -e 's/.*,v \([0-9]*\(\.[0-9]*\)*\).*/\1/' )
if [ "x${fw_file_id}" = "x" ]; then
echo -n "- unable to extract framework file Id - "
return 1
fi
# check file IDs
if [ "x$old_file_id" != "x$fw_file_id" ]; then
echo -n " - file ID mismatch ($old_file_id - $fw_file_id) - "
return 1
fi
# create diff for package files
create_diff $TMP_DIR $source_fw_file $PKG_FILE
if [ "x$?" != "x0" ]; then
return 1
fi
# find new framework file
target_fw_file=$(find $TARGET -type f -name "$old_file_name")
if [ "x${target_fw_file}" = "x" ]; then
echo -n "- not a framework file - "
return 1
fi
# create package file based on framework file
create_pkg_file $TMP_DIR $target_fw_file $PKG_FILE
}
# get submitted params
while getopts "hdvm:s:t:" opt
do
case "$opt" in
h) usage;;
d) DEBUG=on;;
v) VERBOSE=on;;
m) MODULE="$OPTARG";;
s) SOURCE="$OPTARG";;
t) TARGET="$OPTARG";;
\?) # unknown flag
usage;;
esac
done
shift `expr $OPTIND - 1`
# check for turning on shell debug mode
if [ "x${DEBUG}" != "x" ]; then
set -x
fi
# check for needed arguments
if [ "x${MODULE}" = "x" -o "x${SOURCE}" = "x" -o "x${TARGET}" = "x" ]; then
usage
fi
# check for needed directories
for directory_check in $MODULE $SOURCE $TARGET
do
echo -n "Checking for ${directory_check}: "
if [ ! -d "$directory_check" ]; then
echo "failed!"
exit 1
fi
echo "done."
done
create_temp $TMP_DIR
PKG_EXCLUDE="$PKG_EXCLUDE_SOPM $PKG_EXCLUDE_DOC $PKG_EXCLUDE_CONF"
for package_file in `find $MODULE ! -path '*/CVS/*' ! -name "CVS" $PKG_EXCLUDE -type f`
do
echo -n "Handling file '$package_file': "
# try to find related frame work file
handle_pkg_file $package_file
if [ "x$?" != "x0" ]; then
echo "failed, finishing!"
continue
fi
echo "done."
done
#cleanup_temp $TMP_DIR