~percona-toolkit-dev/percona-toolkit/mysql-5.6-test-fixes

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
#!/usr/bin/env bash

# ############################################################################
# Standard startup, find the branch's root directory
# ############################################################################

exit_status=0

die() {
   echo $1  >&2
   exit 1
}

warn() {
   echo $1 >&2
   exit_status=$((exit_status | 1))
}

if [ -n "$PERCONA_TOOLKIT_BRANCH" ]; then
   BRANCH=$PERCONA_TOOLKIT_BRANCH
else
   while [ ! -f Makefile.PL ] && [ $(pwd) != "/" ]; do
      cd ..
   done
   if [ ! -f Makefile.PL ]; then
      die "Cannot find the root directory of the Percona Toolkit branch"
   fi
   BRANCH=`pwd`
fi

# ############################################################################
# Global variables
# ############################################################################

CHECK=${CHECK:-1}

# ############################################################################
# Subroutines
# ############################################################################

file_is_modified() {
   local file=$1
   if [ $CHECK -eq 1 ]; then
      bzr status $file | grep -q modified
   else
      return 1
   fi
}

pkgs_in_tool() {
   local tool=$1
   pkgs=$(grep '^package [A-Za-z]*;' $tool | cut -d' ' -f2 | cut -d';' -f1)
}

replace_pkg_in_tool() {
   local tool_file=$1
   local tmp_file="$tool_file.tmp"

   local pkg_start_line=$(grep -n "^# $pkg package" $tool_file | cut -d':' -f1)
   if [ -z "$pkg_start_line" ]; then
      warn "$tool does not use $pkg"
      return 1
   fi
   pkg_start_line=$((pkg_start_line - 1))

   local pkg_end_line=$(grep -n "^# End $pkg package" $tool_file | cut -d':' -f1)
   if [ -z "$pkg_end_line" ]; then
      warn "Cannot find 'End $pkg' in $tool"
      return 1
   fi
   pkg_end_line=$((pkg_end_line + 1))

   head -n $pkg_start_line $tool_file > $tmp_file

   echo "# $pkg package
# This package is a copy without comments from the original.  The original
# with comments and its test file can be found in the Bazaar repository at,
#   lib/$pkg.pm
#   t/lib/$pkg.t
# See https://launchpad.net/percona-toolkit for more information.
# ###########################################################################
{" >> $tmp_file

   $BRANCH/util/extract-package $pkg $pkg_file | grep -v '^ *#' >> $tmp_file

   echo "}
# ###########################################################################
# End $pkg package" >> $tmp_file

   tail -n +$pkg_end_line  $tool_file >> $tmp_file

   mv $tmp_file $tool_file
} 

# ############################################################################
# Script starts here
# ############################################################################

tool_file=$1
if [ -z "$tool_file" ]; then
   die "Usage: $0 TOOL [MODULE...]"
fi
if [ ! -f $tool_file ]; then
   die "$tool_file does not exist"
fi
if [ -z "$(head -n 1 $tool_file | grep perl)" ]; then
   die "$tool_file is not a Perl tool"
fi

tool=$(basename $tool_file)
tmp_tool_file="/tmp/$tool.tmp";
cp $tool_file $tmp_tool_file

shift
pkgs="$@"
if [ -z "$pkgs" ]; then
   pkgs_in_tool $tool_file
fi

echo "Updating modules in $tool..."
pkgs_updated=0
for pkg in $pkgs; do
   if [ "$pkg" == "main" ]; then
      continue
   fi

   pkg_file="$BRANCH/lib/$pkg.pm"
   if [ ! -f $pkg_file ]; then
      warn "$pkg_file does not exist"
      continue
   fi
   if file_is_modified $pkg_file; then
      warn "$pkg_file has uncommitted changes"
      continue
   fi 

   replace_pkg_in_tool $tmp_tool_file
   if [ $? -eq 0 ]; then
      echo "Updated $pkg"
      pkgs_updated=$((pkgs_updated+1))
   fi
done

if [ $pkgs_updated -ne 0 ]; then
   cp $tmp_tool_file $tool_file
   if [ $? -ne 0 ]; then
      warn "Failed to copy $tmp_tool_file to $tool_file"
   else
      rm $tmp_tool_file > /dev/null
      exit_status=$((exit_status | $?))
   fi
fi

exit $exit_status