Skip to content

Instantly share code, notes, and snippets.

@vmoravec
Created August 2, 2013 15:51
Show Gist options
  • Save vmoravec/6141001 to your computer and use it in GitHub Desktop.
Save vmoravec/6141001 to your computer and use it in GitHub Desktop.
Parse rpm spec file
# 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
@vmoravec
Copy link
Author

vmoravec commented Aug 2, 2013

Macros must be enclosed with %{} otherwise they are not detected by the simple regex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment