folio 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +91 -7
- data/COPYING +162 -669
- data/MANIFEST +52 -16
- data/README +1 -1
- data/RELEASE +14 -0
- data/VERSION +1 -1
- data/demo/sample1/tryme.rb +1 -1
- data/doc/log/basic_stats/index.html +39 -0
- data/doc/log/changelog.html +323 -0
- data/doc/log/changelog.xml +129 -0
- data/doc/log/changelog.xsl +33 -0
- data/doc/log/notes.xml +35 -0
- data/{NEWS → doc/note/release-0.3.0.txt} +1 -1
- data/doc/spec/index.html +552 -0
- data/lib/folio.rb +15 -3
- data/lib/folio/directory.rb +10 -5
- data/lib/folio/document.rb +11 -8
- data/lib/folio/errors.rb +15 -0
- data/lib/folio/fileobject.rb +6 -3
- data/lib/folio/filetest.rb +88 -0
- data/lib/folio/fileutils.rb +39 -0
- data/lib/folio/link.rb +2 -5
- data/lib/folio/minitar.rb +1063 -0
- data/lib/folio/multiglob.rb +91 -0
- data/lib/folio/path.rb +56 -0
- data/lib/folio/shell.rb +289 -50
- data/lib/folio/uploadutils.rb +434 -0
- data/lib/folio/xdg.rb +4 -0
- data/lib/folio/ziputils.rb +489 -0
- data/spec/document/basic.rd +53 -0
- data/spec/document/fixtures/foo.txt +1 -0
- data/spec/document/fixtures/out.txt +1 -0
- data/spec/link/fixtures/foo.txt +1 -0
- data/spec/link/general.rd +44 -0
- data/spec/shell/fixtures/foo.txt +1 -0
- data/spec/shell/fixtures/out.txt +1 -0
- data/spec/shell/general.rd +30 -0
- data/spec/shell/issues.rd +100 -0
- metadata +58 -27
@@ -0,0 +1,434 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'shellwords'
|
3
|
+
require 'tmpdir'
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
warn "UploadUtils needs work! Use at your own risk!"
|
7
|
+
|
8
|
+
# = UploadUtils
|
9
|
+
#
|
10
|
+
# Upload files to host. These means of uploading are current
|
11
|
+
# supported: ftp, sftp, scp and rsync.
|
12
|
+
#
|
13
|
+
# user Username for host.
|
14
|
+
# host Host server's domain name.
|
15
|
+
# root Document root path on host.
|
16
|
+
# copy Directory of files to publish, or
|
17
|
+
# Files to publish using from and to.
|
18
|
+
#
|
19
|
+
# dryrun If true only pretend to upload.
|
20
|
+
# quiet Supress all output.
|
21
|
+
# verbose Provide extra details.
|
22
|
+
#
|
23
|
+
# The copy parameter allows you to simply specify a file
|
24
|
+
# or directory which will be published to host's document
|
25
|
+
# root location.
|
26
|
+
#
|
27
|
+
# If you need more control over which files to publish
|
28
|
+
# where, you can use the copy parameter instead. Provide
|
29
|
+
# an array of pattern strings in the form of "{from} {to}".
|
30
|
+
# If the desitination is the host's document root you do
|
31
|
+
# not need to specify the {to} part. For example:
|
32
|
+
#
|
33
|
+
# copy = [ 'web/*', 'doc/api/* doc/api' ]
|
34
|
+
#
|
35
|
+
# The first copies the files under your project's web directory
|
36
|
+
# to the host's document root. The second copies your projects
|
37
|
+
# doc/api files to the doc/api location on the host.
|
38
|
+
#
|
39
|
+
# The internal template used for the outbound destination
|
40
|
+
# is 'username@host:root/'.
|
41
|
+
#
|
42
|
+
# == TODOs
|
43
|
+
#
|
44
|
+
# * Needs general improvements.
|
45
|
+
# * Reduce shelling-out.
|
46
|
+
# * Incorporate password into scp and ftp ?
|
47
|
+
# * rsync needs --delete option
|
48
|
+
#
|
49
|
+
module UploadUtils
|
50
|
+
|
51
|
+
###############
|
52
|
+
module_function
|
53
|
+
###############
|
54
|
+
|
55
|
+
# Upload via given protocol.
|
56
|
+
#
|
57
|
+
def upload(protocol, opts)
|
58
|
+
__send__(protocol.to_s.downcase,opts)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Use ftp to upload files.
|
62
|
+
#
|
63
|
+
def ftp( keys )
|
64
|
+
keys = upload_parameters(keys)
|
65
|
+
|
66
|
+
# set transfer rules
|
67
|
+
if keys.stage
|
68
|
+
trans = stage_transfer(keys.stage)
|
69
|
+
else
|
70
|
+
files(keys.dir, keys.copy).each do |from|
|
71
|
+
trans << [from,from]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# append location of publication dir to from
|
76
|
+
dir = keys.dir
|
77
|
+
trans.collect!{ |from,to| [File.join(dir,from), to] }
|
78
|
+
|
79
|
+
if keys.dryrun
|
80
|
+
puts "ftp open #{keys.user}@#{keys.host}:#{keys.root}/"
|
81
|
+
keys.trans.each do |f, t|
|
82
|
+
puts "ftp put #{f} #{t}"
|
83
|
+
end
|
84
|
+
else
|
85
|
+
require 'net/ftp'
|
86
|
+
Net::FTP.open(keys.host) do |ftp|
|
87
|
+
ftp.login(keys.user) #password?
|
88
|
+
ftp.chdir(keys.root)
|
89
|
+
keys.trans.each do |f, t|
|
90
|
+
puts "ftp #{f} #{t}" unless keys.quiet
|
91
|
+
ftp.putbinaryfile( f, t, 1024 )
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Use sftp to upload files.
|
98
|
+
#
|
99
|
+
def sftp( keys )
|
100
|
+
keys = upload_parameters(keys)
|
101
|
+
|
102
|
+
# set transfer rules
|
103
|
+
if keys.stage
|
104
|
+
trans = stage_transfer(keys.stage)
|
105
|
+
else
|
106
|
+
files(keys.dir, keys.copy).each do |from|
|
107
|
+
trans << [from,from]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# append location of publication dir to from
|
112
|
+
dir = keys.dir
|
113
|
+
trans.collect!{ |from,to| [File.join(dir,from), to] }
|
114
|
+
|
115
|
+
if keys.dryrun
|
116
|
+
puts "sftp open #{keys.user}@#{keys.host}:#{keys.root}/"
|
117
|
+
keys.trans.each do |f,t|
|
118
|
+
puts "sftp put #{f} #{t}"
|
119
|
+
end
|
120
|
+
else
|
121
|
+
require 'net/sftp'
|
122
|
+
Net::SFTP.start(keys.host, keys.user, keys.pass) do |sftp|
|
123
|
+
#sftp.login( user )
|
124
|
+
sftp.chdir(keys.root)
|
125
|
+
keys.trans.each do |f,t|
|
126
|
+
puts "sftp #{f} #{t}" unless keys.quiet
|
127
|
+
sftp.put_file(f,t) #, 1024 )
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
# Use scp to upload files.
|
134
|
+
#
|
135
|
+
def scp( keys )
|
136
|
+
keys = upload_parameters(keys)
|
137
|
+
|
138
|
+
# append location of publication dir to from
|
139
|
+
dir = keys.dir
|
140
|
+
trans.collect!{ |from,to| [File.join(dir,from), to] }
|
141
|
+
|
142
|
+
flags = []
|
143
|
+
flags << "-v" if keys.verbose
|
144
|
+
flags << "-q" if keys.quiet
|
145
|
+
flags = flags.join(' ').strip
|
146
|
+
flags = ' ' + flags unless flags.empty?
|
147
|
+
|
148
|
+
if keys.stage
|
149
|
+
dir = stage_linkdir(keys.dir, keys.stage)
|
150
|
+
Dir.chdir(dir) do
|
151
|
+
cpy = files(keys.copy)
|
152
|
+
end
|
153
|
+
manifest = File.join(dir,manfile)
|
154
|
+
#cmd = %{rsync#{flags} -L -arz --files-from='#{manifest}' #{dir} #{keys.user}@#{keys.host}:/#{keys.root}}
|
155
|
+
cmd = "scp -r#{flags} * #{keys.user}@#{keys.host}:/#{keys.root}"
|
156
|
+
else
|
157
|
+
dir = keys.dir
|
158
|
+
cpy = files(dir, keys.copy)
|
159
|
+
manifest = File.join(dir,manfile)
|
160
|
+
#cmd = %{rsync#{flags} -arz --files-from='#{manifest}' #{dir} #{keys.user}@#{keys.host}:/#{keys.root}}
|
161
|
+
cmd = "scp -r#{flags} * #{keys.user}@#{keys.host}:/#{keys.root}"
|
162
|
+
end
|
163
|
+
|
164
|
+
Dir.chdir(keys.dir) do
|
165
|
+
begin
|
166
|
+
File.open(manifest, 'w'){ |f| f << cpy.join("\n") }
|
167
|
+
#ENV['RSYNC_PASSWORD'] = keys.pass if keys.pass
|
168
|
+
puts cmd unless keys.quiet
|
169
|
+
system cmd
|
170
|
+
ensure
|
171
|
+
#ENV.delete('RSYNC_PASSWORD') if keys.pass
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
#upload_stage(keys) do #|tmpdir|
|
176
|
+
# puts cmd unless keys.quiet
|
177
|
+
# system cmd unless keys.dryrun
|
178
|
+
#end
|
179
|
+
end
|
180
|
+
|
181
|
+
|
182
|
+
# Use rsync to upload files.
|
183
|
+
#
|
184
|
+
# NOTE: This shells out to the commandline.
|
185
|
+
def rsync( keys )
|
186
|
+
keys = upload_parameters(keys)
|
187
|
+
|
188
|
+
flags = []
|
189
|
+
flags << "-n" if keys.dryrun
|
190
|
+
flags << "-q" if keys.quiet
|
191
|
+
flags << "-v" if keys.verbose
|
192
|
+
flags << "--progress" unless keys.quiet
|
193
|
+
flags = flags.join(' ').strip
|
194
|
+
flags = ' ' + flags unless flags.empty?
|
195
|
+
|
196
|
+
manfile = 'Publish.txt'
|
197
|
+
|
198
|
+
if keys.stage
|
199
|
+
dir = stage_linkdir(keys.dir, keys.stage)
|
200
|
+
Dir.chdir(dir) do
|
201
|
+
cpy = files(keys.copy)
|
202
|
+
end
|
203
|
+
manifest = File.join(dir,manfile)
|
204
|
+
cmd = %{rsync#{flags} -L -arz --files-from='#{manifest}' #{dir} #{keys.user}@#{keys.host}:/#{keys.root}}
|
205
|
+
else
|
206
|
+
dir = keys.dir
|
207
|
+
cpy = files(dir, keys.copy)
|
208
|
+
manifest = File.join(dir,manfile)
|
209
|
+
cmd = %{rsync#{flags} -arz --files-from='#{manifest}' #{dir} #{keys.user}@#{keys.host}:/#{keys.root}}
|
210
|
+
end
|
211
|
+
|
212
|
+
#Dir.chdir(keys.dir) do
|
213
|
+
begin
|
214
|
+
File.open(manifest, 'w'){ |f| f << cpy.join("\n") }
|
215
|
+
ENV['RSYNC_PASSWORD'] = keys.pass if keys.pass
|
216
|
+
puts cmd unless keys.quiet
|
217
|
+
system cmd
|
218
|
+
ensure
|
219
|
+
ENV.delete('RSYNC_PASSWORD') if keys.pass
|
220
|
+
end
|
221
|
+
#end
|
222
|
+
end
|
223
|
+
|
224
|
+
# private (can't do b/c of module_function)
|
225
|
+
|
226
|
+
# parse publishing options.
|
227
|
+
def upload_parameters(opts)
|
228
|
+
keys = OpenStruct===opts ? opts : OpenStruct.new(opts)
|
229
|
+
|
230
|
+
keys.copy = keys.copy || '**/*'
|
231
|
+
keys.host = keys.host || keys.domain
|
232
|
+
keys.user = keys.user || keys.username
|
233
|
+
keys.root = keys.root || '/'
|
234
|
+
#keys.pass = keys.pass || keys.password
|
235
|
+
|
236
|
+
# validate
|
237
|
+
raise ArgumentError, "missing publish parameter -- dir" unless keys.dir
|
238
|
+
raise ArgumentError, "missing publish parameter -- host" unless keys.host
|
239
|
+
raise ArgumentError, "missing publish parameter -- user" unless keys.user
|
240
|
+
#raise ArgumentError, "missing publish parameter -- copy" unless keys.copy
|
241
|
+
#raise ArgumentError, "missing publish parameter -- root" unless keys.root
|
242
|
+
|
243
|
+
keys.root = '' if keys.root.nil?
|
244
|
+
keys.root.sub!(/^\//,'')
|
245
|
+
|
246
|
+
if String===keys.copy and File.directory?(keys.copy)
|
247
|
+
copy = File.join(keys.copy, '*')
|
248
|
+
end
|
249
|
+
keys.copy = [keys.copy].flatten.compact
|
250
|
+
|
251
|
+
# trans = []
|
252
|
+
# keys.copy.each do |from|
|
253
|
+
# #from, to = *Shellwords.shellwords(c)
|
254
|
+
# #to = from if to.nil?
|
255
|
+
# #to = to[1..-1] if to[0,1] == '/'
|
256
|
+
# from.sub('*','**/*') unless from =~ /\*\*/
|
257
|
+
# files = Dir.glob(from)
|
258
|
+
# files.each do |f|
|
259
|
+
# #t = File.join(to,File.basename(f))
|
260
|
+
# #t = t[1..-1] if t[0,1] == '/'
|
261
|
+
# trans << [f,f]
|
262
|
+
# end
|
263
|
+
# end
|
264
|
+
# keys.trans = trans
|
265
|
+
|
266
|
+
return keys
|
267
|
+
end
|
268
|
+
|
269
|
+
# Put together the list of files to copy.
|
270
|
+
def files( dir, copy )
|
271
|
+
Dir.chdir(dir) do
|
272
|
+
del, add = copy.partition{ |f| /^[-]/ =~ f }
|
273
|
+
|
274
|
+
# remove - and + prefixes
|
275
|
+
del.collect!{ |f| f.sub(/^[-]/,'') }
|
276
|
+
add.collect!{ |f| f.sub(/^[+]/,'') }
|
277
|
+
|
278
|
+
#del.concat(must_exclude)
|
279
|
+
|
280
|
+
files = []
|
281
|
+
add.each{ |g| files += Dir.glob(g) }
|
282
|
+
del.each{ |g| files -= Dir.glob(g) }
|
283
|
+
|
284
|
+
files.collect!{ |f| f.sub(/^\//,'') }
|
285
|
+
|
286
|
+
return files
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
# Combine three part stage list into a two part from->to list.
|
291
|
+
#
|
292
|
+
# Using the stage list of three space separated fields.
|
293
|
+
#
|
294
|
+
# fromdir file todir
|
295
|
+
#
|
296
|
+
# This is used to generate a from -> to list of the form:
|
297
|
+
#
|
298
|
+
# fromdir/file todir/file
|
299
|
+
#
|
300
|
+
def stage_transfer( list )
|
301
|
+
trans = []
|
302
|
+
list.each do |line|
|
303
|
+
trans << Shellwords.shellwords(line)
|
304
|
+
end
|
305
|
+
|
306
|
+
trans.collect! do |from, base, to|
|
307
|
+
file = File.join(from,base)
|
308
|
+
to = File.join(to,base)
|
309
|
+
[from, to]
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
# When using stage options this will create temporary linked location.
|
314
|
+
def stage_linkdir( dir, list )
|
315
|
+
folder = File.join(Dir.tmpdir, 'ratchets', 'project', object_id.abs.to_s)
|
316
|
+
FileUtils.mkdir_p(folder)
|
317
|
+
|
318
|
+
Dir.chdir(dir) do
|
319
|
+
stage_transfer(list).each do |file, to|
|
320
|
+
link = File.join(folder,to)
|
321
|
+
FileUtils.ln_s(link,file)
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
return folder
|
326
|
+
end
|
327
|
+
|
328
|
+
end
|
329
|
+
|
330
|
+
|
331
|
+
=begin
|
332
|
+
|
333
|
+
|
334
|
+
#--
|
335
|
+
# SHELLS OUT! Need net/scp library to fix.
|
336
|
+
#++
|
337
|
+
|
338
|
+
# Use rsync to upload files.
|
339
|
+
|
340
|
+
def rsync( keys )
|
341
|
+
keys = upload_parameters(keys)
|
342
|
+
|
343
|
+
flags = []
|
344
|
+
flags << "-n" if keys.dryrun
|
345
|
+
flags << "-v" if keys.verbose
|
346
|
+
flags << "-q" if keys.quiet
|
347
|
+
flags = flags.join(' ').strip
|
348
|
+
flags = ' ' + flags unless flags.empty?
|
349
|
+
|
350
|
+
upload_stage(keys) do #|tmpdir|
|
351
|
+
begin
|
352
|
+
ENV['RSYNC_PASSWORD'] = keys.pass if keys.pass
|
353
|
+
cmd = "rsync -R#{flags} -arz * #{keys.user}@#{keys.host} /#{keys.root}"
|
354
|
+
ensure
|
355
|
+
ENV.delete('RSYNC_PASSWORD') if keys.pass
|
356
|
+
end
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
360
|
+
# Use ftp to upload files.
|
361
|
+
|
362
|
+
def ftp( keys )
|
363
|
+
keys = upload_parameters(keys)
|
364
|
+
|
365
|
+
if keys.dryrun
|
366
|
+
puts "ftp open #{keys.user}@#{keys.host}:#{keys.root}/"
|
367
|
+
keys.trans.each do |f, t|
|
368
|
+
puts "ftp put #{f} #{t}"
|
369
|
+
end
|
370
|
+
else
|
371
|
+
require 'net/ftp'
|
372
|
+
Net::FTP.open(keys.host) do |ftp|
|
373
|
+
ftp.login(keys.user) #password?
|
374
|
+
ftp.chdir(keys.root)
|
375
|
+
keys.trans.each do |f, t|
|
376
|
+
puts "ftp #{f} #{t}" unless keys.quiet
|
377
|
+
ftp.putbinaryfile( f, t, 1024 )
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
# Use sftp to upload files.
|
384
|
+
|
385
|
+
def sftp( keys )
|
386
|
+
keys = upload_parameters(keys)
|
387
|
+
|
388
|
+
if keys.dryrun
|
389
|
+
puts "sftp open #{keys.user}@#{keys.host}:#{keys.root}/"
|
390
|
+
keys.trans.each do |f,t|
|
391
|
+
puts "sftp put #{f} #{t}"
|
392
|
+
end
|
393
|
+
else
|
394
|
+
require 'net/sftp'
|
395
|
+
Net::SFTP.start(keys.host, keys.user, keys.pass) do |sftp|
|
396
|
+
#sftp.login( user )
|
397
|
+
sftp.chdir(keys.root)
|
398
|
+
keys.trans.each do |f,t|
|
399
|
+
puts "sftp #{f} #{t}" unless keys.quiet
|
400
|
+
sftp.put_file(f,t) #, 1024 )
|
401
|
+
end
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
|
407
|
+
|
408
|
+
# Creates a stage from which the whole directory can be uploaded.
|
409
|
+
# This is needed for scp and rsync which have to shelled out,
|
410
|
+
# and can't conveniently copy one file at a time.
|
411
|
+
|
412
|
+
def upload_stage(keys) #:yield:
|
413
|
+
tmp = "scp_#{object_id.abs}_#{ Time.now.strftime("%Y%m%d%H%M%S")}"
|
414
|
+
tmpdir = File.join(Dir.tmpdir,tmp)
|
415
|
+
|
416
|
+
puts "mkdir -p #{tmpdir}" unless keys.quiet
|
417
|
+
FileUtils.mkdir_p(tmpdir) # go ahead and do this even if dryrun
|
418
|
+
|
419
|
+
fu = keys.dryrun ? FileUtils::DryRun : FileUtils
|
420
|
+
keys.trans.each do |f, t|
|
421
|
+
to = File.join(tmpdir, t)
|
422
|
+
fu.mv(f,to)
|
423
|
+
end
|
424
|
+
|
425
|
+
puts "cd #{tmpdir}" unless keys.quiet
|
426
|
+
Dir.chdir(tmpdir) do
|
427
|
+
yield #(tmpdir)
|
428
|
+
end
|
429
|
+
|
430
|
+
puts "rm -r #{tmpdir}" unless keys.quiet
|
431
|
+
FileUtils.rm_r(tmpdir) # now remove the temp dir
|
432
|
+
end
|
433
|
+
|
434
|
+
=end
|
data/lib/folio/xdg.rb
CHANGED
@@ -0,0 +1,489 @@
|
|
1
|
+
#require 'zlib'
|
2
|
+
|
3
|
+
# = ZipUtils
|
4
|
+
#
|
5
|
+
# Function module for compression methods.
|
6
|
+
#
|
7
|
+
# TODO: Much of this shells out. It would be best to internalize.
|
8
|
+
#
|
9
|
+
module ZipUtils
|
10
|
+
|
11
|
+
COMPRESS_FORMAT = {
|
12
|
+
'.tar.gz' => 'tar_gzip',
|
13
|
+
'.tgz' => 'tar_gzip',
|
14
|
+
'.tar.bz2' => 'tar_bzip2',
|
15
|
+
'.zip' => 'zip'
|
16
|
+
}
|
17
|
+
|
18
|
+
###############
|
19
|
+
module_function
|
20
|
+
###############
|
21
|
+
|
22
|
+
# Compress folder or file based on given extension.
|
23
|
+
# Supported extensions are:
|
24
|
+
# * .tar.gz
|
25
|
+
# * .tgz
|
26
|
+
# * .tar.bz2
|
27
|
+
# * .zip
|
28
|
+
#
|
29
|
+
# TODO: support gzip and bzip2 as well.
|
30
|
+
def compress(folder, file, options={})
|
31
|
+
format = COMPRESS_FORMAT[File.extname(file)]
|
32
|
+
if format
|
33
|
+
send(format, folder, file, options)
|
34
|
+
else
|
35
|
+
raise ArgumentError, "unknown compression format -- #{format_extension}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
#
|
41
|
+
def gzip(file, option={})
|
42
|
+
require 'zlib'
|
43
|
+
fname = File.basename(file) + '.gz'
|
44
|
+
if options[:dryrun] or options[:verbose]
|
45
|
+
puts "gzip #{file}"
|
46
|
+
end
|
47
|
+
Zlib::GzipWriter.open(fname) do |gz|
|
48
|
+
gz.write(File.read(file))
|
49
|
+
end unless options[:dryrun] or options[:noop]
|
50
|
+
return File.expand_path(fname)
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
#
|
55
|
+
def ungzip(file, options={})
|
56
|
+
require 'zlib'
|
57
|
+
fname = File.basename(file).chomp(File.extname(file))
|
58
|
+
if options[:dryrun] or options[:verbose]
|
59
|
+
puts "ungzip #{file}"
|
60
|
+
end
|
61
|
+
Zlib::GzipReader.open(file) do |gz|
|
62
|
+
File.open(fname, 'wb'){ |f| f << gz.read }
|
63
|
+
end unless options[:dryrun] or options[:noop]
|
64
|
+
return File.expand_path(fname)
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
#
|
69
|
+
def bzip2(file, option={})
|
70
|
+
cmd = "bzip2 #{file}"
|
71
|
+
puts cmd if options[:dryrun] or options[:verbose]
|
72
|
+
system cmd unless options[:dryrun] or options[:noop]
|
73
|
+
return File.expand_path(file + '.bz2')
|
74
|
+
end
|
75
|
+
|
76
|
+
alias_method :bzip, :bzip2
|
77
|
+
|
78
|
+
#
|
79
|
+
#
|
80
|
+
def unbzip2(file, options={})
|
81
|
+
cmd = "unbzip2 #{file}"
|
82
|
+
puts cmd if options[:dryrun] or options[:verbose]
|
83
|
+
system cmd unless options[:dryrun] or options[:noop]
|
84
|
+
return File.expand_path(file.chomp(File.extname(file)))
|
85
|
+
end
|
86
|
+
|
87
|
+
alias_method :unbzip, :unbzip2
|
88
|
+
|
89
|
+
#
|
90
|
+
#
|
91
|
+
def tar(folder, file=nil, options={})
|
92
|
+
require 'folio/minitar'
|
93
|
+
file ||= File.basename(File.expand_path(folder)) + '.tar'
|
94
|
+
cmd = "tar -cf #{file} #{folder}"
|
95
|
+
puts cmd if options[:verbose] or options[:dryrun]
|
96
|
+
unless options[:noop] or options[:dryrun]
|
97
|
+
gzIO = File.open(file, 'wb')
|
98
|
+
Archive::Tar::Minitar.pack(folder, gzIO)
|
99
|
+
end
|
100
|
+
return File.expand_path(file)
|
101
|
+
end
|
102
|
+
|
103
|
+
#
|
104
|
+
#
|
105
|
+
def untar(file, options={})
|
106
|
+
require 'folio/minitar'
|
107
|
+
#file ||= File.basename(File.expand_path(folder)) + '.tar'
|
108
|
+
cmd = "untar #{file}"
|
109
|
+
puts cmd if options[:verbose] or options[:dryrun]
|
110
|
+
unless options[:noop] or options[:dryrun]
|
111
|
+
gzIO = File.open(file, 'wb')
|
112
|
+
Archive::Tar::Minitar.unpack(gzIO)
|
113
|
+
end
|
114
|
+
return File.expand_path(file)
|
115
|
+
end
|
116
|
+
|
117
|
+
# Tar Gzip
|
118
|
+
#
|
119
|
+
def tar_gzip(folder, file=nil, options={})
|
120
|
+
require 'zlib'
|
121
|
+
require 'folio/minitar'
|
122
|
+
file ||= File.basename(File.expand_path(folder)) + '.tar.gz' # '.tgz' which ?
|
123
|
+
cmd = "tar --gzip -czf #{file} #{folder}"
|
124
|
+
puts cmd if options[:verbose] or options[:dryrun]
|
125
|
+
unless options[:noop] or options[:dryrun]
|
126
|
+
gzIO = Zlib::GzipWriter.new(File.open(file, 'wb'))
|
127
|
+
Archive::Tar::Minitar.pack(folder, gzIO)
|
128
|
+
end
|
129
|
+
return File.expand_path(file)
|
130
|
+
end
|
131
|
+
|
132
|
+
alias_method :tar_z, :tar_gzip
|
133
|
+
|
134
|
+
#def tgz(folder, file=nil, options={})
|
135
|
+
# file ||= File.basename(File.expand_path(folder)) + '.tgz'
|
136
|
+
# tar_gzip(folder, file, options)
|
137
|
+
#end
|
138
|
+
|
139
|
+
# Untar Gzip
|
140
|
+
#
|
141
|
+
# TODO: Write unified untar_gzip function.
|
142
|
+
def untar_gzip(file, options={})
|
143
|
+
untar(ungzip(file, options), options)
|
144
|
+
end
|
145
|
+
|
146
|
+
alias_method :untar_z, :untar_gzip
|
147
|
+
|
148
|
+
# Tar Bzip2
|
149
|
+
#
|
150
|
+
def tar_bzip2(folder, file=nil, options={})
|
151
|
+
# name of file to create
|
152
|
+
file ||= File.basename(File.expand_path(folder)) + '.tar.bz2'
|
153
|
+
cmd = "tar --bzip2 -cf #{file} #{folder}"
|
154
|
+
puts cmd if options[:dryrun] or options[:verbose]
|
155
|
+
system cmd unless options[:dryrun] or options[:noop]
|
156
|
+
return File.expand_path(file)
|
157
|
+
end
|
158
|
+
|
159
|
+
alias_method :tar_bzip, :tar_bzip2
|
160
|
+
|
161
|
+
alias_method :tar_j, :tar_bzip2
|
162
|
+
|
163
|
+
# Untar Bzip2
|
164
|
+
#
|
165
|
+
def untar_bzip2(file, options={})
|
166
|
+
cmd = "tar --bzip2 -xf #{file}"
|
167
|
+
puts cmd if options[:dryrun] or options[:verbose]
|
168
|
+
system cmd unless options[:dryrun] or options[:noop]
|
169
|
+
end
|
170
|
+
|
171
|
+
alias_method :untar_bzip, :untar_bzip2
|
172
|
+
|
173
|
+
alias_method :untar_j, :untar_bzip2
|
174
|
+
|
175
|
+
# Zip
|
176
|
+
#
|
177
|
+
def zip(folder, file=nil, options={})
|
178
|
+
raise ArgumentError if folder == '.*'
|
179
|
+
file ||= File.basename(File.expand_path(folder)) + '.zip'
|
180
|
+
cmd = "zip -rqu #{file} #{folder}"
|
181
|
+
puts cmd if options[:dryrun] or options[:verbose]
|
182
|
+
system cmd unless options[:dryrun] or options[:noop]
|
183
|
+
return File.expand_path(file)
|
184
|
+
end
|
185
|
+
|
186
|
+
# Unzip
|
187
|
+
#
|
188
|
+
def unzip(file, options={})
|
189
|
+
cmd = "unzip #{file}"
|
190
|
+
puts cmd if options[:dryrun] or options[:verbose]
|
191
|
+
system cmd unless options[:dryrun] or options[:noop]
|
192
|
+
end
|
193
|
+
|
194
|
+
end #module ZipUtils
|
195
|
+
|
196
|
+
# Verbose version of ZipUtils.
|
197
|
+
#
|
198
|
+
# This is the same as passing :verbose flag to ZipUtils methods.
|
199
|
+
#
|
200
|
+
module ZipUtils::Verbose
|
201
|
+
module_function
|
202
|
+
|
203
|
+
def compress(format_extension, folder, file=nil, options={})
|
204
|
+
options[:verbose] = true
|
205
|
+
ZipUtils.tar_gzip(format_extension, folder, file, options)
|
206
|
+
end
|
207
|
+
|
208
|
+
def gzip(file, options={})
|
209
|
+
options[:verbose] = true
|
210
|
+
ZipUtils.gzip(file, options)
|
211
|
+
end
|
212
|
+
|
213
|
+
def ungzip(file, options={})
|
214
|
+
options[:verbose] = true
|
215
|
+
ZipUtils.ungzip(file, options)
|
216
|
+
end
|
217
|
+
|
218
|
+
def bzip2(file, options={})
|
219
|
+
options[:verbose] = true
|
220
|
+
ZipUtils.bzip2(file, options)
|
221
|
+
end
|
222
|
+
|
223
|
+
alias_method :bzip, :bzip2
|
224
|
+
|
225
|
+
def unbzip2(file, options={})
|
226
|
+
options[:verbose] = true
|
227
|
+
ZipUtils.unbzip2(file, options)
|
228
|
+
end
|
229
|
+
|
230
|
+
alias_method :unbzip, :unbzip2
|
231
|
+
|
232
|
+
def tar(folder, file=nil, options={})
|
233
|
+
options[:verbose] = true
|
234
|
+
ZipUtils.tar(folder, file, options)
|
235
|
+
end
|
236
|
+
|
237
|
+
def untar(file, options={})
|
238
|
+
options[:verbose] = true
|
239
|
+
ZipUtils.untar(file, options)
|
240
|
+
end
|
241
|
+
|
242
|
+
def tar_gzip(folder, file=nil, options={})
|
243
|
+
options[:verbose] = true
|
244
|
+
ZipUtils.tar_gzip(folder, file, options)
|
245
|
+
end
|
246
|
+
|
247
|
+
def untar_gzip(file, options={})
|
248
|
+
options[:verbose] = true
|
249
|
+
ZipUtils.untar_gzip(file, options)
|
250
|
+
end
|
251
|
+
|
252
|
+
def tar_bzip2(folder, file=nil, options={})
|
253
|
+
options[:verbose] = true
|
254
|
+
ZipUtils.untar_bzip2(folder, file, options)
|
255
|
+
end
|
256
|
+
|
257
|
+
def untar_bzip2(file, options={})
|
258
|
+
options[:verbose] = true
|
259
|
+
ZipUtils.untar_bzip2(file, options)
|
260
|
+
end
|
261
|
+
|
262
|
+
def zip(folder, file=nil, options={})
|
263
|
+
options[:verbose] = true
|
264
|
+
ZipUtils.unzip(folder, file, options)
|
265
|
+
end
|
266
|
+
|
267
|
+
def unzip(file, options={})
|
268
|
+
options[:verbose] = true
|
269
|
+
ZipUtils.unzip(file, options)
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
# NoWrite Version of ZipUtils.
|
274
|
+
#
|
275
|
+
# This is the same as passing :noop flag to ZipUtils methods.
|
276
|
+
#
|
277
|
+
module ZipUtils::NoWrite
|
278
|
+
module_function
|
279
|
+
|
280
|
+
def compress(format_extension, folder, file=nil, options={})
|
281
|
+
options[:noop] = true
|
282
|
+
ZipUtils.tar_gzip(format_extension, folder, file, options)
|
283
|
+
end
|
284
|
+
|
285
|
+
def gzip(file, options={})
|
286
|
+
options[:noop] = true
|
287
|
+
ZipUtils.gzip(file, options)
|
288
|
+
end
|
289
|
+
|
290
|
+
def ungzip(file, options={})
|
291
|
+
options[:noop] = true
|
292
|
+
ZipUtils.ungzip(file, options)
|
293
|
+
end
|
294
|
+
|
295
|
+
def bzip2(file, options={})
|
296
|
+
options[:noop] = true
|
297
|
+
ZipUtils.bzip2(file, options)
|
298
|
+
end
|
299
|
+
|
300
|
+
alias_method :bzip, :bzip2
|
301
|
+
|
302
|
+
def unbzip2(file, options={})
|
303
|
+
options[:noop] = true
|
304
|
+
ZipUtils.unbzip2(file, options)
|
305
|
+
end
|
306
|
+
|
307
|
+
alias_method :unbzip, :unbzip2
|
308
|
+
|
309
|
+
def tar(folder, file=nil, options={})
|
310
|
+
options[:noop] = true
|
311
|
+
ZipUtils.tar(folder, file, options)
|
312
|
+
end
|
313
|
+
|
314
|
+
def untar(file, options={})
|
315
|
+
options[:noop] = true
|
316
|
+
ZipUtils.untar(file, options)
|
317
|
+
end
|
318
|
+
|
319
|
+
def tar_gzip(folder, file=nil, options={})
|
320
|
+
options[:noop] = true
|
321
|
+
ZipUtils.tar_gzip(folder, file, options)
|
322
|
+
end
|
323
|
+
|
324
|
+
def untar_gzip(file, options={})
|
325
|
+
options[:noop] = true
|
326
|
+
ZipUtils.untar_gzip(file, options)
|
327
|
+
end
|
328
|
+
|
329
|
+
def tar_bzip2(folder, file=nil, options={})
|
330
|
+
options[:noop] = true
|
331
|
+
ZipUtils.untar_bzip2(folder, file, options)
|
332
|
+
end
|
333
|
+
|
334
|
+
alias_method :tar_bzip, :tar_bzip2
|
335
|
+
|
336
|
+
def untar_bzip2(file, options={})
|
337
|
+
options[:noop] = true
|
338
|
+
ZipUtils.untar_bzip2(file, options)
|
339
|
+
end
|
340
|
+
|
341
|
+
alias_method :untar_bzip, :untar_bzip2
|
342
|
+
|
343
|
+
def zip(folder, file=nil, options={})
|
344
|
+
options[:noop] = true
|
345
|
+
ZipUtils.unzip(folder, file, options)
|
346
|
+
end
|
347
|
+
|
348
|
+
def unzip(file, options={})
|
349
|
+
options[:noop] = true
|
350
|
+
ZipUtils.unzip(file, options)
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
# Dry-run verions of ZipUtils.
|
355
|
+
#
|
356
|
+
# This is the same as passing the :dryrun flag to ZipUtils.
|
357
|
+
# Which is also equivalent to passing :noop and :verbose together.
|
358
|
+
|
359
|
+
module ZipUtils::DryRun
|
360
|
+
module_function
|
361
|
+
|
362
|
+
def compress(format_extension, folder, file=nil, options={})
|
363
|
+
options[:dryrun] = true
|
364
|
+
ZipUtils.tar_gzip(format_extension, folder, file, options)
|
365
|
+
end
|
366
|
+
|
367
|
+
def gzip(file, options={})
|
368
|
+
options[:dryrun] = true
|
369
|
+
ZipUtils.gzip(file, options)
|
370
|
+
end
|
371
|
+
|
372
|
+
def ungzip(file, options={})
|
373
|
+
options[:dryrun] = true
|
374
|
+
ZipUtils.ungzip(file, options)
|
375
|
+
end
|
376
|
+
|
377
|
+
def bzip2(file, options={})
|
378
|
+
options[:dryrun] = true
|
379
|
+
ZipUtils.bzip2(file, options)
|
380
|
+
end
|
381
|
+
|
382
|
+
alias_method :bzip, :bzip2
|
383
|
+
|
384
|
+
def unbzip2(file, options={})
|
385
|
+
options[:dryrun] = true
|
386
|
+
ZipUtils.unbzip2(file, options)
|
387
|
+
end
|
388
|
+
|
389
|
+
alias_method :unbzip, :unbzip2
|
390
|
+
|
391
|
+
def tar(folder, file=nil, options={})
|
392
|
+
options[:dryrun] = true
|
393
|
+
ZipUtils.tar(folder, file, options)
|
394
|
+
end
|
395
|
+
|
396
|
+
def untar(file, options={})
|
397
|
+
options[:dryrun] = true
|
398
|
+
ZipUtils.untar(file, options)
|
399
|
+
end
|
400
|
+
|
401
|
+
def tar_gzip(folder, file=nil, options={})
|
402
|
+
options[:dryrun] = true
|
403
|
+
ZipUtils.tar_gzip(folder, file, options)
|
404
|
+
end
|
405
|
+
|
406
|
+
def untar_gzip(file, options={})
|
407
|
+
options[:dryrun] = true
|
408
|
+
ZipUtils.untar_gzip(file, options)
|
409
|
+
end
|
410
|
+
|
411
|
+
def tar_bzip2(folder, file=nil, options={})
|
412
|
+
options[:dryrun] = true
|
413
|
+
ZipUtils.untar_bzip2(folder, file, options)
|
414
|
+
end
|
415
|
+
|
416
|
+
alias_method :tar_bzip, :tar_bzip2
|
417
|
+
|
418
|
+
def untar_bzip2(file, options={})
|
419
|
+
options[:dryrun] = true
|
420
|
+
ZipUtils.untar_bzip2(file, options)
|
421
|
+
end
|
422
|
+
|
423
|
+
alias_method :untar_bzip, :untar_bzip2
|
424
|
+
|
425
|
+
def zip(folder, file=nil, options={})
|
426
|
+
options[:dryrun] = true
|
427
|
+
ZipUtils.unzip(folder, file, options)
|
428
|
+
end
|
429
|
+
|
430
|
+
def unzip(file, options={})
|
431
|
+
options[:dryrun] = true
|
432
|
+
ZipUtils.unzip(file, options)
|
433
|
+
end
|
434
|
+
end
|
435
|
+
|
436
|
+
|
437
|
+
|
438
|
+
# OLD VERSION
|
439
|
+
# #
|
440
|
+
# # DryRun version of ZipUtils.
|
441
|
+
# #
|
442
|
+
#
|
443
|
+
# module DryRun
|
444
|
+
# module_function
|
445
|
+
#
|
446
|
+
# def compress( format, folder, to_file=nil )
|
447
|
+
# send(FORMAT_TO_COMPRESS[format], folder, to_file)
|
448
|
+
# end
|
449
|
+
#
|
450
|
+
# # Tar Gzip
|
451
|
+
#
|
452
|
+
# def tar_gzip( folder, to_file=nil )
|
453
|
+
# to_file ||= File.basename(File.expand_path(folder)) + '.tar.gz'
|
454
|
+
# puts "tar --gzip -czf #{to_file} #{folder}"
|
455
|
+
# end
|
456
|
+
#
|
457
|
+
# # Untar Gzip
|
458
|
+
#
|
459
|
+
# def untar_gzip( file )
|
460
|
+
# puts "tar --gzip -xzf #{file}"
|
461
|
+
# end
|
462
|
+
#
|
463
|
+
# # Tar Bzip2
|
464
|
+
#
|
465
|
+
# def tar_bzip( folder, to_file=nil )
|
466
|
+
# puts "tar --bzip2 -cf #{to_file} #{folder}"
|
467
|
+
# end
|
468
|
+
# alias_method :tar_bz2, :tar_bzip
|
469
|
+
#
|
470
|
+
# # Untar Bzip2
|
471
|
+
#
|
472
|
+
# def untar_bzip( file )
|
473
|
+
# puts "tar --bzip2 -xf #{file}"
|
474
|
+
# end
|
475
|
+
# alias_method :untar_bz2, :untar_bzip
|
476
|
+
#
|
477
|
+
# # Zip
|
478
|
+
#
|
479
|
+
# def zip( folder, to_file=nil )
|
480
|
+
# puts "zip -cf #{to_file} #{folder}"
|
481
|
+
# end
|
482
|
+
#
|
483
|
+
# # Unzip
|
484
|
+
#
|
485
|
+
# def unzip( file )
|
486
|
+
# puts "zip -xf #{file}"
|
487
|
+
# end
|
488
|
+
#
|
489
|
+
# end
|