creation
[infrastructure.git] / scripts / import-module.sh
1 #!/bin/bash
2 #
3 # $Id$
4 #
5
6 date=$(date +%Y-%m-%d)
7
8 #### REVIEW this carefully
9 module=new_plc_api
10 cvstag=planetlab-4_0-rc3
11 previous=002-rc3-2007-05-02
12 next=003-${cvstag}-${date}
13
14 ####
15 svn="svn+ssh://thierry@svn.one-lab.org/svn"
16 cvs=":pserver:anon@cvs.planet-lab.org:/cvs"
17 import=$HOME/import-$module-$date
18
19 function message () {
20   echo -n '-------------------- '
21   echo $@
22   echo -n 'Enter return to proceed .. '
23   read x
24 }
25
26 function run () {
27   echo -n '-------------------- '
28   message="$1" ; shift
29   echo $message
30   
31   echo -n 'PWD=' ; pwd
32   echo RUNNING $@
33   $@
34 }
35
36 function run_prompt () {
37   default="$1" ; shift
38   message="$1" ; shift
39   case "$default" in
40     y|Y) label="[y]/n" ;;
41     n|N) label="y/[n]" ;;
42     *) label="y/n (no default)" ;;
43   esac
44
45   echo -n '-------------------- '
46   echo $message
47   
48   echo -n 'PWD= ' ; pwd 
49   echo -n "want to run $@ $label ? "
50   read answer
51   [ -z "$answer" ] && answer=$default
52   while true; do
53     case $answer in
54       y|Y)
55         $@
56         break
57         ;;
58       n|N)
59         echo 'Skipped'
60         break
61         ;;
62       *)
63         echo 'Please enter y or n'
64         ;;
65     esac
66   done
67 }
68
69 function run_loop () {
70   message="$1" ; shift
71   while true; do
72     echo -n '-------------------- '
73     echo $message
74     echo -n PWD= ; pwd
75     echo Running $@
76     $@
77     echo -n 'OK ? y/[n] '
78     read answer
79     [ -z "$answer" ] && answer=n
80     case $answer in
81       y|Y)
82         break ;;
83       *) ;;
84   esac
85 done
86 }
87
88 ### echoes OK on stdout if url exists
89 function svn_url_check () {
90    url=$1; shift
91    svn list $url &> /dev/null && echo OK
92 }
93
94 ### return 'y' or 'n' for being used as prompt
95 function svn_url_needs_creation () {
96    url=$1; shift
97    if [ -n "$(svn_url_check $url)" ] ; then echo n ; else echo y ; fi
98 }
99
100 function dir_needs_creation () {
101    dir=$1; shift
102    if [ -d "$dir" ] ; then echo n ; else echo y ; fi
103 }
104
105 function svn_dir_needs_revert () {
106    dir=$1; shift
107    output=$(svn status $dir 2>&1)
108    if [ -n "$output" ] ; then echo y ; else echo n ; fi
109 }
110
111 function svn_file_needs_commit () {
112    dir=$1; shift
113    output=$(svn status $dir 2>&1)
114    if [ -n "$output" ] ; then echo y ; else echo n ; fi
115 }
116
117 ##############################
118 # guess spec files and extract from there
119 # search for <module.spec> otherwise expects a single *.spec file
120 function figure_module_spec () {
121     src=$1; shift
122     module=$1; shift
123     if [ -f $src/$module.spec ] ; then
124         echo $src/$module.spec
125     else
126         nbspecs=$(ls $src/*.spec | wc -l)
127         if [ $nbspecs = 1 ] ; then
128             echo $(ls $src/*.spec)
129           else
130               echo "$0: could not guess spec file in $src"
131               exit 1
132           fi
133     fi
134 }
135
136 function extract_from_spec () {
137     specfile=$1; shift
138     varname=$1; shift
139     if [ ! -f $specfile ] ; then
140       echo "Could not locate $specfile -- exiting"
141       exit 1
142     fi
143     line=$(grep "^%define[ \t][ \t]*$varname" $specfile)
144     value=$(echo $line | sed -e "s,%define[ \t][ \t]*$varname[ \t][ \t]*,,")
145     echo $value
146 }
147
148 function patch_in_spec () {
149     specfile=$1; shift
150     varname=$1; shift
151     value=$1; shift
152
153     ### sed -i unsupported on MACOS
154     sed -e "s,^%define[ \t][ \t]*$varname[ \t].*$,%define $varname $value," $specfile > ${specfile}.new
155     mv ${specfile}.new $specfile
156 }
157
158 ##############################
159 ##############################
160 ##############################
161
162 function print_warning () {
163
164     cat <<EOF
165 ####### WARNING
166
167  this script is quite rough and should be used with care
168
169  I am slowly trying to automate this painful process,
170
171  so again : USE WITH CARE
172
173 module=$module
174 previous=$previous
175 next=$next
176 cvstag=$cvstag
177  
178 svn=$svn
179 cvs=$cvs
180 date=$date
181 import=$import
182 tagwork=$tagwork
183 tagtrunk=$tagtrunk
184 ####### WARNING
185
186 EOF
187 }
188
189 function run_test () {
190   message Hi there
191   run_prompt n "going to /etc" cd /etc
192   run_prompt y "going to /etc" cd /etc
193   run_prompt y "Cleaning up /foobar" rmdir /foobar
194   run_prompt $(dir_needs_creation /foobar) "create unexisting dir /foobar" mkdir /foobar
195   run_prompt $(dir_needs_creation /var) "create existing dir /var" mkdir /var
196   run_prompt y "Cleaning up /foobar" rmdir /foobar
197   
198   run_loop "Check this and that" ls -ls passwd
199   run "Listing passwd" ls -ls passwd
200   exit 0
201 }
202
203 function import_usage () {
204    echo "Usage: $0"
205    echo "All parameters to be entered in the source file for now - sorry"
206    exit 1
207 }
208
209 function import_module () {
210
211     [[ -n "$@" ]] && import_usage
212
213     print_warning
214
215     # checks current status
216     echo "------------------------------ Checking $module/imports/import-$previous"
217     [ -z "$(svn_url_check $svn/$module/imports/import-$previous)" ] && echo WARNING : import-$previous not found
218
219     echo ''
220
221     # snapshot current status
222     prompt=$(svn_url_needs_creation $svn/$module/imports/before-$next)
223     run_prompt $prompt "Creating snapshot of current status" \
224         svn copy $svn/$module/trunk $svn/$module/imports/before-$next
225
226     # init import dir from previous import
227     prompt=$(svn_url_needs_creation $svn/$module/imports/import-$next)
228     run_prompt $prompt "Preparing import-$next from import-$previous" \
229         svn copy $svn/$module/imports/import-$previous $svn/$module/imports/import-$next
230
231     ####
232     run_prompt $(dir_needs_creation $import/$module/imports) "Creating working dir " \
233         mkdir -p $import/$module/imports
234
235     ###
236     run_prompt $(dir_needs_creation $import/$module/imports/import-$next) \
237         "Checking out import-$next for applying changes" \
238         svn co $svn/$module/imports/import-$next $import/$module/imports/import-$next
239
240     run "Going there" \
241         cd $import/$module/imports
242
243     message 'NOTE: the way we merge changes right below might need to be tested/improved'
244
245     run_prompt n "Incorporating changes since import-$previous in import-$next" \
246         cvs -d $cvs export -r $cvstag -d import-$next $module
247
248     run "Going to import-$next" \
249         cd $import/$module/imports/import-$next
250
251     run_loop "Check the changes in import-$next -- no conflict expected" \
252         svn status $import/$module/imports/import-$next
253
254     run_prompt n "CHECKING IN changes in import-$next" \
255         svn ci -m "Changes since $previous"
256
257     ###
258     run "Going to $import/$module" \
259         cd $import/$module
260
261     run_prompt $(dir_needs_creation $svn/$module/trunk) "Checking out trunk for applying changes" \
262         svn co $svn/$module/trunk 
263
264     run_prompt n "Merging changes from import-$previous to import-$next into trunk" \
265         svn merge $svn/$module/imports/import-$previous $svn/$module/imports/import-$next trunk
266
267     run_loop "Check the changes in trunk -- conflicts should get resolved" \
268         svn status trunk
269
270     run_prompt n "CHECKING IN changes in trunk" \
271         svn ci -m "Merged changes from import-$previous to import-$next"
272
273     prompt=$(svn_url_needs_creation $svn/$module/imports/after-$next)
274     run_prompt $prompt "Backing up into after-$next" \
275         svn copy $svn/$module/$trunk $svn/$module/imports/after-$next
276
277     run_prompt n "Want to rename $import as $import-to-trash" \
278         mv $import $import-totrash
279
280     echo 'Done'
281 }
282
283 function tag_usage () {
284    echo "Usage: $0 module"
285    exit 1
286 }
287
288 function tag_module () {
289
290     [[ -z "$@" ]] && tag_usage
291     module=$1; shift
292     tagwork=$HOME/tags/$module
293     tagtrunk=$HOME/tags/$module/trunk
294     [[ -n "$@" ]] && tag_usage
295
296     echo 'Tagging' $module
297
298     run_prompt $(dir_needs_creation $tagtrunk) "Checking out $tagtrunk" \
299         svn co $svn/$module/trunk $tagtrunk
300
301     run_prompt y "Updating $tagtrunk" \
302         svn up $tagtrunk
303
304     run_prompt $(svn_dir_needs_revert $tagtrunk) "Reverting $tagtrunk for safety " \
305         svn revert -R $tagtrunk
306
307     run "cd $tagtrunk" \
308        cd $tagtrunk
309
310     ### extracting current version info
311     spec=$(figure_module_spec $tagtrunk $module)
312     version=$(extract_from_spec $spec version)
313     subversion=$(extract_from_spec $spec subversion)
314     newsubversion=$(($subversion+1))
315
316     message "FOUND spec=$spec version=$version subversion=$subversion"
317
318     # check that the tags relating to subversion and newsubversion does (resp. does not) exist
319     oldtag=$svn/$module/tags/$module-${version}.${subversion}
320     newtag=$svn/$module/tags/$module-${version}.${newsubversion}
321
322     echo -n "Checking tags status ..."
323     old_check=$(svn_url_check $oldtag)
324     if [ -z "$old_check" ] ; then
325       echo "Former tag does not exist -- exiting"
326       echo "URL was $oldtag"
327       exit 1
328     fi
329     echo -n " old tag OK ... "
330     
331     new_check=$(svn_url_check $newtag)
332     if [ -n "$new_check" ] ; then
333       echo "New tag does exist -- exiting"
334       echo "URL was $newtag"
335       exit 1
336     fi
337     echo " new tag OK"
338     
339     patch_in_spec $spec subversion $newsubversion
340
341     run_loop "Checking differences" \
342        svn diff $tagtrunk
343
344     run_prompt y "Display differences from previous tag" \
345        svn diff $oldtag $svn/$module/trunk
346     
347     run_prompt y "Committing changes in $tagtrunk" \
348        svn ci --editor-cmd=vi $tagtrunk
349         
350     # putting spaces in the message does not work 
351     run_prompt y "Creating tag $newtag" \
352        svn copy --message "$module-${version}.${newsubversion}" $svn/$module/trunk $newtag
353
354     # make changes in build/*-tags.mk
355     tagbuild=$HOME/tags/build/trunk
356     oldname=$module-${version}.${subversion}
357     newname=$module-${version}.${newsubversion}
358
359     run_prompt $(dir_needs_creation $tagbuild) "Checking out $tagbuild" \
360         svn co $svn/$module/build $tagbuild
361
362     run_prompt y "Updating $tagbuild" \
363         svn up $tagbuild
364
365     run_prompt $(svn_dir_needs_revert $tagbuild) "Reverting $tagbuild for safety " \
366         svn revert -R $tagbuild
367
368     run "cd $tagbuild" \
369        cd $tagbuild
370
371     for tagsmk in *-tags.mk; do
372        echo "Managing $tagsmk"
373        sed -i -e "s,$oldname,$newname,g" $tagsmk
374        if [ $(svn_file_needs_commit $tagsmk) = "y" ] ; then
375            echo "Changes in $tagsmk"
376            svn diff $tagsmk
377            run_prompt y "Adopt $newname in $tagsmk " \
378                svn commit -m"Adopting-$newname" $tagsmk
379        fi
380     done    
381 }
382
383 case $0 in
384   *import-module*)
385     import_module $@;;
386   *tag-module*)
387     tag_module $@;;
388   *test*)
389     x=$(figure_module_spec $@) ; echo $x;;
390   *)
391     echo "Unsupported command $0" ; exit 0
392   esac