Skip to content

Commit

Permalink
Merge branch 'toy-master'
Browse files Browse the repository at this point in the history
Conflicts:
	lib/image_optim.rb
	lib/image_optim/worker.rb
  • Loading branch information
Chris Thompson committed Jan 17, 2013
2 parents c2a9e4a + fb07f5c commit 36cabd7
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
12 changes: 9 additions & 3 deletions bin/image_optim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,16 +31,16 @@ 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|
options[bin] = enable
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
Expand Down
2 changes: 1 addition & 1 deletion image_optim.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
17 changes: 11 additions & 6 deletions lib/image_optim.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 #{symlink}])
@resolved_bins[bin] = bin_accessible?(symlink)
else
@resolved_bins[bin] = system(*%W[which #{bin}])
@resolved_bins[bin] = bin_accessible?(bin)
end
end
end
Expand Down Expand Up @@ -208,6 +208,11 @@ def apply_threading(array)
end
end

# Check if bin can be accessed
def bin_accessible?(bin)
`which #{bin.to_s.shellescape}` != ''
end

# http://stackoverflow.com/questions/891537/ruby-detect-number-of-cpus-installed
def processor_count
@processor_count ||= case host_os = RbConfig::CONFIG['host_os']
Expand Down
2 changes: 2 additions & 0 deletions lib/image_optim/image_path.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/image_optim/option_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'image_optim'

class ImageOptim
module OptionHelpers
# Remove option from hash and run through block or return default
Expand Down
10 changes: 3 additions & 7 deletions lib/image_optim/worker.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: UTF-8

require 'shellwords'

require 'image_optim'

class ImageOptim
Expand Down Expand Up @@ -59,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')
$stderr.reopen('/dev/null')
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?

Expand Down
8 changes: 4 additions & 4 deletions spec/image_optim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 36cabd7

Please sign in to comment.