]> icculus.org git repositories - duncan/rpmer.git/blob - lib/rpmer/rpmbuild.rb
Initial commit
[duncan/rpmer.git] / lib / rpmer / rpmbuild.rb
1 # This file can be distributed under the same terms as ruby itself.  
2 # Author: Marek Gilbert <gil (at) fooplanet (dot) com>
3
4 # A simple wrapper around rpmbuild's --showrc command that allows rpm to
5 # tell gem2rpm where to install things.
6 class RpmRc
7   def initialize()
8     IO.popen('rpmbuild --showrc', 'r') do |fd|
9       read_rc(fd)
10     end
11   end
12
13   # Read the rc file and define all the macros in it.
14   def read_rc(fd)
15     @macros = {}
16     fd.each_line() do |line|
17       if line =~ /^-14:\s+(\w+)\s+(\S+)/
18         @macros[$1] = $2
19       end
20     end
21   end
22
23   # Lookup a macro by name.  Use only the name part and leave out the
24   # leading '%{' and trailing '}'.  If any value exists for the macro, it is
25   # evaluated such that the result will be macro-free.
26   def lookup(name)
27     val = @macros[name]
28     return evaluate(val)
29   end
30
31   # Recursively examine the string and substitute any macros with their
32   # values.
33   def evaluate(value)
34     return nil if value.nil?
35 #     value = value.gsub(/%\(([^\)]+)\)/) do |x|
36 #       puts $1
37 #      `#{$1}`
38 #     end
39     return value.gsub(/%\{([^}]+)\}/) { |x| lookup($1) }
40   end
41 end
42
43 class RpmBuildOption
44   attr_reader :name, :bits, :flags
45
46   def initialize(bits, name, flags)
47     @bits = bits
48     @name = name
49     @flags = flags
50   end
51
52   def includes(other)
53     0 != @bits & other.bits
54   end
55
56   BINARIES = RpmBuildOption.new(1, 'binary', '-bb')
57   SOURCES = RpmBuildOption.new(2, 'sources', '-bs')
58   ALL = RpmBuildOption.new(1 + 2, 'all', '-ba')
59 end
60
61 # Wrapper around the rpmbuild command.
62 class RpmBuild
63   attr_reader :rc
64
65   def initialize()
66     @rc = RpmRc.new()
67   end
68
69   def build_binary(rpm)
70     SysUtils.run("rpmbuild #{$options.rpm_build_opts.flags} " +
71       "#{rpm.spec_filename}")
72   end
73
74   def build_dir
75     @rc.lookup('_builddir')
76   end
77
78   def rpm_dir
79     @rc.lookup('_rpmdir')
80   end
81
82   def src_rpm_dir
83     @rc.lookup('_srcrpmdir')
84   end
85
86   def sources_dir
87     @rc.lookup('_sourcedir')
88   end
89
90   def spec_dir
91     @rc.lookup('_specdir')
92   end
93
94   def rpm_filename(rpm)
95     return File.join(rpm_dir, rpm.build_arch, 
96       "#{rpm.name}-#{rpm.version}-#{rpm.release}.#{rpm.build_arch}.rpm")
97   end
98
99   def srpm_filename(rpm)
100     return File.join(src_rpm_dir,
101       "#{rpm.name}-#{rpm.version}-#{rpm.release}.src.rpm")
102   end
103 end