source: trunk/coin-functions @ 1563

Last change on this file since 1563 was 1563, checked in by lou, 11 years ago

Really should have the legal notices and svn id.

File size: 12.9 KB
Line 
1
2# Utility function definitions for the various COIN scripts.
3
4# Copyright (c) 2010 Lou Hafer  Simon Fraser University
5# All Rights Reserved.
6# This file is distributed under the Common Public License.
7# It is part of the BuildTools project in COIN-OR (www.coin-or.org)
8#
9# $Id$
10
11# Functions to disassemble svn URLs and extract relevant bits. Complicated
12# because we have to account for trunk/stable/release and for the variant
13# syntax of Data and ThirdParty URLs. For your normal project, it's
14#   https://projects.coin-or.org/svn/ProjName/trunk
15#   https://projects.coin-or.org/svn/ProjName/stable/M.m
16#   https://projects.coin-or.org/svn/ProjName/releases/M.m.r
17# For ThirdParty, it's just like a normal project, except the prefix string
18# is longer:
19#   https://projects.coin-or.org/svn/BuildTools/ThirdParty/ProjName
20# Data is the real pain, with this form:
21#  https://projects.coin-or.org/svn/Data/trunk/ProjName
22#  https://projects.coin-or.org/svn/Data/stable/M.m/ProjName
23#  https://projects.coin-or.org/svn/Data/releases/M.m.r/ProjName
24
25# Extract the COIN base from the URL, up to svn. Just in case it ever changes.
26# usage: baseURL=`extractBaseURL $svnURL`
27
28extractBaseURL ()
29{
30  exbu_baseURL=`echo $1 | sed -n -e 's|\(.*/svn\)/.*$|\1|p'`
31  echo "$exbu_baseURL"
32}
33
34
35# Extract branch type (trunk/stable/release) from the URL.
36# usage: branchType=`extractTypeFromURL $svnURL`
37
38extractTypeFromURL ()
39{
40  etfu_type="invalid"
41  case "$1" in
42    */trunk* ) etfu_type=trunk ;;
43    */stable/* ) etfu_type=stable ;;
44    */releases/* ) etfu_type=releases ;;
45  esac
46  echo $etfu_type
47}
48
49
50# Extract the project from a svn URL
51# usage: projName=`extractProjFromURL $svnURL`
52
53extractProjFromURL ()
54{
55  epfu_projPat='\([^/][^/]*\)'
56  epfu_sfxPat='.*$'
57  case "$1" in
58    */Data/trunk/* )
59        epfu_pfxPat=svn/Data/trunk/
60        ;;
61    */Data/stable/* | */Data/releases/* )
62        epfu_pfxPat='svn/Data/[^/][^/]*/[0-9][0-9.]*/'
63        ;;
64    */ThirdParty/* )
65        epfu_pfxPat=svn/BuildTools/ThirdParty/
66        ;;
67    *)
68        epfu_pfxPat=svn/
69        ;;
70  esac
71  epfu_projName=`echo $1 | sed -n -e 's|.*/'$epfu_pfxPat$epfu_projPat$epfu_sfxPat'|\1|p'`
72  echo "$epfu_projName"
73}
74
75# Extract the tail (directory) from an external URL. Relevant only for normal
76# projects, where the external will be a subdirectory of the project. Returns
77# null for Data/ThirdParty/BuildTools.
78# usage: projName=`extractTailFromExt $extURL`
79
80extractTailFromExt ()
81{
82  etfe_tail=
83  case "$1" in
84    */Data/* )
85        ;;
86    */BuildTools/ThirdParty/* )
87        ;;
88    */BuildTools/* )
89        ;;
90    *)
91        etfe_pfxPat=
92        case "$1" in
93          */trunk/* )
94              etfe_pfxPat='.*/trunk/'
95              ;;
96          */stable/* )
97              etfe_pfxPat='.*/stable/[0-9][0-9.]*/'
98              ;;
99          */releases/* )
100              etfe_pfxPat='.*/releases/[0-9][0-9.]*/'
101              ;;
102        esac
103        etfe_tail=`echo $1 | sed -n -e 's|'$etfe_pfxPat'\(.*\)|\1|p'`
104        ;;
105  esac
106  echo "$etfe_tail"
107}
108
109
110# Extract the entire version string from a stable or release URL. Returns a
111# null string if handed a trunk URL or if there's no version in the URL.
112# usage: version=`extractVersionFromURL $svnURL`
113
114extractVersionFromURL ()
115{
116  if expr "$1" : '.*/stable/.*' >/dev/null 2>&1 ||
117     expr "$1" : '.*/releases/.*' >/dev/null 2>&1 ; then
118    evfu_verPat='\([0-9.][0-9.]*\)'
119    evfu_sfxPat='.*$'
120    case "$1" in
121      */Data/stable/* | */Data/releases/* )
122          evfu_pfxPat='svn/Data/[^/][^/]*/'
123          ;;
124      */ThirdParty/* )
125          evfu_pfxPat='svn/BuildTools/ThirdParty/[^/][^/]*/[^/][^/]*/'
126          ;;
127      *)
128          evfu_pfxPat='svn/[^/][^/]*/[^/][^/]*/'
129          ;;
130    esac
131    evfu_verVer=`echo $1 | sed -n -e 's|.*/'$evfu_pfxPat$evfu_verPat$evfu_sfxPat'|\1|p'`
132    echo "$evfu_verVer"
133  else
134    echo ""
135  fi
136}
137
138# Replace the entire version string from a stable or release URL. A trunk
139# URL will be unmodified. newRev will not be accessed unless the URL is a
140# release.
141# usage: newURL=`replaceVersionInURL $oldURL $newMajor $newMinor $newRev`
142
143replaceVersionInURL ()
144{
145  if expr "$1" : '.*/stable/.*' >/dev/null 2>&1 ; then
146    rviu_newVersion=$2.$3
147  elif expr "$1" : '.*/releases/.*' >/dev/null 2>&1 ; then
148    rviu_newVersion=$2.$3.$4
149  else
150    rviu_newVersion=
151    rviu_newURL=$1
152  fi
153  if test -n "$rviu_newVersion" ; then
154    rviu_newURL=`echo $1 | sed -n -e 's|^\(.*\)/[0-9][0-9.]*\(.*\)$|\1/'$rviu_newVersion'\2|p'`
155  fi
156  echo $rviu_newURL
157}
158
159# Extract the major version number from a stable or release URL. Returns -1 if
160# handed a trunk URL or if there's no version in the URL
161# usage: majVer=`extractMajorFromURL $svnURL`
162
163extractMajorFromURL ()
164{
165  if expr "$1" : '.*/stable/.*' >/dev/null 2>&1 ||
166     expr "$1" : '.*/releases/.*' >/dev/null 2>&1 ; then
167    ejfu_majPat='\([0-9][0-9]*\)'
168    ejfu_sfxPat='\.[0-9][0-9]*.*$'
169    case "$1" in
170      */Data/stable/* | */Data/releases/* )
171          ejfu_pfxPat='svn/Data/[^/][^/]*/'
172          ;;
173      */ThirdParty/* )
174          ejfu_pfxPat='svn/BuildTools/ThirdParty/[^/][^/]*/[^/][^/]*/'
175          ;;
176      *)
177          ejfu_pfxPat='svn/[^/][^/]*/[^/][^/]*/'
178          ;;
179    esac
180    ejfu_majVer=`echo $1 | sed -n -e 's|.*/'$ejfu_pfxPat$ejfu_majPat$ejfu_sfxPat'|\1|p'`
181    echo "$ejfu_majVer"
182  else
183    echo "-1"
184  fi
185}
186
187# Extract the minor version number from a stable or release URL. Returns -1 if
188# handed a trunk URL or if there's no version in the URL.
189# usage: minVer=`extractMinorFromURL $svnURL`
190
191extractMinorFromURL ()
192{
193  if expr "$1" : '.*/stable/.*' >/dev/null 2>&1 ||
194     expr "$1" : '.*/releases/.*' >/dev/null 2>&1 ; then
195    emfu_minPat='[0-9][0-9]*\.\([0-9][0-9]*\)'
196    emfu_sfxPat='.*$'
197    case "$1" in
198      */Data/stable/* | */Data/releases/* )
199          emfu_pfxPat='svn/Data/[^/][^/]*/'
200          ;;
201      */ThirdParty/* )
202          emfu_pfxPat='svn/BuildTools/ThirdParty/[^/][^/]*/[^/][^/]*/'
203          ;;
204      *)
205          emfu_pfxPat='svn/[^/][^/]*/[^/][^/]*/'
206          ;;
207    esac
208    emfu_minVer=`echo $1 | sed -n -e 's|.*/'$emfu_pfxPat$emfu_minPat$emfu_sfxPat'|\1|p'`
209    echo "$emfu_minVer"
210  else
211    echo "-1"
212  fi
213}
214
215# Extract the release (revision) number from a release URL. Returns -1 if
216# handed a trunk or stable URL
217# usage: minVer=`extractReleaseFromURL $svnURL`
218
219extractReleaseFromURL ()
220{
221  if expr "$1" : '.*/releases/.*' >/dev/null 2>&1 ; then
222    erfu_relPat='[0-9][0-9]*\.[0-9][0-9]*\.\([0-9][0-9]*\)'
223    erfu_sfxPat='.*$'
224    case "$1" in
225      */Data/releases/* )
226          erfu_pfxPat='svn/Data/[^/][^/]*/'
227          ;;
228      */ThirdParty/* )
229          erfu_pfxPat='svn/BuildTools/ThirdParty/[^/][^/]*/[^/][^/]*/'
230          ;;
231      *)
232          erfu_pfxPat='svn/[^/][^/]*/[^/][^/]*/'
233          ;;
234    esac
235    erfu_relVer=`echo $1 | sed -n -e 's|.*/'$erfu_pfxPat$erfu_relPat$erfu_sfxPat'|\1|p'`
236    echo "$erfu_relVer"
237  else
238    echo "-1"
239  fi
240}
241
242# Now some functions to locate the highest-numbered stable or release.
243
244# Return the URL of the most recent stable branch matching the parameters.
245# A value of -1 for major or minor version is interpreted as `highest number'
246# usage: stableURL=`bestStable <URL> <major>`
247
248bestStable ()
249{ bstb_URL=$1
250  bstb_majVer=$2
251
252  # Construct a URL to send to the repository to list the stable branches.
253
254  bstb_baseURL=`extractBaseURL $bstb_URL`
255  bstb_proj=`extractProjFromURL $bstb_URL`
256
257  case "$bstb_URL" in
258    */Data/* )
259        bstb_listURL=$bstb_baseURL/Data/stable
260        ;;
261    */ThirdParty/* )
262        bstb_listURL=$bstb_baseURL/BuildTools/ThirdParty/$bstb_proj/stable
263        ;;
264    * )
265        bstb_listURL=$bstb_baseURL/$bstb_proj/stable
266        ;;
267  esac
268
269  bstb_rawls=`svn list $bstb_listURL | sort -nr -t. -k1,1 -k2,2`
270
271  # echo "Raw ls: $bstb_rawls"
272
273  # Filter the list of stable branches to remove any that do not match the
274  # requested major version.
275
276  bstb_verPat='[0-9][0-9]*/'
277  if test "$bstb_majVer" != "-1" ; then
278    bstb_verPat=$bstb_majVer'\.[0-9][0-9]*'
279  else
280    bstb_verPat='[0-9][0-9.]*'
281  fi
282  bstb_filter=
283  for bstb_ver in $bstb_rawls ; do
284    if  expr "$bstb_ver" : $bstb_verPat >/dev/null 2>&1 ; then
285      bstb_filter="$bstb_filter $bstb_ver"
286    fi
287  done
288
289  # echo "Filtered ls: $bstb_filter"
290
291  # If there are any candidates left ...
292
293  bstb_bestURL=
294  if test -n "$bstb_filter" ; then
295
296    # If we're dealing with Data, we have to work a bit harder to find our
297    # project. See if any of the filtered candidates contain the correct
298    # project.
299
300    if expr "$bstb_URL" : '.*/Data/.*' >/dev/null 2>&1 ; then
301      bstb_projPat='.*'$bstb_proj'/.*'
302      # echo "Pattern: $bstb_projPat"
303      for bstb_ver in $bstb_filter ; do
304        if  expr "$bstb_ver" : $bstb_verPat >/dev/null 2>&1 ; then
305          # echo "  url: $bstb_listURL/$bstb_ver"
306          bstb_svnls2="`svn list $bstb_listURL/$bstb_ver`"
307          # echo "  result: $bstb_svnls2"
308          if expr "$bstb_svnls2" : $bstb_projPat >/dev/null 2>&1 ; then
309            bstb_best=$bstb_ver
310            # echo "  best: $bstb_best"
311            break
312          fi
313        fi
314      done
315      bstb_bestURL=$bstb_listURL/$bstb_best$bstb_proj
316    else
317      bstb_bestURL=`echo $bstb_filter | sed -n -e 's|\([^/]*/\).*$|\1|p'`
318      bstb_bestURL=$bstb_listURL/$bstb_bestURL
319    fi
320
321  fi
322
323  echo $bstb_bestURL
324}
325
326# Return the URL of the most recent release matching the parameters.
327# A value of -1 for major or minor version is interpreted as `highest number'
328# usage: releaseURL=`bestRelease <URL> <major> <minor>`
329
330bestRelease ()
331{ bstb_URL=$1
332  bstb_majVer=$2
333  bstb_minVer=$3
334
335  # Construct a URL to send to the repository to list the releases.
336
337  bstb_baseURL=`extractBaseURL $bstb_URL`
338  bstb_proj=`extractProjFromURL $bstb_URL`
339
340  case "$bstb_URL" in
341    */Data/* )
342        bstb_listURL=$bstb_baseURL/Data/releases
343        ;;
344    */ThirdParty/* )
345        bstb_listURL=$bstb_baseURL/BuildTools/ThirdParty/$bstb_proj/releases
346        ;;
347    * )
348        bstb_listURL=$bstb_baseURL/$bstb_proj/releases
349        ;;
350  esac
351
352  bstb_rawls=`svn list $bstb_listURL | sort -nr -t. -k1,1 -k2,2 -k3,3`
353
354  # echo "Raw ls: $bstb_rawls"
355
356  # Filter the list of releases to remove any that do not match the
357  # requested major and minor version.
358
359  if test "$bstb_majVer" != "-1" ; then
360    bstb_verPat=$bstb_majVer'\.'
361  else
362    bstb_verPat='[0-9][0-9]*\.'
363  fi
364  if test "$bstb_minVer" != "-1" ; then
365    bstb_verPat="${bstb_verPat}$bstb_minVer"
366  else
367    bstb_verPat="${bstb_verPat}"'[0-9][0-9]*'
368  fi
369  bstb_verPat="${bstb_verPat}"'\.[0-9][0-9]*'
370  # echo "Version pattern: $bstb_verPat"
371  bstb_filter=
372  for bstb_ver in $bstb_rawls ; do
373    if  expr "$bstb_ver" : $bstb_verPat >/dev/null 2>&1 ; then
374      bstb_filter="$bstb_filter $bstb_ver"
375    fi
376  done
377
378  # echo "Filtered ls: $bstb_filter"
379
380  # If there are any candidates left ...
381
382  bstb_bestURL=
383  if test -n "$bstb_filter" ; then
384
385    # If we're dealing with Data, we have to work a bit harder to find our
386    # project. See if any of the filtered candidates contain the correct
387    # project.
388
389    if expr "$bstb_URL" : '.*/Data/.*' >/dev/null 2>&1 ; then
390      bstb_projPat='.*'$bstb_proj'/.*'
391      # echo "Pattern: $bstb_projPat"
392      for bstb_ver in $bstb_filter ; do
393        if  expr "$bstb_ver" : $bstb_verPat >/dev/null 2>&1 ; then
394          # echo "  url: $bstb_listURL/$bstb_ver"
395          bstb_svnls2="`svn list $bstb_listURL/$bstb_ver`"
396          # echo "  result: $bstb_svnls2"
397          if expr "$bstb_svnls2" : $bstb_projPat >/dev/null 2>&1 ; then
398            bstb_best=$bstb_ver
399            # echo "  best: $bstb_best"
400            break
401          fi
402        fi
403      done
404      bstb_bestURL=$bstb_listURL/$bstb_best$bstb_proj
405    else
406      bstb_bestURL=`echo $bstb_filter | sed -n -e 's|\([^/]*/\).*$|\1|p'`
407      bstb_bestURL=$bstb_listURL/$bstb_bestURL
408    fi
409
410  fi
411
412  echo $bstb_bestURL
413}
414
415# Count the total number of stable branches for the project for the specified
416# major version number (libtool age). A major version number of -1 means all
417# stable branches (libtool current; see next function). Returns 0 if handed
418# a trunk url, or if the url is Data or BuildTools itself.
419# usage: calcLibtoolAge <svnURL> <major>
420
421calcLibtoolAge ()
422{ cltc_URL=$1
423  cltc_majVer=$2
424
425  # Construct a URL to send to the repository to list the stable branches.
426
427  cltc_baseURL=`extractBaseURL $cltc_URL`
428  cltc_proj=`extractProjFromURL $cltc_URL`
429
430  case "$cltc_URL" in
431    */Data/* )
432        cltc_listURL=
433        ;;
434    */ThirdParty/* )
435        cltc_listURL=$cltc_baseURL/BuildTools/ThirdParty/$cltc_proj/stable
436        ;;
437    */BuildTools/* )
438        cltc_listURL=
439        ;;
440    * )
441        cltc_listURL=$cltc_baseURL/$cltc_proj/stable
442        ;;
443  esac
444
445  # Run an ls and filter for standard stable branches (M.m)
446  if test -n "$cltc_listURL" ; then
447    cltc_rawls=`svn list $cltc_listURL | sed -n -e '/[0-9]\.[0-9]/p'`
448
449    # echo "Raw ls: $cltc_rawls"
450
451    # Filter the list of stable branches to remove any that do not match the
452    # requested major version. -1 means `any major version', hence no filter.
453
454    cltc_verPat='[0-9][0-9]*/'
455    if test "$cltc_majVer" != "-1" ; then
456      cltc_verPat=$cltc_majVer'\.[0-9][0-9]*'
457    else
458      cltc_verPat='[0-9][0-9.]*'
459    fi
460    cltc_filter=
461    for cltc_ver in $cltc_rawls ; do
462      if  expr "$cltc_ver" : $cltc_verPat >/dev/null 2>&1 ; then
463        cltc_filter="$cltc_filter $cltc_ver"
464      fi
465    done
466
467    # echo "Filtered ls: $cltc_filter"
468
469    cltc_cnt=`echo $cltc_filter | wc -w | sed -e 's/ //g'`
470  else
471    cltc_cnt=0
472  fi
473
474  echo $cltc_cnt
475}
476
477# Count the total number of stable branches (libtool current). See comments
478# above for calcLibtoolAge.
479# usage: calcLibtoolCurrent <url>
480calcLibtoolCurrent ()
481{ calcLibtoolAge $1 -1
482}
483
Note: See TracBrowser for help on using the repository browser.