diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet.rb | 12 | ||||
-rw-r--r-- | lib/puppet/external/dot.rb | 8 | ||||
-rwxr-xr-x | lib/puppet/external/nagios.rb | 40 | ||||
-rw-r--r-- | lib/puppet/external/nagios/grammar.ry | 186 | ||||
-rw-r--r-- | lib/puppet/parser/functions/regsubst.rb | 64 | ||||
-rw-r--r-- | lib/puppet/parser/functions/sprintf.rb | 12 | ||||
-rw-r--r-- | lib/puppet/provider/package/appdmg.rb | 10 | ||||
-rwxr-xr-x | lib/puppet/provider/package/apt.rb | 6 | ||||
-rwxr-xr-x | lib/puppet/provider/package/aptitude.rb | 2 | ||||
-rwxr-xr-x | lib/puppet/provider/package/fink.rb | 6 | ||||
-rw-r--r-- | lib/puppet/provider/package/portage.rb | 2 | ||||
-rw-r--r-- | lib/puppet/provider/package/yumhelper.py | 2 | ||||
-rwxr-xr-x | lib/puppet/provider/service/redhat.rb | 2 | ||||
-rw-r--r-- | lib/puppet/provider/user/hpux.rb | 4 | ||||
-rwxr-xr-x | lib/puppet/sslcertificates.rb | 2 | ||||
-rw-r--r-- | lib/puppet/type.rb | 2 | ||||
-rw-r--r-- | lib/puppet/type/file/target.rb | 2 | ||||
-rwxr-xr-x | lib/puppet/type/mount.rb | 2 | ||||
-rw-r--r-- | lib/puppet/type/service.rb | 2 | ||||
-rwxr-xr-x | lib/puppet/type/tidy.rb | 6 | ||||
-rw-r--r-- | lib/puppet/util.rb | 2 | ||||
-rw-r--r-- | lib/puppet/util/log.rb | 30 | ||||
-rw-r--r-- | lib/puppet/util/pidlock.rb | 118 |
23 files changed, 261 insertions, 261 deletions
diff --git a/lib/puppet.rb b/lib/puppet.rb index b216503c2..6e7aa2318 100644 --- a/lib/puppet.rb +++ b/lib/puppet.rb @@ -71,8 +71,8 @@ module Puppet @@settings.setdefaults(section, hash) end - # configuration parameter access and stuff - def self.[](param) + # configuration parameter access and stuff + def self.[](param) case param when :debug if Puppet::Util::Log.level == :debug @@ -83,12 +83,12 @@ module Puppet else return @@settings[param] end - end + end - # configuration parameter access and stuff - def self.[]=(param,value) + # configuration parameter access and stuff + def self.[]=(param,value) @@settings[param] = value - end + end def self.clear @@settings.clear diff --git a/lib/puppet/external/dot.rb b/lib/puppet/external/dot.rb index b94568c12..caeee21ca 100644 --- a/lib/puppet/external/dot.rb +++ b/lib/puppet/external/dot.rb @@ -229,7 +229,7 @@ module DOT t + "]\n" end - end # class DOTNode + end # class DOTNode # A subgraph element is the same to graph, but has another header in dot # notation. @@ -276,7 +276,7 @@ module DOT hdr + options + nodes + t + "}\n" end - end # class DOTSubgraph + end # class DOTSubgraph # This is a graph. @@ -287,7 +287,7 @@ module DOT @dot_string = 'digraph' end - end # class DOTDigraph + end # class DOTDigraph # This is an edge. @@ -314,7 +314,7 @@ module DOT }.compact.join( "\n" ) + "\n" + t + "]\n" end - end # class DOTEdge + end # class DOTEdge class DOTDirectedEdge < DOTEdge diff --git a/lib/puppet/external/nagios.rb b/lib/puppet/external/nagios.rb index 0dcae4c6d..d7f64d59b 100755 --- a/lib/puppet/external/nagios.rb +++ b/lib/puppet/external/nagios.rb @@ -27,24 +27,24 @@ module Nagios NAGIOSVERSION end - class Config - def Config.import(config) - - text = String.new - - File.open(config) { |file| - file.each { |line| - text += line - } - } - parser = Nagios::Parser.new - return parser.parse(text) - end - - def Config.each - Nagios::Object.objects.each { |object| - yield object - } - end - end + class Config + def Config.import(config) + + text = String.new + + File.open(config) { |file| + file.each { |line| + text += line + } + } + parser = Nagios::Parser.new + return parser.parse(text) + end + + def Config.each + Nagios::Object.objects.each { |object| + yield object + } + end + end end diff --git a/lib/puppet/external/nagios/grammar.ry b/lib/puppet/external/nagios/grammar.ry index 87c909280..dc203be5c 100644 --- a/lib/puppet/external/nagios/grammar.ry +++ b/lib/puppet/external/nagios/grammar.ry @@ -5,51 +5,51 @@ token DEFINE NAME STRING PARAM LCURLY RCURLY VALUE RETURN COMMENT INLINECOMMENT rule decls: decl { return val[0] if val[0] } - | decls decl { - if val[1].nil? - result = val[0] - else - if val[0].nil? - result = val[1] - else - result = [ val[0], val[1] ].flatten - end - end - } - ; + | decls decl { + if val[1].nil? + result = val[0] + else + if val[0].nil? + result = val[1] + else + result = [ val[0], val[1] ].flatten + end + end + } + ; decl: object { result = [val[0]] } - | RETURN { result = nil } - | comment - ; - + | RETURN { result = nil } + | comment + ; + comment: COMMENT RETURN { result = nil } - ; + ; object: DEFINE NAME LCURLY RETURN vars RCURLY { - result = Nagios::Base.create(val[1],val[4]) - } - ; + result = Nagios::Base.create(val[1],val[4]) + } + ; vars: var - | vars var { - val[1].each {|p,v| - val[0][p] = v - } - result = val[0] - } - ; + | vars var { + val[1].each {|p,v| + val[0][p] = v + } + result = val[0] + } + ; var: PARAM VALUE icomment returns { result = {val[0],val[1]} } - ; + ; returns: RETURN | returns RETURN ; icomment: # nothing - | INLINECOMMENT - ; + | INLINECOMMENT + ; end @@ -58,61 +58,61 @@ end class ::Nagios::Parser::SyntaxError < RuntimeError; end def parse(src) - @src = src + @src = src - # state variables - @invar = false - @inobject = false - @done = false + # state variables + @invar = false + @inobject = false + @done = false - @line = 0 - @yydebug = true + @line = 0 + @yydebug = true do_parse end # The lexer. Very simple. def token - @src.sub!(/\A\n/,'') - if $& - @line += 1 - return [ :RETURN, "\n" ] - end - - if @done - return nil - end - yytext = String.new - - - # remove comments from this line - @src.sub!(/\A[ \t]*;.*\n/,"\n") - if $& - return [:INLINECOMMENT, ""] - end - - @src.sub!(/\A#.*\n/,"\n") - if $& - return [:COMMENT, ""] - end - - @src.sub!(/#.*/,'') - - if @src.length == 0 - @done = true - return [false, '$'] - end - - if @invar - @src.sub!(/\A[ \t]+/,'') - @src.sub!(/\A([^;\n]+)(\n|;)/,'\2') - if $1 - yytext += $1 - end - @invar = false - return [:VALUE, yytext] - else - @src.sub!(/\A[\t ]*(\S+)([\t ]*|$)/,'') + @src.sub!(/\A\n/,'') + if $& + @line += 1 + return [ :RETURN, "\n" ] + end + + if @done + return nil + end + yytext = String.new + + + # remove comments from this line + @src.sub!(/\A[ \t]*;.*\n/,"\n") + if $& + return [:INLINECOMMENT, ""] + end + + @src.sub!(/\A#.*\n/,"\n") + if $& + return [:COMMENT, ""] + end + + @src.sub!(/#.*/,'') + + if @src.length == 0 + @done = true + return [false, '$'] + end + + if @invar + @src.sub!(/\A[ \t]+/,'') + @src.sub!(/\A([^;\n]+)(\n|;)/,'\2') + if $1 + yytext += $1 + end + @invar = false + return [:VALUE, yytext] + else + @src.sub!(/\A[\t ]*(\S+)([\t ]*|$)/,'') if $1 yytext = $1 case yytext @@ -152,11 +152,11 @@ def token end end end - end + end end def next_token - token + token end def yydebug @@ -168,18 +168,18 @@ def yywrap end def on_error(token, value, vstack ) - msg = "" - unless value.nil? - msg = "line #{@line}: syntax error at '#{value}'" - else - msg = "line #{@line}: syntax error at '#{token}'" - end - unless @src.size > 0 - msg = "line #{@line}: Unexpected end of file" - end - if token == '$end'.intern - puts "okay, this is silly" - else - raise ::Nagios::Parser::SyntaxError, msg - end + msg = "" + unless value.nil? + msg = "line #{@line}: syntax error at '#{value}'" + else + msg = "line #{@line}: syntax error at '#{token}'" + end + unless @src.size > 0 + msg = "line #{@line}: Unexpected end of file" + end + if token == '$end'.intern + puts "okay, this is silly" + else + raise ::Nagios::Parser::SyntaxError, msg + end end diff --git a/lib/puppet/parser/functions/regsubst.rb b/lib/puppet/parser/functions/regsubst.rb index 2bd39200d..8a1a244ab 100644 --- a/lib/puppet/parser/functions/regsubst.rb +++ b/lib/puppet/parser/functions/regsubst.rb @@ -1,6 +1,6 @@ module Puppet::Parser::Functions newfunction(:regsubst, :type => :rvalue, - :doc => " + :doc => " Perform regexp replacement on a string. - **Parameters** (in order): @@ -36,36 +36,36 @@ Put angle brackets around each octet in the node's IP address:: $x = regsubst($ipaddress, '([0-9]+)', '<\\1>', 'G') ") \ do |args| - flag_mapping = { - "E" => Regexp::EXTENDED, - "I" => Regexp::IGNORECASE, - "M" => Regexp::MULTILINE, - } - if args.length < 3 or args.length > 5 - raise Puppet::ParseError, ("regsub(): wrong number of arguments" + - " (#{args.length}; min 3, max 5)") - end - str, regexp, replacement, flags, lang = args - reflags = 0 - global = false - (flags or "").each_byte do |f| - f = f.chr - if f == "G" - global = true - else - fvalue = flag_mapping[f] - if !fvalue - raise Puppet::ParseError, "regsub(): bad flag `#{f}'" - end - reflags |= fvalue - end - end - re = Regexp.compile(regexp, reflags, lang) - if global - result = str.gsub(re, replacement) - else - result = str.sub(re, replacement) - end - return result + flag_mapping = { + "E" => Regexp::EXTENDED, + "I" => Regexp::IGNORECASE, + "M" => Regexp::MULTILINE, + } + if args.length < 3 or args.length > 5 + raise Puppet::ParseError, ("regsub(): wrong number of arguments" + + " (#{args.length}; min 3, max 5)") + end + str, regexp, replacement, flags, lang = args + reflags = 0 + global = false + (flags or "").each_byte do |f| + f = f.chr + if f == "G" + global = true + else + fvalue = flag_mapping[f] + if !fvalue + raise Puppet::ParseError, "regsub(): bad flag `#{f}'" + end + reflags |= fvalue + end + end + re = Regexp.compile(regexp, reflags, lang) + if global + result = str.gsub(re, replacement) + else + result = str.sub(re, replacement) + end + return result end end diff --git a/lib/puppet/parser/functions/sprintf.rb b/lib/puppet/parser/functions/sprintf.rb index f6daeff49..d84b1866a 100644 --- a/lib/puppet/parser/functions/sprintf.rb +++ b/lib/puppet/parser/functions/sprintf.rb @@ -1,12 +1,12 @@ module Puppet::Parser::Functions newfunction(:sprintf, :type => :rvalue, - :doc => "Perform printf-style formatting of text. + :doc => "Perform printf-style formatting of text. The first parameter is format string describing how the rest of the parameters should be formatted. See the documentation for the ``Kernel::sprintf()`` function in Ruby for all the details.") do |args| - if args.length < 1 - raise Puppet::ParseError, 'sprintf() needs at least one argument' - end - fmt = args.shift() - return sprintf(fmt, *args) + if args.length < 1 + raise Puppet::ParseError, 'sprintf() needs at least one argument' + end + fmt = args.shift() + return sprintf(fmt, *args) end end diff --git a/lib/puppet/provider/package/appdmg.rb b/lib/puppet/provider/package/appdmg.rb index ee8726cbc..4b9730c1c 100644 --- a/lib/puppet/provider/package/appdmg.rb +++ b/lib/puppet/provider/package/appdmg.rb @@ -20,7 +20,7 @@ Puppet::Type.type(:package).provide(:appdmg, :parent => Puppet::Provider::Packag commands :hdiutil => "/usr/bin/hdiutil" commands :curl => "/usr/bin/curl" commands :ditto => "/usr/bin/ditto" - + # JJM We store a cookie for each installed .app.dmg in /var/db def self.instances_by_name Dir.entries("/var/db").find_all { |f| @@ -40,7 +40,7 @@ Puppet::Type.type(:package).provide(:appdmg, :parent => Puppet::Provider::Packag def self.installapp(source, name, orig_source) appname = File.basename(source); - ditto "--rsrc", source, "/Applications/#{appname}" + ditto "--rsrc", source, "/Applications/#{appname}" File.open("/var/db/.puppet_appdmg_installed_#{name}", "w") do |t| t.print "name: '#{name}'\n" t.print "source: '#{orig_source}'\n" @@ -77,9 +77,9 @@ Puppet::Type.type(:package).provide(:appdmg, :parent => Puppet::Provider::Packag mounts.each do |fspath| Dir.entries(fspath).select { |f| f =~ /\.app$/i - }.each do |pkg| - installapp("#{fspath}/#{pkg}", name, source) - end + }.each do |pkg| + installapp("#{fspath}/#{pkg}", name, source) + end end # mounts.each do ensure hdiutil "eject", mounts[0] diff --git a/lib/puppet/provider/package/apt.rb b/lib/puppet/provider/package/apt.rb index a99ee4c57..ee645242e 100755 --- a/lib/puppet/provider/package/apt.rb +++ b/lib/puppet/provider/package/apt.rb @@ -85,9 +85,9 @@ Puppet::Type.type(:package).provide :apt, :parent => :dpkg, :source => :dpkg do end end - # - # preseeds answers to dpkg-set-selection from the "responsefile" - # + # + # preseeds answers to dpkg-set-selection from the "responsefile" + # def run_preseed if response = @resource[:responsefile] and FileTest.exist?(response) self.info("Preseeding %s to debconf-set-selections" % response) diff --git a/lib/puppet/provider/package/aptitude.rb b/lib/puppet/provider/package/aptitude.rb index 699df0e51..0842856c1 100755 --- a/lib/puppet/provider/package/aptitude.rb +++ b/lib/puppet/provider/package/aptitude.rb @@ -26,6 +26,6 @@ Puppet::Type.type(:package).provide :aptitude, :parent => :apt, :source => :dpkg def purge aptitude '-y', 'purge', @resource[:name] - end + end end diff --git a/lib/puppet/provider/package/fink.rb b/lib/puppet/provider/package/fink.rb index 4d560666b..1a779f083 100755 --- a/lib/puppet/provider/package/fink.rb +++ b/lib/puppet/provider/package/fink.rb @@ -56,9 +56,9 @@ Puppet::Type.type(:package).provide :fink, :parent => :dpkg, :source => :dpkg do end end - # - # preseeds answers to dpkg-set-selection from the "responsefile" - # + # + # preseeds answers to dpkg-set-selection from the "responsefile" + # def run_preseed if response = @resource[:responsefile] and FileTest.exists?(response) self.info("Preseeding %s to debconf-set-selections" % response) diff --git a/lib/puppet/provider/package/portage.rb b/lib/puppet/provider/package/portage.rb index ae7194f89..4a09a2385 100644 --- a/lib/puppet/provider/package/portage.rb +++ b/lib/puppet/provider/package/portage.rb @@ -100,7 +100,7 @@ Puppet::Type.type(:package).provide :portage, :parent => Puppet::Provider::Packa case packages.size when 0 - not_found_value = "%s/%s" % [@resource[:category] ? @resource[:category] : "<unspecified category>", @resource[:name]] + not_found_value = "%s/%s" % [@resource[:category] ? @resource[:category] : "<unspecified category>", @resource[:name]] raise Puppet::Error.new("No package found with the specified name [#{not_found_value}]") when 1 return packages[0] diff --git a/lib/puppet/provider/package/yumhelper.py b/lib/puppet/provider/package/yumhelper.py index 6263d3473..7d73cdd23 100644 --- a/lib/puppet/provider/package/yumhelper.py +++ b/lib/puppet/provider/package/yumhelper.py @@ -43,7 +43,7 @@ def pkg_lists(my): # Yum 2.2/2.3 python libraries require a couple of extra function calls to setup package sacks. # They also don't have a __version__ attribute try: - yumver = yum.__version__ + yumver = yum.__version__ except AttributeError: my.doRepoSetup() my.doSackSetup() diff --git a/lib/puppet/provider/service/redhat.rb b/lib/puppet/provider/service/redhat.rb index f31903e84..02ff96b2a 100755 --- a/lib/puppet/provider/service/redhat.rb +++ b/lib/puppet/provider/service/redhat.rb @@ -37,7 +37,7 @@ Puppet::Type.type(:service).provide :redhat, :parent => :init do if output =~ /.* off$/ return :false end - + return :true end diff --git a/lib/puppet/provider/user/hpux.rb b/lib/puppet/provider/user/hpux.rb index 672b77da6..76549ec69 100644 --- a/lib/puppet/provider/user/hpux.rb +++ b/lib/puppet/provider/user/hpux.rb @@ -20,11 +20,11 @@ Puppet::Type.type(:user).provide :hpuxuseradd, :parent => :useradd do has_features :manages_homedir, :allows_duplicates def deletecmd - super.insert(1,"-F") + super.insert(1,"-F") end def modifycmd(param,value) - super.insert(1,"-F") + super.insert(1,"-F") end end diff --git a/lib/puppet/sslcertificates.rb b/lib/puppet/sslcertificates.rb index 5155b3d5b..fb5c1b749 100755 --- a/lib/puppet/sslcertificates.rb +++ b/lib/puppet/sslcertificates.rb @@ -93,7 +93,7 @@ module Puppet::SSLCertificates ex << ef.create_extension("keyUsage", key_usage.join(",")) if key_usage ex << ef.create_extension("extendedKeyUsage", ext_key_usage.join(",")) if ext_key_usage - ex << ef.create_extension("subjectAltName", subject_alt_name.join(",")) if ! subject_alt_name.empty? + ex << ef.create_extension("subjectAltName", subject_alt_name.join(",")) if ! subject_alt_name.empty? #if @ca_config[:cdp_location] then # ex << ef.create_extension("crlDistributionPoints", diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index 99ac909cd..320f82f0c 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -1397,7 +1397,7 @@ class Type subscribe => File[nagconf] } } - + Currently the ``exec``, ``mount`` and ``service`` type support refreshing. " diff --git a/lib/puppet/type/file/target.rb b/lib/puppet/type/file/target.rb index 1d85e05bc..73787c7e9 100644 --- a/lib/puppet/type/file/target.rb +++ b/lib/puppet/type/file/target.rb @@ -45,7 +45,7 @@ module Puppet end end - @resource.send(:property_fix) + @resource.send(:property_fix) :link_created end diff --git a/lib/puppet/type/mount.rb b/lib/puppet/type/mount.rb index bb488c275..2ceacb3fe 100755 --- a/lib/puppet/type/mount.rb +++ b/lib/puppet/type/mount.rb @@ -4,7 +4,7 @@ module Puppet @doc = "Manages mounted filesystems, including putting mount information into the mount table. The actual behavior depends on the value of the 'ensure' parameter. - + Note that if a ``mount`` receives an event from another resource, it will try to remount the filesystems if ``ensure`` is set to ``mounted``." diff --git a/lib/puppet/type/service.rb b/lib/puppet/type/service.rb index 894cf6c7e..b296f7c39 100644 --- a/lib/puppet/type/service.rb +++ b/lib/puppet/type/service.rb @@ -16,7 +16,7 @@ module Puppet command matching the service name), but the more information you can provide the better behaviour you will get. Or, you can just use a platform that has very good service support. - + Note that if a ``service`` receives an event from another resource, the service will get restarted. The actual command to restart the service depends on the platform. You can provide a special command diff --git a/lib/puppet/type/tidy.rb b/lib/puppet/type/tidy.rb index 5eb23f2b8..d9469aee4 100755 --- a/lib/puppet/type/tidy.rb +++ b/lib/puppet/type/tidy.rb @@ -21,9 +21,9 @@ Puppet::Type.newtype(:tidy) do newparam(:matches) do desc "One or more (shell type) file glob patterns, which restrict - the list of files to be tidied to those whose basenames match - at least one of the patterns specified. Multiple patterns can - be specified using an array. + the list of files to be tidied to those whose basenames match + at least one of the patterns specified. Multiple patterns can + be specified using an array. Example:: diff --git a/lib/puppet/util.rb b/lib/puppet/util.rb index f8a872122..fc9f36083 100644 --- a/lib/puppet/util.rb +++ b/lib/puppet/util.rb @@ -320,7 +320,7 @@ module Util exit!(1) end # begin; rescue end # if child_pid - + # read output in if required if ! arguments[:squelch] diff --git a/lib/puppet/util/log.rb b/lib/puppet/util/log.rb index db6177be2..8f8d46097 100644 --- a/lib/puppet/util/log.rb +++ b/lib/puppet/util/log.rb @@ -253,21 +253,21 @@ class Puppet::Util::Log newdesttype :console do - RED = {:console => "[0;31m", :html => "FFA0A0"} - GREEN = {:console => "[0;32m", :html => "00CD00"} - YELLOW = {:console => "[0;33m", :html => "FFFF60"} - BLUE = {:console => "[0;34m", :html => "80A0FF"} - PURPLE = {:console => "[0;35m", :html => "FFA500"} - CYAN = {:console => "[0;36m", :html => "40FFFF"} - WHITE = {:console => "[0;37m", :html => "FFFFFF"} - HRED = {:console => "[1;31m", :html => "FFA0A0"} - HGREEN = {:console => "[1;32m", :html => "00CD00"} - HYELLOW = {:console => "[1;33m", :html => "FFFF60"} - HBLUE = {:console => "[1;34m", :html => "80A0FF"} - HPURPLE = {:console => "[1;35m", :html => "FFA500"} - HCYAN = {:console => "[1;36m", :html => "40FFFF"} - HWHITE = {:console => "[1;37m", :html => "FFFFFF"} - RESET = {:console => "[0m", :html => ""} + RED = {:console => "[0;31m", :html => "FFA0A0"} + GREEN = {:console => "[0;32m", :html => "00CD00"} + YELLOW = {:console => "[0;33m", :html => "FFFF60"} + BLUE = {:console => "[0;34m", :html => "80A0FF"} + PURPLE = {:console => "[0;35m", :html => "FFA500"} + CYAN = {:console => "[0;36m", :html => "40FFFF"} + WHITE = {:console => "[0;37m", :html => "FFFFFF"} + HRED = {:console => "[1;31m", :html => "FFA0A0"} + HGREEN = {:console => "[1;32m", :html => "00CD00"} + HYELLOW = {:console => "[1;33m", :html => "FFFF60"} + HBLUE = {:console => "[1;34m", :html => "80A0FF"} + HPURPLE = {:console => "[1;35m", :html => "FFA500"} + HCYAN = {:console => "[1;36m", :html => "40FFFF"} + HWHITE = {:console => "[1;37m", :html => "FFFFFF"} + RESET = {:console => "[0m", :html => "" } @@colormap = { :debug => WHITE, diff --git a/lib/puppet/util/pidlock.rb b/lib/puppet/util/pidlock.rb index fae8134a5..d24b8378e 100644 --- a/lib/puppet/util/pidlock.rb +++ b/lib/puppet/util/pidlock.rb @@ -1,68 +1,68 @@ require 'fileutils' class Puppet::Util::Pidlock - attr_reader :lockfile - - def initialize(lockfile) - @lockfile = lockfile - end - - def locked? - clear_if_stale - File.exists? @lockfile - end - - def mine? - Process.pid == lock_pid - end + attr_reader :lockfile - def anonymous? - return false unless File.exists?(@lockfile) - File.read(@lockfile) == "" - end - - def lock(opts = {}) - opts = {:anonymous => false}.merge(opts) + def initialize(lockfile) + @lockfile = lockfile + end - if locked? - mine? - else - if opts[:anonymous] - File.open(@lockfile, 'w') { |fd| true } - else - File.open(@lockfile, "w") { |fd| fd.write(Process.pid) } - end - true - end - end - - def unlock(opts = {}) - opts = {:anonymous => false}.merge(opts) + def locked? + clear_if_stale + File.exists? @lockfile + end - if mine? or (opts[:anonymous] and anonymous?) - File.unlink(@lockfile) - true - else - false - end - end + def mine? + Process.pid == lock_pid + end - private - def lock_pid - if File.exists? @lockfile - File.read(@lockfile).to_i - else - nil - end - end + def anonymous? + return false unless File.exists?(@lockfile) + File.read(@lockfile) == "" + end - def clear_if_stale - return if lock_pid.nil? - - begin - Process.kill(0, lock_pid) - rescue Errno::ESRCH - File.unlink(@lockfile) - end - end + def lock(opts = {}) + opts = {:anonymous => false}.merge(opts) + + if locked? + mine? + else + if opts[:anonymous] + File.open(@lockfile, 'w') { |fd| true } + else + File.open(@lockfile, "w") { |fd| fd.write(Process.pid) } + end + true + end + end + + def unlock(opts = {}) + opts = {:anonymous => false}.merge(opts) + + if mine? or (opts[:anonymous] and anonymous?) + File.unlink(@lockfile) + true + else + false + end + end + + private + def lock_pid + if File.exists? @lockfile + File.read(@lockfile).to_i + else + nil + end + end + + def clear_if_stale + return if lock_pid.nil? + + begin + Process.kill(0, lock_pid) + rescue Errno::ESRCH + File.unlink(@lockfile) + end + end end |