log.pl
changeset 12 b3edfead728b
parent 11 80890c874a90
child 13 d9694ca1b7fc
equal deleted inserted replaced
11:80890c874a90 12:b3edfead728b
     1 #! /usr/bin/perl 
     1 #! /usr/bin/perl 
     2 # $Id$
     2 # $Id$
     3 # $URL$
     3 # $URL$
     4 my $USAGE = <<'#';
     4 my $USAGE = <<'#';
     5 $ME [options]
     5 $ME [options]
     6     --[no]db	insert into log database [$opt_db]
     6     --[no]db	 insert into log database [$opt_db]
     7     --[no]mail	send mails to @config::mailto [$opt_mail]
     7     --[no]mail	 send mails to @config::mailto [$opt_mail]
     8     --message=s message
     8     --init-dir=s initialize specified directory with VCS repository
       
     9     --message=s  message
     9                 - if the message starts with '\@' it's considered
    10                 - if the message starts with '\@' it's considered
    10 		  to be a message FILE.
    11 		  to be a message FILE.
    11 		- message can be: apt[:text]\@<FILE> - this will
    12 		- message can be: apt[:text]\@<FILE> - this will
    12 		  start special file processing (assuming output
    13 		  start special file processing (assuming output
    13 		  from apt, with text (default: APT: upgrade) going
    14 		  from apt, with text (default: APT: upgrade) going
    17 use strict;
    18 use strict;
    18 use warnings;
    19 use warnings;
    19 use File::Basename;
    20 use File::Basename;
    20 use File::Temp qw(tempfile);
    21 use File::Temp qw(tempfile);
    21 use File::stat;
    22 use File::stat;
       
    23 use File::Which;
    22 use Getopt::Long;
    24 use Getopt::Long;
    23 use Mail::Mailer;
    25 use Mail::Mailer;
    24 use DBI;
    26 use DBI;
    25 use MIME::QuotedPrint;
    27 use MIME::QuotedPrint;
    26 
    28 
    49 
    51 
    50 my $LOG = "$ENV{HOME}/LOG";
    52 my $LOG = "$ENV{HOME}/LOG";
    51 my $EDITOR = $ENV{VISUAL} || $ENV{EDITOR} || "vim";
    53 my $EDITOR = $ENV{VISUAL} || $ENV{EDITOR} || "vim";
    52 my $MAGIC = "#--- all changes below are ignored ---#\n";
    54 my $MAGIC = "#--- all changes below are ignored ---#\n";
    53 
    55 
    54 my $opt_db = 1;
    56 my $opt_db = 0;
    55 my $opt_mail = 1;
    57 my $opt_mail = 0;
    56 my $opt_message = "";
    58 my $opt_message = "";
    57 my $opt_apt = "";
    59 my $opt_apt = "";
    58 
    60 my $opt_initdir = "";
    59 
       
    60 
    61 
    61 my $Dbh;
    62 my $Dbh;
    62 
    63 
    63 sub identity();
    64 sub identity();
    64 sub hostname();
    65 sub hostname();
    65 sub mailto();
    66 sub mailto();
       
    67 sub check_hg_bin();
    66 
    68 
    67 MAIN: {
    69 MAIN: {
    68 
    70 
    69     GetOptions("db!" => \$opt_db, 
    71     GetOptions("db!" => \$opt_db, 
    70 	"mail!" => \$opt_mail,
    72 	"mail!" => \$opt_mail,
    71 	"message=s" => \$opt_message,
    73 	"message=s" => \$opt_message,
    72 	"type=s" => \$opt_apt,
    74 	"type=s" => \$opt_apt,
       
    75 	"init-dir=s" => \$opt_initdir,
    73     ) or die eval "\"$USAGE\"";
    76     ) or die eval "\"$USAGE\"";
    74 
    77 
    75     if ($opt_message =~ /^@(.*)/) {
    78     if ($opt_message =~ /^@(.*)/) {
    76 	@ARGV = $1;
    79 	@ARGV = $1;
    77 	$opt_message = join "", <>;
    80 	$opt_message = join "", <>;
    89 
    92 
    90     if ($opt_message =~ /\n/) {
    93     if ($opt_message =~ /\n/) {
    91 	$opt_message =~ s/\n/\n    /g;
    94 	$opt_message =~ s/\n/\n    /g;
    92     }
    95     }
    93 
    96 
       
    97     if (defined @config::notify_dirs || $opt_initdir) {
       
    98 	check_hg_bin();
       
    99     }
       
   100 
       
   101     if ($opt_initdir) {
       
   102 	print "$ME: Trying to initialize $opt_initdir as mercurial repository.\n";
       
   103 	-d $opt_initdir or
       
   104 	    die "$ME: directory $opt_initdir does not exist!";
       
   105 
       
   106 	system("hg status $opt_initdir > /dev/null 2>&1");
       
   107 	if (($? >> 8)  == 0) {
       
   108 	    die "$ME: directory already initialized, skipping\n";
       
   109 	}
       
   110 
       
   111 	system("hg init $opt_initdir");
       
   112 	if ( ($? >> 8) != 0) {
       
   113 	    die "E: initialization failed\n";
       
   114 	} else {
       
   115 	    system("cd $opt_initdir && hg addremove && hg commit -m 'initial autocommit'");
       
   116 	    print "$ME: initialization done.\n";
       
   117 	} 
       
   118 
       
   119 	exit 0;
       
   120     }
       
   121 
       
   122     my $hg_status_text = "";
       
   123     if (defined @config::notify_dirs) {
       
   124 	foreach my $dir (@config::notify_dirs) {
       
   125 	    -d $dir or next;
       
   126 
       
   127 	    print "$ME: Checking $dir for modifications\n";
       
   128 	    -d "$dir/.hg" or 
       
   129 		die "$ME: directory $dir not initialized pleas call: \n",
       
   130 		    "  # $ME --init-dir $dir \n";
       
   131 
       
   132 	    system("hg addremove $dir");
       
   133 
       
   134 	    $hg_status_text .= `cd / && hg status $dir`;
       
   135 	}
       
   136     }
       
   137 
    94     if ($opt_db) {
   138     if ($opt_db) {
    95 	$Dbh = DBI->connect($DSN, $USER, $PW, {RaiseError => 1})
   139 	$Dbh = DBI->connect($DSN, $USER, $PW, {RaiseError => 1})
    96 	    or die $DBI::errstr;
   140 	    or die $DBI::errstr;
    97 	END { $Dbh->disconnect() if $Dbh; }
   141 	END { $Dbh->disconnect() if $Dbh; }
    98     }
   142     }
    99 
   143 
   100     # Temporärfile öffnen
   144     # Temporärfile öffnen
   101     my ($fh, $file);
   145     my ($fh, $file);
   102     END { unlink $file if $file; }
   146     END { unlink $file if $file; }
   103     ($fh, $file) = tempfile(DIR => "/tmp");
   147     ($fh, $file) = tempfile(DIR => "/tmp");
       
   148 
       
   149     my $auto_message = (not $hg_status_text) ? "" :
       
   150 	"\n"
       
   151 	. " Modified config files since last log entry listend below...\n"
       
   152 	. $hg_status_text
       
   153 	. "\n";
   104 
   154 
   105     # Kopftext eintragen
   155     # Kopftext eintragen
   106     print $fh 
   156     print $fh 
   107 	    "Date: ", scalar(localtime()), "\n",
   157 	    "Date: ", scalar(localtime()), "\n",
   108 	    "User: ", identity(), "\n",
   158 	    "User: ", identity(), "\n",
   109 	    "MailTo: ", mailto(), "\n",
   159 	    "MailTo: ", mailto(), "\n",
   110 	    "\n",
   160 	    "\n",
   111 	    "  * $opt_message",
   161 	    "  * $opt_message",
   112 	    "\n",
   162 	    "\n",
       
   163 	    $auto_message,
   113 	    "\n", $MAGIC, "\n";
   164 	    "\n", $MAGIC, "\n";
   114 
   165 
   115     if (!-e $LOG) {
   166     if (!-e $LOG) {
   116 	open(X, $_ = ">>$LOG") or die "Can't open $_: $!\n";
   167 	open(X, $_ = ">>$LOG") or die "Can't open $_: $!\n";
   117 	close X;
   168 	close X;
   178 		"Subject" => $subject});
   229 		"Subject" => $subject});
   179 	    print $mailer $head, "\n", $text;
   230 	    print $mailer $head, "\n", $text;
   180 	    close $mailer;
   231 	    close $mailer;
   181 	    print STDERR "Mail sent (to $mailto).\n";
   232 	    print STDERR "Mail sent (to $mailto).\n";
   182 	}
   233 	}
       
   234 
       
   235 	if (defined @config::notify_dirs) {
       
   236 	    foreach my $dir (@config::notify_dirs) {
       
   237 		-d $dir or next;
       
   238 
       
   239 		system("cd $dir && hg commit -m 'autocommit by logbuch'");
       
   240 	    }
       
   241 	}
   183     }
   242     }
   184 
   243 
   185     # Und jetzt das aus der alten Datei dort anhängen
   244     # Und jetzt das aus der alten Datei dort anhängen
   186     open(IN, $_ = $LOG) or die "Can't open $_: $!\n";
   245     open(IN, $_ = $LOG) or die "Can't open $_: $!\n";
   187     print $fh <IN>;
   246     print $fh <IN>;
   210 sub mailto()
   269 sub mailto()
   211 {
   270 {
   212     return join(", ", @config::mailto);
   271     return join(", ", @config::mailto);
   213 }
   272 }
   214 
   273 
       
   274 
       
   275 sub check_hg_bin()
       
   276 {
       
   277     if (not defined which('hg')) {
       
   278 	print STDERR << 'EOF';
       
   279 
       
   280 You requested an operation based on hg/mercurial but this tool is 
       
   281 not installed!
       
   282 
       
   283 Either you could change the configuration in /etc/lobbuch/config.pm and
       
   284 remove lines starting with @notify_dirs, or you could simply install the
       
   285 required packages:
       
   286 
       
   287     # aptitude install mercurial
       
   288 
       
   289 Exiting!
       
   290 EOF
       
   291 	exit 1;
       
   292     }
       
   293 }
       
   294 
       
   295 
   215 # vim:sts=4 sw=4 aw ai sm:
   296 # vim:sts=4 sw=4 aw ai sm:
   216 
   297