-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchangelog.pl
executable file
·415 lines (318 loc) · 8.62 KB
/
changelog.pl
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
#! /usr/bin/perl -w
##
## ChangeLog Viewer/Editor
## Copyright (c) 2006 by Michal Nazarewicz (mina86/AT/mina86.com)
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
## This is part of Tiny Applications Collection
## -> http://tinyapps.sourceforge.net/
##
use warnings;
use strict;
use Pod::Usage;
use File::Temp qw/ tempfile /;
use Text::Wrap;
sub err (@) { print STDERR join(': ', 'changelog', @_), "\n"; }
sub d (@) { die join(': ', 'changelog', @_), "\n"; }
## The directory must exist before first run of changelog.pl
#my $DIR = '/srv/changelog';
#my $DIR = '/usr/local/share/changelog';
#my $DIR = '/usr/share/changelog';
my $DIR = '/var/changelog';
##
## Add entry
##
if (@ARGV && ($ARGV[0] eq '-a' || $ARGV[0] eq '--add')) {
my (undef, undef, undef, $mday, $mon, $date) = localtime();
$date = sprintf '%02d%02d%02d', $date % 100, $mon + 1, $mday;
my $message;
shift;
if (@ARGV) {
# Message given at command line
$message = join ' ', @ARGV;
} else {
$File::Temp::KEEP_ALL = 1;
$File::Temp::DEBUG = 1;
# Temporary file
my ($fh, $fn) = tempfile();
d 'tempfile: unable to create temporary file' unless defined $fn;
# Run editor
my $prog = $ENV{'EDITOR'} || $ENV{'VISUAL'} || 'vi';
system $prog, $fn;
if ($?==-1) {
d 'system', $prog, "$!";
} elsif ($? & 127) {
d $prog, 'died with signal ' . ($? & 127);
} elsif ($?) {
d $prog, 'exited with ' . ($? >> 8);
}
# Read file
$message = do { local $/; <$fh> };
$message =~ s/^\s+|\s+$//g;
close $fh;
d 'no message' if $message eq '';
}
# Add entry
d 'open', $DIR . '/' . $date, "$!"
unless open FH, '>>', $DIR . '/' . $date;
print FH 'Time: ', time() , "\n";
print FH 'User: ', (getpwuid($<))[0], "\n" if $<;
$message =~ s/\n/\n /;
print FH $message, "\n";
print FH "---------- END ----------\n";
close FH;
exit 0;
}
##
## Usage
##
if (@ARGV) {
if ($ARGV[0] eq '--help' || $ARGV[0] eq '-h') {
pod2usage( -exitval => 0, -verbose => 1 );
} elsif ($ARGV[0] eq '--man') {
pod2usage( -exitval => 0, -verbose => 2 );
} elsif (@ARGV >=3 ) {
pod2usage( -exitval => 1, -verbose => 0, -output => \*STDERR );
}
}
##
## Home directory
##
sub _dir ($) { defined $_[0] && -d $_[0] ? $_[0] : undef; }
my $HOME = (
_dir $ENV{'HOME'} or
_dir $ENV{'LOGDIR'} or
_dir eval { local $SIG{'__DIE__'} = ''; (getpwuid($<))[7]; } or
_dir eval {
use File::Glob ':glob';
my $h = File::Glob::bsd_glob('~', GLOB_TILDE);
$h eq '~' ? undef : $h;
} or
_dir $ENV{'USERPROFILE'} or
_dir do {
if ($ENV{HOMEDRIVE} && $ENV{HOMEPATH}) {
File::Spec->catpath($ENV{HOMEDRIVE}, $ENV{HOMEPATH}, '');
} else {
undef;
}
} or
_dir eval {
require Mac::Files;
Mac::Files::FindFolder(
Mac::Files::kOnSystemDisk(),
Mac::Files::kDesktopFolderType(),
);
}
) or err 'could not localize home directory';
##
## Parse arguments
##
my ($print_date, $date, $num, $new, @messages);
if (@ARGV == 0) {
$num = 5;
}
if (@ARGV == 2) {
($date, $num) = @ARGV;
# Number
d $num, 'invalid number' unless $num =~ /^\d+$/ && $num != 0;
}
if (@ARGV == 1) {
$_ = $ARGV[0];
my ($mday, $mon);
# Number
if (!defined $num && /^\d{1,3}$/) {
$num = $_;
d $num, 'invalid number' unless $num != 0;
# Date
} elsif (lc eq 'today') {
(undef, undef, undef, $mday, $mon, $date) = localtime();
$date = sprintf '%02d%02d%02d', $date % 100, $mon + 1, $mday;
$print_date = lc;
} elsif (lc eq 'yesterday') {
(undef, undef, undef, $mday, $mon, $date) =
localtime time() - 24*3600;
$date = sprintf '%02d%02d%02d', $date % 100, $mon + 1, $mday;
$print_date = lc;
} elsif (lc eq 'new') {
undef $date;
$new = 1;
} elsif (m#^(?:\d\d)(\d\d)([ -/]\d|[ -/]?\d\d)([ -/]\d|[ -/]?\d\d)$#) {
($date, $mon, $mday) = ($1, $2, $3);
s/^\D// for ($mon, $mday);
$print_date = sprintf '20%02d/%02d/%02d', $date, $mon, $mday;
$date = sprintf '%02d%02d%02d', $date, $mon, $mday;
} else {
d $_, 'invalid argument';
}
}
##
## Read records
##
sub read_messages ($) {
return () unless -f $DIR . '/' . $_[0] && -s _;
local (*FH, $?);
d 'open', $DIR.'/'.$_[0], "$!" unless open FH, '<', $DIR.'/'.$_[0];
$/ = "---------- END ----------\n";
my (@messages) = (<FH>);
close FH;
return @messages;
}
if ($date) {
# Sinble message
@messages = read_messages $date;
unless (@messages) {
print "No ChangLog entries for $print_date.\n";
exit 0;
}
} else {
# Read New
my $ndate;
if ($new) {
if (stat $HOME . '/.changelog') {
$new = (stat _)[9];
my ($mday, $mon);
(undef, undef, undef, $mday, $mon, $ndate) = localtime $new;
$ndate = sprintf '%02d%02d%02d', $ndate % 100, $mon + 1, $mday;
} else {
$ndate = 0;
}
}
# Read maching file names
d 'opendir', $DIR, "$!" unless opendir DIR, $DIR;
my @days = grep {
m/^[01][0-9][01][0-9][0-3][0-9]$/ && (!$new || $_ >= $ndate);
} readdir DIR;
closedir DIR;
# No entries
unless (@days) {
print "No new ChangeLog entries\n";
exit 0;
}
# Read messages
@days = sort { $b cmp $a } @days;
if ($num) {
@messages = ( read_messages(shift @days), @messages )
while @days && @messages < $num;
} else {
@messages = ( read_messages($_), @messages ) for (@days);
}
}
# Mark new
if (($new || !$date) && $HOME && (open FH, '>', $HOME . '/.changelog')) {
print FH time();
close FH;
}
##
## Wrtie entries
##
# No entries
my $printed = 0;
unless (@messages) {
print "No new ChangeLog entries\n";
exit 0;
}
# Print all
$num = $num ? @messages - $num : 0;
for ($num = $num < 0 ? 0 : $num; $num < @messages; ++$num) {
$_ = $messages[$num];
my ($time, $user);
# Parse headers
if (s/^Time:\s*(\d+)\s*\n//m) {
$time = $1;
}
if (s/^User:\s*(.*\S)\s*\n//m) {
$user = $1;
}
s/\n---+\s*END\s*---+$//m;
# Not new?
next if defined $time && $new && $time < $new;
# Print date
if ($time) {
my (undef, undef, undef, $mday, $mon, $year) = localtime $time;
$time = sprintf '%02d%02d%02d', $year % 100, $mon + 1, $mday;
if (!defined($date) || $time != $date) {
print "\n" if $printed;
printf "------- 20%02d/%02d/%02d ", $year % 100, $mon+1, $mday;
for (my $i = 19; $i<=74; ++$i) { print '-'; }
print "\n";
$date = $time;
}
}
# Print user name
print "\n";
if ($user && length $user<8) {
printf '%-7s ', $user;
} else {
print $user, "\n" if $user;
print ' ';
}
# Print entry
s/\s+$//mg;
s/\n{2,}/\n /mg;
$Text::Wrap::columns = 64;
$_ = fill '', '', $_;
s/\n(.)/\n $1/g;
print $_, "\n";
$printed = 1;
}
print "No new ChangeLog entries\n" unless $printed;
__END__
=head1 NAME
changelog.pl - Displays and adds change log entries
=head1 SYNOPSIS
changelog.pl -h | --help | --man
getlyrics.pl [ I<day> | new ] [ I<number> ]
changelog.pl -a [ I<message> ]
=head1 DESCRIPTION
The general idea of changelog.pl is to provide a global change log for
given machine or network. In particular, system administrator (or
anyone else who has sufficient privileges) adds entries using
B<changelog -a> command which than can be displayed in B</etc/profile>
so that all users get the news.
=head1 OPTIONS
=over 8
=item B<-h> B<--help>
Displays usage information.
=item B<--man>
Displays a whole command's man page.
=item B<-a> B<--add>
If this is the only argument program will run your editor (it will
first try VISUAL environmental variable, then EDITOR and if both are
empty will run B<vi>) to let you enter message; otherwise the rest of
the arguments are treated as a message.
=item I<day>
Specifies the day to display entries for. It can be either
B<yesterday>, B<today> or date in the format B<YYYYMMDD>.
=item B<new>
Tells changelog to display only the new entries since the last run.
=item I<number>
Application will display at most I<number> entries.
=back
=head1 EXAMPLES
=over 8
=item B<changelog.pl -a A new version of foo installed>
Adds an entry saying new version of foo has been installed.
=item B<changelog.pl>
Displays last 5 entries.
=item B<changelog.pl 20060101>
Displays all entries for 2006/01/01.
=item B<changelog.pl new>
Displays all new entries.
=item B<changelog.pl 20060101 5>
Displays last 5 entries for 2006/01/01.
=back
=head1 AUTHOR
Michal Nazarewicz <[email protected]>
=cut