3
3
# ####################################################
4
4
5
5
using Base. Filesystem
6
+ using ChannelBuffers
6
7
7
8
# collect the keys from local database (MATRIXDICT or USERMATRIXDICT)
8
9
# provide a numerical id counting from 1 for either database.
@@ -106,28 +107,6 @@ function update(db::MatrixDatabase=MATRIX_DB)
106
107
downloadindices (db, ignoredb= true )
107
108
end
108
109
109
- function gunzip (fname)
110
- endswith (fname, " .gz" ) || error (" gunzip: $fname : unknown suffix" )
111
-
112
- destname = rsplit (fname, " .gz" , limit= 2 )[1 ]
113
- BUFFSIZE = 1000000
114
- try
115
- open (destname, " w" ) do f
116
- open (GzipDecompressorStream, fname) do g
117
- buffer = read (g, BUFFSIZE)
118
- while length (buffer) > 0
119
- write (f, buffer)
120
- buffer = read (g, BUFFSIZE)
121
- end
122
- end
123
- end
124
- catch
125
- @warn " decompression error - file $destname set to empty."
126
- open (destname, " w" ) do f; end
127
- end
128
- destname
129
- end
130
-
131
110
"""
132
111
loadmatrix(data::RemoteMatrixData)
133
112
@@ -144,28 +123,26 @@ function loadmatrix(data::RemoteMatrixData)
144
123
addmetadata! (data)
145
124
return 0
146
125
end
147
- dirfn = localfile (data)
148
126
dir = dirname (localdir (data))
127
+ dirt = string (dir, " .tmp" )
128
+ rm (dirt, force= true , recursive= true )
129
+ mkpath (dirt)
149
130
url = redirect (dataurl (data))
150
- tarfile = " "
151
-
152
131
isdir (dir) || mkpath (dir)
153
132
wdir = pwd ()
154
133
try
155
134
@info (" downloading: $url " )
156
- downloadfile (url, dirfn)
157
- tarfile = gunzip (dirfn)
158
- cd (dir)
159
- rfile = relpath (string (tarfile))
160
- if endswith (tarfile, " .tar" )
161
- run (` tar -xf $rfile ` )
162
- rm (tarfile; force= true )
135
+ pipe = downloadpipeline (url, dirt)
136
+ wait (run (pipe))
137
+ cd (dirt)
138
+ for file in readdir (dirt)
139
+ mv (file, joinpath (dir, file), force= true )
163
140
end
164
- catch
165
-
141
+ catch ex
142
+ @warn ( " download of $url failed: $ex " )
166
143
finally
167
144
cd (wdir)
168
- rm (dirfn , force= true )
145
+ rm (dirt , force= true , recursive = true )
169
146
end
170
147
addmetadata! (data)
171
148
1
@@ -187,20 +164,19 @@ function loadinfo(data::RemoteMatrixData)
187
164
return 0
188
165
end
189
166
url = redirect (dataurl (data))
190
- pipe = downloadpipeline (url)
167
+ io = ChannelPipe ()
168
+ pipe = downloadpipeline (url, io)
169
+ tl = run (pipe)
191
170
out = IOBuffer ()
192
171
s = try
193
172
@info (" downloading head of $url " )
194
- open (pipe, " r" ) do io
195
- skip = 0
196
- while ( s = readline (io) ) != " "
197
- skip = s[1 ] == ' %' || isempty (strip (s)) ? 0 : skip + 1
198
- skip <= 1 && println (out, s)
199
- if skip == 1 && length (split (s)) == 3
200
- break
201
- end
202
- end ;
203
- close (io)
173
+ skip = 0
174
+ while ( s = readskip (io) ) != " "
175
+ skip = s[1 ] == ' %' || isempty (strip (s)) ? 0 : skip + 1
176
+ skip <= 1 && println (out, s)
177
+ if skip == 1 && length (split (s)) == 3
178
+ break
179
+ end
204
180
end
205
181
String (take! (out))
206
182
catch ex
@@ -225,22 +201,29 @@ loadinfo(data::MatrixData) = 0
225
201
226
202
Set up a command pipeline (external processes to download and expand data)
227
203
"""
228
- function downloadpipeline (url:: AbstractString )
204
+ function downloadpipeline (url:: AbstractString , dir = nothing )
229
205
urls = rsplit (url, ' .' , limit= 3 )
230
- cmd = []
231
- push! (cmd, downloadcommand (url))
206
+ cmd = Any[ChannelBuffers. curl (url)]
232
207
if urls[end ] == " gz"
233
- push! (cmd, ` gzip -dc ` )
208
+ push! (cmd, gunzip () )
234
209
resize! (urls, length (urls)- 1 )
210
+ url = url[1 : end - 3 ]
235
211
end
236
- if urls[end ] == " tar"
237
- push! (cmd, ` tar -xOf -` )
212
+ if dir isa AbstractString
213
+ if urls[end ] == " tar"
214
+ push! (cmd, tarx (dir))
215
+ else
216
+ file = rsplit (url, ' /' , limit= 2 )
217
+ push! (cmd, joinpath (dir, file[end ]))
218
+ end
219
+ elseif dir isa ChannelPipe
220
+ push! (cmd, dir)
238
221
end
239
222
pipeline (cmd... )
240
223
end
241
224
242
225
function downloadcommand (url:: AbstractString , filename:: AbstractString = " -" )
243
- ` sh -c 'curl "' $url '" -Lso "' $ filename'"' `
226
+ curl (url) → (filename == " - " ? stdout : filename)
244
227
end
245
228
246
229
function data_warn (data:: RemoteMatrixData , dn, i1, i2)
@@ -338,7 +321,6 @@ issvdok(::MatrixData) = false
338
321
Copy file from remote or local url. Works around julia Downloads #69 and #36
339
322
"""
340
323
function downloadfile (url:: AbstractString , out:: AbstractString )
341
- run (downloadcommand (url, out))
324
+ wait ( run (downloadcommand (url, out) ))
342
325
nothing
343
326
end
344
-
0 commit comments