enhancerepo 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +27 -0
  3. data/bin/enhancerepo +2 -3
  4. data/lib/enhance_repo.rb +8 -13
  5. data/lib/enhance_repo/array_arg.rb +85 -0
  6. data/lib/enhance_repo/cli.rb +23 -78
  7. data/lib/enhance_repo/config_opts.rb +207 -13
  8. data/lib/enhance_repo/logger.rb +14 -10
  9. data/lib/enhance_repo/other.rb +11 -12
  10. data/lib/enhance_repo/package_id.rb +21 -12
  11. data/lib/enhance_repo/pattern.rb +50 -0
  12. data/lib/enhance_repo/product_id.rb +3 -1
  13. data/lib/enhance_repo/rpm_md.rb +3 -1
  14. data/lib/enhance_repo/rpm_md/data.rb +6 -7
  15. data/lib/enhance_repo/rpm_md/delta_info.rb +58 -67
  16. data/lib/enhance_repo/rpm_md/extra_primary_data.rb +4 -4
  17. data/lib/enhance_repo/rpm_md/file_lists.rb +12 -13
  18. data/lib/enhance_repo/rpm_md/index.rb +32 -27
  19. data/lib/enhance_repo/rpm_md/other.rb +7 -8
  20. data/lib/enhance_repo/rpm_md/pattern_writer.rb +131 -0
  21. data/lib/enhance_repo/rpm_md/patterns.rb +158 -0
  22. data/lib/enhance_repo/rpm_md/primary.rb +32 -33
  23. data/lib/enhance_repo/rpm_md/products.rb +19 -24
  24. data/lib/enhance_repo/rpm_md/repo.rb +33 -36
  25. data/lib/enhance_repo/rpm_md/resource.rb +6 -6
  26. data/lib/enhance_repo/rpm_md/suse_data.rb +41 -50
  27. data/lib/enhance_repo/rpm_md/suse_info.rb +12 -15
  28. data/lib/enhance_repo/rpm_md/update.rb +65 -36
  29. data/lib/enhance_repo/rpm_md/update_info.rb +38 -42
  30. data/lib/enhance_repo/rpm_md/update_smart_fields.rb +24 -24
  31. data/lib/enhance_repo/susetags/pattern_reader.rb +142 -0
  32. data/lib/enhance_repo/version.rb +5 -0
  33. data/lib/enhance_repo/xml_comparer.rb +28 -24
  34. metadata +190 -221
  35. data/CHANGELOG.rdoc +0 -18
  36. data/Manifest.txt +0 -85
  37. data/README +0 -10
  38. data/README.rdoc +0 -15
  39. data/Rakefile +0 -48
  40. data/TODO.rdoc +0 -4
  41. data/lib/tempdir.rb +0 -62
  42. data/lib/tempdir/tempfile.rb +0 -21
  43. data/test/all_tests.rb +0 -27
  44. data/test/data/packages/a-1.0.spec +0 -19
  45. data/test/data/packages/a-2.0.spec +0 -24
  46. data/test/data/repodata/update-test-11.1/filelists.xml.gz +0 -0
  47. data/test/data/repodata/update-test-11.1/other.xml.gz +0 -0
  48. data/test/data/repodata/update-test-11.1/primary.xml.gz +0 -0
  49. data/test/data/repodata/update-test-11.1/repomd.xml +0 -21
  50. data/test/data/repomd.xml +0 -21
  51. data/test/data/rpms/repo-1/a-1.0-0.x86_64.rpm +0 -0
  52. data/test/data/rpms/repo-1/a-1.0_2.0-0_0.x86_64.delta.rpm +0 -0
  53. data/test/data/rpms/repo-1/a-2.0-0.x86_64.rpm +0 -0
  54. data/test/data/rpms/repo-1/repodata/deltainfo.xml.gz +0 -0
  55. data/test/data/rpms/repo-1/repodata/deltainfo.xml.gz.gz +0 -0
  56. data/test/data/rpms/repo-1/repodata/filelists.xml.gz +0 -0
  57. data/test/data/rpms/repo-1/repodata/other.xml.gz +0 -0
  58. data/test/data/rpms/repo-1/repodata/primary.xml.gz +0 -0
  59. data/test/data/rpms/repo-1/repodata/repomd.xml +0 -45
  60. data/test/data/rpms/repo-1/repodata/susedata.xml.gz +0 -0
  61. data/test/data/rpms/repo-1/repodata/updateinfo.xml.gz +0 -0
  62. data/test/data/rpms/repo-1/repoparts/update-a-1.xml +0 -22
  63. data/test/data/rpms/repo-with-product/a-1.0-0.x86_64.rpm +0 -0
  64. data/test/data/rpms/repo-with-product/openSUSE-release-11.2-1.6.i586.rpm +0 -0
  65. data/test/data/rpms/repo-with-product/openSUSE-release-dvd-11.2-1.6.i586.rpm +0 -0
  66. data/test/data/rpms/repo-with-product/repodata/filelists.xml.gz +0 -0
  67. data/test/data/rpms/repo-with-product/repodata/other.xml.gz +0 -0
  68. data/test/data/rpms/repo-with-product/repodata/primary.xml.gz +0 -0
  69. data/test/data/rpms/repo-with-product/repodata/products.xml.gz +0 -0
  70. data/test/data/rpms/repo-with-product/repodata/repomd.xml +0 -27
  71. data/test/data/rpms/update-test-11.1/update-test-affects-package-manager.rpm +0 -0
  72. data/test/data/rpms/update-test-11.1/update-test-interactive.rpm +0 -0
  73. data/test/data/rpms/update-test-11.1/update-test-optional.rpm +0 -0
  74. data/test/data/rpms/update-test-11.1/update-test-reboot-needed.rpm +0 -0
  75. data/test/data/rpms/update-test-11.1/update-test-security.rpm +0 -0
  76. data/test/data/rpms/update-test-11.1/update-test-trival.rpm +0 -0
  77. data/test/data/rpms/update-test-factory/update-test-affects-package-manager-0-9999.1.2.noarch.rpm +0 -0
  78. data/test/data/rpms/update-test-factory/update-test-interactive-0-9999.1.2.noarch.rpm +0 -0
  79. data/test/data/rpms/update-test-factory/update-test-optional-0-9999.1.2.noarch.rpm +0 -0
  80. data/test/data/rpms/update-test-factory/update-test-reboot-needed-0-9999.1.2.noarch.rpm +0 -0
  81. data/test/data/rpms/update-test-factory/update-test-relogin-suggested-0-9999.1.2.noarch.rpm +0 -0
  82. data/test/data/rpms/update-test-factory/update-test-security-0-9999.1.2.noarch.rpm +0 -0
  83. data/test/deltainfo_test.rb +0 -50
  84. data/test/extensions_test.rb +0 -31
  85. data/test/index_test.rb +0 -51
  86. data/test/primary_test.rb +0 -51
  87. data/test/products_test.rb +0 -51
  88. data/test/repo_test.rb +0 -72
  89. data/test/repomdindex_test.rb +0 -58
  90. data/test/rpmmd_test.rb +0 -60
  91. data/test/susedata_test.rb +0 -53
  92. data/test/tempdir_test.rb +0 -62
  93. data/test/test_helper.rb +0 -53
  94. data/test/update_test.rb +0 -80
  95. data/test/updateinfo_test.rb +0 -59
@@ -1,5 +1,7 @@
1
+ # Encoding: utf-8
2
+
1
3
  #--
2
- #
4
+ #
3
5
  # enhancerepo is a rpm-md repository metadata tool.
4
6
  # Copyright (C) 2008, 2009 Novell Inc.
5
7
  # Author: Duncan Mac-Vicar P. <dmacvicar@suse.de>
@@ -28,18 +30,16 @@ require 'builder'
28
30
 
29
31
  module EnhanceRepo
30
32
  module RpmMd
31
-
32
33
  # represents a set non standard data tags
33
34
  # but it is not part of the standard, yet still associated
34
35
  # with a particular package (so with primary.xml semantics
35
36
  class ExtraPrimaryData
36
37
  include Logger
37
-
38
+
38
39
  # initialize the extra data with a name
39
40
  def initialize(name)
40
41
  @name = name
41
42
  end
42
43
  end
43
-
44
44
  end
45
45
  end
@@ -1,5 +1,7 @@
1
+ # Encoding: utf-8
2
+
1
3
  #--
2
- #
4
+ #
3
5
  # enhancerepo is a rpm-md repository metadata tool.
4
6
  # Copyright (C) 2008, 2009 Novell Inc.
5
7
  # Author: Duncan Mac-Vicar P. <dmacvicar@suse.de>
@@ -25,17 +27,15 @@
25
27
 
26
28
  module EnhanceRepo
27
29
  module RpmMd
28
-
29
30
  # represents
30
31
  # filelist data
31
32
  #
32
33
  # See:
33
34
  # http://en.opensuse.org/Standards/Rpm_Metadata
34
35
  #
35
- class FileLists
36
-
36
+ class FileLists < Data
37
37
  include Logger
38
-
38
+
39
39
  def initialize(dir)
40
40
  @dir = dir
41
41
  @rpmfiles = []
@@ -44,7 +44,7 @@ module EnhanceRepo
44
44
  def read
45
45
  Dir["#{@dir}/**/*.rpm"].each do |rpmfile|
46
46
  @rpmfiles << rpmfile
47
- end
47
+ end
48
48
  end
49
49
 
50
50
  def empty?
@@ -52,9 +52,9 @@ module EnhanceRepo
52
52
  end
53
53
 
54
54
  def write_package(file, rpmfile)
55
- b = Builder::XmlMarkup.new(:target=>file, :indent=>2, :initial=>2)
55
+ b = Builder::XmlMarkup.new(target: file, indent: 2, initial: 2)
56
56
  pkgid = PackageId.new(rpmfile)
57
- b.package('pkgid'=>pkgid.checksum, 'name' => pkgid.name, 'arch'=>pkgid.arch ) do | b |
57
+ b.package('pkgid' => pkgid.checksum, 'name' => pkgid.name, 'arch' => pkgid.arch) do
58
58
  b.version('epoch' => pkgid.version.e, 'ver' => pkgid.version.v, 'rel' => pkgid.version.r)
59
59
  pkgid.files.each do |f|
60
60
  b.file f
@@ -62,20 +62,19 @@ module EnhanceRepo
62
62
  end
63
63
  # done package tag
64
64
  end
65
-
65
+
66
66
  # write filelists.xml
67
67
  def write(file)
68
- builder = Builder::XmlMarkup.new(:target=>file, :indent=>2)
68
+ builder = Builder::XmlMarkup.new(target: file, indent: 2)
69
69
  builder.instruct!
70
- xml = builder.tag!("filelists", 'xmlns' => 'xmlns="http://linux.duke.edu/metadata/filelists"', 'packages'=> @rpmfiles.size ) do |b|
70
+ builder.filelists('xmlns' => 'http://linux.duke.edu/metadata/filelists',
71
+ 'packages' => @rpmfiles.size) do |_b|
71
72
  @rpmfiles.each do |rpmfile|
72
73
  write_package(file, rpmfile)
73
74
  end
74
75
  # next package
75
76
  end
76
77
  end
77
-
78
78
  end
79
-
80
79
  end
81
80
  end
@@ -1,5 +1,7 @@
1
+ # Encoding: utf-8
2
+
1
3
  #--
2
- #
4
+ #
3
5
  # enhancerepo is a rpm-md repository metadata tool.
4
6
  # Copyright (C) 2008, 2009 Novell Inc.
5
7
  # Author: Duncan Mac-Vicar P. <dmacvicar@suse.de>
@@ -27,15 +29,13 @@ require 'enhance_repo/rpm_md/resource'
27
29
 
28
30
  module EnhanceRepo
29
31
  module RpmMd
30
-
31
32
  # represents the repomd index
32
33
  class Index < Data
33
-
34
34
  include EnhanceRepo::Logger
35
-
35
+
36
36
  attr_accessor :products, :keywords
37
37
  attr_accessor :resources
38
-
38
+
39
39
  # constructor
40
40
  # log logger object
41
41
  def initialize
@@ -47,10 +47,10 @@ module EnhanceRepo
47
47
  def should_compress?
48
48
  false
49
49
  end
50
-
50
+
51
51
  # add a file resource. Takes care of setting
52
- # all the metadata.
53
- def add_file_resource(abspath, path, type=nil)
52
+ # all the metadata.
53
+ def add_file_resource(abspath, path, type = nil)
54
54
  r = Resource.new
55
55
  r.type = type
56
56
  # figure out the type of resource
@@ -64,18 +64,20 @@ module EnhanceRepo
64
64
  ext = File.extname(base)
65
65
  base = File.basename(base, ext)
66
66
  end
67
-
67
+
68
68
  r.type = base if r.type.nil?
69
69
  r.location = path
70
70
  r.timestamp = File.mtime(abspath).to_i.to_s
71
- r.checksum = Digest::SHA1.hexdigest(File.new(abspath).read)
71
+ r.checksum = EnhanceRepo::ConfigOpts.instance.digest_class.hexdigest(File.new(abspath).read)
72
72
  r.openchecksum = r.checksum
73
+ r.size = File.size(abspath)
74
+ r.opensize = r.size
73
75
  if File.extname(abspath) == '.gz'
74
76
  # we have a different openchecksum
75
- r.openchecksum = Digest::SHA1.hexdigest(Zlib::GzipReader.new(File.new(abspath)).read)
77
+ r.openchecksum = EnhanceRepo::ConfigOpts.instance.digest_class.hexdigest(Zlib::GzipReader.new(File.new(abspath)).read)
78
+ r.opensize = Zlib::GzipReader.new(File.new(abspath)).read.bytesize
76
79
  end
77
80
  add_resource(r)
78
-
79
81
  end
80
82
 
81
83
  # add resource
@@ -89,11 +91,11 @@ module EnhanceRepo
89
91
  @resources << r
90
92
  else
91
93
  # replace it
92
- #log.warn("Resource #{r.location} already exists. Replacing.")
94
+ # log.warn("Resource #{r.location} already exists. Replacing.")
93
95
  @resources[index] = r
94
96
  end
95
97
  end
96
-
98
+
97
99
  # read data from a file
98
100
  def read_file(file)
99
101
  doc = REXML::Document.new(file)
@@ -110,6 +112,12 @@ module EnhanceRepo
110
112
  resource.timestamp = attrel.text
111
113
  when 'open-checksum'
112
114
  resource.openchecksum = attrel.text
115
+ when 'size'
116
+ resource.size = attrel.text
117
+ when 'open-size'
118
+ resource.opensize = attrel.text
119
+ when 'database_version'
120
+ resource.database_version = attrel.text
113
121
  else
114
122
  raise "unknown tag #{attrel.name}"
115
123
  end # case
@@ -117,28 +125,25 @@ module EnhanceRepo
117
125
  add_resource(resource)
118
126
  end # iterate over data elements
119
127
  end
120
-
128
+
121
129
  # write the index to xml file
122
130
  def write(file)
123
- builder = Builder::XmlMarkup.new(:target=>file, :indent=>2)
131
+ builder = Builder::XmlMarkup.new(target: file, indent: 2)
124
132
  builder.instruct!
125
- xml = builder.repomd('xmlns' => "http://linux.duke.edu/metadata/repo") do |b|
133
+ builder.repomd('xmlns' => 'http://linux.duke.edu/metadata/repo') do |b|
126
134
  @resources.each do |resource|
127
- b.data('type' => resource.type) do |b|
135
+ b.data('type' => resource.type) do
128
136
  b.location('href' => resource.location)
129
- b.checksum(resource.checksum, 'type' => 'sha')
137
+ b.checksum(resource.checksum, 'type' => EnhanceRepo::ConfigOpts.instance.digest_name)
130
138
  b.timestamp(resource.timestamp)
131
- b.tag!('open-checksum', resource.openchecksum, 'type' => 'sha')
139
+ b.size(resource.size) if resource.size
140
+ b.tag!('open-size', resource.opensize) if resource.opensize
141
+ b.tag!('open-checksum', resource.openchecksum, 'type' => EnhanceRepo::ConfigOpts.instance.digest_name)
142
+ b.tag!('database_version', resource.database_version) if resource.database_version
132
143
  end
133
144
  end
134
-
135
- end #builder
136
-
145
+ end # builder
137
146
  end
138
-
139
147
  end
140
-
141
-
142
148
  end
143
-
144
149
  end
@@ -1,5 +1,7 @@
1
+ # Encoding: utf-8
2
+
1
3
  #--
2
- #
4
+ #
3
5
  # enhancerepo is a rpm-md repository metadata tool.
4
6
  # Copyright (C) 2008, 2009 Novell Inc.
5
7
  # Author: Duncan Mac-Vicar P. <dmacvicar@suse.de>
@@ -31,7 +33,6 @@ module EnhanceRepo
31
33
  # http://en.opensuse.org/Standards/Rpm_Metadata
32
34
  #
33
35
  class Other < Data
34
-
35
36
  def initialize(dir)
36
37
  @dir = dir
37
38
  @rpmfiles = []
@@ -40,7 +41,7 @@ module EnhanceRepo
40
41
  def read
41
42
  Dir["#{@dir}/**/*.rpm"].each do |rpmfile|
42
43
  @rpmfiles << rpmfile
43
- end
44
+ end
44
45
  end
45
46
 
46
47
  def empty?
@@ -49,12 +50,12 @@ module EnhanceRepo
49
50
 
50
51
  # write filelists.xml
51
52
  def write(file)
52
- builder = Builder::XmlMarkup.new(:target=>file, :indent=>2)
53
+ builder = Builder::XmlMarkup.new(target: file, indent: 2)
53
54
  builder.instruct!
54
- xml = builder.tag!("otherdata", 'xmlns' => 'xmlns="http://linux.duke.edu/metadata/other"', 'packages'=> @rpmfiles.size ) do |b|
55
+ builder.tag!('otherdata', 'xmlns' => 'xmlns="http://linux.duke.edu/metadata/other"', 'packages' => @rpmfiles.size) do |b|
55
56
  @rpmfiles.each do |rpmfile|
56
57
  pkgid = PackageId.new(rpmfile)
57
- b.package('pkgid'=>pkgid.checksum, 'name' => pkgid.name, 'arch'=>pkgid.arch ) do | b |
58
+ b.package('pkgid' => pkgid.checksum, 'name' => pkgid.name, 'arch' => pkgid.arch) do
58
59
  b.version('epoch' => pkgid.version.e, 'ver' => pkgid.version.v, 'rel' => pkgid.version.r)
59
60
  end
60
61
  # done package tag
@@ -62,8 +63,6 @@ module EnhanceRepo
62
63
  # next package
63
64
  end
64
65
  end
65
-
66
66
  end
67
-
68
67
  end
69
68
  end
@@ -0,0 +1,131 @@
1
+ # Encoding: utf-8
2
+
3
+ #--
4
+ #
5
+ # enhancerepo is a rpm-md repository metadata tool.
6
+ # Copyright (C) 2008, 2009 Novell Inc.
7
+ #
8
+ # Author: Michael Calmer <mc@suse.de>
9
+ #
10
+ # This program is free software; you can redistribute it and/or modify
11
+ # it under the terms of the GNU General Public License as published by
12
+ # the Free Software Foundation; either version 2 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # This program is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with this program; if not, write to the Free Software
22
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
24
+ # also available at http://www.gnu.org/copyleft/gpl.html.
25
+ #
26
+ #++
27
+ #
28
+ require 'nokogiri'
29
+ require 'yaml'
30
+ require 'prettyprint'
31
+ require 'stringio'
32
+
33
+ module EnhanceRepo
34
+ module RpmMd
35
+ # helper to write out a pattern in
36
+ # rpmmd format
37
+ module PatternWriter
38
+ def to_xml
39
+ buffer = StringIO.new
40
+ write_xml(buffer)
41
+ buffer.string
42
+ end
43
+
44
+ def write_xml(io = STDOUT)
45
+ PatternWriter.write_xml(self, io)
46
+ end
47
+
48
+ def self.write_xml_dependency(xml, pattern, name)
49
+ dep = name.to_sym
50
+ list = pattern.send(dep).keys.sort do |a, b|
51
+ puts a
52
+ if pattern.send(dep)[a] == 'pattern' &&
53
+ pattern.send(dep)[b] != 'pattern'
54
+ -1
55
+ else
56
+ a <=> b
57
+ end
58
+ end
59
+ unless list.empty?
60
+ xml['rpm'].send(dep) do
61
+ list.each do |pkg|
62
+ kind = pattern.send(dep)[pkg]
63
+ if kind == 'package'
64
+ xml['rpm'].entry('name' => pkg)
65
+ else
66
+ xml['rpm'].entry('name' => "#{kind}:#{pkg}")
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ def self.write_xml(pattern, io = STDOUT)
74
+ builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
75
+ xml.pattern('xmlns' => 'http://novell.com/package/metadata/suse/pattern',
76
+ 'xmlns:rpm' => 'http://linux.duke.edu/metadata/rpm') do
77
+ xml.name pattern.name
78
+ xml.arch pattern.architecture
79
+ xml.version 'epoch' => '0', 'ver' => pattern.version, 'rel' => pattern.release
80
+ xml.icon pattern.icon if pattern.icon
81
+ xml.order pattern.order
82
+ pattern.summary.each do |lang, text|
83
+ if lang.empty?
84
+ xml.summary text
85
+ else
86
+ xml.summary text, 'lang' => lang.to_s
87
+ end
88
+ end
89
+ pattern.description.each do |lang, text|
90
+ if lang.empty?
91
+ xml.description text
92
+ else
93
+ xml.description text, 'lang' => lang.to_s
94
+ end
95
+ end
96
+ pattern.category.each do |lang, text|
97
+ if lang.empty?
98
+ xml.category text
99
+ else
100
+ xml.category text, 'lang' => lang.to_s
101
+ end
102
+ end
103
+ xml.uservisible if pattern.visible
104
+ write_xml_dependency(xml, pattern, :conflicts)
105
+ write_xml_dependency(xml, pattern, :supplements)
106
+ write_xml_dependency(xml, pattern, :provides)
107
+ write_xml_dependency(xml, pattern, :requires)
108
+ write_xml_dependency(xml, pattern, :recommends)
109
+ write_xml_dependency(xml, pattern, :suggests)
110
+
111
+ unless pattern.extends.empty?
112
+ xml.extends do
113
+ pattern.extends.each do |pkg, kind|
114
+ xml.item('pattern' => pkg) if kind == 'pattern'
115
+ end
116
+ end
117
+ end
118
+ unless pattern.includes.empty?
119
+ xml.includes do
120
+ pattern.includes.each do |pkg, kind|
121
+ xml.item('pattern' => pkg) if kind == 'pattern'
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ io << builder.to_xml
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,158 @@
1
+ # Encoding: utf-8
2
+
3
+ #--
4
+ #
5
+ # enhancerepo is a rpm-md repository metadata tool.
6
+ # Copyright (C) 2008, 2009 Novell Inc.
7
+ #
8
+ # Author: Michael Calmer <mc@suse.de>
9
+ #
10
+ # This program is free software; you can redistribute it and/or modify
11
+ # it under the terms of the GNU General Public License as published by
12
+ # the Free Software Foundation; either version 2 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # This program is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with this program; if not, write to the Free Software
22
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
24
+ # also available at http://www.gnu.org/copyleft/gpl.html.
25
+ #
26
+ #++
27
+ #
28
+
29
+ require 'rubygems'
30
+ require 'nokogiri'
31
+ require 'rexml/document'
32
+ require 'yaml'
33
+ require 'prettyprint'
34
+ require 'fileutils'
35
+ require 'zlib'
36
+ require 'stringio'
37
+ require 'enhance_repo/rpm_md/update'
38
+ require 'enhance_repo/susetags/pattern_reader'
39
+ require 'enhance_repo/pattern'
40
+
41
+ module EnhanceRepo
42
+ module RpmMd
43
+ class Patterns < Data
44
+ def initialize(config)
45
+ @dir = config.dir
46
+ @basedir = config.updatesbasedir
47
+
48
+ # update files
49
+ @patterns = Set.new
50
+ end
51
+
52
+ def empty?
53
+ @patterns.empty?
54
+ end
55
+
56
+ def size
57
+ @patterns.size
58
+ end
59
+
60
+ # add all patterns in a repoparts directory
61
+ # by default look in repoparts/
62
+ # otherwise pass the :repoparts_path option
63
+ def read_repoparts(opts = {})
64
+ repoparts_path = opts[:repoparts_path] || File.join(@dir, 'repoparts')
65
+ log.info "Reading patterns parts from #{repoparts_path}"
66
+ Dir[File.join(repoparts_path, 'pattern-*.xml')].each do |patternfile|
67
+ log.info("`-> adding pattern #{patternfile}")
68
+ @patterns << patternfile
69
+ end
70
+ # end of directory iteration
71
+ end
72
+
73
+ # generates a patterns.xml from a list of package names
74
+ # it compares the last version of those package names
75
+ # with their previous ones
76
+ #
77
+ # outputdir is the directory where to save the patch to.
78
+ def generate_patterns(files, outputdir)
79
+ pats = []
80
+ files.each do |file|
81
+ raise "#{file} does not exist" unless File.exist?(file)
82
+ Zlib::GzipReader.open(file) do |gz|
83
+ pats += EnhanceRepo::Susetags::PatternReader.read_patterns_from_tags(gz)
84
+ end
85
+ end
86
+
87
+ FileUtils.mkdir_p(outputdir)
88
+ pats.each do |pat|
89
+ pattern_filename = File.join(outputdir, "pattern-#{pat.name}_0.xml")
90
+ File.open(pattern_filename, 'w') do |f|
91
+ log.info "write pattern #{pattern_filename}"
92
+ pat.write_xml(f)
93
+ end
94
+ end
95
+ end
96
+
97
+ # splits the patterns.xml file into serveral pattern files
98
+ # it writes those files into outputdir
99
+ # output filenames will be pattern-name_<num>.xml
100
+ # where name is the name of the pattern
101
+ #
102
+ # outputdir is the directory where to save the pattern to.
103
+ def split_patterns(outputdir)
104
+ FileUtils.mkdir_p outputdir
105
+ patternsfile = File.join(@dir, metadata_filename)
106
+
107
+ # we can't split without an patterns file
108
+ raise "#{patternsfile} does not exist" unless File.exist?(patternsfile)
109
+ Zlib::GzipReader.open(patternsfile) do |gz|
110
+ document = REXML::Document.new(gz)
111
+ root = document.root
112
+ root.each_element('pattern') do |patternElement|
113
+ name = nil
114
+ patternElement.each_element('name') do |elementName|
115
+ name = elementName.text
116
+ end
117
+ if name.nil?
118
+ log.warning 'No name found. Setting name to NON_NAME_FOUND'
119
+ name = 'NON_NAME_FOUND'
120
+ end
121
+ version = 0
122
+ updatefilename = ''
123
+ while File.exist?(patternfilename = File.join(outputdir, "pattern-#{name}_#{version}.xml"))
124
+ version += 1
125
+ end
126
+ log.info "Saving pattern part to '#{patternfilename}'."
127
+ File.open(patternfilename, 'w') do |patternfile|
128
+ patternfile << patternElement
129
+ patternfile << "\n"
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ # write a update out
136
+ def write(file)
137
+ builder = Builder::XmlMarkup.new(target: file, indent: 2)
138
+ builder.instruct!
139
+ builder.patterns('xmlns' => 'http://novell.com/package/metadata/suse/pattern',
140
+ 'xmlns:rpm' => 'http://linux.duke.edu/metadata/rpm') do |_b|
141
+ pattern_regex = Regexp.new('<pattern\s+xmlns.+>\s*$')
142
+ @patterns.each do |pattern|
143
+ File.open(pattern).each_line do |line|
144
+ next if line.start_with?('<?xml')
145
+ file << if line.match(pattern_regex)
146
+ # all single pattern have the namespace attributes
147
+ # we can remove them in the combined file
148
+ "<pattern>\n"
149
+ else
150
+ line
151
+ end
152
+ end
153
+ end
154
+ end # done builder
155
+ end
156
+ end
157
+ end
158
+ end