-
Notifications
You must be signed in to change notification settings - Fork 1
/
apt-history.perl-curses-ui
473 lines (421 loc) · 11.9 KB
/
apt-history.perl-curses-ui
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/bin/perl
# vim: set tabstop=2
# vim: set expandtab
use warnings;
use strict;
BEGIN
{
my @modules_failed;
foreach my $module (qw(Curses::UI Curses::UI::Common))
{
eval "use $module";
if ($@) {push @modules_failed,$module;}
}
die "Please install @modules_failed, I couldn't load them"
if @modules_failed > 0;
}
use Curses::UI;
use Curses::UI::Common;
use Data::Dumper;
sub check_dependencies
{
( -e "/usr/bin/grep-status" ) or die "Please install dctrl-tools";
( -e "/bin/zcat" ) or die "Please install gzip";
# Check that modules are installed
}
sub init_curses_ui
{
my $cui = new Curses::UI ( -color_support => 1) or die "$!";
#my $cui = new Curses::UI ( -color_support => 1, -debug => 1);
#my $fh = "binding.log"; open STDERR, ">$fh";
my $win = $cui->add('window_id', 'Window') or die "$!";
return ($cui,$win);
}
sub create_packages_list
{
my $win = shift;
my $packages_version = shift;
my @packages = sort keys %$packages_version;
my $packages_list = $win->add(
"packages_list",
"Listbox",
-values => \@packages,
-height => $win->height()-15,
-title => "Packages (".scalar @packages.")",
-border => "true",
-vscrollbar => "true",
) || die ($!);
$packages_list->clear_binding("option-select");
$packages_list->set_binding ( sub { exit 0; }, "q" );
$packages_list->set_binding ( sub { exit 0; }, "\cq" );
return $packages_list;
}
sub create_history_tabs
{
my $win = shift;
my $history = shift;
my $packages_list = shift;
my $packages_version = shift;
my @packages = keys %$packages_version;
my $package = $packages[0];
my $history_tab = $win->add(
"tab",
"Notebook",
-y => $win->height()-15,
-height => 12
);
$history_tab->set_binding ( sub { $packages_list -> focus(); }, CUI_TAB());
my $apt_history_tab = $history_tab -> add_page ("Apt History");
my $apt_list = $apt_history_tab->add(
undef,
"Listbox",
-values => [@{$history->{package}{apt}{$package}}],
-title => "Apt log of $package (currently $packages_version->{$package})",
-tfg => "blue",
-bfg => "blue",
-border => "true",
) or die "$!";
my $dpkg_history_tab = $history_tab -> add_page ("Dpkg History");
my $dpkg_list = $dpkg_history_tab->add(
undef,
"Listbox",
-values => [@{$history->{package}{dpkg}{$package}}],
-title => "Dpkg log of $package (currently $packages_version->{$package})",
-tfg => "blue",
-bfg => "blue",
-border => "true",
) or die "$!";
foreach my $list ($apt_list, $dpkg_list)
{
$list->clear_binding("option-select");
$list->set_binding ( sub { exit 0; }, "q" );
$list->set_binding ( sub { exit 0; }, "\cq" );
}
return ($dpkg_list,$apt_list);
}
sub create_status
{
my $win = shift;
$win->add ( "help text",
"Label",
-text => "j/k/up/down: change selection | /: search".
" | tab: change focus".
" | RePag/AvPag: change tab history | q: quit",
-border => 1,
-title => "keybindings",
-width => -1,
-y => -1,
-paddingspaces => 1
) or die ("Cannot create status widget $!");
}
sub read_packages_version
{
my %list;
my $i=0;
my ($package, $arch, $status, $version);
open my $grep_status, "grep-status -n -FPackage '' ".
" -s Package -s Architecture".
" -s Version -s Status -n |".
"grep . |";
while (<$grep_status>)
{
chomp;
$i++;
if ($i % 4 == 1) { $package = $_; }
if ($i % 4 == 2) { $arch = $_; }
if ($i % 4 == 3) { $version = $_; }
if ($i % 4 == 0)
{
$status = $_; $status =~ s, ,/,g ;
$list{"$package:$arch"}="$version"."_"."$status";
}
}
return %list;
}
my ($first_action_date);
sub read_package_apt_history
{
my $history = shift;
my $packages_version = shift;
my ($start_date, $end_date);
my ($start_time, $end_time);
my ($commandline, $action, $changes,$error)=("","","",0);
my ($resume_log, $message_error);
open my $hfh, '{
zcat $(ls -rt /var/log/apt/history.log*gz)
cat /var/log/apt/history.log;
} |';
while (<$hfh>)
{
if (/^Start-Date: (....-..-..) +(..:..:..)/)
{
$start_date = $1;
$start_time = $2;
if (not $first_action_date)
{
$first_action_date = "$start_date $start_time";
}
$end_date = "";
$end_time = "";
}
elsif (/^End-Date: (....-..-..) +(..:..:..)/)
{
$end_date = $1;
$end_time = $2;
# package:architecture
# first version
# possible second version
while ($changes =~ /
([a-z0-9][a-z0-9.+-]+):([a-z0-9-]+)\s
\(([A-Za-z0-9.~+-]+)? # Purge log not show any version
(?:,\s([A-Za-z0-9.~+-]+))?\)
/xg)
{
my $package=$1;
my $arch=$2;
my $version_1='unknown';
if ($3)
{
$version_1="$3";
}
my $version_transition=$version_1;
if ($4) {
$version_transition .= " => $4";
}
if ($error == 1)
{
$message_error="Possible failed, see dpkg logs";
$resume_log="$start_date $start_time:".
" $action $arch ($version_transition) [$message_error]";
}
else
{
$resume_log="$start_date $start_time:".
" $action $arch ($version_transition)";
}
if (not $packages_version->{"$package:$arch"})
{
$packages_version -> {"$package:$arch"} = "unknown";
}
push @{$history->{package}{apt}{"$package:$arch"}},
"$start_date $start_time: $commandline # until $end_date $end_time";
push @{$history->{package}{apt}{"$package:$arch"}}, $resume_log;
$error=0;
}
}
elsif (/^Commandline: (.*)/)
{
$commandline = $1;
}
elsif (/^(Install|Upgrade|Remove|Purge|Downgrade): (.*)$/)
{
$action=$1;
$changes=$2;
}
elsif (/^Error/)
{
$error=1;
}
}
foreach my $package (keys %$packages_version)
{
if (not $history->{package}{apt}{$package})
{
push @{$history->{package}{apt}{$package}},
"Without changes since $first_action_date";
}
}
}
#
# 5 conffile (add apt-file?)
# 9828 configure
# 1009 install
# 523 purge
# 1135 remove
# 1371 startup => ??
# 111883 status
# 2198 trigproc
# 9031 upgrade
sub read_package_dpkg_history
{
my $history = shift;
my $packages_version = shift;
my %packages_hash = map { $_ => 1 } keys %$packages_version;
# Packages not installed and not seen at apt log
my %packages_forgotten;
open my $hfh, '{
zcat $(ls -rt /var/log/dpkg.log*gz)
cat /var/log/dpkg.log ;
}|';
my $startup;
my %startup_tmp;
while (<$hfh>)
{
my $package;
chomp;
if (/....-..-..\s..:..:..\s
(?:configure|install|purge|
remove|trigproc|upgrade)\s(\S+)\s
/x)
{
$package = $1;
}
elsif (/....-..-..\s..:..:..\s
status\s(?:config-files|
half-configured|half-installed|
installed|not-installed|
triggers-awaited|triggers-pending|
unpacked)\s(\S+)\s
/x)
{
$package = $1;
}
elsif (/....-..-..\s..:..:..\s
conffile\s(\S+)/x)
{
next ;
#$package=`dpkg -S $1 2> /dev/null| grep -o '^[^:]*'`;
}
elsif (/....-..-..\s..:..:..\s(startup.*)$/)
{
$startup=$_;
%startup_tmp=();
next;
}
else
{
print "$_ not match ...\n";
}
if ($packages_hash{"$package"})
{
if ($startup =~ /./ and not $startup_tmp{$package} )
{
push @{$history->{package}{dpkg}{"$package"}},$startup;
$startup_tmp{$package} = 1;
}
push @{$history->{package}{dpkg}{"$package"}},$_;
}
else
{
$packages_forgotten{$package} = 1;
$packages_hash{"$package"}=1;
push @{$history->{package}{dpkg}{"$package"}},$_;
push @{$history->{package}{apt}{"$package"}},
"Not log found about $package at /var/log/apt/history.log*";
}
}
foreach my $package (keys %$packages_version)
{
if (not $history->{package}{dpkg}{$package})
{
push @{$history->{package}{dpkg}{$package}},
"Without changes since $first_action_date";
}
}
my %packages_forgotten_by_arch;
foreach my $package (keys %packages_forgotten)
{
if ($package =~ /^([^:]*):(.*)$/)
{
my $name = $1;
my $arch = $2;
$packages_forgotten_by_arch{$arch}{$name}=1;
# Until we get correct version running grep-available ...
# or just in the case that $package is not available
$packages_version -> {"$name:$arch"} = "not-version";
unshift @{$history->{package}{dpkg}{"$name:$arch"}},
("Currently not installed, there is not any version candidate");
}
}
foreach my $arch (keys %packages_forgotten_by_arch)
{
my $packages_forgotten_regex = join "|",
map { "^$_\$"; }
keys %{$packages_forgotten_by_arch{$arch}};
open my $grep_available,
"grep-available -n -e -P '$packages_forgotten_regex'".
" -a -F Architecture $arch".
" -s Package,Version |";
while (<$grep_available>)
{
my ($package, $version);
my $i =0;
chomp;
$i++;
if ($i % 2 == 1) { $package = $_; }
if ($i % 2 == 0)
{
$version = $_;
$history->{package}{dpkg}{"$package:$arch"}[0]=
"Currently not installed, version candidate of".
" $package:$arch is $version";
print "Añadimos '($package):($arch)' ... \n";
$packages_version -> {"$package:$arch"} = $version;
}
}
}
}
sub update_history
{
my $packages_list = shift;
my ($apt_list, $dpkg_list, $packages_version, $history) = @_;
my $package = $packages_list->get_active_value ();
my $version_status = $packages_version -> {$package};
# if (not $version_status)
# {
# open my $log,">apt-history.log";
# print $log "Review $package\n" ;
# exit 1;
# }
my $title_apt="Apt log of $package ".
"(currently (version_status) $version_status)";
my $title_dpkg="Dpkg log of $package ".
"(currently (version_status) $version_status)";
$apt_list -> values ([@{$history->{package}{apt}{$package}}]);
$apt_list -> title ("$title_apt");
$dpkg_list -> values (@{$history->{package}{dpkg}{$package}});
$dpkg_list -> title ("$title_dpkg");
# horrible hack !
$apt_list -> focus();
$dpkg_list -> focus();
$packages_list -> focus();
}
check_dependencies;
my %packages_version = read_packages_version;
my %history;
read_package_apt_history(\%history, \%packages_version);
read_package_dpkg_history(\%history, \%packages_version);
my ($cui,$win)=init_curses_ui();
my $packages_list = create_packages_list($win, \%packages_version);
my ($dpkg_list,$apt_list) = create_history_tabs($win,
\%history, $packages_list, \%packages_version);
create_status($win);
$packages_list -> onSelectionChange (
sub { my $p = shift;
update_history($p,$apt_list,$dpkg_list,\%packages_version,\%history);
});
$packages_list->focus();
$cui->mainloop();
__END__
=head1 NAME
apt-history-curses - Browser your apt/dpkg logs by packages
on a ncurses interface
=head1 SYPNOSIS
B<apt-history>
=head1 DESCRIPTION
apt-history-curses will scan /var/log/{dpkg.*,apt/history*} and show you logs
of the selected package
=head1 DEPENDENCIES
You will need to install the next packages:
* libcurses-ui-perl (for ncurses widgets)
* dctrl-tools (for read apt db status (/var/lib/dpkg/{available, status}))
* gzip (for read compressed logs)
* may be in the future apt-file (for guessing packages at conffile log stanza)
=head1 OPTIONS
There is not options
=head1 CONFIGURATION FILE
There is not configuration file
=head1 LICENSE
WTFPL
=head1 AUTHOR
Javier Barroso <[email protected]>