diff options
| author | Jack Nagel | 2013-02-09 18:19:50 -0600 | 
|---|---|---|
| committer | Jack Nagel | 2013-02-09 18:19:50 -0600 | 
| commit | 97d3ae1775da31a0f5a63d0af4d8a786da73848f (patch) | |
| tree | 383bf44d837623f7e8195cda7c7dc637df3a5864 /Library/Homebrew/formula_lock.rb | |
| parent | 97f9f93f25f54890f7b9005d455baa5c0dd460cc (diff) | |
| download | brew-97d3ae1775da31a0f5a63d0af4d8a786da73848f.tar.bz2 | |
Extract formula locks into a class
Diffstat (limited to 'Library/Homebrew/formula_lock.rb')
| -rw-r--r-- | Library/Homebrew/formula_lock.rb | 40 | 
1 files changed, 40 insertions, 0 deletions
diff --git a/Library/Homebrew/formula_lock.rb b/Library/Homebrew/formula_lock.rb new file mode 100644 index 000000000..183c2dd9c --- /dev/null +++ b/Library/Homebrew/formula_lock.rb @@ -0,0 +1,40 @@ +class FormulaLock +  LOCKDIR = HOMEBREW_CACHE_FORMULA + +  def initialize(name) +    @name = name +    @path = LOCKDIR.join("#{@name}.brewing") +  end + +  def lock +    LOCKDIR.mkpath +    @lockfile = get_or_create_lockfile +    unless @lockfile.flock(File::LOCK_EX | File::LOCK_NB) +      raise OperationInProgressError, @name +    end +  end + +  def unlock +    unless @lockfile.nil? || @lockfile.closed? +      @lockfile.flock(File::LOCK_UN) +      @lockfile.close +    end +  end + +  def with_lock +    lock +    yield +  ensure +    unlock +  end + +  private + +  def get_or_create_lockfile +    if @lockfile.nil? || @lockfile.closed? +      @path.open(File::RDWR | File::CREAT) +    else +      @lockfile +    end +  end +end  | 
