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(distro, distro_version, kernel_version, gflag = false, qflag = true, sflag = false) ⇒ File

Returns a new instance of File.



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

def initialize( distro, distro_version, kernel_version, gflag = false, qflag = true, sflag = false)
  @distro = distro
  @distro_version = Gem::Version.new(distro_version)
  @kernel_version = Gem::Version.new(kernel_version)
  @nparams = 0
  @mismatches = []
  @errors = []
  @skips = []
  @warns = []
  @generated = []
  if gflag
    @generated << "# Generated at #{Time.now.strftime("%c %Z")}"
    @generated << "# Distro: #{@distro} Version: #{@distro_version} Kernel: #{@kernel_version}"
  end
  @gflag = gflag
  @qflag = qflag
  @sflag = sflag
end

Instance Attribute Details

#distroObject (readonly)

Returns the value of attribute distro.



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

def distro
  @distro
end

#distro_versionObject (readonly)

Returns the value of attribute distro_version.



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

def distro_version
  @distro_version
end

#gflagObject (readonly)

Returns the value of attribute gflag.



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

def gflag
  @gflag
end

#kernel_versionObject (readonly)

Returns the value of attribute kernel_version.



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

def kernel_version
  @kernel_version
end

#qflagObject (readonly)

Returns the value of attribute qflag.



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

def qflag
  @qflag
end

#sflagObject (readonly)

Returns the value of attribute sflag.



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

def sflag
  @sflag
end

Instance Method Details

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



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tweek/file.rb', line 104

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

#condfail(cond) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tweek/file.rb', line 78

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



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

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

#generate(type, entry) ⇒ Object

Generate an output file recording what was found (check) or done (set)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tweek/file.rb', line 31

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

#messagesObject



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

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



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
145
146
147
148
149
150
151
152
# File 'lib/tweek/file.rb', line 118

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

    comment = line.slice!(/#.*$/)
    if line.empty?
      section.push Tweek::Entry.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 Tweek::Entry.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



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/tweek/file.rb', line 154

def results
  unless @qflag
    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?
    @generated.each { |l| STDOUT.puts l}
  end
  return @mismatches.size
end

#skipcond(entry) ⇒ Object



74
75
76
# File 'lib/tweek/file.rb', line 74

def skipcond entry
  @skips.push entry
end

#warn(msg) ⇒ Object



70
71
72
# File 'lib/tweek/file.rb', line 70

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