~ubuntu-branches/debian/sid/simpleitk/sid

« back to all changes in this revision

Viewing changes to Utilities/Maintenance/SourceTarball.bash

  • Committer: Package Import Robot
  • Author(s): Ghislain Antony Vaillant
  • Date: 2017-11-02 08:49:18 UTC
  • Revision ID: package-import@ubuntu.com-20171102084918-7hs09ih668xq87ej
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env bash
 
2
#==========================================================================
 
3
#
 
4
#   Copyright Insight Software Consortium
 
5
#
 
6
#   Licensed under the Apache License, Version 2.0 (the "License");
 
7
#   you may not use this file except in compliance with the License.
 
8
#   You may obtain a copy of the License at
 
9
#
 
10
#          http://www.apache.org/licenses/LICENSE-2.0.txt
 
11
#
 
12
#   Unless required by applicable law or agreed to in writing, software
 
13
#   distributed under the License is distributed on an "AS IS" BASIS,
 
14
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
#   See the License for the specific language governing permissions and
 
16
#   limitations under the License.
 
17
#
 
18
#==========================================================================*/
 
19
 
 
20
usage() {
 
21
  die 'USAGE: SourceTarball.bash [(--tgz|--txz|--zip)...] \
 
22
        [--verbose] [-v <version>] build_dir [<tag>|<commit>]'
 
23
}
 
24
 
 
25
info() {
 
26
  echo "$@" 1>&2
 
27
}
 
28
 
 
29
die() {
 
30
  echo "$@" 1>&2; exit 1
 
31
}
 
32
 
 
33
return_pipe_status() {
 
34
  echo ${PIPESTATUS[@]} |grep -q -v "[1-9]"
 
35
}
 
36
 
 
37
find_data_objects() {
 
38
  git ls-tree --full-tree -r "$1" |
 
39
  egrep '\.(md5)$' |
 
40
  while read mode type obj path; do
 
41
    case "$path" in
 
42
      *.md5)  echo MD5/$(git cat-file blob $obj) ;;
 
43
      *)      die "Unknown ExternalData content link: $path" ;;
 
44
    esac
 
45
  done | sort | uniq
 
46
  return_pipe_status
 
47
}
 
48
 
 
49
validate_MD5() {
 
50
  md5sum=$(md5sum "$1" | sed 's/ .*//') &&
 
51
  if test "$md5sum" != "$2"; then
 
52
    die "Object MD5/$2 is corrupt: $1"
 
53
  fi
 
54
}
 
55
 
 
56
download_object() {
 
57
  algo="$1" ; hash="$2" ; path="$3"
 
58
  mkdir -p $(dirname "$path") &&
 
59
  if wget "https://www.itk.org/files/ExternalData/$algo/$hash" -O "$path.tmp$$" 1>&2; then
 
60
    mv "$path.tmp$$" "$path"
 
61
  elif wget "http://midas3.kitware.com/midas/api/rest?method=midas.bitstream.download&checksum=$hash&algorithm=$algo" -O "$path.tmp$$" 1>&2; then
 
62
    mv "$path.tmp$$" "$path"
 
63
  else
 
64
    rm -f "$path.tmp$$"
 
65
    false
 
66
  fi
 
67
}
 
68
 
 
69
index_data_objects() {
 
70
  # Input lines have format <algo>/<hash>
 
71
  while IFS=/ read algo hash; do
 
72
    # Final path in source tarball
 
73
    path=".ExternalData/$algo/$hash"
 
74
    # Find the object file on disk
 
75
    if test -f "$path"; then
 
76
      file="$path" # available in place
 
77
    elif test -f ~/"$path" ; then
 
78
      file=~/"$path" # available in home dir
 
79
    else
 
80
      download_object "$algo" "$hash" "$path" &&
 
81
      file="$path"
 
82
    fi &&
 
83
    validate_$algo "$file" "$hash" &&
 
84
    obj=$(git hash-object -t blob -w "$file") &&
 
85
    echo "100644 blob $obj      $path" ||
 
86
    return
 
87
  done |
 
88
  git update-index --index-info
 
89
  return_pipe_status
 
90
}
 
91
 
 
92
index_additional_object() {
 
93
 
 
94
   file="$1"
 
95
   path=$file
 
96
   test -n "$2" && path=$2/$(basename $file)
 
97
 
 
98
   obj=$(git hash-object -t blob -w "$file") &&
 
99
    echo "100644 blob $obj      $path" |
 
100
    git update-index --index-info
 
101
 
 
102
  return_pipe_status
 
103
 
 
104
}
 
105
 
 
106
 
 
107
# NOTE: this methods exports the GIT_ALTERNATE_OBJECT_DIRECTORIES variable to effect later processes
 
108
index_submodule_objects() {
 
109
 
 
110
   SUBMODULE_LIST=$(git submodule foreach --quiet 'echo $(git rev-parse --git-dir)/objects')
 
111
   export GIT_ALTERNATE_OBJECT_DIRECTORIES=$SUBMODULE_LIST
 
112
   IFS=' '
 
113
   git submodule foreach --quiet  'git ls-tree --full-tree -r $sha1 | awk  -v path=$path '"'"'{ print path, $1, $2, $3, path "/" $4 }'"'"'' |
 
114
   while read path mode type object file; do
 
115
    echo "$mode $type $object   $file" ||
 
116
    return
 
117
   done  |
 
118
   git update-index --index-info
 
119
   return_pipe_status
 
120
}
 
121
 
 
122
load_data_objects() {
 
123
  find_data_objects "$@" |
 
124
  index_data_objects
 
125
  return_pipe_status
 
126
}
 
127
 
 
128
load_data_files() {
 
129
  git ls-tree -r "$1" -- '.ExternalData' |
 
130
  git update-index --index-info
 
131
  return_pipe_status
 
132
}
 
133
 
 
134
git_archive_tgz() {
 
135
  out="$2.tar.gz" && tmp="$out.tmp$$" &&
 
136
  if test -n "$3"; then prefix="$3"; else prefix="$2"; fi &&
 
137
  git -c core.autocrlf=false archive $verbose --format=tar --prefix=$prefix/ $1 |
 
138
  gzip -9 > "$tmp" &&
 
139
  mv "$tmp" "$out" &&
 
140
  info "Wrote $out"
 
141
}
 
142
 
 
143
git_archive_txz() {
 
144
  out="$2.tar.xz" && tmp="$out.tmp$$" &&
 
145
  if test -n "$3"; then prefix="$3"; else prefix="$2"; fi &&
 
146
  git -c core.autocrlf=false archive $verbose --format=tar --prefix=$prefix/ $1 |
 
147
  xz -9 > "$tmp" &&
 
148
  mv "$tmp" "$out" &&
 
149
  info "Wrote $out"
 
150
}
 
151
 
 
152
git_archive_zip() {
 
153
  out="$2.zip" && tmp="$out.tmp$$" &&
 
154
  if test -n "$3"; then prefix="$3"; else prefix="$2"; fi &&
 
155
  git -c core.autocrlf=true archive $verbose --format=zip --prefix=$prefix/ $1 > "$tmp" &&
 
156
  mv "$tmp" "$out" &&
 
157
  info "Wrote $out"
 
158
}
 
159
 
 
160
#-----------------------------------------------------------------------------
 
161
 
 
162
formats=
 
163
commit=
 
164
version=
 
165
verbose=
 
166
build_dir=
 
167
 
 
168
# Parse command line options.
 
169
while test $# != 0; do
 
170
  case "$1" in
 
171
    --tgz) formats="$formats tgz" ;;
 
172
    --txz) formats="$formats txz" ;;
 
173
    --zip) formats="$formats zip" ;;
 
174
    --verbose) verbose=-v ;;
 
175
    --) shift; break ;;
 
176
    -v) shift; version="$1" ;;
 
177
    -*) usage ;;
 
178
    *) { test -z "$build_dir" && build_dir="$1"; } ||
 
179
       { test -z "$commit" && commit="$1"; } ||
 
180
       usage ;;
 
181
  esac
 
182
  shift
 
183
done
 
184
test $# = 0 || usage
 
185
test -n "$commit" || commit=HEAD
 
186
test -n "$formats" || formats=tgz
 
187
 
 
188
test -n "$build_dir" ||
 
189
  die "Missing required build_dir argument."
 
190
 
 
191
test -e "${build_dir}/SimpleITKConfig.cmake" ||
 
192
  die "invalid build directory."
 
193
 
 
194
 
 
195
if ! git rev-parse --verify -q "$commit" >/dev/null ; then
 
196
  die "'$commit' is not a valid commit"
 
197
fi
 
198
if test -z "$version"; then
 
199
  desc=$(git describe $commit) &&
 
200
  if test "${desc:0:1}" != "v"; then
 
201
    die "'git describe $commit' is '$desc'; use -v <version>"
 
202
  fi &&
 
203
  version=${desc#v} &&
 
204
  echo "$commit is version $version"
 
205
fi
 
206
 
 
207
# Create temporary git index to construct source tree
 
208
export GIT_INDEX_FILE="$(pwd)/tmp-$$-index" &&
 
209
trap "rm -f '$GIT_INDEX_FILE'" EXIT &&
 
210
 
 
211
 
 
212
result=0 &&
 
213
 
 
214
 
 
215
info "Loading source tree from $commit..." &&
 
216
rm -f "$GIT_INDEX_FILE" &&
 
217
git read-tree -m -i $commit &&
 
218
git rm -rf -q --cached '.ExternalData' &&
 
219
index_additional_object "${build_dir}/sitkSourceVersionVars.cmake" "CMake" &&
 
220
 
 
221
 
 
222
tree=$(git write-tree) &&
 
223
info "Generating source archive(s)..." &&
 
224
for fmt in $formats; do
 
225
  git_archive_$fmt $tree "SimpleITK-$version" || result=1
 
226
done &&
 
227
 
 
228
info "Loading data for $commit..." &&
 
229
rm -f "$GIT_INDEX_FILE" &&
 
230
load_data_objects $commit &&
 
231
load_data_files $commit &&
 
232
tree=$(git write-tree) &&
 
233
 
 
234
 
 
235
info "Generating data archive(s)..." &&
 
236
for fmt in $formats; do
 
237
  git_archive_$fmt $tree "SimpleITKData-$version" "SimpleITK-$version" || result=1
 
238
done &&
 
239
exit $result