Class: AptControl::ControlFile

Inherits:
Object
  • Object
show all
Defined in:
lib/apt_control/control_file.rb

Overview

Loads and models the contents of a control.ini file see example-control.ini in root of project for an example

Defined Under Namespace

Classes: Distribution, PackageRule

Instance Method Summary collapse

Constructor Details

#initialize(path, logger) ⇒ ControlFile

Returns a new instance of ControlFile.



9
10
11
12
13
14
15
# File 'lib/apt_control/control_file.rb', line 9

def initialize(path, logger)
  @logger = logger
  @watch_mutex = Mutex.new
  @path = path
  inifile = IniFile.load(@path)
  @distributions = parse!(inifile)
end

Instance Method Details

#distributionsObject



17
18
19
# File 'lib/apt_control/control_file.rb', line 17

def distributions
  @watch_mutex.synchronize { @distributions }
end

#dumpObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/apt_control/control_file.rb', line 21

def dump
  @watch_mutex.synchronize do
    @distributions.each do |d|
      puts "#{d.name}"
      d.package_rules.each do |pr|
        puts "  #{pr.package_name} #{pr.restriction} #{pr.version}"
      end
    end
  end
end

#parse!(inifile) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/apt_control/control_file.rb', line 32

def parse!(inifile)
  inifile.sections.map do |section|
    rules = inifile[section].map do |key, value|
      PackageRule.new(key, value)
    end
    Distribution.new(section, rules)
  end
end

#watch(&block) ⇒ Object

Watch the control file for changes, rebuilding internal data structures when it does



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/apt_control/control_file.rb', line 43

def watch(&block)
  path = File.expand_path(@path)
  @logger.info("Watching for changes to #{path}")
  dir = File.dirname(path)
  fname = File.basename(path)
  Listen.to(dir, :filter => /#{Regexp.quote(fname)}/) do |modified, added, removed|
    begin
      @logger.info("Change to control file detected...")
      inifile = IniFile.load(path)
      distributions = parse!(inifile)

      @watch_mutex.synchronize do
        @distributions = distributions
      end
      @logger.info("...rebuilt")
      yield if block_given?
    rescue => e
      @logger.error("Error reloading changes: #{e}")
      @logger.error(e)
    end
  end
end