summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Walters <cw@opscode.com>2010-06-23 15:45:33 -0700
committerChris Walters <cw@opscode.com>2010-06-23 15:45:33 -0700
commita67d264c0c35756532e58722b98bfce2451ff57f (patch)
tree6e911d3031957e69c22f988f5ad5d29fa0315991
parent329fc8e5173eba810a53115228a684ddb187824a (diff)
downloadchef-a67d264c0c35756532e58722b98bfce2451ff57f.tar.gz
Splitting monkey patches out into their own files
-rw-r--r--chef/lib/chef.rb76
-rw-r--r--chef/lib/chef/monkey_patches/dir.rb18
-rw-r--r--chef/lib/chef/monkey_patches/string.rb9
-rw-r--r--chef/lib/chef/monkey_patches/tempfile.rb42
-rw-r--r--chef/lib/chef/rest.rb1
5 files changed, 73 insertions, 73 deletions
diff --git a/chef/lib/chef.rb b/chef/lib/chef.rb
index f38947b380..8e477693a3 100644
--- a/chef/lib/chef.rb
+++ b/chef/lib/chef.rb
@@ -34,76 +34,6 @@ require 'chef/run_status'
require 'chef/handler'
require 'chef/handler/json_file'
-# Adds a Dir.glob to Ruby 1.8.5, for compat
-if RUBY_VERSION < "1.8.6" || RUBY_PLATFORM =~ /mswin|mingw32|windows/
- class Dir
- class << self
- alias_method :glob_, :glob
- def glob(pattern, flags=0)
- raise ArgumentError unless (
- !pattern.nil? and (
- pattern.is_a? Array and !pattern.empty?
- ) or pattern.is_a? String
- )
- pattern.gsub!(/\\/, "/") if RUBY_PLATFORM =~ /mswin|mingw32|windows/
- [pattern].flatten.inject([]) { |r, p| r + glob_(p, flags) }
- end
- alias_method :[], :glob
- end
- end
-end
-
-
-# On ruby 1.9, Strings are aware of multibyte characters, so #size and length
-# give the actual number of characters. In Chef::REST, we need the bytesize
-# so we can correctly set the Content-Length headers, but ruby 1.8.6 and lower
-# don't define String#bytesize. Monkey patching time!
-class String
- unless method_defined?(:bytesize)
- alias :bytesize :size
- end
-end
-
-
-# Tempfile has a horrible bug where it causes an IOError: closed stream in its
-# finalizer, leading to intermittent application crashes with confusing stack
-# traces. Here we monkey patch the fix into place. You can track the bug on
-# ruby's redmine: http://redmine.ruby-lang.org/issues/show/3119
-class Tempfile
- # Tempfile has changes between 1.8.x and 1.9.x
- # so we monkey patch separately
- if RUBY_VERSION =~ /^1\.8/
- def unlink
- # keep this order for thread safeness
- begin
- File.unlink(@tmpname) if File.exist?(@tmpname)
- @@cleanlist.delete(@tmpname)
- @tmpname = nil
- ObjectSpace.undefine_finalizer(self)
- rescue Errno::EACCES
- # may not be able to unlink on Windows; just ignore
- end
- end
- alias delete unlink
-
-
- # There is a patch for this, to be merged into 1.9 at some point.
- # When that happens, we'll want to also check the RUBY_PATCHLEVEL
- elsif RUBY_VERSION =~ /^1\.9/
- def unlink
- # keep this order for thread safeness
- return unless @tmpname
- begin
- if File.exist?(@tmpname)
- File.unlink(@tmpname)
- end
- # remove tmpname from remover
- @data[0] = @data[2] = nil
- @tmpname = nil
- rescue Errno::EACCES
- # may not be able to unlink on Windows; just ignore
- end
- end
- alias delete unlink
- end
-end
+require 'chef/monkey_patches/tempfile'
+require 'chef/monkey_patches/dir'
+require 'chef/monkey_patches/string'
diff --git a/chef/lib/chef/monkey_patches/dir.rb b/chef/lib/chef/monkey_patches/dir.rb
new file mode 100644
index 0000000000..5dbffb7eab
--- /dev/null
+++ b/chef/lib/chef/monkey_patches/dir.rb
@@ -0,0 +1,18 @@
+# Adds a Dir.glob to Ruby 1.8.5, for compat
+if RUBY_VERSION < "1.8.6" || RUBY_PLATFORM =~ /mswin|mingw32|windows/
+ class Dir
+ class << self
+ alias_method :glob_, :glob
+ def glob(pattern, flags=0)
+ raise ArgumentError unless (
+ !pattern.nil? and (
+ pattern.is_a? Array and !pattern.empty?
+ ) or pattern.is_a? String
+ )
+ pattern.gsub!(/\\/, "/") if RUBY_PLATFORM =~ /mswin|mingw32|windows/
+ [pattern].flatten.inject([]) { |r, p| r + glob_(p, flags) }
+ end
+ alias_method :[], :glob
+ end
+ end
+end
diff --git a/chef/lib/chef/monkey_patches/string.rb b/chef/lib/chef/monkey_patches/string.rb
new file mode 100644
index 0000000000..c5fc954b2c
--- /dev/null
+++ b/chef/lib/chef/monkey_patches/string.rb
@@ -0,0 +1,9 @@
+# On ruby 1.9, Strings are aware of multibyte characters, so #size and length
+# give the actual number of characters. In Chef::REST, we need the bytesize
+# so we can correctly set the Content-Length headers, but ruby 1.8.6 and lower
+# don't define String#bytesize. Monkey patching time!
+class String
+ unless method_defined?(:bytesize)
+ alias :bytesize :size
+ end
+end
diff --git a/chef/lib/chef/monkey_patches/tempfile.rb b/chef/lib/chef/monkey_patches/tempfile.rb
new file mode 100644
index 0000000000..52a61ec507
--- /dev/null
+++ b/chef/lib/chef/monkey_patches/tempfile.rb
@@ -0,0 +1,42 @@
+# Tempfile has a horrible bug where it causes an IOError: closed stream in its
+# finalizer, leading to intermittent application crashes with confusing stack
+# traces. Here we monkey patch the fix into place. You can track the bug on
+# ruby's redmine: http://redmine.ruby-lang.org/issues/show/3119
+class Tempfile
+ # Tempfile has changes between 1.8.x and 1.9.x
+ # so we monkey patch separately
+ if RUBY_VERSION =~ /^1\.8/
+ def unlink
+ # keep this order for thread safeness
+ begin
+ File.unlink(@tmpname) if File.exist?(@tmpname)
+ @@cleanlist.delete(@tmpname)
+ @tmpname = nil
+ ObjectSpace.undefine_finalizer(self)
+ rescue Errno::EACCES
+ # may not be able to unlink on Windows; just ignore
+ end
+ end
+ alias delete unlink
+
+
+ # There is a patch for this, to be merged into 1.9 at some point.
+ # When that happens, we'll want to also check the RUBY_PATCHLEVEL
+ elsif RUBY_VERSION =~ /^1\.9/
+ def unlink
+ # keep this order for thread safeness
+ return unless @tmpname
+ begin
+ if File.exist?(@tmpname)
+ File.unlink(@tmpname)
+ end
+ # remove tmpname from remover
+ @data[0] = @data[2] = nil
+ @tmpname = nil
+ rescue Errno::EACCES
+ # may not be able to unlink on Windows; just ignore
+ end
+ end
+ alias delete unlink
+ end
+end
diff --git a/chef/lib/chef/rest.rb b/chef/lib/chef/rest.rb
index 3c7100c388..d4813d12cf 100644
--- a/chef/lib/chef/rest.rb
+++ b/chef/lib/chef/rest.rb
@@ -27,6 +27,7 @@ require 'tempfile'
require 'chef/api_client'
require 'chef/rest/auth_credentials'
require 'chef/rest/rest_request'
+require 'chef/monkey_patches/string'
class Chef
class REST