Class: EnhanceRepo::RpmMd::Patterns

Inherits:
Data
  • Object
show all
Defined in:
lib/enhance_repo/rpm_md/patterns.rb

Instance Method Summary collapse

Methods inherited from Data

#metadata_filename, #name, #should_compress?

Methods included from Logger

#log

Constructor Details

#initialize(config) ⇒ Patterns

Returns a new instance of Patterns.



44
45
46
47
48
49
50
# File 'lib/enhance_repo/rpm_md/patterns.rb', line 44

def initialize(config)
  @dir = config.dir
  @basedir = config.updatesbasedir

  # update files
  @patterns = Set.new
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/enhance_repo/rpm_md/patterns.rb', line 52

def empty?
  @patterns.empty?
end

#generate_patterns(files, outputdir) ⇒ Object

generates a patterns.xml from a list of package names it compares the last version of those package names with their previous ones

outputdir is the directory where to save the patch to.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/enhance_repo/rpm_md/patterns.rb', line 78

def generate_patterns(files, outputdir)
  pats = []
  files.each do |file|
    raise "#{file} does not exist" unless File.exist?(file)
    Zlib::GzipReader.open(file) do |gz|
      pats += EnhanceRepo::Susetags::PatternReader.read_patterns_from_tags(gz)
    end
  end

  FileUtils.mkdir_p(outputdir)
  pats.each do |pat|
    pattern_filename = File.join(outputdir, "pattern-#{pat.name}_0.xml")
    File.open(pattern_filename, 'w') do |f|
      log.info "write pattern #{pattern_filename}"
      pat.write_xml(f)
    end
  end
end

#read_repoparts(opts = {}) ⇒ Object

add all patterns in a repoparts directory by default look in repoparts/ otherwise pass the :repoparts_path option



63
64
65
66
67
68
69
70
71
# File 'lib/enhance_repo/rpm_md/patterns.rb', line 63

def read_repoparts(opts = {})
  repoparts_path = opts[:repoparts_path] || File.join(@dir, 'repoparts')
  log.info "Reading patterns parts from #{repoparts_path}"
  Dir[File.join(repoparts_path, 'pattern-*.xml')].each do |patternfile|
    log.info("`-> adding pattern #{patternfile}")
    @patterns << patternfile
  end
  # end of directory iteration
end

#sizeObject



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

def size
  @patterns.size
end

#split_patterns(outputdir) ⇒ Object

splits the patterns.xml file into serveral pattern files it writes those files into outputdir output filenames will be pattern-name_<num>.xml where name is the name of the pattern

outputdir is the directory where to save the pattern to.



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
# File 'lib/enhance_repo/rpm_md/patterns.rb', line 103

def split_patterns(outputdir)
  FileUtils.mkdir_p outputdir
  patternsfile = File.join(@dir, )

  # we can't split without an patterns file
  raise "#{patternsfile} does not exist" unless File.exist?(patternsfile)
  Zlib::GzipReader.open(patternsfile) do |gz|
    document = REXML::Document.new(gz)
    root = document.root
    root.each_element('pattern') do |patternElement|
      name = nil
      patternElement.each_element('name') do |elementName|
        name = elementName.text
      end
      if name.nil?
        log.warning 'No name found. Setting name to NON_NAME_FOUND'
        name = 'NON_NAME_FOUND'
      end
      version = 0
      updatefilename = ''
      while File.exist?(patternfilename = File.join(outputdir, "pattern-#{name}_#{version}.xml"))
        version += 1
      end
      log.info "Saving pattern part to '#{patternfilename}'."
      File.open(patternfilename, 'w') do |patternfile|
        patternfile << patternElement
        patternfile << "\n"
      end
    end
  end
end

#write(file) ⇒ Object

write a update out



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/enhance_repo/rpm_md/patterns.rb', line 136

def write(file)
  builder = Builder::XmlMarkup.new(target: file, indent: 2)
  builder.instruct!
  builder.patterns('xmlns' => 'http://novell.com/package/metadata/suse/pattern',
                   'xmlns:rpm' => 'http://linux.duke.edu/metadata/rpm') do |_b|
    pattern_regex = Regexp.new('<pattern\s+xmlns.+>\s*$')
    @patterns.each do |pattern|
      File.open(pattern).each_line do |line|
        next if line.start_with?('<?xml')
        file << if line.match(pattern_regex)
                  # all single pattern have the namespace attributes
                  # we can remove them in the combined file
                  "<pattern>\n"
                else
                  line
                end
      end
    end
  end # done builder
end