summaryrefslogtreecommitdiffstats
path: root/fontpackages/yum.rb
blob: 4047b2db0c5dcf96c9c877175a060f2e0cd38efc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
106
107
108
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# yum.rb
# Copyright (C) 2010 Red Hat, Inc.

# Authors:
#   Akira TAGOH  <tagoh@redhat.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

require 'rubygems'
gem 'ruby-stemp'
require 'stemp'

require 'fileutils'
require 'optparse'
require 'shellwords'
require 'tmpdir'
begin
  require 'fontpackages/compat'
rescue LoadError
  require File.join(File.dirname(__FILE__), 'comps')
end


class OptionParser

  module Arguable

    alias :orig_options :options

    def options
      @yum_config ||= []
      orig_options do |opt|
        opt.on('-C', '--cache', '[YUM] run from cache only') {|v| @yum_config << '-C'}
        opt.on('--enablerepo=REPO', '[YUM] enable one or more repositories (wildcards allowed)') {|v| @yum_config << build_yumopt(:enablerepo, v)}
        opt.on('--disablerepo=REPO', '[YUM] disable one or more repositories (wildcards allowed)') {|v| @yum_config << build_yumopt(:disablerepo, v)}

        yield opt
      end
    end # def options

    def yum_options
      @yum_config
    end # def yum_options

    private

    def build_yumopt(key, val)
      sprintf("--%s=%s", key, val.shellescape)
    end

  end # module Arguable

end # class OptionParser

module FontPackages

  class YumRepos

    def initialize(yumopts)
      @yumopts = yumopts
      @query_format = ""
      @ignore_error = false
    end # def initialize

    attr_accessor :query_format, :ignore_error

    def query(name, &block)
      repoquery([yum_options, "-q", @query_format.empty? ? "" : sprintf("--qf=%s", @query_format), name], &block)
    end # def query

    def packagelist(name, &block)
      repoquery([yum_options, "-l", name], &block)
    end # def packagelist

    def download(name)
      tmpdir = nil
      nvra = nil
      begin
        old_qf = query_format
        self.query_format = "%{name}-%{version}-%{release}.%{arch}"
        query(name) do |ret|
          nvra = ret
          break
        end
      ensure
        self.query_format = old_qf
      end
      if nvra.nil? then
        e = RuntimeError.new(sprintf("No such packages: %s", name))
        if ignore_error then
          STDERR.printf("E: %s\n", e.message)
          return
        else
          raise e
        end
      end
      if block_given? then
        tmpdir = STemp.mkdtemp(File.join(Dir.tmpdir, sprintf("%sXXXXXXXX", name)))
      end
      cwd = Dir.pwd
      begin
        Dir.chdir(tmpdir) unless tmpdir.nil?
        cmd = sprintf("yumdownloader %s %s > /dev/null 2>&1", yum_options, nvra)
        STDERR.printf("D: %s\n", cmd) if $DEBUG
        system(cmd)
        rpm = sprintf("%s.rpm", nvra)
        unless File.exist?(rpm) then
          e = RuntimeError.new(sprintf("Unable to download rpm: %s", nvra))
          if ignore_error then
            STDERR.printf("E: %s\n", e.message)
          else
            raise e
          end
        end
        yield self, rpm
      ensure
        FileUtils.rm_rf(tmpdir) unless tmpdir.nil?
        Dir.chdir(cwd)
      end
    end # def download

    def extract(name)
      download(name) do |x, rpm|
        cmd = sprintf("rpm2cpio %s | cpio -id > /dev/null 2>&1", rpm)
        STDERR.printf("D: %s\n", cmd) if $DEBUG
        system(cmd)
        yield x, rpm
      end
    end # def extract

    private

    def yum_options
      if @yumopts.kind_of?(Array) then
        @yumopts.join(' ')
      elsif !@yumopts.nil? then
	@yumopts
      else
        ""
      end
    end # def yum_options

    def repoquery(opts)
      cmd = sprintf("repoquery %s 2> /dev/null", opts.join(' '))
      STDERR.printf("D: %s\n", cmd) if $DEBUG
      IO.popen(cmd) do |f|
        until f.eof? do
          s = f.gets
          yield s.chomp unless s.nil?
        end
      end
    end # def repoquery

  end # class YumRepos

end # module FontPackages