Class: EnhanceRepo::RpmMd::Index

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

Overview

represents the repomd index

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log

Methods inherited from Data

#metadata_filename, #name

Constructor Details

#initializeIndex

constructor log logger object



41
42
43
44
# File 'lib/enhance_repo/rpm_md/index.rb', line 41

def initialize
  super('repomd')
  @resources = []
end

Instance Attribute Details

#keywordsObject

Returns the value of attribute keywords.



36
37
38
# File 'lib/enhance_repo/rpm_md/index.rb', line 36

def keywords
  @keywords
end

#productsObject

Returns the value of attribute products.



36
37
38
# File 'lib/enhance_repo/rpm_md/index.rb', line 36

def products
  @products
end

#resourcesObject

Returns the value of attribute resources.



37
38
39
# File 'lib/enhance_repo/rpm_md/index.rb', line 37

def resources
  @resources
end

Instance Method Details

#add_file_resource(abspath, path, type = nil) ⇒ Object

add a file resource. Takes care of setting all the metadata.



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

def add_file_resource(abspath, path, type = nil)
  r = Resource.new
  r.type = type
  # figure out the type of resource
  # the extname to remove is different if it is gzipped
  ext = File.extname(path)
  base = File.basename(path, ext)

  # if it was gzipped, repeat the operation
  # to get the real basename
  if ext == '.gz'
    ext = File.extname(base)
    base = File.basename(base, ext)
  end

  r.type = base if r.type.nil?
  r.location = path
  r.timestamp = File.mtime(abspath).to_i.to_s
  r.checksum = EnhanceRepo::ConfigOpts.instance.digest_class.hexdigest(File.new(abspath).read)
  r.openchecksum = r.checksum
  r.size = File.size(abspath)
  r.opensize = r.size
  if File.extname(abspath) == '.gz'
    # we have a different openchecksum
    r.openchecksum = EnhanceRepo::ConfigOpts.instance.digest_class.hexdigest(Zlib::GzipReader.new(File.new(abspath)).read)
    r.opensize = Zlib::GzipReader.new(File.new(abspath)).read.bytesize
  end
  add_resource(r)
end

#add_resource(r) ⇒ Object

add resource any resource of the same location is overwritten



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/enhance_repo/rpm_md/index.rb', line 86

def add_resource(r)
  # first check if this resource is already in
  # if yes then override it
  if (index = @resources.index(r)).nil?
    # add it
    @resources << r
  else
    # replace it
    # log.warn("Resource #{r.location} already exists. Replacing.")
    @resources[index] = r
  end
end

#read_file(file) ⇒ Object

read data from a file



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

def read_file(file)
  doc = REXML::Document.new(file)
  doc.elements.each('repomd/data') do |datael|
    resource = Resource.new
    resource.type = datael.attributes['type']
    datael.elements.each do |attrel|
      case attrel.name
      when 'location'
        resource.location = attrel.attributes['href']
      when 'checksum'
        resource.checksum = attrel.text
      when 'timestamp'
        resource.timestamp = attrel.text
      when 'open-checksum'
        resource.openchecksum = attrel.text
      when 'size'
        resource.size = attrel.text
      when 'open-size'
        resource.opensize = attrel.text
      when 'database_version'
        resource.database_version = attrel.text
      else
        raise "unknown tag #{attrel.name}"
      end # case
    end # iterate over data subelements
    add_resource(resource)
  end # iterate over data elements
end

#should_compress?Boolean

Reimplemented from EnhanceRepo::RpmMd::Data

Returns:

  • (Boolean)


47
48
49
# File 'lib/enhance_repo/rpm_md/index.rb', line 47

def should_compress?
  false
end

#write(file) ⇒ Object

write the index to xml file



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/enhance_repo/rpm_md/index.rb', line 130

def write(file)
  builder = Builder::XmlMarkup.new(target: file, indent: 2)
  builder.instruct!
  builder.repomd('xmlns' => 'http://linux.duke.edu/metadata/repo') do |b|
    @resources.each do |resource|
      b.data('type' => resource.type) do
        b.location('href' => resource.location)
        b.checksum(resource.checksum, 'type' => EnhanceRepo::ConfigOpts.instance.digest_name)
        b.timestamp(resource.timestamp)
        b.size(resource.size) if resource.size
        b.tag!('open-size', resource.opensize) if resource.opensize
        b.tag!('open-checksum', resource.openchecksum, 'type' => EnhanceRepo::ConfigOpts.instance.digest_name)
        b.tag!('database_version', resource.database_version) if resource.database_version
      end
    end
  end # builder
end