-
-
Save vmoravec/6122088 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RPMSpec | |
attr_reader :spec | |
def initialize(spec) | |
@tags = Hash.new {|h,k| h[k] = []} | |
@defines = {} | |
@spec = spec | |
@sections = Hash.new {|h,k| h[k] = []} | |
#@rpm_build_dir = rpm_build_dir | |
parse | |
end | |
def tags(name) | |
if @tags.include? name.to_s.downcase | |
@tags[name.to_s.downcase] | |
else | |
nil | |
end | |
end | |
def tag(name) | |
t = tags(name) | |
t && t.first | |
end | |
def sources | |
@tags.map do |k,v| | |
if k =~ /source[0-9]+/ | |
v | |
else | |
nil | |
end | |
end.compact.flatten | |
end | |
private | |
def parse | |
lines = spec.scan(/^(.*?[^\\])\n+/m).flatten.reject do |l| | |
l =~ /^#/ || l.size == 0 | |
end.map do |l| | |
l.gsub(/\\$/, '') | |
end | |
parse_lines(lines) | |
end | |
def parse_lines(lines, cur_section=nil) | |
while !lines.empty? | |
l = lines.shift | |
case l | |
when /^%define\s+(.*)$/m | |
parse_define($1) | |
when /^(.*?):\s*(.*)$/m # is it a tag def | |
@tags[$1.downcase] << process_val($2) | |
when /^%([a-z]*)\s*$/m | |
section = $1 | |
parse_lines(lines, section) | |
else | |
@sections[cur_section] << process_val(l) | |
end | |
end | |
end | |
def parse_define(rest) | |
key, val = rest.split(/\s+/m, 2) | |
@defines[key] = process_val(val) | |
end | |
def process_val(str) | |
str.gsub(/%\{(.*?)\}/) do |match| | |
d = @defines[$1] || tag($1) | |
if d | |
d | |
else | |
match | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment