slack-utils 0.6.2 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  # Copyright 2010,2011 Vincent Batts, Vienna, VA
2
4
  # All rights reserved.
3
5
  #
@@ -18,7 +20,7 @@
18
20
  # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
19
21
  # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20
22
 
21
- # started - Fri Oct 9 15:48:43 CDT 2009
23
+ # started - Fri Oct 9 15:48:43 CDT 2009
22
24
  # updated for args - Tue Mar 23 14:54:19 CDT 2010
23
25
  # Copyright 2009, 2010 Vincent Batts, http://hashbangbash.com/
24
26
 
@@ -35,224 +37,224 @@ require 'slackware'
35
37
 
36
38
  # This is base builder of the packe list
37
39
  def build_packages(opts = {}, args = [])
38
- pkgs = Slackware::System.installed_packages
39
-
40
- # separated this little thing out, since it adds a little more time
41
- if (opts[:time])
42
- pkgs = pkgs.each {|p| p.get_time }
43
- end
44
-
45
- if (opts[:all])
46
- if (args.count > 0)
47
- args.each {|arg|
48
- # about 0.012s performance improvement,
49
- # by compiling it here, instead of inside the iteration.
50
- if (opts[:case_insensitive])
51
- re = Regexp.compile(/#{arg}/i)
52
- else
53
- re = Regexp.compile(/#{arg}/)
54
- end
55
-
56
- pkgs = pkgs.find_all {|pkg| pkg.fullname =~ re }
57
- }
58
- end
59
- re = nil
60
- end
61
- if (opts[:pkg])
62
- if (opts[:case_insensitive])
63
- re = Regexp.compile(/#{opts[:pkg]}/i)
64
- else
65
- re = Regexp.compile(/#{opts[:pkg]}/)
66
- end
67
- pkgs = pkgs.map {|p|
68
- if p.name =~ re
69
- if (opts[:color])
70
- p.name = p.name.gsub(re, "#{@st}\\&#{@en}")
71
- end
72
- p
73
- end
74
- }.compact
75
- re = nil
76
- end
77
- if (opts[:Version])
78
- if (opts[:case_insensitive])
79
- re = Regexp.compile(Regexp.escape(/#{opts[:Version]}/i))
80
- else
81
- re = Regexp.compile(Regexp.escape(/#{opts[:Version]}/))
82
- end
83
- pkgs = pkgs.map {|p|
84
- if p.version =~ re
85
- if (opts[:color])
86
- p.version = p.version.gsub(re, "#{@st}\\&#{@en}")
87
- end
88
- p
89
- end
90
- }.compact
91
- re = nil
92
- end
93
- if (opts[:arch])
94
- if (opts[:case_insensitive])
95
- re = Regexp.compile(/#{opts[:arch]}/i)
96
- else
97
- re = Regexp.compile(/#{opts[:arch]}/)
98
- end
99
- pkgs = pkgs.map {|p|
100
- if p.arch =~ re
101
- if (opts[:color])
102
- p.arch = p.arch.gsub(re, "#{@st}\\&#{@en}")
103
- end
104
- p
105
- end
106
- }.compact
107
- re = nil
108
- end
109
- if (opts[:build])
110
- if (opts[:case_insensitive])
111
- re = Regexp.compile(/#{opts[:build]}/i)
112
- else
113
- re = Regexp.compile(/#{opts[:build]}/)
114
- end
115
- pkgs = pkgs.map {|p|
116
- if p.build =~ re
117
- if (opts[:color])
118
- p.build = p.build.gsub(re, "#{@st}\\&#{@en}")
119
- end
120
- p
121
- end
122
- }.compact
123
- re = nil
124
- end
125
- if (opts[:tag])
126
- if (opts[:case_insensitive])
127
- re = Regexp.compile(/#{opts[:tag]}/i)
128
- else
129
- re = Regexp.compile(/#{opts[:tag]}/)
130
- end
131
- pkgs = pkgs.map {|p|
132
- if p.tag =~ re
133
- if (opts[:color])
134
- p.tag = p.tag.gsub(re, "#{@st}\\&#{@en}")
135
- end
136
- p
137
- end
138
- }.compact
139
- re = nil
140
- end
141
-
142
- return pkgs
40
+ pkgs = Slackware::System.installed_packages
41
+
42
+ # separated this little thing out, since it adds a little more time
43
+ if (opts[:time])
44
+ pkgs = pkgs.each {|p| p.get_time }
45
+ end
46
+
47
+ if (opts[:all])
48
+ if (args.count > 0)
49
+ args.each {|arg|
50
+ # about 0.012s performance improvement,
51
+ # by compiling it here, instead of inside the iteration.
52
+ if (opts[:case_insensitive])
53
+ re = /#{arg}/i
54
+ else
55
+ re = /#{arg}/
56
+ end
57
+
58
+ pkgs = pkgs.find_all {|pkg| pkg.fullname =~ re }
59
+ }
60
+ end
61
+ re = nil
62
+ end
63
+ if (opts[:pkg])
64
+ if (opts[:case_insensitive])
65
+ re = /#{opts[:pkg]}/i
66
+ else
67
+ re = /#{opts[:pkg]}/
68
+ end
69
+ pkgs = pkgs.map {|p|
70
+ if p.name =~ re
71
+ if (opts[:color])
72
+ p.name = p.name.gsub(re, "#{@st}\\&#{@en}")
73
+ end
74
+ p
75
+ end
76
+ }.compact
77
+ re = nil
78
+ end
79
+ if (opts[:Version])
80
+ if (opts[:case_insensitive])
81
+ re = Regexp.new(Regexp.escape(opts[:Version]), Regexp::IGNORECASE)
82
+ else
83
+ re = Regexp.new(Regexp.escape(opts[:Version]))
84
+ end
85
+ pkgs = pkgs.map {|p|
86
+ if p.version =~ re
87
+ if (opts[:color])
88
+ p.version = p.version.gsub(re, "#{@st}\\&#{@en}")
89
+ end
90
+ p
91
+ end
92
+ }.compact
93
+ re = nil
94
+ end
95
+ if (opts[:arch])
96
+ if (opts[:case_insensitive])
97
+ re = /#{opts[:arch]}/i
98
+ else
99
+ re = /#{opts[:arch]}/
100
+ end
101
+ pkgs = pkgs.map {|p|
102
+ if p.arch =~ re
103
+ if (opts[:color])
104
+ p.arch = p.arch.gsub(re, "#{@st}\\&#{@en}")
105
+ end
106
+ p
107
+ end
108
+ }.compact
109
+ re = nil
110
+ end
111
+ if (opts[:build])
112
+ if (opts[:case_insensitive])
113
+ re = /#{opts[:build]}/i
114
+ else
115
+ re = /#{opts[:build]}/
116
+ end
117
+ pkgs = pkgs.map {|p|
118
+ if p.build =~ re
119
+ if (opts[:color])
120
+ p.build = p.build.gsub(re, "#{@st}\\&#{@en}")
121
+ end
122
+ p
123
+ end
124
+ }.compact
125
+ re = nil
126
+ end
127
+ if (opts[:tag])
128
+ if (opts[:case_insensitive])
129
+ re = /#{opts[:tag]}/i
130
+ else
131
+ re = /#{opts[:tag]}/
132
+ end
133
+ pkgs = pkgs.map {|p|
134
+ if p.tag =~ re
135
+ if (opts[:color])
136
+ p.tag = p.tag.gsub(re, "#{@st}\\&#{@en}")
137
+ end
138
+ p
139
+ end
140
+ }.compact
141
+ re = nil
142
+ end
143
+
144
+ return pkgs
143
145
  end
144
146
 
145
147
  def print_packages(pkgs)
146
- if (pkgs.count > 0 && pkgs.first.class == Slackware::Package)
147
- pkgs.each {|pkg|
148
- printf("%s\n", pkg.fullname )
149
- }
150
- end
148
+ if (pkgs.count > 0 && pkgs.first.class == Slackware::Package)
149
+ pkgs.each {|pkg|
150
+ printf("%s\n", pkg.fullname )
151
+ }
152
+ end
151
153
  end
152
154
 
153
155
  def print_packages_times(pkgs, epoch = false)
154
- if (epoch == true)
155
- pkgs.each {|pkg| printf("%s : %s\n", pkg.fullname, pkg.time.to_i) }
156
- else
157
- pkgs.each {|pkg| printf("%s : %s\n", pkg.fullname, pkg.time.to_s) }
158
- end
156
+ if (epoch == true)
157
+ pkgs.each {|pkg| printf("%s : %s\n", pkg.fullname, pkg.time.to_i) }
158
+ else
159
+ pkgs.each {|pkg| printf("%s : %s\n", pkg.fullname, pkg.time.to_s) }
160
+ end
159
161
  end
160
162
 
161
163
  def print_packages_description(pkgs)
162
- if (pkgs.count > 0 && pkgs.first.class == Slackware::Package)
163
- pkgs.each {|pkg|
164
- printf("%s: COMPRESSED SIZE: %s\n", pkg.fullname, pkg.compressed_size )
165
- printf("%s: UNCOMPRESSED SIZE: %s\n", pkg.fullname, pkg.uncompressed_size )
166
- pkg.package_description.each {|line|
167
- printf("%s: %s\n", pkg.fullname, line )
168
- }
169
- }
170
- end
164
+ if (pkgs.count > 0 && pkgs.first.class == Slackware::Package)
165
+ pkgs.each {|pkg|
166
+ printf("%s: COMPRESSED SIZE: %s\n", pkg.fullname, pkg.compressed_size )
167
+ printf("%s: UNCOMPRESSED SIZE: %s\n", pkg.fullname, pkg.uncompressed_size )
168
+ pkg.package_description.each {|line|
169
+ printf("%s: %s\n", pkg.fullname, line )
170
+ }
171
+ }
172
+ end
171
173
  end
172
174
 
173
175
  # package file listing
174
176
  def print_package_file_list(pkgs)
175
- if (pkgs.count > 1)
176
- pkgs.each {|pkg|
177
- pkg.get_owned_files.each {|line|
178
- puts pkg.name + ": " + line
179
- }
180
- }
181
- else
182
- pkgs.each {|pkg| puts pkg.get_owned_files }
183
- end
177
+ if (pkgs.count > 1)
178
+ pkgs.each {|pkg|
179
+ pkg.get_owned_files.each {|line|
180
+ puts pkg.name + ": " + line
181
+ }
182
+ }
183
+ else
184
+ pkgs.each {|pkg| puts pkg.get_owned_files }
185
+ end
184
186
  end
185
187
 
186
188
  # search Array of Slackware::Package's for files
187
189
  # and print the items found
188
190
  def print_package_searched_files(pkgs, files)
189
- found_files = []
190
- files.each {|file|
191
- found_files = found_files.concat(Slackware::System.owns_file(file))
192
- }
193
- found_files.each {|file|
194
- puts file[0].fullname + ": " + file[1]
195
- }
191
+ found_files = []
192
+ files.each {|file|
193
+ found_files += Slackware::System.owns_file(file)
194
+ }
195
+ found_files.each {|file|
196
+ puts file[0].fullname + ": " + file[1]
197
+ }
196
198
  end
197
199
 
198
200
  # find orpaned files from /etc/
199
- # * build a list of files from removed_packages
200
- # * check the list to see if they are currently owned by a package
201
- # * check the unowned members, to see if they still exist on the filesystem
202
- # * return existing files
201
+ # * build a list of files from removed_packages
202
+ # * check the list to see if they are currently owned by a package
203
+ # * check the unowned members, to see if they still exist on the filesystem
204
+ # * return existing files
203
205
  def find_orphaned_config_files
204
- # build a list of config files currently installed
205
- installed_config_files = Slackware::System.installed_packages.map {|pkg|
206
- pkg.get_owned_files.map {|file|
207
- if not(file =~ /\/$/)
208
- if (file =~ /^etc\//)
209
- file
210
- end
211
- end
212
- }
213
- }.flatten.compact
214
-
215
- # this Array is where we'll stash removed packages that have config file to check
216
- pkgs = Array.new
217
- Slackware::System.removed_packages.each {|r_pkg|
218
- # find config files for this removed package
219
- config = r_pkg.get_owned_files.grep(/^etc\/.*[\w|\d]$/)
220
- # continue if there are none
221
- if (config.count > 0)
222
- # remove config files that are owned by a currently installed package
223
- config = config.map {|file|
224
- if not(installed_config_files.include?(file))
225
- if not(installed_config_files.include?(file + ".new"))
226
- file
227
- end
228
- end
229
- }.compact
230
- # check again, and continue if there are no config files left
231
- if (config.count > 0)
232
- # otherwise add this package, and its files, to the stack
233
- pkgs << {:pkg => r_pkg, :files => config}
234
- end
235
- end
236
- }
237
-
238
- # setup list of files to check whether they still exist on the filesystem
239
- files = []
240
- pkgs.map {|pkg| files << pkg[:files] }
241
- files.flatten!.uniq!
242
-
243
- orphaned_config_files = []
244
- files.each {|file|
245
- if (File.exist?("/" + file))
246
- orphaned_config_files << file
247
- end
248
- }
249
-
250
- return orphaned_config_files
206
+ # build a list of config files currently installed
207
+ installed_config_files = Slackware::System.installed_packages.map {|pkg|
208
+ pkg.get_owned_files.map {|file|
209
+ if not(file =~ /\/$/)
210
+ if (file =~ /^etc\//)
211
+ file
212
+ end
213
+ end
214
+ }
215
+ }.flatten.compact
216
+
217
+ # this Array is where we'll stash removed packages that have config file to check
218
+ pkgs = Array.new
219
+ Slackware::System.removed_packages.each {|r_pkg|
220
+ # find config files for this removed package
221
+ config = r_pkg.get_owned_files.grep(/^etc\/.*[\w|\d]$/)
222
+ # continue if there are none
223
+ if (config.count > 0)
224
+ # remove config files that are owned by a currently installed package
225
+ config = config.map {|file|
226
+ if not(installed_config_files.include?(file))
227
+ if not(installed_config_files.include?(file + ".new"))
228
+ file
229
+ end
230
+ end
231
+ }.compact
232
+ # check again, and continue if there are no config files left
233
+ if (config.count > 0)
234
+ # otherwise add this package, and its files, to the stack
235
+ pkgs << {:pkg => r_pkg, :files => config}
236
+ end
237
+ end
238
+ }
239
+
240
+ # setup list of files to check whether they still exist on the filesystem
241
+ files = []
242
+ pkgs.map {|pkg| files << pkg[:files] }
243
+ files.flatten!.uniq!
244
+
245
+ orphaned_config_files = []
246
+ files.each {|file|
247
+ if (File.exist?("/" + file))
248
+ orphaned_config_files << file
249
+ end
250
+ }
251
+
252
+ return orphaned_config_files
251
253
 
252
254
  end
253
255
 
254
256
  def print_orphaned_files(files)
255
- puts files
257
+ puts files
256
258
  end
257
259
 
258
260
  # XXX This is a work in progress
@@ -264,69 +266,70 @@ end
264
266
  # That way, those entries alone could be updated if they are newer,
265
267
  # otherwise it's just a query.
266
268
  def find_linked(file_to_find)
267
- require 'find'
268
-
269
- dirs = %w{ /lib /lib64 /usr/lib /usr/lib64 /bin /sbin /usr/bin /usr/sbin }
270
- re_so = Regexp.new(/ELF.*shared object/)
271
- re_exec = Regexp.new(/ELF.*executable/)
272
-
273
- if File.exist?(file_to_find)
274
- file_to_find = File.expand_path(file_to_find)
275
- end
276
- if not(filemagic(File.dirname(file_to_find) + "/" + File.readlink(file_to_find)) =~ re_so)
277
- printf("%s is not a shared object\n",file_to_find)
278
- return nil
279
- end
280
-
281
- includes_linked = []
282
- printf("Searching through ... ")
283
- dirs.each {|dir|
284
- printf("%s ", dir)
285
- Find.find(dir) {|file|
286
- if File.directory?(file)
287
- next
288
- end
289
- file_magic = filemagic(file)
290
- if (file_magic =~ re_so || file_magic =~ re_exec)
291
- l = `/usr/bin/ldd #{file} 2>/dev/null `
292
- if l.include?(file_to_find)
293
- printf(".")
294
- l = l.sub(/\t/, '').split(/\n/)
295
- includes_linked << {:file => file, :links => l}
296
- end
297
- end
298
- }
299
- }
300
- printf("\n")
301
- return includes_linked
269
+ require 'find'
270
+
271
+ dirs = %w{ /lib /lib64 /usr/lib /usr/lib64 /bin /sbin /usr/bin /usr/sbin }
272
+ re_so = /ELF.*shared object/
273
+ re_exec = /ELF.*executable/
274
+
275
+ if File.exist?(file_to_find)
276
+ file_to_find = File.expand_path(file_to_find)
277
+ end
278
+ if not(filemagic(File.dirname(file_to_find) + "/" + File.readlink(file_to_find)) =~ re_so)
279
+ printf("%s is not a shared object\n",file_to_find)
280
+ return nil
281
+ end
282
+
283
+ includes_linked = []
284
+ printf("Searching through ... ")
285
+ dirs.each {|dir|
286
+ printf("%s ", dir)
287
+ Find.find(dir) {|file|
288
+ if File.directory?(file)
289
+ next
290
+ end
291
+ file_magic = filemagic(file)
292
+ if (file_magic =~ re_so || file_magic =~ re_exec)
293
+ l = `/usr/bin/ldd #{file} 2>/dev/null `
294
+ if l.include?(file_to_find)
295
+ printf(".")
296
+ l = l.sub(/\t/, '').split(/\n/)
297
+ includes_linked << {:file => file, :links => l}
298
+ end
299
+ end
300
+ }
301
+ }
302
+ printf("\n")
303
+ return includes_linked
302
304
  end
303
305
 
304
306
  # This is intended to take the return of the find_linked() method
305
307
  def packages_of_linked_files(linked_files_arr)
306
- pkgs = Slackware::System.installed_packages
307
- owned_pkgs = []
308
- pkgs.map {|pkg|
309
- files.each {|file|
310
- if pkg.owned_files.include?(file)
311
- owned_pkgs << {:pkg => pkg, :file => file}
312
- end
313
- }
314
- }
315
- return owned_pkgs
308
+ pkgs = Slackware::System.installed_packages
309
+ owned_pkgs = []
310
+ pkgs.map {|pkg|
311
+ files.each {|file|
312
+ if pkg.owned_files.include?(file)
313
+ owned_pkgs << {:pkg => pkg, :file => file}
314
+ end
315
+ }
316
+ }
317
+ return owned_pkgs
316
318
  end
317
319
 
318
320
  # Pretty print the output of find_linked()
319
321
  def print_find_linked(file_to_find)
320
- files = find_linked(file_to_find)
321
- printf("files linked to '%s' include:\n", file_to_find)
322
- files.each {|file|
323
- printf(" %s\n", file)
324
- }
322
+ files = find_linked(file_to_find)
323
+ printf("files linked to '%s' include:\n", file_to_find)
324
+ files.each {|file|
325
+ printf(" %s\n", file)
326
+ }
325
327
  end
326
328
 
327
329
  private
328
330
 
329
331
  def filemagic(file)
330
- return `/usr/bin/file "#{file}"`.chomp
332
+ return `/usr/bin/file "#{file}"`.chomp
331
333
  end
332
334
 
335
+ # vim:sw=2:sts=2:et: