File: binary-repo-lib.sh

package info (click to toggle)
scala 2.11.12-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 62,828 kB
  • sloc: javascript: 28,808; java: 13,415; xml: 3,250; sh: 1,620; python: 756; makefile: 38; awk: 36; ansic: 6
file content (234 lines) | stat: -rwxr-xr-x 7,161 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env bash
#
# Library to push and pull binary artifacts from a remote repository using CURL.

remote_urlget="https://dl.bintray.com/typesafe/scala-sha-bootstrap/org/scala-lang/bootstrap"
remote_urlpush="https://dl.bintray.com/typesafe/scala-sha-bootstrap/org/scala-lang/bootstrap"
libraryJar="$(pwd)/lib/scala-library.jar"
desired_ext=".desired.sha1"
push_jar="$(pwd)/tools/push.jar"

if [[ "$OSTYPE" == *Cygwin* || "$OSTYPE" == *cygwin* ]]; then push_jar="$(cygpath -m "$push_jar")"; fi
# Cache dir has .sbt in it to line up with SBT build.
SCALA_BUILD_REPOS_HOME=${SCALA_BUILD_REPOS_HOME:=$HOME}
cache_dir="${SCALA_BUILD_REPOS_HOME}/.sbt/cache/scala"

# Checks whether or not curl is installed and issues a warning on failure.
checkCurl() {
 if ! which curl >/dev/null; then
    cat <<EOM
No means of downloading or uploading binary artifacts found.   

Please install curl for your OS.  e.g.
* sudo apt-get install curl
* brew install curl
EOM
  fi
}

# Executes the `curl` command to publish artifacts into a maven/ivy repository.
# Arugment 1 - The location to publish the file.
# Argument 2 - The file to publish.
# Argument 3 - The user to publish as.
# Argument 4 - The password for the user.
curlUpload() {
  checkCurl
  local remote_location=$1
  local data=$2
  local user=$3
  local password=$4
  local url="${remote_urlpush}/${remote_location}"
  java -jar $push_jar "$data" "$url" "$user" "$password"
  if (( $? != 0 )); then
    echo "Error uploading $data to $url"
    echo "$url"
    exit 1
  fi
}

# Executes the `curl` command to download a file.
# Argument 1 - The location to store the file.
# Argument 2 - The URL to download.
curlDownload() {
  checkCurl
  local jar=$1
  local url=$2
  if [[ "$OSTYPE" == *Cygwin* || "$OSTYPE" == *cygwin* ]]; then
    jar=$(cygpath -m $1)
  fi
  http_code=$(curl --write-out '%{http_code}' --silent --fail -L --output "$jar" "$url")
  if (( $? != 0 )); then
    echo "Error downloading $jar: response code: $http_code"
    echo "$url"
    exit 1
  fi
}

# Pushes a local JAR file to the remote repository and updates the .desired.sha1 file.
# Argument 1 - The JAR file to update.
# Argument 2 - The root directory of the project.
# Argument 3 - The user to use when publishing artifacts.
# Argument 4 - The password to use when publishing artifacts.
pushJarFile() {
  local jar=$1
  local basedir=$2
  local user=$3
  local pw=$4
  local jar_dir=$(dirname $jar)
  local jar_name=${jar#$jar_dir/}
  pushd $jar_dir >/dev/null
  local version=$(makeJarSha $jar_name)
  local remote_uri=${version}${jar#$basedir}
  echo "  Pushing to ${remote_urlpush}/${remote_uri} ..."
  echo "	$curl"
  curlUpload $remote_uri $jar_name $user $pw
  echo "  Making new sha1 file ...."
  echo "$version ?$jar_name" > "${jar_name}${desired_ext}"
  popd >/dev/null
  # TODO - Git remove jar and git add jar.desired.sha1
  # rm $jar
}

makeJarSha() {
  local jar=$1
  if which sha1sum 2>/dev/null >/dev/null; then
    shastring=$(sha1sum "$jar")
    echo "$shastring" | sed 's/ .*//'
  elif which shasum 2>/dev/null >/dev/null; then
    shastring=$(shasum "$jar")
    echo "$shastring" | sed 's/ .*//'
  else
    shastring=$(openssl sha1 "$jar")
    echo "$shastring" | sed 's/^.*= //'
  fi
}

getJarSha() {
  local jar=$1
  if [[ ! -f "$jar" ]]; then
    echo ""
  else
    echo $(makeJarSha $jar)
  fi
}

# Tests whether or not the .desired.sha1 hash matches a given file.
# Arugment 1 - The jar file to test validity.
# Returns: Empty string on failure, "OK" on success.
isJarFileValid() {
  local jar=$1
  if [[ ! -f "$jar" ]]; then
    echo ""
  else
    local jar_dir=$(dirname $jar)
    local jar_name=${jar#$jar_dir/}
    pushd $jar_dir >/dev/null
    local valid=$(shasum -p --check ${jar_name}${desired_ext} 2>/dev/null)
    echo "${valid#$jar_name: }"
    popd >/dev/null
  fi
}

# Pushes any jar file in the local repository for which the corresponding SHA1 hash is invalid or nonexistent.
# Argument 1 - The base directory of the project.
# Argument 2 - The user to use when pushing artifacts.
# Argument 3 - The password to use when pushing artifacts.
pushJarFiles() {
  local basedir=$1
  local user=$2
  local password=$3
  # TODO - ignore target/ and build/
  local jarFiles="$(find ${basedir}/lib -name "*.jar") $(find ${basedir}/test/files -name "*.jar") $(find ${basedir}/tools -name "*.jar")"
  local changed="no"
  for jar in $jarFiles; do
    local valid=$(isJarFileValid $jar)
    if [[ "$valid" != "OK" ]]; then
      echo "$jar has changed, pushing changes...."
      changed="yes"
      pushJarFile $jar $basedir $user $password
    fi
  done
  if test "$changed" == "no"; then
    echo "No jars have been changed."
  else
    echo "Binary changes have been pushed.  You may now submit the new *${desired_ext} files to git."
  fi
} 


checkJarSha() {
  local jar=$1
  local sha=$2
  local testsha=$(getJarSha "$jar")
  if test "$sha" == "$testsha"; then
    echo "OK"
  fi
}

makeCacheLocation() {
  local uri=$1
  local sha=$2
  local cache_loc="$cache_dir/$uri"
  local cdir=$(dirname $cache_loc)
  if [[ ! -d "$cdir" ]]; then
    mkdir -p "$cdir"
  fi
  echo "$cache_loc"
}

# Pulls a single binary artifact from a remote repository.
# Argument 1 - The uri to the file that should be downloaded.
# Argument 2 - SHA of the file...
# Returns: Cache location.
pullJarFileToCache() {
  local uri=$1
  local sha=$2
  local cache_loc="$(makeCacheLocation $uri)"
  # TODO - Check SHA of local cache is accurate.
  if test -f "$cache_loc" && test "$(checkJarSha "$cache_loc" "$sha")" != "OK"; then
    echo "Found bad cached file: $cache_loc"
    rm -f "$cache_loc"
  fi
  if [[ ! -f "$cache_loc" ]]; then
    # Note: After we follow up with JFrog, we should check the more stable raw file server first
    # before hitting the more flaky artifactory.
    curlDownload $cache_loc ${remote_urlget}/${uri}
    if test "$(checkJarSha "$cache_loc" "$sha")" != "OK"; then
      echo "Trouble downloading $uri.  Please try pull-binary-libs again when your internet connection is stable."
      exit 2
    fi
  fi
}

# Pulls a single binary artifact from a remote repository.
# Argument 1 - The jar file that needs to be downloaded.
# Argument 2 - The root directory of the project.
pullJarFile() {
  local jar=$1
  local basedir=$2
  local sha1=$(cat ${jar}${desired_ext})
  local jar_dir=$(dirname $jar)
  local jar_name=${jar#$jar_dir/}
  local version=${sha1%% *}
  local remote_uri=${version}/${jar#$basedir/}
  echo "Resolving [${remote_uri}]"
  pullJarFileToCache $remote_uri $version
  local cached_file=$(makeCacheLocation $remote_uri)
  cp $cached_file $jar
}

# Pulls binary artifacts from the remote repository.
# Argument 1 - The directory to search for *.desired.sha1 files that need to be retrieved.
pullJarFiles() {
  local basedir=$1
  local desiredFiles="$(find ${basedir}/lib -name *${desired_ext}) $(find ${basedir}/test/files -name *${desired_ext}) $(find ${basedir}/tools -name *${desired_ext})"
  for sha in $desiredFiles; do
    jar=${sha%$desired_ext}
    local valid=$(isJarFileValid $jar)
    if [[ "$valid" != "OK" ]]; then
      pullJarFile $jar $basedir
    fi
  done
}