From 4a10735a57529b6e76d3c9336d52d0d9e7de8532 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 16 Jan 2013 00:48:29 +0100 Subject: [PATCH 1/6] don't use autoload; requires --- lib/image_optim.rb | 8 ++++---- lib/image_optim/image_path.rb | 2 ++ lib/image_optim/option_helpers.rb | 2 ++ lib/image_optim/worker.rb | 1 + 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/image_optim.rb b/lib/image_optim.rb index 099446b5..fdee90ff 100644 --- a/lib/image_optim.rb +++ b/lib/image_optim.rb @@ -1,11 +1,11 @@ require 'in_threads' require 'shellwords' -class ImageOptim - autoload :ImagePath, 'image_optim/image_path' - autoload :OptionHelpers, 'image_optim/option_helpers' - autoload :Worker, 'image_optim/worker' +require 'image_optim/image_path' +require 'image_optim/option_helpers' +require 'image_optim/worker' +class ImageOptim class ConfigurationError < StandardError; end class BinNotFoundError < StandardError; end diff --git a/lib/image_optim/image_path.rb b/lib/image_optim/image_path.rb index 7d2c953f..c38cea5a 100644 --- a/lib/image_optim/image_path.rb +++ b/lib/image_optim/image_path.rb @@ -1,6 +1,8 @@ require 'fspath' require 'image_size' +require 'image_optim' + class ImageOptim class ImagePath < FSPath # Get temp path for this file with same extension diff --git a/lib/image_optim/option_helpers.rb b/lib/image_optim/option_helpers.rb index 4c74845e..870c7765 100644 --- a/lib/image_optim/option_helpers.rb +++ b/lib/image_optim/option_helpers.rb @@ -1,3 +1,5 @@ +require 'image_optim' + class ImageOptim module OptionHelpers # Remove option from hash and run through block or return default diff --git a/lib/image_optim/worker.rb b/lib/image_optim/worker.rb index 8eb2fee8..b6685fb2 100644 --- a/lib/image_optim/worker.rb +++ b/lib/image_optim/worker.rb @@ -1,6 +1,7 @@ # encoding: UTF-8 require 'shellwords' + require 'image_optim' class ImageOptim From 2f87430dca4808c80b9767a3f25fc9ed1cce0e1c Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 16 Jan 2013 23:44:59 +0100 Subject: [PATCH 2/6] reordered and organized bin/image_optim options summary --- bin/image_optim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bin/image_optim b/bin/image_optim index f382faf9..2486b2f5 100755 --- a/bin/image_optim +++ b/bin/image_optim @@ -17,6 +17,12 @@ Usege: TEXT + op.on('-r', '-R', '--recursive', 'Recurively scan directories for images') do |recursive| + options[:recursive] = recursive + end + + op.separator nil + op.on('--[no-]threads NUMBER', Integer, 'Number of threads or disable (defaults to number of processors)') do |threads| options[:threads] = threads end @@ -25,6 +31,8 @@ Usege: options[:nice] = nice end + op.separator nil + ImageOptim::Worker.klasses.each do |klass| bin = klass.underscored_name.to_sym op.on("--no-#{bin}", "disable #{bin} worker") do |enable| @@ -32,9 +40,7 @@ Usege: end end - op.on('-r', '-R', '--recursive', 'Scan directories') do |recursive| - options[:recursive] = recursive - end + op.separator nil op.on('-v', '--verbose', 'Verbose info') do |verbose| options[:verbose] = verbose From de7698b8180a69ee92575f324fd35acb8964432b Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 16 Jan 2013 23:50:32 +0100 Subject: [PATCH 3/6] extracted checking access to bin --- lib/image_optim.rb | 9 +++++++-- spec/image_optim_spec.rb | 8 ++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/image_optim.rb b/lib/image_optim.rb index fdee90ff..ca16470f 100644 --- a/lib/image_optim.rb +++ b/lib/image_optim.rb @@ -175,9 +175,9 @@ def resolve_bin!(bin) symlink.make_symlink(File.expand_path(path)) at_exit{ symlink.unlink } - @resolved_bins[bin] = system(*%W[which -s #{symlink}]) + @resolved_bins[bin] = bin_accessible?(symlink) else - @resolved_bins[bin] = system(*%W[which -s #{bin}]) + @resolved_bins[bin] = bin_accessible?(bin) end end end @@ -208,6 +208,11 @@ def apply_threading(array) end end + # Check if bin can be accessed + def bin_accessible?(bin) + system(*%W[which -s #{bin}]) + end + # http://stackoverflow.com/questions/891537/ruby-detect-number-of-cpus-installed def processor_count @processor_count ||= case host_os = RbConfig::CONFIG['host_os'] diff --git a/spec/image_optim_spec.rb b/spec/image_optim_spec.rb index 363e5910..afbdd52b 100644 --- a/spec/image_optim_spec.rb +++ b/spec/image_optim_spec.rb @@ -196,7 +196,7 @@ def with_env(key, value) it "should resolve bin in path" do with_env 'LS_BIN', nil do image_optim = ImageOptim.new - image_optim.should_receive(:system).with(*%w[which -s ls]).once.and_return(true) + image_optim.should_receive(:bin_accessible?).with(:ls).once.and_return(true) FSPath.should_not_receive(:temp_dir) 5.times do @@ -212,7 +212,7 @@ def with_env(key, value) symlink = stub(:symlink) image_optim = ImageOptim.new - image_optim.should_receive(:system).with(*%W[which -s #{symlink}]).once.and_return(true) + image_optim.should_receive(:bin_accessible?).with(symlink).once.and_return(true) FSPath.should_receive(:temp_dir).once.and_return(tmpdir) tmpdir.should_receive(:/).with(:image_optim).once.and_return(symlink) symlink.should_receive(:make_symlink).with(File.expand_path(path)).once @@ -235,7 +235,7 @@ def with_env(key, value) it "should raise on failure to resolve bin" do with_env 'SHOULD_NOT_EXIST_BIN', nil do image_optim = ImageOptim.new - image_optim.should_receive(:system).with(*%w[which -s should_not_exist]).once.and_return(false) + image_optim.should_receive(:bin_accessible?).with(:should_not_exist).once.and_return(false) FSPath.should_not_receive(:temp_dir) 5.times do @@ -253,7 +253,7 @@ def with_env(key, value) symlink = stub(:symlink) image_optim = ImageOptim.new - image_optim.should_receive(:system).with(*%W[which -s #{symlink}]).once.and_return(false) + image_optim.should_receive(:bin_accessible?).with(symlink).once.and_return(false) FSPath.should_receive(:temp_dir).once.and_return(tmpdir) tmpdir.should_receive(:/).with(:should_not_exist).once.and_return(symlink) symlink.should_receive(:make_symlink).with(File.expand_path(path)).once From 049b56a4e23367a2d9dc4bd68490ca9f6d6d2263 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 16 Jan 2013 23:54:56 +0100 Subject: [PATCH 4/6] -s option of which command is custom and should not be used, closes #7 --- lib/image_optim.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/image_optim.rb b/lib/image_optim.rb index ca16470f..21a2c45c 100644 --- a/lib/image_optim.rb +++ b/lib/image_optim.rb @@ -210,7 +210,7 @@ def apply_threading(array) # Check if bin can be accessed def bin_accessible?(bin) - system(*%W[which -s #{bin}]) + `which #{bin.to_s.shellescape}` != '' end # http://stackoverflow.com/questions/891537/ruby-detect-number-of-cpus-installed From b6fa79f95424d2aea17fd80f7b2052174729db2d Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Thu, 17 Jan 2013 00:44:39 +0100 Subject: [PATCH 5/6] use `env` and `nice` commands + io redirection instead of forking and setting env, priority and reopening io using ruby methods, closes #8 --- lib/image_optim/worker.rb | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/image_optim/worker.rb b/lib/image_optim/worker.rb index b6685fb2..f64e75f9 100644 --- a/lib/image_optim/worker.rb +++ b/lib/image_optim/worker.rb @@ -60,15 +60,10 @@ def execute(bin, *arguments) resolve_bin!(bin) command = [bin, *arguments].map(&:to_s).shelljoin + env_path = "#{@image_optim.resolve_dir}:#{ENV['PATH']}" start = Time.now - Process.wait(fork do - ENV['PATH'] = "#{@image_optim.resolve_dir}:#{ENV['PATH']}" - $stdout.reopen('/dev/null', 'w') - $stderr.reopen('/dev/null', 'w') - Process.setpriority(Process::PRIO_PROCESS, 0, @image_optim.nice) - exec command - end) + system "env PATH=#{env_path.shellescape} nice -n #{@image_optim.nice} #{command} >& /dev/null" raise SignalException.new($?.termsig) if $?.signaled? From fb07f5cdd6c173d6cd40f1278f3392bbb267e5d2 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Thu, 17 Jan 2013 01:37:01 +0100 Subject: [PATCH 6/6] v0.7.0 --- image_optim.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/image_optim.gemspec b/image_optim.gemspec index 875c2c9f..3afd2ca4 100644 --- a/image_optim.gemspec +++ b/image_optim.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |s| s.name = 'image_optim' - s.version = '0.6.0' + s.version = '0.7.0' s.summary = %q{Optimize (lossless compress) images (jpeg, png, gif) using external utilities (advpng, gifsicle, jpegoptim, jpegtran, optipng, pngcrush, pngout)} s.homepage = "http://github.com/toy/#{s.name}" s.authors = ['Ivan Kuchin']