-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathripit.rb
executable file
·465 lines (384 loc) · 12.9 KB
/
ripit.rb
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#! /usr/bin/env ruby
##########################################################
# ripit.rb
#
# control Pioneer DRM-7000 DVD jukebox
#
##########################################################
require 'trollop'
require 'open4'
# TODO: switch to color logging and tee to STDOUT
#require 'log4r-color'
#include Log4r
require 'logger'
# TODO: check for "not ready" status return by mtx - warn about front panel lockout
##########################################################
# PARAMETERS & ERROR CHECKING
##########################################################
CAROUSEL_DEVICE='/dev/sg8'
FIRST_INPUT_SLOT=1
LAST_INPUT_SLOT=350
FIRST_OUTPUT_SLOT=401
LAST_OUTPUT_SLOT=750
FIRST_ERR_SLOT=751
LAST_ERR_SLOT=770
SLOTS_PER_CASSETTE=50
DRIVES=['/dev/sr1', '/dev/sr2']
RIPDIR='/rip'
def valid_slot?(s, slot_type=:any)
return unless s.is_a?(Integer)
case slot_type
when :input
return s >= FIRST_INPUT_SLOT && s <= LAST_INPUT_SLOT
when :output
return s >= FIRST_OUTPUT_SLOT && s <= LAST_OUTPUT_SLOT
when :error
return valid_slot?(s, :input) || (s >= FIRST_ERR_SLOT && s <= LAST_ERR_SLOT)
else # :any
return valid_slot?(s, :input) || valid_slot?(s, :output) || valid_slot?(s, :error)
end
end
def valid_drive?(d)
d.is_a?(Integer) && d >=0 && d < DRIVES.length
end
##########################################################
# COMMAND LINE PARSING
##########################################################
COMMANDS = %w(load unload rip info title status test_all_cassettes rip_one rip_all)
opts = Trollop::options do
version "ripit.rb v1.0 (c) 2011 Fandor.com / Our Film Festival, Inc."
banner <<-EOS
ripit.rb controls Fandor's Pioneer DRM-7000 DVD jukebox.
Usage:
ripit [action] [options]
where [action] is one of the following:
main actions:
status show status of all device slots
rip_one run rip process on a single slot
rip_all run rip process on all discs in robot
test actions:
test_all_cassettes test error state of all cassettes
controller actions:
load load from slot into specified drive
unload unload from drive into slot
rip rip disc in specified drive
info show info on disc in specified drive
title show title of disc in specified drive
and [options] include:
EOS
opt :drive, "Drive number (0-#{DRIVES.length-1})", :type => :int
opt :slot, "Slot number (#{FIRST_INPUT_SLOT}-#{LAST_ERR_SLOT})", :type => :int
opt :dry_run, "Display commands without doing anything", :short => "-n"
opt :debug, "Display debugging information to console", :short => "-!"
end
cmd = ARGV.shift
# validate options
Trollop::die "Unknown command #{cmd}" unless COMMANDS.include?(cmd)
# most commands should include a drive number
if %w(rip_all status test_all_cassettes rip_one).include? cmd
Trollop::die :drive, "should not be specified for a #{cmd} command" unless opts[:drive].nil?
if %w(rip_one).include? cmd
Trollop::die :slot, "must be a valid slot number" unless valid_slot?(opts[:slot])
else
Trollop::die :slot, "should not be specified for a #{cmd} command" unless opts[:slot].nil?
end
else
Trollop::die :drive, "must be a valid drive number" unless valid_drive?(opts[:drive])
# only some commands should include a slot number
if %w(rip info title).include? cmd
Trollop::die :slot, "should not be specified for a #{cmd} command" unless opts[:slot].nil?
else
Trollop::die :slot, "must be a valid slot number" unless valid_slot?(opts[:slot])
end
end
$dry_run = opts[:dry_run]
$debug = opts[:debug]
############################################################
# LOGGER
############################################################
#Logger.global.level = ALL
#formatter =
$log = $debug ? Logger.new(STDOUT) : Logger.new("#{RIPDIR}/ripit.log", 'daily')
$log.level = Logger::INFO
############################################################
# DEVICE INFO
############################################################
def output_slot(slot_num)
raise "Invalid slot number" unless valid_slot?(slot_num, :input)
(slot_num-FIRST_INPUT_SLOT)+FIRST_OUTPUT_SLOT
end
############################################################
# DEVICE CONTROL
############################################################
def disc_title(drive_num)
raise "Invalid drive number" unless valid_drive?(drive_num)
title_cmd = "df | grep #{DRIVES[drive_num]}"
puts title_cmd if $dry_run
$log.info title_cmd
unless $dry_run
pid, stdin, stdout, stderr = Open4::popen4(title_cmd)
ignored, status = Process::waitpid2(pid)
case status
# success
when 0
$log.info "Title read successful."
title=stdout.readlines.to_s
$log.info "stdout:\t#{title}"
title = title.match(/media\/(.*)$/) if title
title = title[1] if title
$log.info "title:\t#{title}"
return title
when 256
# status = 256 on empty drive / drive not ready
$log.warn "No disc in drive #{drive_num}"
return false
else
$log.error "Error reading disc title"
return nil
end
end
end
def disc_info(drive_num)
raise "Invalid drive number" unless valid_drive?(drive_num)
info_cmd = "dvdbackup --info --input=#{DRIVES[drive_num]}"
puts info_cmd if $dry_run
$log.info info_cmd
unless $dry_run
pid, stdin, stdout, stderr = Open4::popen4(info_cmd)
ignored, status = Process::waitpid2(pid)
# TODO: add logic to deal with return code
$log.info "status:\t#{status}"
$log.info "stderr:#{stderr.readlines.to_s}"
info=stdout.readlines.to_s
$log.info "stdout:#{info}"
return info
end
end
def load_disc(drive_num, slot_num)
raise "Invalid drive number" unless valid_drive?(drive_num)
raise "Invalid slot number" unless valid_slot?(slot_num)
load_cmd = "mtx -f #{CAROUSEL_DEVICE} load #{slot_num} #{drive_num}"
puts load_cmd if $dry_run
$log.info load_cmd
unless $dry_run
pid, stdin, stdout, stderr = Open4::popen4(load_cmd)
ignored, status = Process::waitpid2(pid)
case status
# success
when 0
$log.info "load successful."
return true
else
# status = 256 on failure
errmsg = stderr.readlines.to_s
case errmsg
when /Empty/
$log.warn "No disc to load from slot #{slot_num}."
return false
when /Full/
$log.error "Cannot load into full drive #{drive_num}."
return nil
else
$log.error "Cartridge or device not ready loading from slot #{slot_num}."
return nil
end
end
end
end
def unload_disc(drive_num, slot_num)
raise "Invalid drive number" unless valid_drive?(drive_num)
raise "Invalid slot number" unless valid_slot?(slot_num)
unload_cmd = "mtx -f #{CAROUSEL_DEVICE} unload #{slot_num} #{drive_num}"
puts unload_cmd if $dry_run
$log.info unload_cmd
unless $dry_run
pid, stdin, stdout, stderr = Open4::popen4(unload_cmd)
ignored, status = Process::waitpid2(pid)
case status
# success
when 0
$log.info "unload successful."
return true
else
# status = 256 on failure
errmsg = stderr.readlines.to_s
case errmsg
when /Empty/
$log.warn "No disc to unload from drive #{drive_num}."
return false
when /Full/
$log.error "Cannot unload drive #{drive_num} into full slot #{slot_num}."
return nil
else
$log.error "Cartridge or device not ready unloading into slot #{slot_num}."
return nil
end
end
end
end
def device_status()
status_cmd = "mtx -f #{CAROUSEL_DEVICE} status"
puts status_cmd if $dry_run
$log.info status_cmd
unless $dry_run
pid, stdin, stdout, stderr = Open4::popen4(status_cmd)
ignored, status = Process::waitpid2(pid)
$log.info "status:\t#{status}"
$log.info "stderr:#{stderr.readlines.to_s}"
dev_status=stdout.readlines.to_s
$log.info "stdout:#{dev_status}"
return dev_status
end
end
def test_all_cassettes()
(1..LAST_ERR_SLOT).step(SLOTS_PER_CASSETTE) do |s|
begin
# nil is the only real "failure" case
if !load_disc(0, s).nil?
unload_disc(0, s)
puts "Slot #{s} - cassette tests okay."
else
puts "Slot #{s} - cassette missing or failed test."
end
rescue
puts "Slot #{s} - error."
end
end
end
def rip_disc(drive_num, use_generic_title=false)
raise "Invalid drive number" unless valid_drive?(drive_num)
rip_cmd = "dvdbackup --mirror --input=#{DRIVES[drive_num]} --output=#{RIPDIR}"
if use_generic_title
# if use_generic_title is true, we need to create a unique "anonymous" title
ripcmd += '--name="' + "generic_rip #{Time.now}" + '"'
else
# check that the current disc's title is not already used (e.g., same-named disc or previously failed rip)
#MK TODO FIX THIS
end
puts rip_cmd if $dry_run
$log.info rip_cmd
unless $dry_run
pid, stdin, stdout, stderr = Open4::popen4(rip_cmd)
ignored, status = Process::waitpid2(pid)
case status
# success
when 0
$log.info "rip successful."
return true
# status 2 = occurs when disc title is blank / generic
when 2 && !use_generic_title
# call recursively if it has a generic name
$log.info "title has generic name; retrying"
return rip_disc(drive_num, true)
else
# status -1 = failure
# status 1 = usage error (should never occur)
# status 2 = title name error (should never occur if use_generic_title is true)
$log.error "ripping error!"
$log.error "status:\t#{status}"
$log.error "stdout:#{stdout.readlines.to_s}"
$log.error "stderr:#{stderr.readlines.to_s}"
return nil
end
end
end
##########################################################
# RIPPING OPERATIONS
##########################################################
def log_disc_info(drive_num, error=false)
title=disc_title(drive_num)
info=disc_info(drive_num)
File.open("#{RIPDIR}/#{title}.log", 'w') {|f| f.puts info}
File.open("#{RIPDIR}/error.log", "w") { |f| f.puts error } if error
end
def process_rip(slot)
# use drive 0 for even, drive 1 for odd source slots
drive = slot % 2
puts s="Ripping slot #{slot} using drive #{drive}..."
$log.info(s)
# load disc from slot
l=load_disc(drive, slot)
# if no disc (false) or a failure (nil), return
unless l
if l==false
puts s = "No disc in slot #{slot}"
$log.info(s)
else
puts s = "Error loading slot #{slot}"
$log.error(s)
end
return l
end
# wait for disc to mount
while disc_title(drive)==false
sleep 10
end
# rip disc
r = rip_disc(drive)
# log the disc info and set output slot to the "success" bank on success, original slot on error
out_slot = r ? output_slot(slot) : slot
log_disc_info(drive, r)
u = unload_disc(drive, out_slot)
# check for error, if slot is full return to overflow bank?
while !u
# handle error on unloading disc
out_slot = (out_slot < FIRST_ERR_SLOT) ? FIRST_ERR_SLOT : out_slot + 1
raise "CRISIS: ALL OUTPUT SLOTS FULL!" if out_slot > LAST_ERR_SLOT
$log.warn("Attempting alternate unload to slot #{out_slot}.")
u = unload_disc(drive, out_slot)
end
s="Done ripping slot #{slot}, returned to slot #{out_slot}"
return r
end
# run two ripping processes in parallel
def rip_all_discs()
results = []
start_time = Time.now()
# fork to rip odd-numbered
pid = fork {
(FIRST_INPUT_SLOT..LAST_INPUT_SLOT).step(2) { |o| results[o]=process_rip(o) }
}
# rip even-numbered
((FIRST_INPUT_SLOT+1)..LAST_INPUT_SLOT).step(2) { |e| results[e]=process_rip(e)}
# wait for things to finish
Process.waitpid(pid)
# generate summary report based on results array
# create an output file to log all discs
success = 0
failure = 0
empty = 0
1.upto(LAST_INPUT_SLOT) do |i|
case results[i]
when true
success += 1
when false
empty += 1
when nil
failure += 1
end
end
File.open("#{RIPDIR}/rip_all.log", 'w') do |f|
f.puts "rip_all operation completed:"
f.puts " Successful rips: #{success}"
f.puts " Empty slots: #{empty}"
f.puts " Failed rips: #{failure}"
f.puts "Elapsed time: #{(Time.now - start_time).round} seconds"
end
end
##########################################################
# MAIN BODY
##########################################################
# record the command & start time in the log
$log.info ARGV
# process the command
case cmd
when "load" then load_disc(opts[:drive], opts[:slot])
when "unload" then unload_disc(opts[:drive], opts[:slot])
when "rip" then rip_disc(opts[:drive])
when "info" then puts disc_info(opts[:drive])
when "title" then puts disc_title(opts[:drive])
when "status" then puts device_status()
when "test_all_cassettes" then test_all_cassettes()
when "rip_one" then process_rip(opts[:slot])
when "rip_all" then rip_all_discs()
end