Class: EnhanceRepo::RpmMd::Repo

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/enhance_repo/rpm_md/repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log

Constructor Details

#initialize(config) ⇒ Repo

Returns a new instance of Repo.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/enhance_repo/rpm_md/repo.rb', line 57

def initialize(config)
  @dir = config.dir
  @outputdir = config.outputdir

  @index = Index.new
  repomdfile = File.join(@dir, @index.)
  # populate the index
  @index.read_file(File.new(repomdfile)) if File.exist?(repomdfile)

  @primary = Primary.new(config.dir)
  @primary.indent = config.indent

  @filelists = FileLists.new(config.dir)
  @other = Other.new(config.dir)
  @susedata = SuseData.new(config.dir)
  @updateinfo = UpdateInfo.new(config)
  @suseinfo = SuseInfo.new(config.dir)
  @deltainfo = DeltaInfo.new(config.dir)
  @products = Products.new(config.dir)
  @patterns = Patterns.new(config)
end

Instance Attribute Details

#deltainfoObject (readonly)

extensions



56
57
58
# File 'lib/enhance_repo/rpm_md/repo.rb', line 56

def deltainfo
  @deltainfo
end

#filelistsObject (readonly)

extensions



56
57
58
# File 'lib/enhance_repo/rpm_md/repo.rb', line 56

def filelists
  @filelists
end

#indexObject

Returns the value of attribute index.



53
54
55
# File 'lib/enhance_repo/rpm_md/repo.rb', line 53

def index
  @index
end

#otherObject (readonly)

extensions



56
57
58
# File 'lib/enhance_repo/rpm_md/repo.rb', line 56

def other
  @other
end

#patternsObject (readonly)

extensions



56
57
58
# File 'lib/enhance_repo/rpm_md/repo.rb', line 56

def patterns
  @patterns
end

#primaryObject (readonly)

extensions



56
57
58
# File 'lib/enhance_repo/rpm_md/repo.rb', line 56

def primary
  @primary
end

#productsObject (readonly)

extensions



56
57
58
# File 'lib/enhance_repo/rpm_md/repo.rb', line 56

def products
  @products
end

#susedataObject (readonly)

extensions



56
57
58
# File 'lib/enhance_repo/rpm_md/repo.rb', line 56

def susedata
  @susedata
end

#suseinfoObject (readonly)

extensions



56
57
58
# File 'lib/enhance_repo/rpm_md/repo.rb', line 56

def suseinfo
  @suseinfo
end

#updateinfoObject (readonly)

extensions



56
57
58
# File 'lib/enhance_repo/rpm_md/repo.rb', line 56

def updateinfo
  @updateinfo
end

Instance Method Details

#sign(keyid) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/enhance_repo/rpm_md/repo.rb', line 79

def sign(keyid)
  # check if the index is written to disk
  repomdfile = File.join(@dir, @index.)
  raise "#{repomdfile} does not exist." unless File.exist?(repomdfile)
  # call gpg to sign the repository
  `gpg -sab -u #{keyid} -o '#{repomdfile}.asc' '#{repomdfile}'`
  if !File.exist?("#{repomdfile}.asc")
    log.info "Could't not generate signature #{repomdfile}.asc"
    exit(1)
  else
    log.info "#{repomdfile}.asc signature generated"
  end

  # now export the public key
  `gpg --export -a -o '#{repomdfile}.key' #{keyid}`

  if !File.exist?("#{repomdfile}.key")
    log.info "Could't not generate public key #{repomdfile}.key"
    exit(1)
  else
    log.info "#{repomdfile}.key public key generated"
  end
end

#writeObject



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
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/enhance_repo/rpm_md/repo.rb', line 103

def write
  datas = [@primary, @filelists, @other, @updateinfo,
           @susedata, @suseinfo, @deltainfo, @products, @patterns]

  # select the datas that are not empty
  # those need to be saved
  non_empty_data = datas.reject(&:empty?)
  # files present in the index, which were changed
  changed_files = []
  # files present on disk, but not in the index
  missing_files = []
  # files present in the index, but not on disk
  superflous_files = []

  # now look for files that changed or dissapeared
  Dir.chdir(@dir) do
    # look all files except the index itself
     = Dir['repodata/*.xml*'].reject do |x|
      x =~ /#{@index.}/ ||
        x =~ /\.key$/ ||
        x =~ /\.asc$/
    end
    # remove datas in the index not present in the disk
    @index.resources.reject! do |resource|
      reject = !.include?(resource.location)
      log.info "Removing not existing #{resource.location} from index" if reject
      reject
    end

    non_empty_files = non_empty_data.map(&:metadata_filename)
    # ignore it if it is already in the non_empty_list
    # as it will be added to the index anyway
    .reject! { |x| non_empty_files.include?(x) }
    .each do ||
      # find the indexed resource for this file
      indexed_resource = @index.resources.select { |x| x.location ==  }.first
      # add it to the list of changed resources if the timestamp
      # are differents
      if indexed_resource.nil?
        missing_files << 
        next
      elsif File.mtime(File.join(@dir, )).to_i != indexed_resource.timestamp.to_i
        changed_files << 
        next
      end
    end
  end

  # write down changed datas
  non_empty_data.each do |data|
    write_gz_extension_file(data)
  end

  # update the index
  non_empty_data.each do |d|
    log.info "Adding #{d.} to #{@index.} index"
    @index.add_file_resource(File.join(@outputdir, d.), d.)
  end

  missing_files.each do |f|
    log.info "Adding missing #{f} to #{@index.} index"
    @index.add_file_resource(File.join(@outputdir, f), f)
  end

  changed_files.each do |f|
    log.info "Replacing changed #{f} on #{@index.} index"
    @index.add_file_resource(File.join(@outputdir, f), f)
  end

  # now write the index
  unless File.exist?(File.dirname(File.join(@outputdir, @index.)))
    FileUtils.mkdir_p(File.dirname(File.join(@outputdir, @index.)))
  end
  File.open(File.join(@outputdir, @index.), 'w') do |f|
    log.info "Saving #{@index.} .."
    @index.write(f)
  end
end

#write_gz_extension_file(data) ⇒ Object

writes an extension to an xml filename if the extension is not empty



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/enhance_repo/rpm_md/repo.rb', line 184

def write_gz_extension_file(data)
  filename = Pathname.new(File.join(@outputdir, data.))
  FileUtils.mkdir_p filename.dirname
  log.info "Saving #{filename} .."
  unless filename.dirname.exist?
    log.info "Creating non existing #{filename.dirname} .."
    filename.dirname.mkpath
  end
  # compress the output
  Zlib::GzipWriter.open(filename) do |gz|
    data.write(gz)
  end
end