first draft for also supporting an easier tagging process - automatically handles...
[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 ##############################
112 # guess spec files and extract from there
113 # search for <module.spec> otherwise expects a single *.spec file
114 function figure_module_spec () {
115     src=$1; shift
116     module=$1; shift
117     if [ -f $src/$module.spec ] ; then
118         echo $src/$module.spec
119     else
120         nbspecs=$(ls $src/*.spec | wc -l)
121         if [ $nbspecs = 1 ] ; then
122             echo $(ls $src/*.spec)
123           else
124               echo "$0: could not guess spec file in $src"
125               exit 1
126           fi
127     fi
128 }
129
130 function extract_from_spec () {
131     specfile=$1; shift
132     varname=$1; shift
133     if [ ! -f $specfile ] ; then
134       echo "Could not locate $specfile -- exiting"
135       exit 1
136     fi
137     line=$(grep "^%define[ \t][ \t]*$varname" $specfile)
138     value=$(echo $line | sed -e "s,%define[ \t][ \t]*$varname[ \t][ \t]*,,")
139     echo $value
140 }
141
142 function patch_in_spec () {
143     specfile=$1; shift
144     varname=$1; shift
145     value=$1; shift
146
147     ### sed -i unsupported on MACOS
148     sed -e "s,^%define[ \t][ \t]*$varname[ \t].*$,%define $varname $value," $specfile > ${specfile}.new
149     mv ${specfile}.new $specfile
150 }
151
152 ##############################
153 ##############################
154 ##############################
155
156 function print_warning () {
157
158     cat <<EOF
159 ####### WARNING
160
161  this script is quite rough and should be used with care
162
163  I am slowly trying to automate this painful process,
164
165  so again : USE WITH CARE
166
167 module=$module
168 previous=$previous
169 next=$next
170 cvstag=$cvstag
171  
172 svn=$svn
173 cvs=$cvs
174 date=$date
175 import=$import
176 tagwork=$tagwork
177 tagtrunk=$tagtrunk
178 ####### WARNING
179
180 EOF
181 }
182
183 function run_test () {
184   message Hi there
185   run_prompt n "going to /etc" cd /etc
186   run_prompt y "going to /etc" cd /etc
187   run_prompt y "Cleaning up /foobar" rmdir /foobar
188   run_prompt $(dir_needs_creation /foobar) "create unexisting dir /foobar" mkdir /foobar
189   run_prompt $(dir_needs_creation /var) "create existing dir /var" mkdir /var
190   run_prompt y "Cleaning up /foobar" rmdir /foobar
191   
192   run_loop "Check this and that" ls -ls passwd
193   run "Listing passwd" ls -ls passwd
194   exit 0
195 }
196
197 function import_usage () {
198    echo "Usage: $0"
199    echo "All parameters to be entered in the source file for now - sorry"
200    exit 1
201 }
202
203 function import_module () {
204
205     [[ -n "$@" ]] && import_usage
206
207     print_warning
208
209     # checks current status
210     echo "------------------------------ Checking $module/imports/import-$previous"
211     [ -z "$(svn_url_check $svn/$module/imports/import-$previous)" ] && echo WARNING : import-$previous not found
212
213     echo ''
214
215     # snapshot current status
216     prompt=$(svn_url_needs_creation $svn/$module/imports/before-$next)
217     run_prompt $prompt "Creating snapshot of current status" \
218         svn copy $svn/$module/trunk $svn/$module/imports/before-$next
219
220     # init import dir from previous import
221     prompt=$(svn_url_needs_creation $svn/$module/imports/import-$next)
222     run_prompt $prompt "Preparing import-$next from import-$previous" \
223         svn copy $svn/$module/imports/import-$previous $svn/$module/imports/import-$next
224
225     ####
226     run_prompt $(dir_needs_creation $import/$module/imports) "Creating working dir " \
227         mkdir -p $import/$module/imports
228
229     ###
230     run_prompt $(dir_needs_creation $import/$module/imports/import-$next) \
231         "Checking out import-$next for applying changes" \
232         svn co $svn/$module/imports/import-$next $import/$module/imports/import-$next
233
234     run "Going there" \
235         cd $import/$module/imports
236
237     message 'NOTE: the way we merge changes right below might need to be tested/improved'
238
239     run_prompt n "Incorporating changes since import-$previous in import-$next" \
240         cvs -d $cvs export -r $cvstag -d import-$next $module
241
242     run "Going to import-$next" \
243         cd $import/$module/imports/import-$next
244
245     run_loop "Check the changes in import-$next -- no conflict expected" \
246         svn status $import/$module/imports/import-$next
247
248     run_prompt n "CHECKING IN changes in import-$next" \
249         svn ci -m "Changes since $previous"
250
251     ###
252     run "Going to $import/$module" \
253         cd $import/$module
254
255     run_prompt $(dir_needs_creation $svn/$module/trunk) "Checking out trunk for applying changes" \
256         svn co $svn/$module/trunk 
257
258     run_prompt n "Merging changes from import-$previous to import-$next into trunk" \
259         svn merge $svn/$module/imports/import-$previous $svn/$module/imports/import-$next trunk
260
261     run_loop "Check the changes in trunk -- conflicts should get resolved" \
262         svn status trunk
263
264     run_prompt n "CHECKING IN changes in trunk" \
265         svn ci -m "Merged changes from import-$previous to import-$next"
266
267     prompt=$(svn_url_needs_creation $svn/$module/imports/after-$next)
268     run_prompt $prompt "Backing up into after-$next" \
269         svn copy $svn/$module/$trunk $svn/$module/imports/after-$next
270
271     run_prompt n "Want to rename $import as $import-to-trash" \
272         mv $import $import-totrash
273
274     echo 'Done'
275 }
276
277 function tag_usage () {
278    echo "Usage: $0 module"
279    exit 1
280 }
281
282 function tag_module () {
283
284     [[ -z "$@" ]] && tag_usage
285     module=$1; shift
286     tagwork=$HOME/tags/$module
287     tagtrunk=$HOME/tags/$module/trunk
288     [[ -n "$@" ]] && tag_usage
289
290     echo 'Tagging' $module
291
292     run_prompt $(dir_needs_creation $tagwork) "Creating $tagwork" \
293         mkdir -p $tagwork
294
295     run_prompt $(dir_needs_creation $tagtrunk) "Checking out $tagtrunk" \
296         svn co $svn/$module/trunk $tagtrunk
297
298     run_prompt y "Updating $tagtrunk" \
299         svn up $tagtrunk
300
301     run_prompt $(svn_dir_needs_revert $tagtrunk) "Reverting $tagtrunk for safety " \
302         svn revert -R $tagtrunk
303
304     run "cd $tagtrunk" \
305        cd $tagtrunk
306
307     ### extracting current version info
308     spec=$(figure_module_spec $tagtrunk $module)
309     version=$(extract_from_spec $spec version)
310     subversion=$(extract_from_spec $spec subversion)
311     newsubversion=$(($subversion+1))
312
313     message "FOUND spec=$spec version=$version subversion=$subversion"
314
315     # check that the tags relating to subversion and newsubversion does (resp. does not) exist
316     oldtag=$svn/$module/tags/$module-${version}.${subversion}
317     newtag=$svn/$module/tags/$module-${version}.${newsubversion}
318
319     echo -n "Checking tags status ..."
320     old_check=$(svn_url_check $oldtag)
321     if [ -z "$old_check" ] ; then
322       echo "Former tag does not exist -- exiting"
323       echo "URL was $oldtag"
324       exit 1
325     fi
326     echo -n " old tag OK ... "
327     
328     new_check=$(svn_url_check $newtag)
329     if [ -n "$new_check" ] ; then
330       echo "New tag does exist -- exiting"
331       echo "URL was $newtag"
332       exit 1
333     fi
334     echo " new tag OK"
335     
336     patch_in_spec $spec subversion $newsubversion
337
338     run_loop "Checking differences" \
339        svn diff $tagtrunk
340
341     run_prompt y "Display differences from previous tag" \
342        svn diff $oldtag $svn/$module/trunk
343     
344     run_prompt y "Committing changes in $tagtrunk" \
345        svn ci --editor-cmd=vi $tagtrunk
346         
347     # putting spaces in the message does not work 
348     run_prompt y "Creating tag $newtag" \
349        svn copy --message "$module-${version}.${newsubversion}" $svn/$module/trunk $newtag
350
351     echo "This command does not handle changes in onelab-tags.mk - yet"
352
353     
354 }
355
356 case $0 in
357   *import-module*)
358     import_module $@;;
359   *tag-module*)
360     tag_module $@;;
361   *test*)
362     x=$(figure_module_spec $@) ; echo $x;;
363   *)
364     echo "Unsupported command $0" ; exit 0
365   esac