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