lib/handbrake/cli.rb in handbrake-0.3.0 vs lib/handbrake/cli.rb in handbrake-0.3.1

- old
+ new

@@ -1,6 +1,7 @@ require 'handbrake' +require 'fileutils' module HandBrake ## # The main entry point for this API. See {file:README.md} for usage # examples. @@ -17,21 +18,31 @@ # # @return [Boolean] attr_writer :trace ## + # Set whether dry_run mode is selected. + # + # @return [Boolean] + attr_writer :dry_run + + ## # @param [Hash] options # @option options [String] :bin_path ('HandBrakeCLI') the full # path to the executable to use # @option options [Boolean] :trace (false) whether {#trace?} is # enabled + # @option options [Boolean] :dry_run (false) if true, nothing will + # actually be executed. The commands that would have been + # executed will be printed to standard out. # @option options [#run] :runner (a PopenRunner instance) the class # encapsulating the execution method for HandBrakeCLI. You # shouldn't usually need to replace this. def initialize(options={}) @bin_path = options[:bin_path] || 'HandBrakeCLI' @trace = options[:trace].nil? ? false : options[:trace] + @dry_run = options[:dry_run] || false @runner = options[:runner] || PopenRunner.new(self) @args = [] end @@ -54,17 +65,35 @@ def trace? @trace end ## + # Is this instance in `dry_run` mode? + # + # If it is, then no commands will actually be executed. The + # constructed `HandBrakeCLI` invocations will be printed to + # standard out, instead. + def dry_run? + @dry_run + end + + def fileutils_options + { + :noop => dry_run?, + :verbose => trace? || dry_run? + } + end + private :fileutils_options + + ## # Performs a conversion. This method immediately begins the # transcoding process; set all other options first. # # @param [String] filename the desired name for the final output - # file + # file. # @param [Hash] options additional options to control the behavior - # of the output process + # of the output process. The provided hash will not be modified. # @option options [Boolean,:ignore] :overwrite (true) determines # the behavior if the desired output file already exists. If # `true`, the file is replaced. If `false`, an exception is # thrown. If `:ignore`, the file is skipped; i.e., HandBrakeCLI # is not invoked. @@ -82,10 +111,11 @@ # overwritten). This option is intended to aid in writing # automatically resumable batch scripts. # # @return [void] def output(filename, options={}) + options = options.dup overwrite = options.delete :overwrite case overwrite when true, nil # no special behavior when false @@ -114,10 +144,11 @@ unless options.empty? raise "Unknown options for output: #{options.keys.inspect}" end + FileUtils.mkdir_p(File.dirname(interim_filename), fileutils_options) run('--output', interim_filename) if filename != interim_filename replace = if File.exist?(filename) @@ -133,11 +164,12 @@ true end else true end - FileUtils.mv(interim_filename, filename, :verbose => trace?) if replace + FileUtils.mkdir_p(File.dirname(filename), fileutils_options) + FileUtils.mv(interim_filename, filename, fileutils_options) if replace end end def partial_filename(name) if File.basename(name).index '.' @@ -278,16 +310,21 @@ output = '' cmd = "'" + arguments.unshift(@cli.bin_path).join("' '") + "' 2>&1" $stderr.puts "Spawning HandBrakeCLI using #{cmd.inspect}" if @cli.trace? - IO.popen(cmd) do |io| - while line = io.read(60) - output << line - $stderr.write(line) if @cli.trace? + if @cli.dry_run? + puts cmd + RunnerResult.new('', 0) + else + IO.popen(cmd) do |io| + while line = io.read(60) + output << line + $stderr.write(line) if @cli.trace? + end end + RunnerResult.new(output, $?) end - RunnerResult.new(output, $?) end end ## # @private