Class: AptControl::ControlFile
- Inherits:
-
Object
- Object
- AptControl::ControlFile
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
TODO some sort of actor wrapper that provides sequential write access to the data model
Defined Under Namespace
Classes: Distribution, PackageRule
Instance Method Summary
collapse
Constructor Details
#initialize(path, logger) ⇒ ControlFile
Returns a new instance of ControlFile.
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/apt_control/control_file.rb', line 12
def initialize(path, logger)
@logger = logger
@watch_mutex = Mutex.new
@path = path
@distributions =
if File.exists?(@path)
inifile = IniFile.load(@path)
parse!(inifile)
else
[]
end
end
|
Instance Method Details
#[](dist_name) ⇒ Object
32
33
34
|
# File 'lib/apt_control/control_file.rb', line 32
def [](dist_name)
distributions.find {|d| d.name == dist_name }
end
|
#distributions ⇒ Object
25
26
27
28
29
30
|
# File 'lib/apt_control/control_file.rb', line 25
def distributions
@watch_mutex.synchronize { @distributions }
end
|
#dump ⇒ Object
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/apt_control/control_file.rb', line 36
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
63
64
65
66
67
68
69
70
|
# File 'lib/apt_control/control_file.rb', line 63
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
|
#reload! ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/apt_control/control_file.rb', line 72
def reload!
inifile = IniFile.load(@path)
distributions = parse!(inifile)
@watch_mutex.synchronize do
@distributions = distributions
end
end
|
#watch(fs_listener_factory, &block) ⇒ Object
Watch the control file for changes, rebuilding internal data structures when it does
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/apt_control/control_file.rb', line 83
def watch(fs_listener_factory, &block)
path = File.expand_path(@path)
dir = File.dirname(path)
fname = File.basename(path)
@logger.info("Watching for changes to #{path}")
fs_listener_factory.new(dir, /#{Regexp.quote(fname)}/) do |modified, added, removed|
begin
@logger.info("Change to control file detected...")
reload!
yield if block_given?
@logger.info("...rebuilt")
rescue => e
@logger.error("Error reloading changes: #{e}")
@logger.error(e)
end
end.start.join
end
|
#write ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/apt_control/control_file.rb', line 47
def write
IniFile.new.tap do |inifile|
inifile.filename = @path
distributions.each do |distribution|
inifile[distribution.name] = distribution.inject({}) do |hash, rule|
hash.tap do |h|
h[rule.package_name] = '"' + rule.restriction_string + '"'
end
end
end
inifile.write
end
end
|