mco.sh adopted to new maintenance process
[opensuse:pack-tools.git] / mco.sh
1 #!/bin/bash
2 #set -x
3
4 USER=pgajdos
5 isc="osc -A https://api.suse.de"
6 osc="osc -A https://api.opensuse.org"
7 DEVEL_DIST="devel"
8 DEFAULT_DIST="$DEVEL_DIST"
9 OSC_PROJECTS="$DEVEL_DIST 11.3 11.4 12.1"
10 ISC_PROJECTS="SLE-9-SP4 SLE-10-SP3 SLE-10-SP4 SLE-11-SP1 SLE-11-SP2"
11
12 ALL_PROJECTS="$OSC_PROJECTS $ISC_PROJECTS"
13 OSC_DISTDIRS=$OSC_PROJECTS
14 ISC_DISTDIRS=`echo $ISC_PROJECTS | sed "s:SLE-\|-::g" | tr [A-Z] [a-z]`
15 ALL_DISTDIRS="$DEVEL_DIST $OSC_DISTDIRS $ISC_DISTDIRS"
16 ALL_DIST_ARGUMENTS="all obs ibs $ALL_DISTDIRS"
17
18 function usage
19 {
20   echo "Usage: $0 \$package [all|ibs|obs|\$dist] [wipe]"
21   echo ""
22   echo "       \$dist: $ALL_DISTDIRS"
23   echo "              package is saved into \$dist/\$package;"
24   echo "              when no second argument is supplied: "
25   echo "              - when you are in some of dist subdirectory"
26   echo "                ($ALL_DISTDIRS) "
27   echo "                name of this subdirectory is used for \$dist"
28   echo "                and package is saved into ./\$package;"
29   echo "              - otherwise $DEFAULT_DIST is used for \$dist"
30   echo "                and package is saved into $DEFAULT_DIST/\$package"
31   echo "       wipe:  when specified, home:$USER:branches:*"
32   echo "              will be erased before osc branch;"
33   echo "              for $DEVEL_DIST doesn't make sense (noop)"
34   exit 1
35 }
36
37 # handle arguments
38 if [ $# -lt 1 -o $# -gt 3 ]; then
39   echo "ERROR: Wrong number of arguments ($#)."
40   usage
41 fi
42
43 PACKAGE=$1
44 DIST=""
45 WIPE=0
46 shift
47 while (( "$#" )); do
48   if [ "`echo $ALL_DIST_ARGUMENTS | grep -i $1`" ]; then
49     DIST=`echo $1 | tr [A-Z] [a-z]`
50   elif [ "$1" == wipe ]; then
51     WIPE=1
52   else
53     echo "ERROR: Wrong argument ($1)."
54     usage
55   fi
56   shift
57 done
58
59 # DIST
60 IN_DISTDIR="no"
61
62 if [ "$DIST" == "" ]; then
63   CURDIR=`echo $PWD | sed "s:^.*/::"`
64   if [ "`echo $ALL_DISTDIRS | grep $CURDIR`" != "" ]; then
65     DIST="$CURDIR"
66     IN_DISTDIR="yes"
67   else
68     DIST="$DEFAULT_DIST"  # default DIST
69   fi
70 fi
71
72 # 11sp1 --> SLE-11-SP1, etc.
73 if [ "`echo $ISC_DISTDIRS | grep $DIST`" != "" ]; then
74   DIST=`echo $DIST | tr [a-z] [A-Z] | sed "s:\(^[0-9]\+\):SLE-\1-:" | sed "s:-$::"`
75 fi
76
77 DIST=`echo $DIST | sed "s:all:$ALL_PROJECTS:" | sed "s:ibs:$ISC_PROJECTS:" | sed "s:obs:$OSC_PROJECTS:"`
78
79 # results
80 echo
81 echo "Package:          [$PACKAGE]"
82 echo "Distribution(s):  [$DIST]"
83 echo "Wipe:             [$WIPE]"
84
85 # intended to wipe packages from home:$USER:branches:*
86 # !!! wipe used to behave the same way for openSUSE and SLE versions;
87 #     I hope it will do also in the future:
88 #     $CMD rdelete --recursive -m "delete" "$PRJ:$PACKAGE"
89 #     where $CMD \in {$ibs, $obs} (replaces TYPE) will be enough then
90 function wipe()
91 {
92   TYPE=$1
93   DIST=$2
94
95   if [ $WIPE -eq 1 ]; then
96     if [ "$TYPE" == "ibs" ]; then
97       echo "Wiping home:$USER:branches:SUSE:$DIST[:GA|[[:Update]:Test]]/$PACKAGE"
98       $isc rdelete -m "delete" "home:$USER:branches:SUSE:$DIST:GA" $PACKAGE >/dev/null 2>&1
99       $isc rdelete -m "delete" "home:$USER:branches:SUSE:$DIST:Update" $PACKAGE >/dev/null 2>&1
100       $isc rdelete -m "delete" "home:$USER:branches:SUSE:$DIST:Update:Test" $PACKAGE >/dev/null 2>&1
101     else
102       echo "Wiping home:$USER:branches:OBS_Maintained:$PACKAGE/$PACKAGE.openSUSE_$DIST"
103 #      $osc rdelete -m "delete" "home:$USER:branches:OBS_Maintained:$PACKAGE/$PACKAGE.openSUSE_$DIST >/dev/null 2>&1"
104       echo "* This will not work for current osc mbranch. It could be ok when osc mbranch for one distribution package"
105       echo "* will be implemented. Then it could be possible to mbranch/remove packages independently. Now it works this way:"
106       echo "*   pgajdos@laura:~/pokus> osc mbranch dosfstools"
107       echo "*   Project home:pgajdos:branches:OBS_Maintained:dosfstools created."
108       echo "*   pgajdos@laura:~/pokus> osc rdelete -m "delete" home:pgajdos:branches:OBS_Maintained:dosfstools/dosfstools.openSUSE_11.4"
109       echo "*   pgajdos@laura:~/pokus> osc mbranch dosfstools"
110       echo "*   BuildService API error: branch target package already exists: home:pgajdos:branches:OBS_Maintained:dosfstools/dosfstools.openSUSE_11.3"
111       echo
112       echo 'Invoke manually $osc rdelete --recursive -m "delete" "home:$USER:branches:OBS_Maintained:$PACKAGE'
113       echo to remove all packages from home:$USER:branches:OBS_Maintained:$PACKAGE.
114     fi
115   fi
116 }
117
118 # download package and save in current dir
119 function get_package()
120 {
121   DIST=$1
122   TYPE=$2
123
124   if [ -e $PACKAGE ]; then
125     echo "Removing $DIST/$PACKAGE directory"
126     rm -rf $PACKAGE
127   fi 
128
129   case $TYPE in  
130
131     "devel")
132       COPROJECT=`$osc meta pkg openSUSE:Factory $PACKAGE | grep "<devel" | sed "s:.*project=\"*::" | sed "s:\".*::"`
133       echo
134       echo "devel project ($COPROJECT)"
135       if [ "$COPROJECT" == "" ]; then
136         echo "ERROR: Devel project of package $PACKAGE couldn't be figured out, package not downloaded."
137         echo "       Check if $PACKAGE lies in openSUSE:Factory."
138       fi
139       $osc co -c $COPROJECT $PACKAGE
140       ;;
141
142     "obs")
143       wipe $TYPE $DIST
144       COPROJECT="home:$USER:branches:OBS_Maintained:$PACKAGE/$PACKAGE.openSUSE_$DIST"
145       echo
146       echo "openSUSE:$DIST ($COPROJECT)"
147       if [ $OBS_MBRANCHED -eq 0 ]; then
148         RESULT=`$osc mbranch $PACKAGE 2>&1`
149         if [ $? -ne 0 ]; then
150           if [ "`echo $RESULT | grep 'branch target package already exists'`" != "" ]; then
151             # no action here
152             true
153           elif [ "`echo $RESULT | grep 'no packages found'`" != "" ]; then
154             echo "ERROR: package not found"
155             return # terminate get_package(), but not terminate whole download script
156           else
157             echo "ERROR: unknown osc MBRANCH message ($RESULT)"
158             echo "$0 needs to be fixed to handle it"
159             exit 1
160           fi
161         fi
162       fi
163       OBS_MBRANCHED=1
164       $osc co -c $COPROJECT
165       mv "$PACKAGE.openSUSE_$DIST" $PACKAGE
166       ;;
167
168     "ibs")
169       wipe $TYPE $DIST
170       COPROJECT=`$isc branch -m "maintanence update" "SUSE:$DIST:GA" $PACKAGE 2>&1 | grep "home:$USER:branches" | sed "s/.*\(home.*\)/\1/"`
171       echo
172       echo "SUSE:$DIST ($COPROJECT)"
173       if [ "$COPROJECT" == "" ]; then
174         echo "ERROR: package not found"
175         return
176       fi
177       $isc co -c $COPROJECT
178       ;;
179   esac
180   sed -i "s:\(Release.*\).<.*>:\1:" $PACKAGE/$PACKAGE.spec
181 }
182
183 OBS_MBRANCHED=0
184 for d in $DIST; do
185   if [ "$d" == "$DEVEL_DIST" ]; then
186     TYPE="devel"
187   elif [ "`echo $OSC_PROJECTS | grep $d`" ]; then
188     TYPE="obs"
189   elif [ "`echo $ISC_PROJECTS | grep $d`" ]; then
190     TYPE="ibs"
191   fi
192
193   echo
194   if [ "$IN_DISTDIR" == "no" ]; then
195     DISTDIR=`echo $d | sed "s:SLE-\|-::g" | tr [A-Z] [a-z]`
196     if [ ! -e $DISTDIR ]; then
197       echo "Creating $DISTDIR directory"
198       mkdir $DISTDIR;
199     fi
200     cd $DISTDIR
201   fi
202
203   get_package $d $TYPE
204
205   if [ "$IN_DISTDIR" == "no" ]; then
206     cd ..
207   fi
208 done
209