diff options
author | Seth Chisamore <schisamo@opscode.com> | 2012-10-30 10:39:35 -0400 |
---|---|---|
committer | Seth Chisamore <schisamo@opscode.com> | 2012-10-30 10:39:35 -0400 |
commit | 24dc69a9a97e82a6e4207de68d6dcc664178249b (patch) | |
tree | 19bb289c9f88b4bbab066bc56b95d6d222fd5c35 /lib/chef/monkey_patches | |
parent | 9348c1c9c80ee757354d624b7dc1b78ebc7605c4 (diff) | |
download | chef-24dc69a9a97e82a6e4207de68d6dcc664178249b.tar.gz |
[OC-3564] move core Chef to the repo root \o/ \m/
The opscode/chef repository now only contains the core Chef library code
used by chef-client, knife and chef-solo!
Diffstat (limited to 'lib/chef/monkey_patches')
-rw-r--r-- | lib/chef/monkey_patches/dir.rb | 36 | ||||
-rw-r--r-- | lib/chef/monkey_patches/moneta.rb | 50 | ||||
-rw-r--r-- | lib/chef/monkey_patches/net_http.rb | 22 | ||||
-rw-r--r-- | lib/chef/monkey_patches/numeric.rb | 15 | ||||
-rw-r--r-- | lib/chef/monkey_patches/object.rb | 9 | ||||
-rw-r--r-- | lib/chef/monkey_patches/regexp.rb | 34 | ||||
-rw-r--r-- | lib/chef/monkey_patches/string.rb | 49 | ||||
-rw-r--r-- | lib/chef/monkey_patches/tempfile.rb | 64 |
8 files changed, 279 insertions, 0 deletions
diff --git a/lib/chef/monkey_patches/dir.rb b/lib/chef/monkey_patches/dir.rb new file mode 100644 index 0000000000..c86edcf013 --- /dev/null +++ b/lib/chef/monkey_patches/dir.rb @@ -0,0 +1,36 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +if RUBY_VERSION < "1.8.6" || RUBY_PLATFORM =~ /mswin|mingw32|windows/ + class Dir + class << self + alias_method :glob_, :glob + # Adds a Dir.glob to Ruby 1.8.5, for compat + 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/lib/chef/monkey_patches/moneta.rb b/lib/chef/monkey_patches/moneta.rb new file mode 100644 index 0000000000..1c2895db56 --- /dev/null +++ b/lib/chef/monkey_patches/moneta.rb @@ -0,0 +1,50 @@ +# +# Author:: Seth Chisamore (<schisamo@opscode.com>) +# Copyright:: Copyright (c) 2011 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# ensure data is written and read in binary mode +# stops "dump format error for symbol(0x75)" errors +module Moneta + class BasicFile + + def store(key, value, options = {}) + ensure_directory_created(::File.dirname(path(key))) + ::File.open(path(key), "wb") do |file| + if @expires + data = {:value => value} + if options[:expires_in] + data[:expires_at] = Time.now + options[:expires_in] + end + contents = Marshal.dump(data) + else + contents = Marshal.dump(value) + end + file.puts(contents) + end + end + + def raw_get(key) + if ::File.respond_to?(:binread) + data = ::File.binread(path(key)) + else + data = ::File.open(path(key),"rb") { |f| f.read } + end + Marshal.load(data) + end + + end +end diff --git a/lib/chef/monkey_patches/net_http.rb b/lib/chef/monkey_patches/net_http.rb new file mode 100644 index 0000000000..ad4ba957f6 --- /dev/null +++ b/lib/chef/monkey_patches/net_http.rb @@ -0,0 +1,22 @@ + +# Module gets mixed in to Net::HTTP exception classes so we can attach our +# RESTRequest object to them and get the request parameters back out later. +module ChefNetHTTPExceptionExtensions + attr_accessor :chef_rest_request +end + +require 'net/http' +module Net + class HTTPError + include ChefNetHTTPExceptionExtensions + end + class HTTPRetriableError + include ChefNetHTTPExceptionExtensions + end + class HTTPServerException + include ChefNetHTTPExceptionExtensions + end + class HTTPFatalError + include ChefNetHTTPExceptionExtensions + end +end diff --git a/lib/chef/monkey_patches/numeric.rb b/lib/chef/monkey_patches/numeric.rb new file mode 100644 index 0000000000..1f5ff14209 --- /dev/null +++ b/lib/chef/monkey_patches/numeric.rb @@ -0,0 +1,15 @@ +unless 0.respond_to?(:fdiv) + class Numeric + def fdiv(other) + to_f / other + end + end +end + +# String elements referenced with [] <= 1.8.6 return a Fixnum. Cheat to allow +# for the simpler "test"[2].ord construct +class Numeric + def ord + return self + end +end diff --git a/lib/chef/monkey_patches/object.rb b/lib/chef/monkey_patches/object.rb new file mode 100644 index 0000000000..017a4b7938 --- /dev/null +++ b/lib/chef/monkey_patches/object.rb @@ -0,0 +1,9 @@ +class Object + unless new.respond_to?(:tap) + def tap + yield self + return self + end + end +end + diff --git a/lib/chef/monkey_patches/regexp.rb b/lib/chef/monkey_patches/regexp.rb new file mode 100644 index 0000000000..9304209edf --- /dev/null +++ b/lib/chef/monkey_patches/regexp.rb @@ -0,0 +1,34 @@ +# Copyright (c) 2009 Marc-Andre Lafortune +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +class Regexp + # Standard in Ruby 1.8.7+. See official documentation[http://www.ruby-doc.org/core-1.8.7/classes/Regexp.html] + class << self + unless (union(%w(a b)) rescue false) + alias :union_without_array_argument :union + + def union(*arg) + return union_without_array_argument(*arg) unless arg.size == 1 + union_without_array_argument(*arg.first) + end + end + end +end
\ No newline at end of file diff --git a/lib/chef/monkey_patches/string.rb b/lib/chef/monkey_patches/string.rb new file mode 100644 index 0000000000..c77c5c8816 --- /dev/null +++ b/lib/chef/monkey_patches/string.rb @@ -0,0 +1,49 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# == String (Patch) +# 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! + +begin + require 'enumerator' +rescue LoadError +end + +class String + unless method_defined?(:bytesize) + alias :bytesize :size + end + + unless method_defined?(:lines) + def lines + enum_for(:each) + end + end +end + +# <= 1.8.6 needs some ord! +class String + unless method_defined?(:ord) + def ord + self.unpack('C').first + end + end +end diff --git a/lib/chef/monkey_patches/tempfile.rb b/lib/chef/monkey_patches/tempfile.rb new file mode 100644 index 0000000000..3135fb1a00 --- /dev/null +++ b/lib/chef/monkey_patches/tempfile.rb @@ -0,0 +1,64 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# == Tempfile (Patch) +# 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 +# +# The patch is slightly different for Ruby 1.8 and Ruby 1.9, both patches are +# included here. +class Tempfile # :nodoc: + # 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 |