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, mflag = :query, oflag = 0) ⇒ File

Returns a new instance of File.



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

def initialize( distro, distro_version, kernel_version, mflag = :query, oflag = 0)
  @distrov = distro
  @dv = Gem::Version.new(distro_version)
  @kv = Gem::Version.new(kernel_version)
  @distro_version = distro_version.bold
  @kernel_version = kernel_version.bold
  @distro = distro.bold
  @nparams = 0
  @mismatches = 0
  @errors = []
  @skips = 0
  @warns = []
  @generated = []
  @mflag = mflag
  @oflag = oflag

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

#mflagObject (readonly)

Returns the value of attribute mflag.



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

def mflag
  @mflag
end

#mismatchesObject (readonly)

Returns the value of attribute mismatches.



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

def mismatches
  @mismatches
end

#oflagObject (readonly)

Returns the value of attribute oflag.



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

def oflag
  @oflag
end

#skipsObject (readonly)

Returns the value of attribute skips.



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

def skips
  @skips
end

Instance Method Details

#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
103
104
105
# 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|
    raise ArgumentError.new("entry has invalid condition variable: #{req}") unless /^[kvd]/ =~ req
    var = $&
    ver = $'
    ok =  case var
          when 'k'
            Gem::Requirement.new(ver).satisfied_by?(@kv)
          when 'v'
            Gem::Requirement.new(ver).satisfied_by?(@dv)
          when 'd'
            op = ver.slice!(/^[<>~!=]+/)
            begin
              eval "@distrov #{op} #{ver}"
            rescue Exception => e
              raise ArgumentError.new("entry has condition error: #{e.message}")
            end
          end
    failures += 1 unless ok
  end
  skipped = (failures > 0)
  @skips += 1 if skipped
  return skipped
end

#error(msg, lineno = 0) ⇒ Object



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

def error msg, lineno = 0
  line = (lineno == 0) ? msg : sprintf("%3d: #{msg}", lineno)
  @errors.push line unless @errors.first == line
end

#generate(entry) ⇒ Object

Called for every entry, generates output as required



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
# File 'lib/tweek/file.rb', line 31

def generate entry

  line = entry.generate

  case entry.type

  when :literal
    @generated.push line if @oflag > 1

  when :section
    @generated.push line unless entry.section_type.nil? or @oflag < 2

  when :parameter
    @nparams += 1
    if entry.condfail
      @generated.push line if @oflag > 1
      STDERR.print "s".colorize(:yellow) if @oflag == 1
    else
      @generated.push line if @oflag == 3 or ((@oflag == 2) and entry.mismatch)
      if entry.mismatch
        @mismatches += 1
        STDERR.print "F".colorize(:red) if @oflag == 1
      else
        STDERR.print ".".colorize(:green) if @oflag == 1
      end
    end
  end
end

#generatedObject



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

def generated
  return @generated.size > 0 ? @generated.join("\n") : ""
end

#messagesObject



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

def messages
  return @errors.size > 0 ? @errors.join("\n") : ""
end

#read_sections(handle) ⇒ Object

Read the entire file and split into sections



109
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
# File 'lib/tweek/file.rb', line 109

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

    begin
      entry = Tweek::Entry.parse(section, line)
      entry.lineno = handle.lineno
      case entry.type

      when :literal
        section.push entry

      when :section
        section.process
        section = Tweek::Section.new(self, entry)

      when :parameter
        section.push entry

      else
        error "#{handle.lineno}: Unrecognized line: #{line}"
      end
    rescue
      error "#{handle.lineno}: #{$!.message}"
    end
  end
  section.process
end

#resultsObject



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

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

#warn(msg, lineno = 0) ⇒ Object



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

def warn msg, lineno = 0
  line = (lineno == 0) ? msg : sprintf("%3d: #{msg}", lineno)
  @warns.push line unless @warns.first == line
end