54
54
# ' }
55
55
# ' @param overwrite If \code{TRUE}, download and overwrite the existing tar file in localDir
56
56
# ' and force re-install Spark (in case the local directory or file is corrupted)
57
- # ' @return \code{install.spark} returns the local directory where Spark is found or installed
57
+ # ' @return the (invisible) local directory where Spark is found or installed
58
58
# ' @rdname install.spark
59
59
# ' @name install.spark
60
60
# ' @aliases install.spark
@@ -115,17 +115,35 @@ install.spark <- function(hadoopVersion = "2.7", mirrorUrl = NULL,
115
115
} else {
116
116
if (releaseUrl != " " ) {
117
117
message(" Downloading from alternate URL:\n - " , releaseUrl )
118
- downloadUrl(releaseUrl , packageLocalPath , paste0(" Fetch failed from " , releaseUrl ))
118
+ success <- downloadUrl(releaseUrl , packageLocalPath )
119
+ if (! success ) {
120
+ unlink(packageLocalPath )
121
+ stop(paste0(" Fetch failed from " , releaseUrl ))
122
+ }
119
123
} else {
120
124
robustDownloadTar(mirrorUrl , version , hadoopVersion , packageName , packageLocalPath )
121
125
}
122
126
}
123
127
124
128
message(sprintf(" Installing to %s" , localDir ))
125
- untar(tarfile = packageLocalPath , exdir = localDir )
126
- if (! tarExists || overwrite ) {
129
+ # There are two ways untar can fail - untar could stop() on errors like incomplete block on file
130
+ # or, tar command can return failure code
131
+ success <- tryCatch(untar(tarfile = packageLocalPath , exdir = localDir ) == 0 ,
132
+ error = function (e ) {
133
+ message(e )
134
+ message()
135
+ FALSE
136
+ },
137
+ warning = function (w ) {
138
+ # Treat warning as error, add an empty line with message()
139
+ message(w )
140
+ message()
141
+ FALSE
142
+ })
143
+ if (! tarExists || overwrite || ! success ) {
127
144
unlink(packageLocalPath )
128
145
}
146
+ if (! success ) stop(" Extract archive failed." )
129
147
message(" DONE." )
130
148
Sys.setenv(SPARK_HOME = packageLocalDir )
131
149
message(paste(" SPARK_HOME set to" , packageLocalDir ))
@@ -135,8 +153,7 @@ install.spark <- function(hadoopVersion = "2.7", mirrorUrl = NULL,
135
153
robustDownloadTar <- function (mirrorUrl , version , hadoopVersion , packageName , packageLocalPath ) {
136
154
# step 1: use user-provided url
137
155
if (! is.null(mirrorUrl )) {
138
- msg <- sprintf(" Use user-provided mirror site: %s." , mirrorUrl )
139
- message(msg )
156
+ message(" Use user-provided mirror site: " , mirrorUrl )
140
157
success <- directDownloadTar(mirrorUrl , version , hadoopVersion ,
141
158
packageName , packageLocalPath )
142
159
if (success ) {
@@ -156,7 +173,7 @@ robustDownloadTar <- function(mirrorUrl, version, hadoopVersion, packageName, pa
156
173
packageName , packageLocalPath )
157
174
if (success ) return ()
158
175
} else {
159
- message(" Unable to find preferred mirror site. " )
176
+ message(" Unable to download from preferred mirror site: " , mirrorUrl )
160
177
}
161
178
162
179
# step 3: use backup option
@@ -165,8 +182,11 @@ robustDownloadTar <- function(mirrorUrl, version, hadoopVersion, packageName, pa
165
182
success <- directDownloadTar(mirrorUrl , version , hadoopVersion ,
166
183
packageName , packageLocalPath )
167
184
if (success ) {
168
- return (packageLocalPath )
185
+ return ()
169
186
} else {
187
+ # remove any partially downloaded file
188
+ unlink(packageLocalPath )
189
+ message(" Unable to download from default mirror site: " , mirrorUrl )
170
190
msg <- sprintf(paste(" Unable to download Spark %s for Hadoop %s." ,
171
191
" Please check network connection, Hadoop version," ,
172
192
" or provide other mirror sites." ),
@@ -201,14 +221,20 @@ directDownloadTar <- function(mirrorUrl, version, hadoopVersion, packageName, pa
201
221
msg <- sprintf(fmt , version , ifelse(hadoopVersion == " without" , " Free build" , hadoopVersion ),
202
222
packageRemotePath )
203
223
message(msg )
204
- downloadUrl(packageRemotePath , packageLocalPath , paste0( " Fetch failed from " , mirrorUrl ) )
224
+ downloadUrl(packageRemotePath , packageLocalPath )
205
225
}
206
226
207
- downloadUrl <- function (remotePath , localPath , errorMessage ) {
227
+ downloadUrl <- function (remotePath , localPath ) {
208
228
isFail <- tryCatch(download.file(remotePath , localPath ),
209
229
error = function (e ) {
210
- message(errorMessage )
211
- print(e )
230
+ message(e )
231
+ message()
232
+ TRUE
233
+ },
234
+ warning = function (w ) {
235
+ # Treat warning as error, add an empty line with message()
236
+ message(w )
237
+ message()
212
238
TRUE
213
239
})
214
240
! isFail
@@ -234,10 +260,9 @@ sparkCachePath <- function() {
234
260
if (.Platform $ OS.type == " windows" ) {
235
261
winAppPath <- Sys.getenv(" LOCALAPPDATA" , unset = NA )
236
262
if (is.na(winAppPath )) {
237
- msg <- paste(" %LOCALAPPDATA% not found." ,
263
+ stop( paste(" %LOCALAPPDATA% not found." ,
238
264
" Please define the environment variable" ,
239
- " or restart and enter an installation path in localDir." )
240
- stop(msg )
265
+ " or restart and enter an installation path in localDir." ))
241
266
} else {
242
267
path <- file.path(winAppPath , " Apache" , " Spark" , " Cache" )
243
268
}
0 commit comments