Created
August 2, 2013 15:51
-
-
Save vmoravec/6141001 to your computer and use it in GitHub Desktop.
Parse rpm spec file
This file contains hidden or 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
# a naive version outputing a map of binaries and packages | |
class SpecParser | |
module Tag | |
REQUIRES = /^Requires:/ | |
BUILD_REQUIRES = /^BuildRequires:/ | |
end | |
class << self | |
attr_reader :spec_file | |
def load_file path | |
@spec_file = File.read path if File.exists? path | |
end | |
def parse path | |
load_file path | |
matched_lines = [] | |
dependencies = {:binaries=>[], :packages=>[]} | |
if spec_file | |
spec_file.each_line do |line| | |
matched_lines << line if line.match Tag::REQUIRES | |
end | |
end | |
rpm_macro_pattern = /(%\{.*?\})/ | |
matched_lines.each do |line| | |
line.slice! Tag::REQUIRES # remove the tag | |
while line.match rpm_macro_pattern # remove macros | |
line.slice! rpm_macro_pattern | |
end | |
line.split.grep(/[a-zA-Z]/).each do |dep| | |
if dep.match /\A\// | |
dependencies[:binaries] << dep | |
else | |
dependencies[:packages] << dep | |
end | |
end | |
end | |
dependencies | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Macros must be enclosed with
%{}
otherwise they are not detected by the simple regex