X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=superrip.rb;h=e7acb5e8001e806e21c041a4f21a06ad3295c16a;hb=9d3f243e756dcd94797aefdb11fb478e6cded042;hp=bdf7512e138a5fcf45b88131b7eb0bdbdd723820;hpb=2a40e7cb797b97314ad82e9f7405f9ce5dda4bf2;p=cmccabe-bin diff --git a/superrip.rb b/superrip.rb index bdf7512..e7acb5e 100755 --- a/superrip.rb +++ b/superrip.rb @@ -16,7 +16,6 @@ require 'ostruct' #----------------------------------------------------------------- # constants #----------------------------------------------------------------- -$cd_dev = "/dev/cdrom" $children = Hash.new #----------------------------------------------------------------- @@ -36,7 +35,7 @@ end def get_number_of_tracks_on_cd look_for_tracks = false lines = Array.new - IO.popen("cdda2wav -v summary -J dev=#{$cd_dev} 2>&1", "r") do |io| + IO.popen("cdda2wav -v summary -J dev=#{$opts.cd_dev} 2>&1", "r") do |io| io.readlines.each do |line| line.chomp! lines << line @@ -44,8 +43,8 @@ def get_number_of_tracks_on_cd look_for_tracks = true elsif (look_for_tracks == true) then look_for_tracks = false - line =~ /[ \t]*1-([1234567890][1234567890]*)[^1234567890]/ \ - or raise "couldn't understand cdda2wav output!" + line =~ /[ \t]*1-([ 1234567890][1234567890]*)[^1234567890]/ \ + or raise "couldn't understand cdda2wav output! (line:#{line})" return $1.to_i end end @@ -57,39 +56,44 @@ end # Process the WAV file into an MP3 and FLAC file. # This is done in a background process. def process_wav(track) - FileUtils.mkdir_p(track.flac_dir, $fu_args) + FileUtils.mkdir_p(track.flac_dir, verbose: true, noop: !$opts.dry_run) my_system("flac -f '#{track.wav_file_name}' \ --output-name='#{track.flac_file_name}' &>/dev/null") my_system("flac --test '#{track.flac_file_name}' &>/dev/null") - FileUtils.mkdir_p(track.mp3_dir, $fu_args) + FileUtils.mkdir_p(track.mp3_dir, verbose: true, noop: !$opts.dry_run) my_system("lame -q 1 -b 192 '#{track.wav_file_name}' \ '#{track.mp3_file_name}' &>/dev/null") - FileUtils.rm_f(track.wav_file_name, $fu_args) + FileUtils.rm_f(track.wav_file_name, verbose: true, noop: !$opts.dry_run) end def audiorip(tnum, track) begin - my_system("nice -1 cdparanoia -w -d #{$cd_dev} #{tnum}") + my_system("nice -1 cdparanoia -w -d #{$opts.cd_dev} #{tnum}") rescue raise "failed to rip track #{tnum} (#{track.name})" end # cdparanoia always outputs to cdda.wav - FileUtils.mv("cdda.wav", track.wav_file_name, $fu_args) + FileUtils.mv("cdda.wav", track.wav_file_name, verbose: true, noop: !$opts.dry_run) # If there are too many processes, wait for one of them to terminate if ($children.keys.length > $opts.max_children) then - pid = Process.wait(-1) + pid, status = Process.wait2(-1) + if (status.exitstatus != 0) then + raise "process #{pid} failed with exitstatus #{status.exitstatus}" + end $children.delete(pid) end pid = Process.fork if (pid == nil) then + retcode = 0 begin process_wav(track) - Kernel.exit(0) - rescue - Kernel.exit(1) + rescue Exception => e + puts "*** FATAL ERROR: #{e}" + retcode = 1 end + Kernel.exit(retcode) else $children[pid] = 1 end @@ -103,16 +107,19 @@ class MyOptions opts = OpenStruct.new opts.dry_run = false opts.max_children = 4 - $fu_args = { :verbose => true } + opts.cd_dev = "/dev/cdrom" # Fill in opts values parser = OptionParser.new do |myparser| myparser.banner = "Usage: #{ File.basename($0) } [opts]" myparser.separator("Specific options:") + myparser.on("--dev [DEV]", "-D", + "choose the cdrom device file to use") do |dev| + opts.cd_dev = dev + end myparser.on("--dry-run", "-d", "Show what would be done, without doing it.") do |a| opts.dry_run = true - $fu_args = { :verbose => true, :noop => true } end myparser.on("--tracklist [FILE]", "-t", "Provide a list of tracks to use.") do |file| @@ -184,11 +191,16 @@ class Manifest if (@t.empty?) then raise "you must define some tracks" end - @t.each { |t| t.validate } - if (not $opts.partial) then - (1..num_tracks).each do |t| - if not @t[t].defined? - raise "don't know what to do with track #{t}" + if ($opts.partial) then + highest_track = @t.keys.sort[-1] + if (num_tracks < highest_track) then + raise "can't rip track #{highest_track}, because there are \ +only #{num_tracks} tracks" + end + else + (1..num_tracks).each do |tnum| + if not @t.has_key?(tnum) + raise "don't know what to do with track #{tnum}" end end end @@ -201,7 +213,12 @@ class Manifest next unless @t.has_key?(tnum) audiorip(tnum, @t[tnum]) end - Process.waitall + prc = Process.waitall + prc.each do |pair| + if (pair[1].exitstatus != 0) then + raise "process #{pair[0]} failed with exitstatus #{pair[1].exitstatus}" + end + end end def inspect @@ -235,6 +252,7 @@ manifest = Manifest.new($opts.manifest_file) puts manifest.inspect num_tracks = get_number_of_tracks_on_cd() puts "found #{num_tracks} tracks" +manifest.validate(num_tracks) manifest.rip(num_tracks) puts "*** FINISHED ***" exit 0