forked from CNXTEoEorg/packages-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles-rpm-packages.rb
executable file
·216 lines (169 loc) · 4.94 KB
/
files-rpm-packages.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env ruby
# Usage: ./files-rpm-packages.rb -d templates/centos6/centos6.spec.tpl -f <DESTDIR_INSTALL_PATH> -g
$: << File.dirname(__FILE__)
require 'optparse'
require 'ostruct'
require 'find'
require 'pp'
require 'fileutils'
MACRO_DICT = {
"%{_sysconfdir}" => "/etc",
"%{_bindir}" => "/usr/bin",
"%{_sbindir}" => "/usr/sbin",
"%{_mandir}" => "/usr/share/man",
"%{_javadir}" => "/usr/share/java",
"%{_datadir}" => "/usr/share",
"%{_tmppath}" => "/var/tmp",
"%{_sharedstatedir}" => "/var/lib",
"%{_datarootdir}" => "/usr/share",
"%{_localstatedir}" => "/var",
"%{_unitdir}" => "/lib/systemd/system"
}
# if __FILE__ == $0
# keys = MACRO_DICT.collect{|k,v|k}.join(" ")
# puts "for macro in #{keys};do"
# puts "echo \"\\\"$macro\\\" => \\\"$(rpm --eval $macro)\\\"\","
# puts "done"
# end
TIMESTAMP = Time.now.strftime("%Y%m%d%H%M")
VALID_DIRECTIVES = /^\s*(%files|%config|%{|\/)/
SECTIONS = %w(
%build
%changelog
%clean
%description
%files
%install
%package
%post
%prep?
%setup
)
SECTIONS_REGEXP = Regexp.new("^(#{SECTIONS.join('|')})(?:(?: )(.*)$)?")
# Monkey-patch to add colours
class String
def red; colorize(31); end
def green; colorize(32); end
def blue; colorize(34); end
def colorize(color_code); "\e[#{color_code}m#{self}\e[0m"; end
end
def banner(str)
puts
puts str.blue
end
class Pkgfiles < Array
attr_accessor :files
def initialize(files)
files.each do |file|
self << Pkgfile.new(file)
end
end
def match(regex)
m = self.select do |f|
f.match(regex)
end
!m.empty?
end
def filenames(opts,&block)
pkgs = self.select(&block)
if opts.glob
filenames = pkgs.collect do |f|
# count the entries starting with this path:
count_files = pkgs.count{|e| e.dir == f.dir}
# files in dir
dir_path = opts.dir.split('/')[0..(-opts.keep-1)].join('/')
dir_path = File.join(dir_path,f.dir,"*")
files_in_dir = Dir[dir_path].count
if files_in_dir == count_files
f.dir + "/*"
else
f.filename
end
end.uniq
else
filenames = pkgs.collect{|f| f.filename}
end
filenames
end
end
class Pkgfile
attr_accessor :filename, :occurrences
def initialize(filename)
@filename = filename
@occurrences = 0
end
def match(regex)
if regex.match(@filename)
@occurrences += 1
true
else
false
end
end
def dir
File.dirname(@filename)
end
end
opts = OpenStruct.new
OptionParser.new do |o|
o.on("-d RPM") {|e| opts.rpm = e}
o.on("-f FILES") {|e| opts.dir = e}
o.on("-p PREFIX") {|e| opts.prefix = e}
o.on("-k KEEPNUM") {|e| opts.keep = e}
o.on("-g") {|e| opts.glob = e}
end.parse!
# starting from right to left of the DIR option, keep this number of elements
opts.keep ||= "0"
opts.keep = opts.keep.to_i.abs
opts.prefix ||= "/"
# leading path to be removed
opts.rm_start = opts.dir.split('/')[0..(-1-opts.keep)].join('/')
files = Array.new
Find.find(opts.dir).each do |file|
# skip directories
next if File.directory? file
# remove the leading path
file = file[(opts.rm_start.length + 1)..-1]
# prepend the prefix
file = File.join(opts.prefix,file) if opts.prefix
# store the file
files << file
end
pkgfiles = Pkgfiles.new(files)
current_package = nil
File.open(opts.rpm).each do |line|
line.strip!
if (m = line.match(SECTIONS_REGEXP))
section, arg = m.to_a[1..-1]
if section == "%files"
arg ||= "opennebula"
current_package = arg
banner current_package
else
current_package = nil
end
next
end
next unless current_package
next unless line =~ VALID_DIRECTIVES
file = line.split[-1]
MACRO_DICT.each{|k,v| file[k]=v if file.include? k}
if file.match(/\*$/)
file_regex = Regexp.new("^" + file.gsub("*",".*") + "$")
else
if File.directory?(File.join(opts.dir,file))
file_regex = Regexp.new("^" + file + "(/.*|$)")
else
file_regex = Regexp.new("^" + file + "$")
end
end
if !pkgfiles.match(file_regex)
puts "\t#{file}"
end
end
banner "unclassified"
unclassified = pkgfiles.filenames(opts){|e| e.occurrences == 0}
unclassified.each {|f| puts "\t#{f}" }
banner "duplicated"
duplicated = pkgfiles.filenames(opts){|e| e.occurrences > 1}
duplicated.each {|f| puts "\t#{f}" }