barron 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/Rakefile +151 -0
- data/barron.gemspec +39 -0
- data/bin/barron +67 -0
- data/lib/barron.rb +18 -0
- data/lib/barron/tmpfile.rb +30 -0
- metadata +99 -0
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rdoc'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), *%w[lib]))
|
7
|
+
|
8
|
+
#############################################################################
|
9
|
+
#
|
10
|
+
# Helper functions
|
11
|
+
#
|
12
|
+
#############################################################################
|
13
|
+
|
14
|
+
def name
|
15
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
16
|
+
end
|
17
|
+
|
18
|
+
def version
|
19
|
+
line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
|
20
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
21
|
+
end
|
22
|
+
|
23
|
+
def date
|
24
|
+
Date.today.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def rubyforge_project
|
28
|
+
name
|
29
|
+
end
|
30
|
+
|
31
|
+
def gemspec_file
|
32
|
+
"#{name}.gemspec"
|
33
|
+
end
|
34
|
+
|
35
|
+
def gem_file
|
36
|
+
"#{name}-#{version}.gem"
|
37
|
+
end
|
38
|
+
|
39
|
+
def replace_header(head, header_name)
|
40
|
+
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
|
41
|
+
end
|
42
|
+
|
43
|
+
#############################################################################
|
44
|
+
#
|
45
|
+
# Standard tasks
|
46
|
+
#
|
47
|
+
#############################################################################
|
48
|
+
|
49
|
+
task :default => [:test, :features]
|
50
|
+
|
51
|
+
require 'rake/testtask'
|
52
|
+
Rake::TestTask.new(:test) do |test|
|
53
|
+
if `which pygmentize` == ''
|
54
|
+
puts "You must have Pygments installed to run the tests."
|
55
|
+
exit 1
|
56
|
+
end
|
57
|
+
test.libs << 'lib' << 'test'
|
58
|
+
test.pattern = 'test/**/test_*.rb'
|
59
|
+
test.verbose = true
|
60
|
+
end
|
61
|
+
|
62
|
+
desc "Generate RCov test coverage and open in your browser"
|
63
|
+
task :coverage do
|
64
|
+
require 'rcov'
|
65
|
+
sh "rm -fr coverage"
|
66
|
+
sh "rcov test/test_*.rb"
|
67
|
+
sh "open coverage/index.html"
|
68
|
+
end
|
69
|
+
|
70
|
+
require 'rdoc/task'
|
71
|
+
Rake::RDocTask.new do |rdoc|
|
72
|
+
rdoc.rdoc_dir = 'rdoc'
|
73
|
+
rdoc.title = "#{name} #{version}"
|
74
|
+
rdoc.rdoc_files.include('README*')
|
75
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
76
|
+
end
|
77
|
+
|
78
|
+
desc "Open an irb session preloaded with this library"
|
79
|
+
task :console do
|
80
|
+
sh "irb -rubygems -r ./lib/#{name}.rb"
|
81
|
+
end
|
82
|
+
|
83
|
+
#############################################################################
|
84
|
+
#
|
85
|
+
# Custom tasks (add your own tasks here)
|
86
|
+
#
|
87
|
+
#############################################################################
|
88
|
+
|
89
|
+
begin
|
90
|
+
require 'cucumber/rake/task'
|
91
|
+
Cucumber::Rake::Task.new(:features) do |t|
|
92
|
+
t.cucumber_opts = "--format progress"
|
93
|
+
end
|
94
|
+
rescue LoadError
|
95
|
+
desc 'Cucumber rake task not available'
|
96
|
+
task :features do
|
97
|
+
abort 'Cucumber rake task is not available. Be sure to install cucumber as a gem or plugin'
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
#############################################################################
|
102
|
+
#
|
103
|
+
# Packaging tasks
|
104
|
+
#
|
105
|
+
#############################################################################
|
106
|
+
|
107
|
+
task :release => :build do
|
108
|
+
unless `git branch` =~ /^\* master$/
|
109
|
+
puts "You must be on the master branch to release!"
|
110
|
+
exit!
|
111
|
+
end
|
112
|
+
sh "git commit --allow-empty -m 'Release #{version}'"
|
113
|
+
sh "git tag v#{version}"
|
114
|
+
sh "git push origin master"
|
115
|
+
sh "git push origin v#{version}"
|
116
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
117
|
+
end
|
118
|
+
|
119
|
+
task :build => :gemspec do
|
120
|
+
sh "mkdir -p pkg"
|
121
|
+
sh "gem build #{gemspec_file}"
|
122
|
+
sh "mv #{gem_file} pkg"
|
123
|
+
end
|
124
|
+
|
125
|
+
task :gemspec do
|
126
|
+
# read spec file and split out manifest section
|
127
|
+
spec = File.read(gemspec_file)
|
128
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
129
|
+
|
130
|
+
# replace name version and date
|
131
|
+
replace_header(head, :name)
|
132
|
+
replace_header(head, :version)
|
133
|
+
replace_header(head, :date)
|
134
|
+
#comment this out if your rubyforge_project has a different name
|
135
|
+
# replace_header(head, :rubyforge_project)
|
136
|
+
|
137
|
+
# determine file list from git ls-files
|
138
|
+
files = `git ls-files`.
|
139
|
+
split("\n").
|
140
|
+
sort.
|
141
|
+
reject { |file| file =~ /^\./ }.
|
142
|
+
reject { |file| file =~ /^(rdoc|pkg|coverage)/ }.
|
143
|
+
map { |file| " #{file}" }.
|
144
|
+
join("\n")
|
145
|
+
|
146
|
+
# piece file back together and write
|
147
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
148
|
+
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
|
149
|
+
File.open(gemspec_file, 'w') { |io| io.write(spec) }
|
150
|
+
puts "Updated #{gemspec_file}"
|
151
|
+
end
|
data/barron.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
3
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
4
|
+
s.rubygems_version = '1.3.5'
|
5
|
+
|
6
|
+
s.name = 'barron'
|
7
|
+
s.version = '0.1.0'
|
8
|
+
s.date = '2012-01-04'
|
9
|
+
|
10
|
+
s.summary = "Barron helps you with locking"
|
11
|
+
s.description = "Barron uses file-system locks, but eventually configurable backends would be nice. Robert Barron patented a double-acting tumbler lock in 1778."
|
12
|
+
|
13
|
+
s.authors = ["Matthew Beale"]
|
14
|
+
s.email = 'matt.beale@madhatted.com'
|
15
|
+
s.homepage = 'http://github.com/mixonic/barron'
|
16
|
+
|
17
|
+
s.require_paths = %w[lib]
|
18
|
+
|
19
|
+
s.executables = ["barron"]
|
20
|
+
|
21
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
22
|
+
s.extra_rdoc_files = %w[]
|
23
|
+
|
24
|
+
s.add_development_dependency('rake', "~> 0.9")
|
25
|
+
s.add_development_dependency('rdoc', "~> 3.11")
|
26
|
+
|
27
|
+
# = MANIFEST =
|
28
|
+
s.files = %w[
|
29
|
+
Gemfile
|
30
|
+
Rakefile
|
31
|
+
barron.gemspec
|
32
|
+
bin/barron
|
33
|
+
lib/barron.rb
|
34
|
+
lib/barron/tmpfile.rb
|
35
|
+
]
|
36
|
+
# = MANIFEST =
|
37
|
+
|
38
|
+
s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
|
39
|
+
end
|
data/bin/barron
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
|
4
|
+
|
5
|
+
help = <<HELP
|
6
|
+
Barron uses filesystem locks, but provides an identical
|
7
|
+
backend across a Ruby process (via gem) and the command-line.
|
8
|
+
|
9
|
+
Basic Command Line Usage:
|
10
|
+
# Return success if available, error if not
|
11
|
+
barron "locked-resource"
|
12
|
+
# Block until the resource is free
|
13
|
+
barron "locked-resource" --block
|
14
|
+
|
15
|
+
Other options follow:
|
16
|
+
|
17
|
+
HELP
|
18
|
+
|
19
|
+
require 'optparse'
|
20
|
+
require 'barron'
|
21
|
+
|
22
|
+
resource = nil
|
23
|
+
options = {
|
24
|
+
:block => false
|
25
|
+
}
|
26
|
+
opts = OptionParser.new do |opts|
|
27
|
+
opts.banner = help
|
28
|
+
|
29
|
+
opts.on("--tmpdir [DIRECTORY]", "Where to write the lockfiles") do |directory|
|
30
|
+
options[:tmpdir] = directory unless directory.nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
opts.on("--block", "If barron should block until the lock is released") do
|
34
|
+
options[:block] = true
|
35
|
+
end
|
36
|
+
|
37
|
+
opts.on("--version", "Display current version") do
|
38
|
+
puts "Spinto " + Spinto::VERSION
|
39
|
+
exit 0
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Read command line options into `options` hash
|
44
|
+
opts.parse!
|
45
|
+
|
46
|
+
|
47
|
+
# Get source and destintation from command line
|
48
|
+
case ARGV.size
|
49
|
+
when 1
|
50
|
+
resource = ARGV[0]
|
51
|
+
else
|
52
|
+
puts "Invalid options. Run `barron --help` for assistance."
|
53
|
+
exit(1)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Fire the lock, return success if it is free,
|
57
|
+
# or hang until it is free.
|
58
|
+
Barron.lock resource, options do
|
59
|
+
$stdout.puts "Requested resource is free."
|
60
|
+
exit(0)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Unless barron was told to hang, we will pass
|
64
|
+
# through to here when barron cannot get a lock.
|
65
|
+
# So return failue.
|
66
|
+
$stderr.puts "Requested resource is being used."
|
67
|
+
exit(1)
|
data/lib/barron.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module Barron::Tmpfile
|
2
|
+
|
3
|
+
require 'digest/md5'
|
4
|
+
|
5
|
+
class << self
|
6
|
+
|
7
|
+
def lock name, options={}
|
8
|
+
options = {
|
9
|
+
:block => true,
|
10
|
+
:tmpdir => '/tmp'
|
11
|
+
}.merge(options)
|
12
|
+
value = nil
|
13
|
+
open("#{options[:tmpdir]}/barron-#{Digest::MD5.hexdigest("#{name}")}", File::RDWR|File::CREAT, 0666) do |f|
|
14
|
+
begin
|
15
|
+
if options[:block]
|
16
|
+
f.flock(File::LOCK_EX)
|
17
|
+
value = yield f
|
18
|
+
elsif f.flock(File::LOCK_EX|File::LOCK_NB)
|
19
|
+
value = yield f
|
20
|
+
end
|
21
|
+
ensure
|
22
|
+
f.flock(File::LOCK_UN)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
value
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: barron
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matthew Beale
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-01-04 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 25
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 9
|
31
|
+
version: "0.9"
|
32
|
+
type: :development
|
33
|
+
requirement: *id001
|
34
|
+
name: rake
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 17
|
43
|
+
segments:
|
44
|
+
- 3
|
45
|
+
- 11
|
46
|
+
version: "3.11"
|
47
|
+
type: :development
|
48
|
+
requirement: *id002
|
49
|
+
name: rdoc
|
50
|
+
description: Barron uses file-system locks, but eventually configurable backends would be nice. Robert Barron patented a double-acting tumbler lock in 1778.
|
51
|
+
email: matt.beale@madhatted.com
|
52
|
+
executables:
|
53
|
+
- barron
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- Gemfile
|
60
|
+
- Rakefile
|
61
|
+
- barron.gemspec
|
62
|
+
- bin/barron
|
63
|
+
- lib/barron.rb
|
64
|
+
- lib/barron/tmpfile.rb
|
65
|
+
homepage: http://github.com/mixonic/barron
|
66
|
+
licenses: []
|
67
|
+
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options:
|
70
|
+
- --charset=UTF-8
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
requirements: []
|
92
|
+
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 1.8.13
|
95
|
+
signing_key:
|
96
|
+
specification_version: 2
|
97
|
+
summary: Barron helps you with locking
|
98
|
+
test_files: []
|
99
|
+
|