include REXML
-class Pkginfo
+class Dependency
attr_accessor :name, :op, :version
- def initialize name=nil, op=nil, version=nil
+ def initialize( name=nil, op=nil, version=nil )
@name = name
@op = op
@version = version
end
def to_s
- "name=#{@name} op=#{@op} version=#{@version}"
+ "<name>=#{@name} <op>=#{@op} <version>=#{@version}\n"
end
end
-class Provides < Pkginfo
+class Provides < Dependency
@@count = 0
- def initialize name=nil, op=nil, version=nil
+ def initialize( name=nil, op=nil, version=nil )
super
@@count += 1
end
@@count
end
def to_s
- '[provides] ' + super
+ ' [p] ' + super
end
end
-class Conflicts < Pkginfo
+class Conflicts < Dependency
@@count = 0
- def initialize name=nil, op=nil, version=nil
+ def initialize( name=nil, op=nil, version=nil )
super
@@count += 1
end
@@count
end
def to_s
- '[conflicts] ' + super
+ ' [c] ' + super
end
end
-class Requires < Pkginfo
+class Requires < Dependency
@@count = 0
- def initialize name=nil, op=nil, version=nil
+ def initialize( name=nil, op=nil, version=nil )
super
@@count += 1
end
@@count
end
def to_s
- '[requires] ' + super
+ ' [r] ' + super
end
end
+class Package
+ attr_accessor :type, :name, :arch, :epoch, :ver, :rel, :chksum, :chktype, :pkgid, :summary, :descr, :provides, :conflicts, :requires
+
+ @@count = 0
+
+ def initialize( name )
+ @name = name
+ @type = 'rpm'
+ @arch = 'i386'
+ @epoch = 0
+ @ver = 1 # Version
+ @rel = 1 # Release
+ @chksum = 0 # Checksum
+ @chktype = 'md5' # Checksum type; not needed for now; helix uses md5, yum uses sha-1
+ @pkgid = 'NO' # YES if chksum exists
+ @summary = 'Summary n/a'
+ @descr = 'Description n/a' # Package description
+ @provides = Array.new
+ @conflicts = Array.new
+ @requires = Array.new
+ @@count += 1
+ end
+
+ def Package.count
+ @@count
+ end
+
+ def add_provides( name, op, version )
+ @provides << Provides.new( name, op, version )
+ end
+
+ def add_conflicts( name, op, version )
+ @conflicts << Conflicts.new( name, op, version )
+ end
+
+ def add_requires( name, op, version )
+ @requires << Requires.new( name, op, version )
+ end
+
+ def to_s
+ "[Package] <name>=#{@name}\n#{@provides}#{@conflicts}#{@requires}"
+ end
+
+end
+
# global defaults
show = false
output = Document.new
-pkg_name = Array.new
-md5sum = Array.new
-epoch = Array.new
-version = Array.new
-release = Array.new
-provides = Array.new
-requires = Array.new
-conflicts = Array.new
+packages = Array.new
+
# get all needed elements
-XPath.each( input, '//name' ) { |elem| pkg_name.push elem.text }
-XPath.each( input, '//md5sum' ) { |elem| md5sum.push elem.text }
-XPath.each( input, '//epoch' ) { |elem| epoch.push elem.text }
-XPath.each( input, '//version' ) { |elem| version.push elem.text }
-XPath.each( input, '//release' ) { |elem| release.push elem.text }
-# TODO provides
-XPath.each( input, '//provides' ) do |elem|
- count_dep = 0
- #puts elem
- elem.elements.each('dep') do |dep|
- provides[count_dep] = Provides.new( dep.attributes['name'],
- dep.attributes['op'],
- dep.attributes['version'] )
- #puts dep.attributes['name']
- #print dep.attributes['op']
- #puts dep.attributes['version']
- count_dep += 1
+current = XPath.first( input, '/channel/subchannel/package' )
+while current != nil
+ pkg = Package.new( current.elements['name'].text )
+
+ # get dependencies
+ XPath.each( current, 'provides/dep' ) do |p|
+ pkg.add_provides( p.attributes['name'], p.attributes['op'], p.attributes['version'] )
end
+ XPath.each( current, 'conflicts/dep' ) do |c|
+ pkg.add_conflicts( c.attributes['name'], c.attributes['op'], c.attributes['version'] )
+ end
+ XPath.each( current, 'requires/dep' ) do |r|
+ pkg.add_requires( r.attributes['name'], r.attributes['op'], r.attributes['version'] )
+ end
+
+ # add current package to packages-array
+ packages << pkg
+
+ current = current.next_element
end
-# TODO requires
-#XPath.each( input, '//requires' ) { |elem| puts elem }
-# TODO conflicts
-#XPath.each( input, '//conflicts' ) { |elem| puts elem }
+puts packages
-pkg_name.reverse!
-md5sum.reverse!
-epoch.reverse!
-version.reverse!
-release.reverse!
-#provides.reverse!
-#requires.reverse!
-#conflicts.reverse!
+#XPath.each( input, '//md5sum' ) { |elem| md5sum.push elem.text }
+#XPath.each( input, '//epoch' ) { |elem| epoch.push elem.text }
+#XPath.each( input, '//version' ) { |elem| version.push elem.text }
+#XPath.each( input, '//release' ) { |elem| release.push elem.text }
- # debug
- puts provides
output << XMLDecl.new
output << Element.new( 'metadata' )
output.root.add_attribute( 'xmlns', 'http://linux.duke.edu/metadata/common' )
output.root.add_attribute( 'xmlns:rpm', 'http://linux.duke.edu/metadata/rpm' )
-output.root.add_attribute( 'packages', pkg_name.length )
-
-
-while !pkg_name.empty?
- pkg = Element.new( 'package' )
- pkg.add_attribute( 'type', 'rpm' )
- name = Element.new( 'name' ).add_text pkg_name.pop
- arch = Element.new( 'arch' ).add_text 'i386'
- ver = Element.new( 'version' )
- ver.add_attribute( 'epoch', epoch.pop )
- ver.add_attribute( 'ver', version.pop )
- ver.add_attribute( 'rel', release.pop )
- csum = Element.new( 'checksum' ).add_text md5sum.pop
- csum.add_attribute( 'pkgid', 'YES' )
- csum.add_attribute( 'type', 'md5sum' )
-
- pkg << name
- pkg << arch
- pkg << ver
-# pkg << csum # not needed at the moment; helix uses md5, yum uses sha-1
-
- output.root << pkg
-end
+output.root.add_attribute( 'packages', Package.count )
+
+
+# while !pkg_name.empty?
+# pkg = Element.new( 'package' )
+# pkg.add_attribute( 'type', 'rpm' )
+# name = Element.new( 'name' ).add_text pkg_name.pop
+# arch = Element.new( 'arch' ).add_text 'i386'
+# ver = Element.new( 'version' )
+# ver.add_attribute( 'epoch', epoch.pop )
+# ver.add_attribute( 'ver', version.pop )
+# ver.add_attribute( 'rel', release.pop )
+# csum = Element.new( 'checksum' ).add_text md5sum.pop
+# csum.add_attribute( 'pkgid', 'YES' )
+# csum.add_attribute( 'type', 'md5sum' )
+#
+# pkg << name
+# pkg << arch
+# pkg << ver
+# # pkg << csum
+#
+# output.root << pkg
+# end
# write formated xml into file or stdout
if not outfile.nil?
output.write( outfile, 0 )
+ outfile.close
else
output.write( $stdout, 0 )
end
-puts " "
+puts ';-)'
rescue => exc
exit 1
ensure
infile.close
- outfile.close
end