Class: Tweek::File

Inherits:
Object
  • Object
show all
Defined in:
lib/tweek/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gflag, tty, distro, distro_version, kernel_version) ⇒ File

Returns a new instance of File.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tweek/file.rb', line 11

def initialize( gflag, tty, distro, distro_version, kernel_version)
  @distro = distro
  @distro_version = Gem::Version.new(distro_version)
  @kernel_version = Gem::Version.new(kernel_version)
  @nparams = 0
  @mismatches = []
  @errors = []
  @skips = []
  @warns = []
  @generated = []
  @gflag = gflag
  @tty = tty
end

Instance Attribute Details

#distroObject (readonly)

Returns the value of attribute distro.



9
10
11
# File 'lib/tweek/file.rb', line 9

def distro
  @distro
end

#distro_versionObject (readonly)

Returns the value of attribute distro_version.



9
10
11
# File 'lib/tweek/file.rb', line 9

def distro_version
  @distro_version
end

#kernel_versionObject (readonly)

Returns the value of attribute kernel_version.



9
10
11
# File 'lib/tweek/file.rb', line 9

def kernel_version
  @kernel_version
end

Instance Method Details

#assert_equal(expected, actual, msg = '') ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/tweek/file.rb', line 96

def assert_equal expected, actual, msg = ''
  @nparams += 1
  if expected === actual
    STDERR.print "." if @tty
    return 0
  else
    STDERR.print "F" if @tty
    @mismatches.push "#{msg}: Expected #{expected}\n#{" "*(msg.size+4)}Actual #{actual}"
  end
  return 1
end

#condfail(cond) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tweek/file.rb', line 70

def condfail cond
  return false unless cond
  cond.strip!
  failures = 0
  reqs = cond.split(/\s*,\s*/)
  reqs.each do |req|
    ok =  case var = req.slice!(0)
          when 'k'
            Gem::Requirement.new(req).satisfied_by?(kernel_version)
          when 'v'
            Gem::Requirement.new(req).satisfied_by?(distro_version)
          when 'd'
            op = req.slice!(/^[<>~!=]+/)
            begin
              eval "distro #{op} #{req}"
            rescue Exception => e
              raise ArgumentError.new("entry has condition error: #{e.message}")
            end
          else
            raise ArgumentError.new("entry has invalid condition variable: #{var}")
          end
    failures += 1 unless ok
  end
  return failures > 0
end

#error(msg) ⇒ Object



53
54
55
# File 'lib/tweek/file.rb', line 53

def error msg
  @errors.push msg unless @errors.first == msg
end

#generate(type, e) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tweek/file.rb', line 25

def generate type, e
  return unless @gflag
  case type
  when :line
    @generated.push "#{e.line}#{e.comment}"
  when :section
    @generated.push "#{e.type} #{e.list} #{e.comment}"
  when :entry
    cond = e.cond.nil? ? "": " if #{e.cond}"
    if e.actual.nil?
      line = "#{e.param} = #{e.value}#{cond}"
      note = "[condition not met]"
    else
      line = "#{e.param} = #{e.actual}#{cond}"
      if e.value === e.actual
        note = nil
      else
        note = "[expected #{e.value}]"
      end
    end
    if e.comment.nil?
      @generated.push (note ? line + " # " + note : line)
    else
      @generated.push (note ? line + " " + comment + " " + note : line + " " + comment)
    end
  end
end

#messagesObject



57
58
59
60
# File 'lib/tweek/file.rb', line 57

def messages
  strings =  @errors + @mismatches
  return strings.size == 0 ? "": strings.join("\n")
end

#read_sections(handle) ⇒ Object

Read the entire file and split into sections



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/tweek/file.rb', line 110

def read_sections handle
  section = Tweek::Section.new(0,'<initial>','','')
  while line = handle.gets
    line.chomp!
    if (line =~ /^=begin/)..(line =~ /^=end/)
      section.push OpenStruct.new( :line => line, :lineno => handle.lineno )
      next
    end

    comment = line.slice!(/#.*$/)
    if line.empty?
      section.push OpenStruct.new( :line => line, :lineno => handle.lineno, :comment => comment )
      next
    end

    if /^\s*([A-Z0-9]+)\s*(.*?)\s*$/ =~ line # We've hit a new section
      section.process(self)
      section = Tweek::Section.new(handle.lineno, $1, $2, comment)
      next
    end

    if /^\s*(.+?)\s*=\s*(.*?)\s*(\bif\b(.*))?$/ =~ line
      if $4 and $4.strip.empty?
        error "#{handle.lineno}: Missing condition after 'if'"
      else
        section.push OpenStruct.new( :lineno => handle.lineno, :param => $1, :value => $2,
                                     :cond => $4, :comment => comment )
      end
      next
    end
    error "#{handle.lineno}: Unrecognized line: #{line}"
  end
  section.process(self)

end

#resultsObject



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/tweek/file.rb', line 146

def results
  STDERR.puts "\nDistro: #{@distro} Version: #{@distro_version} Kernel: #{@kernel_version}"
  STDERR.puts "\n#{@nparams} parameters checked, #{@skips.size} conditions not met, #{@mismatches.size} mismatches, #{@warns.size} warnings, #{@errors.size} errors"
  STDERR.puts "\n#{@errors.join("\n")}" unless @errors.empty?
  STDERR.puts "\n#{@warns.join("\n")}" unless @warns.empty?
  STDERR.puts "\n#{@mismatches.join("\n")}" unless @mismatches.empty?
  if @gflag
    @generated.unshift "# Generated at #{Time.now.strftime("%c %Z")}\n# Distro: #{@distro} Version: #{@distro_version} Kernel: #{@kernel_version}"
    @generated.each { |l| STDOUT.puts l}
  end
  return @mismatches.size
end

#skipcond(entry) ⇒ Object



66
67
68
# File 'lib/tweek/file.rb', line 66

def skipcond entry
  @skips.push entry
end

#warn(msg) ⇒ Object



62
63
64
# File 'lib/tweek/file.rb', line 62

def warn msg
  @warns.push msg unless @warns.first == msg
end