simple_lockfile 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Josh Nesbitt <josh@josh-nesbitt.net>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ class LockFile
2
+ attr_accessor :path, :filename
3
+
4
+ def initialize(path="/tmp", filename="lockfile.lock")
5
+ @path, @filename = path, filename
6
+ end
7
+
8
+ def qualified_path
9
+ File.join(@path, @filename)
10
+ end
11
+
12
+ def process_id
13
+ locked? ? read_lockfile(self.qualified_path).strip.to_i : nil
14
+ end
15
+
16
+ def lock!
17
+ locked? ? false : create_lockfile(self.qualified_path)
18
+ end
19
+
20
+ def unlock!
21
+ unlocked? ? false : destroy_lockfile(self.qualified_path)
22
+ end
23
+
24
+ def locked?
25
+ lockfile_exists?(self.qualified_path)
26
+ end
27
+
28
+ def unlocked?
29
+ !lockfile_exists?(self.qualified_path)
30
+ end
31
+
32
+ protected
33
+ def lockfile_exists?(file)
34
+ File.exists?(file)
35
+ end
36
+
37
+ def create_lockfile(lockfile)
38
+ begin
39
+ File.open(lockfile, "w") { |f| f.write(Process.pid) }
40
+ rescue
41
+ raise LockFileExists
42
+ end
43
+ end
44
+
45
+ def read_lockfile(lockfile)
46
+ begin
47
+ File.open(lockfile, "r").gets
48
+ rescue
49
+ raise LockFileMissing
50
+ end
51
+ end
52
+
53
+ def destroy_lockfile(lockfile)
54
+ begin
55
+ File.delete(lockfile)
56
+ rescue
57
+ raise LockFileMissing
58
+ end
59
+ end
60
+
61
+ public
62
+ class LockFileError < StandardError
63
+ end
64
+ class LockFileExists < LockFileError
65
+ end
66
+ class LockFileMissing < LockFileError
67
+ end
68
+
69
+ end
data/lib/lockfile.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'lockfile', 'lockfile')
@@ -0,0 +1,57 @@
1
+ describe LockFile do
2
+
3
+ before do
4
+ @lockfile_args = [File.expand_path(File.join(File.dirname(__FILE__), "..", "tmp")), "example.lock"]
5
+
6
+ File.delete(@lockfile_args.join("/")) if File.exists?(@lockfile_args.join("/"))
7
+ end
8
+
9
+ it "should return a qualified pathname" do
10
+ @lockfile = LockFile.new(*@lockfile_args)
11
+
12
+ @lockfile.path.should =~ /tmp/
13
+ @lockfile.filename.should == "example.lock"
14
+ @lockfile.qualified_path.should =~ /tmp\/example.lock/
15
+ end
16
+
17
+ it "should have the correct error classes present" do
18
+ LockFile::LockFileError.ancestors.should include(StandardError)
19
+ LockFile::LockFileExists.ancestors.should include(StandardError)
20
+ LockFile::LockFileMissing.ancestors.should include(StandardError)
21
+ end
22
+
23
+ it "should not initially be locked" do
24
+ @lockfile = LockFile.new(*@lockfile_args)
25
+
26
+ @lockfile.locked?.should be_false
27
+ end
28
+
29
+ it "should be able to lock" do
30
+ @lockfile = LockFile.new(*@lockfile_args)
31
+
32
+ @lockfile.lock!.should be_true
33
+ @lockfile.locked?.should be_true
34
+ @lockfile.unlocked?.should be_false
35
+ end
36
+
37
+ it "should be able to unlock" do
38
+ @lockfile = LockFile.new(*@lockfile_args)
39
+
40
+ @lockfile.lock!.should be_true
41
+ @lockfile.locked?.should be_true
42
+ @lockfile.unlocked?.should be_false
43
+ @lockfile.unlock!.should be_true
44
+ @lockfile.locked?.should be_false
45
+ @lockfile.unlocked?.should be_true
46
+ end
47
+
48
+ it "should return the process ID of the lockfile if it exists" do
49
+ @lockfile = LockFile.new(*@lockfile_args)
50
+
51
+ @lockfile.process_id.should be_nil
52
+ @lockfile.lock!
53
+ @lockfile.process_id.should_not be_nil
54
+ @lockfile.process_id.class.should == Fixnum
55
+ end
56
+
57
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.expand_path(File.join(File.dirname(__FILE__), '..')), 'lib', 'lockfile.rb')
data/spec/watch.rb ADDED
@@ -0,0 +1,30 @@
1
+ # A simple alternative to autotest that isnt as painful
2
+ puts ">> Watching spec folder for changes..."
3
+
4
+ options = {
5
+ :options => "--require 'spec/spec_helper' --format nested --color",
6
+ :binary => "spec"
7
+ }
8
+
9
+ watch("lib/(.*)\.rb") do |match|
10
+ puts %x[ clear ]
11
+
12
+ file = match[match.size - 1]
13
+ opts = options[:options]
14
+ binary = options[:binary]
15
+ files = []
16
+
17
+ %w{ lib }.each do |dir|
18
+ ["spec/#{dir}/*/*.rb", "spec/#{dir}/*.rb"].each do |glob|
19
+ Dir.glob(glob).each { |f| files << f }
20
+ end
21
+ end
22
+
23
+ puts "Found:"
24
+ files.each { |f| puts "+ #{f}" }
25
+ puts ""
26
+ command = "#{binary} #{files.collect! { |f| File.expand_path(f) }.join(" ")} #{opts}"
27
+
28
+ system(command)
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_lockfile
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Nesbitt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-16 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple Lock File implementation.
17
+ email: josh@josh-nesbitt.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ files:
25
+ - lib/lockfile.rb
26
+ - lib/lockfile/lockfile.rb
27
+ - LICENSE
28
+ has_rdoc: true
29
+ homepage: http://github.com/joshnesbitt/lockfile
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --charset=UTF-8
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.3.5
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: A simple Lock File implementation.
56
+ test_files:
57
+ - spec/lib/lockfile.rb
58
+ - spec/spec_helper.rb
59
+ - spec/watch.rb