david_williams | 97651d1 | 2007-07-28 03:46:34 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # build renamer script - fix/rename all of a given I/M/S build's artefacts instead of regen'ing them |
| 4 | # copy existing build folder; rename zips, md5s; sed text files |
| 5 | |
| 6 | # default path to builds |
| 7 | buildDropsDir=$PWD; |
| 8 | |
| 9 | tmpdir=/home/data/users/jlanuti/downloads/webtools/committers/drops/tmp-renameBuild.sh-$USER; |
| 10 | mkdir -p $tmpdir; |
| 11 | |
| 12 | verbose=""; |
| 13 | pairs=""; |
| 14 | |
| 15 | # to use these colour escapes, must do `echo -e -e` |
| 16 | red="\033[1;31m"; green="\033[1;32m"; |
| 17 | yellow="\033[1;33m"; blue="\033[1;34m" |
| 18 | norm="\033[0;39m"; |
| 19 | |
| 20 | # default to default properties file |
| 21 | defaultPropertiesFile=./promoteToEclipse.properties |
| 22 | propertiesFiles=""; |
| 23 | |
| 24 | if [ $# -lt 1 ]; then |
| 25 | echo ""; |
| 26 | echo "[ren] This script is used to copy, then rename an existing local build so that it can be promoted"; |
| 27 | echo " again under a new name. By renaming instead of rebuilding, binary compatibility is possible"; |
| 28 | echo " w/o having to retest. If -sub is not specified, script will look for the folder to rename "; |
| 29 | echo -e " in the current folder ("$yellow""$PWD""$norm")."; |
| 30 | echo ""; |
| 31 | echo "[ren] Usage (<> = required, [] = optional, string replacements must be listed LAST, and will be"; |
| 32 | echo " processed IN ORDER listed.):"; |
| 33 | echo ""; |
| 34 | echo -e " "$green"sudo -u www-data $PWD/renameBuild.sh"$norm" [-sub <subprojectName>] \\" |
| 35 | echo -e " -branch [sourceBranch"$blue"="$norm"]<targetBranch> -buildID <sourceBuildID"$blue"="$norm"targetBuildID> \\" |
| 36 | echo -e " -buildAlias [sourceAlias"$blue"="$norm"]<targetAlias> \\"; |
| 37 | echo -e " [-verbose] [-buildDropsDir /path/to/some/folder] \\"; |
| 38 | echo -e " [beforestring"$blue"="$norm"afterstring] [beforestring2"$blue"="$norm"afterstring2] ... \\"; |
| 39 | echo -e " "$yellow"2>&1 | tee ~/renameBuild_\`date +%Y%m%d_%H%M%S\`.txt"$norm""; |
| 40 | echo ""; |
| 41 | echo "[ren] To rename with string replacements [Eclipse driver folder & file]: "; |
| 42 | echo ""; |
| 43 | echo -e " "$green"sudo -u www-data $PWD/renameBuild.sh"$norm" -sub "$red"emf"$norm" -verbose \\"; |
| 44 | echo -e " -branch 2.2.0 -buildID S200606271057"$blue"="$norm"R200606271057 -buildAlias 2.2.0RC9a"$blue"="$norm"2.2.0 \\"; |
| 45 | echo -e " eclipse-SDK-M20060609-1217-linux-gtk.tar.gz"$blue"="$norm"eclipse-SDK-3.2-linux-gtk.tar.gz \\"; |
| 46 | echo -e " M20060609-1217"$blue"="$norm"R-3.2-200606281325 \\"; |
| 47 | echo -e " /eclipse/downloads/drops/"$blue"="$norm"http://download.eclipse.org/downloads/drops/ \\"; # TODO: remove this hack once index.html pages are fixed after 2.2.0 |
| 48 | echo -e " fullmoon.torolab.ibm.com"$blue"="$norm"download.eclipse.org"; |
| 49 | echo ""; |
| 50 | echo "[ren] To rename with string replacements [Eclipse + EMF driver folders & files]: "; |
| 51 | echo ""; |
| 52 | echo -e " "$green"sudo -u www-data $PWD/renameBuild.sh"$norm" -sub "$red"uml2"$norm" -verbose \\"; |
| 53 | echo -e " -branch 2.0.0 -buildID S200606221411"$blue"="$norm"R200606221411 -buildAlias 2.0RC9a"$blue"="$norm"2.0.0 \\"; |
| 54 | echo -e " eclipse-SDK-M20060609-1217-linux-gtk.tar.gz"$blue"="$norm"eclipse-SDK-3.2-linux-gtk.tar.gz \\"; |
| 55 | echo -e " M20060609-1217"$blue"="$norm"R-3.2-200606281325 \\"; |
| 56 | echo -e " /eclipse/downloads/drops/"$blue"="$norm"http://download.eclipse.org/downloads/drops/ \\"; # TODO: remove this hack once index.html pages are fixed after 2.0.0 |
| 57 | echo -e " emf-sdo-xsd-SDK-2.2.0RC9.zip"$blue"="$norm"emf-sdo-xsd-SDK-2.2.0.zip \\"; |
| 58 | echo -e " S200606221156"$blue"="$norm"R200606271057 \\"; |
| 59 | #echo -e " /tools/emf/downloads/drops/"$blue"="$norm"../../../../../../tools/emf/downloads/drops/ \\"; # TODO: remove this hack once index.html pages are fixed after 2.0.0 |
| 60 | echo -e " fullmoon.torolab.ibm.com"$blue"="$norm"download.eclipse.org"; |
| 61 | echo -e " emf.torolab.ibm.com"$blue"="$norm"download.eclipse.org"; |
| 62 | echo ""; |
| 63 | echo -e " "$green"sudo -u www-data $PWD/renameBuild.sh"$norm" -sub "$red"ocl"$norm" -verbose \\"; |
| 64 | echo -e " -branch 1.0.0 -buildID S200606261119"$blue"="$norm"R200606261119 -buildAlias 1.0.0RC6"$blue"="$norm"1.0.0 \\"; |
| 65 | echo -e " eclipse-SDK-M20060609-1217-linux-gtk.tar.gz"$blue"="$norm"eclipse-SDK-3.2-linux-gtk.tar.gz \\"; |
| 66 | echo -e " M20060609-1217"$blue"="$norm"R-3.2-200606281325 \\"; |
| 67 | echo -e " emf-sdo-xsd-SDK-2.2.0RC9.zip"$blue"="$norm"emf-sdo-xsd-SDK-2.2.0.zip \\"; |
| 68 | echo -e " S200606221156"$blue"="$norm"R200606271057 \\"; |
| 69 | echo -e " fullmoon.torolab.ibm.com"$blue"="$norm"download.eclipse.org"; |
| 70 | echo -e " emf.torolab.ibm.com"$blue"="$norm"download.eclipse.org"; |
| 71 | echo ""; |
| 72 | exit; |
| 73 | fi |
| 74 | |
| 75 | echo ""; |
| 76 | echo -e "[ren] Started `date +%Y%m%d\ %H\:%M\:%S`."; |
| 77 | |
| 78 | # collect cmdline options |
| 79 | while [ $# -gt 0 ]; do |
| 80 | case $1 in |
| 81 | '-branch') |
| 82 | # check if param 2 contains a "=" |
| 83 | t=$2;t=${t##*=*}; # get a nullstring if there was a "=" in the string |
| 84 | if [ "x$t" = "x" ]; then # $ |
| 85 | sourceBranch=$2; sourceBranch=${sourceBranch%%=*}; # trim from = to end |
| 86 | targetBranch=$2; targetBranch=${targetBranch##*=}; # trim up to the = |
| 87 | else |
| 88 | sourceBranch=$2; |
| 89 | targetBranch=$2; |
| 90 | fi |
| 91 | shift 1; |
| 92 | ;; |
| 93 | '-buildID') |
| 94 | sourceBuildID=$2; sourceBuildID=${sourceBuildID%%=*}; # trim from = to end |
| 95 | targetBuildID=$2; targetBuildID=${targetBuildID##*=}; # trim up to the = |
| 96 | shift 1; |
| 97 | ;; |
| 98 | '-buildAlias') |
| 99 | # check if param 2 contains a "=" |
| 100 | t=$2;t=${t##*=*}; # get a nullstring if there was a "=" in the string |
| 101 | if [ "x$t" = "x" ]; then # $ |
| 102 | sourceAlias=$2; sourceAlias=${sourceAlias%%=*}; # trim from = to end |
| 103 | targetAlias=$2; targetAlias=${targetAlias##*=}; # trim up to the = |
| 104 | else |
| 105 | sourceAlias=$sourceBuildID; |
| 106 | targetAlias=$2; |
| 107 | fi |
| 108 | shift 1; |
| 109 | ;; |
| 110 | '-sub') |
| 111 | subprojectName=$2; |
| 112 | #echo " $1 $2"; |
| 113 | # chain them together in order of priority: override (if applic), subproj specific one, default |
| 114 | propertiesFiles=$propertiesFiles" ./promoteToEclipse."$subprojectName".properties "$defaultPropertiesFile; |
| 115 | loaded=0; |
| 116 | for propertiesFile in $propertiesFiles; do |
| 117 | if [ "$loaded" -eq 0 ] && [ -r $propertiesFile ]; then |
| 118 | echo -n " [loading $propertiesFile ... "; . $propertiesFile; echo "done]"; loaded=1; |
| 119 | fi |
| 120 | done |
| 121 | if [ "$loaded" -eq 0 ]; then |
| 122 | echo " [Can't load any of: $propertiesFiles. Exiting!]"; |
| 123 | exit 99; |
| 124 | fi |
| 125 | shift 1 |
| 126 | ;; |
| 127 | |
| 128 | '-buildDropsDir') |
| 129 | buildDropsDir=$2; |
| 130 | shift 1; |
| 131 | ;; |
| 132 | '-verbose') |
| 133 | verbose="true"; |
| 134 | shift 0; |
| 135 | ;; |
| 136 | *) |
| 137 | pairs=$pairs" "$1; |
| 138 | shift 0; |
| 139 | ;; |
| 140 | esac |
| 141 | shift 1 |
| 142 | done |
| 143 | |
| 144 | if [ "$subprojectName" = "" ]; then # no value set! |
| 145 | echo "[promote] No subproject name set in properties file or by -sub flag. Script cannot continue. Exiting..."; |
| 146 | exit 99; |
| 147 | fi |
| 148 | |
| 149 | getBuildType () |
| 150 | { |
| 151 | tmpType=$1; tmpType=${tmpType:0:1}; # one of N, M, I, S, R |
| 152 | #echo -e "tmpType=$tmpType"; |
| 153 | case $tmpType in |
| 154 | 'N') |
| 155 | tmpType='Nightly'; |
| 156 | ;; |
| 157 | 'M') |
| 158 | tmpType='Maintenance'; |
| 159 | ;; |
| 160 | 'I') |
| 161 | tmpType='Integration'; |
| 162 | ;; |
| 163 | 'S') |
| 164 | tmpType='Stable'; |
| 165 | ;; |
| 166 | 'R') |
| 167 | tmpType='Release'; |
| 168 | ;; |
| 169 | esac |
| 170 | } |
| 171 | |
| 172 | getBuildType $sourceBuildID; sourceType=$tmpType; |
| 173 | getBuildType $targetBuildID; targetType=$tmpType; |
| 174 | |
| 175 | echo ""; |
| 176 | echo -e "[ren] Source: branch=$red$sourceBranch$norm; build=$red$sourceBuildID$norm; type=$red$sourceType$norm; alias=$red$sourceAlias$norm"; |
| 177 | echo -e "[ren] Target: branch=$green$targetBranch$norm; build=$green$targetBuildID$norm; type=$green$targetType$norm; alias=$green$targetAlias$norm"; |
| 178 | if [ "x$verbose" != "x" ] && [ "x$pairs" != "x" ]; then |
| 179 | echo -e "[ren] Substitution pairs:"; |
| 180 | for pair in $pairs; do |
| 181 | before=$pair; before=${before%%=*}; # trim from the = to the end |
| 182 | after=$pair; after=${after##*=}; # trim up to the = |
| 183 | if [ "$before" != "$after" ]; then |
| 184 | echo -e " $red$before$norm -> $green$after$norm"; |
| 185 | fi |
| 186 | done |
| 187 | fi |
| 188 | |
| 189 | # define source/target folders |
| 190 | sourceFolder="$buildDropsDir/$sourceBranch/$sourceBuildID"; |
| 191 | targetFolder="$buildDropsDir/$targetBranch/$targetBuildID"; |
| 192 | |
| 193 | echo ""; |
| 194 | echo -e "[ren] Step 1: copy $sourceFolder"; |
| 195 | echo -e " to $targetFolder"; |
| 196 | mkdir -p $targetFolder; cp -r $sourceFolder/* $targetFolder/; |
| 197 | |
| 198 | echo ""; echo -e "[ren] Step 2: rename any zip/md5 files in target folder matching "$sourceBuildID; |
| 199 | cd $targetFolder; |
| 200 | |
| 201 | list=`find $targetFolder -name "*$sourceBuildID\.zip*" -o -name "*$sourceAlias\.zip*"` |
| 202 | num=0; |
| 203 | for file in $list; do |
| 204 | (( num++ )); |
| 205 | done |
| 206 | |
| 207 | cnt=0; |
| 208 | for file in $list; do |
| 209 | (( cnt++ )); |
| 210 | targ="$file"; |
| 211 | targ="${targ//$sourceAlias/$targetAlias}"; |
| 212 | targ="${targ//$sourceBuildID/$targetAlias}"; |
| 213 | targ="${targ//$sourceBranch/$targetBranch}"; |
| 214 | if [ "x$verbose" != "x" ]; then |
| 215 | echo -e "[ren] [$blue$cnt$norm/$blue$num$norm] "${file##*\/}" -> "${targ##*\/}; |
| 216 | fi |
| 217 | mv -f "$file" "$targ"; |
| 218 | done |
| 219 | |
| 220 | if [ "x$verbose" = "x" ]; then |
| 221 | echo -e "[ren] $green$num$norm files renamed."; |
| 222 | fi |
| 223 | |
| 224 | echo ""; |
| 225 | echo -e "[ren] Step 3: fix text files (xml, md5, html, txt) with $red$sourceAlias$norm, $red$sourceBuildID$norm, $red$sourceBranch$norm or $red$sourceType$norm"; |
| 226 | |
| 227 | # pass in a /fully/qualifed/path/to/a/file and a group owner (eg., www-data); fsize will contain the filesize in bytes |
| 228 | getFileSize() |
| 229 | { |
| 230 | f=$1; # file name |
| 231 | g=$2; # group name |
| 232 | fsize=`alias ls='ls' && ls -ls "$f"`; fsize=${fsize%%"$f"*}; fsize=${fsize##*$g}; |
| 233 | fsize=`echo -e $fsize | sed -e 's/[a-zA-Z\-]//g' -e 's/.* //g' -e 's/[0-9]\+\ \+[0-9]\+\:[0-9]\+//g' -e 's/[\/\ \.]\+//g'` |
| 234 | } |
| 235 | |
| 236 | totalnum=0; |
| 237 | for ext in "xml" "md5" "html" "txt" "cfg" "php"; do |
| 238 | list=`find $targetFolder -name "*\.$ext"`; |
| 239 | num=0; |
| 240 | for file in $list; do |
| 241 | (( totalnum++ )); |
| 242 | (( num++ )); |
| 243 | done |
| 244 | |
| 245 | cnt=0; |
| 246 | for file in $list; do |
| 247 | (( cnt++ )); |
| 248 | if [ `cat $file | grep -c "$sourceBuildID\|$sourceBranch"` > 0 ]; then # file contains the string, must sed it |
| 249 | filename="${file##*\/}"; |
| 250 | tmpfile="$tmpdir/$filename"; |
| 251 | tmpfile2="$tmpdir/$filename"2; |
| 252 | if [ "x$verbose" != "x" ]; then |
| 253 | echo -e -n "[ren] [$blue$cnt$norm/$blue$num$norm] Replacing $yellow$filename$norm: "; |
| 254 | fi |
| 255 | |
| 256 | # get old filesize |
| 257 | getFileSize "$file" "www-data"; res1=$fsize; |
| 258 | |
| 259 | # make changes: |
| 260 | # replace "N2004...".zip with "2.0.2".zip |
| 261 | # replace "Nightly Build: N2004..." with "Release Build: 2.0.2" |
| 262 | # replace "Nightly" with "Release" |
| 263 | # replace "N2004..." with "R2004..." |
| 264 | # replace "2.0.3.1" with "2.0.4" |
| 265 | |
| 266 | # to escape "." as "\." use ${foo//./\\.} instead of $foo |
| 267 | cat "$file" | sed \ |
| 268 | -e 's/'${sourceAlias//./\\.}'/'${targetAlias//./\\.}'/g' \ |
| 269 | -e 's/'${sourceBuildID//./\\.}\.zip'/'${targetAlias//./\\.}\.zip'/g' \ |
| 270 | -e 's/'$sourceType' Build\: '${sourceBuildID//./\\.}'/'$targetType' Build\: '${targetAlias//./\\.}'/g' \ |
| 271 | -e 's/'$sourceType'/'$targetType'/g' \ |
| 272 | -e 's/'${sourceBuildID//./\\.}'/'${targetBuildID//./\\.}'/g' \ |
| 273 | -e 's/'${sourceBranch//./\\.}'/'${targetBranch//./\\.}'/g' \ |
| 274 | > "$tmpfile"; |
| 275 | |
| 276 | # replace <beforestring> with <afterstring>, eg., eclipse-SDK-I20050201-0800-linux-gtk.zip with eclipse-SDK-3.1M5-linux-gtk.tar.gz |
| 277 | if [ "x$pairs" != "x" ]; then |
| 278 | for pair in $pairs; do |
| 279 | before=$pair; before=${before%%=*}; # trim from the = to the end |
| 280 | after=$pair; after=${after##*=}; # trim up to the = |
| 281 | before=${before//\//\\\/}; before=${before//./\\.}; # escape "." and "/" |
| 282 | after=${after//\//\\\/}; after=${after//./\\.}; # escape "." and "/" |
| 283 | if [ "$before" != "$after" ]; then |
| 284 | cat "$tmpfile" | sed -e 's/'"$before"'/'"$after"'/g' > "$tmpfile2"; |
| 285 | mv -f "$tmpfile2" "$tmpfile"; |
| 286 | fi |
| 287 | done |
| 288 | fi |
| 289 | |
| 290 | # get new filesize |
| 291 | getFileSize "$tmpfile" "www-data"; res2=$fsize; |
| 292 | |
| 293 | # replace file |
| 294 | if [ "x$verbose" != "x" ]; then |
| 295 | echo -e "size: $res1 -> $res2"; |
| 296 | fi |
| 297 | mv -f "$tmpfile" "$file"; |
| 298 | fi |
| 299 | done |
| 300 | if [ "x$verbose" != "x" ]; then |
| 301 | echo ""; |
| 302 | fi |
| 303 | done |
| 304 | |
| 305 | if [ "x$verbose" = "x" ]; then |
| 306 | echo -e "[ren] $green$totalnum$norm files changed."; |
| 307 | echo ""; |
| 308 | fi |
| 309 | |
| 310 | rm -fr $tmpdir; |
| 311 | echo "[ren] Finished `date +%Y%m%d\ %H\:%M\:%S`. Please verify that your build's index.html contains no invalid links."; |
| 312 | echo ""; |