# HG changeset patch # User Heiko Schlittermann # Date 1238055807 -3600 # Node ID 3a86e78a18b107670494755276cb347052c7d69a # Parent 9032fd09c2743fa27d2be67531411a494c42c144 cosmetic: added perltidy and used it diff -r 9032fd09c274 -r 3a86e78a18b1 .perltidyrc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.perltidyrc Thu Mar 26 09:23:27 2009 +0100 @@ -0,0 +1,1 @@ +--paren-tightness=2 diff -r 9032fd09c274 -r 3a86e78a18b1 log.pl --- a/log.pl Thu Mar 26 09:22:30 2009 +0100 +++ b/log.pl Thu Mar 26 09:23:27 2009 +0100 @@ -47,18 +47,18 @@ my $ME = basename $0; -my $DSN = "DBI:mysql:logbuch:pu.schlittermann.de"; +my $DSN = "DBI:mysql:logbuch:pu.schlittermann.de"; my $USER = "logbuch"; -my $PW = "HIDDEN"; +my $PW = "HIDDEN"; -my $LOG = "$ENV{HOME}/LOG"; +my $LOG = "$ENV{HOME}/LOG"; my $EDITOR = $ENV{VISUAL} || $ENV{EDITOR} || "vim"; -my $MAGIC = "#--- all changes below are ignored ---#\n"; +my $MAGIC = "#--- all changes below are ignored ---#\n"; -my $opt_db = 1; -my $opt_mail = 1; +my $opt_db = 1; +my $opt_mail = 1; my $opt_message = ""; -my $opt_apt = ""; +my $opt_apt = ""; my $opt_initdir = ""; my $Dbh; @@ -70,72 +70,74 @@ MAIN: { - GetOptions("db!" => \$opt_db, - "mail!" => \$opt_mail, - "message=s" => \$opt_message, - "type=s" => \$opt_apt, - "init-dir=s" => \$opt_initdir, + GetOptions( + "db!" => \$opt_db, + "mail!" => \$opt_mail, + "message=s" => \$opt_message, + "type=s" => \$opt_apt, + "init-dir=s" => \$opt_initdir, ) or die eval "\"$USAGE\""; if ($opt_message =~ /^@(.*)/) { - @ARGV = $1; - $opt_message = join "", <>; - } elsif ($opt_message eq "-") { - $opt_message = join "", ; - } elsif ($opt_message =~ /^apt(?::(.*))?\@(\S+)/) { - open(I, $2) or die "Can't open $2: $!\n"; - $opt_message = ($1 ? "APT: $1\n" : "APT: upgrade\n") - . join "", map { - if (/^\d/) { ($_) } - elsif ( /^(?:Inst|Conf|Remv|Purg)/ ) { ("- $_") } - else { () } - } ; + @ARGV = $1; + $opt_message = join "", <>; + } + elsif ($opt_message eq "-") { + $opt_message = join "", ; + } + elsif ($opt_message =~ /^apt(?::(.*))?\@(\S+)/) { + open(I, $2) or die "Can't open $2: $!\n"; + $opt_message = ($1 ? "APT: $1\n" : "APT: upgrade\n") . join "", map { + if (/^\d/) { ($_) } + elsif (/^(?:Inst|Conf|Remv|Purg)/) { ("- $_") } + else { () } + } ; } if ($opt_message =~ /\n/) { - $opt_message =~ s/\n/\n /g; + $opt_message =~ s/\n/\n /g; } if (defined @config::notify_dirs || $opt_initdir) { - check_hg_bin(); + check_hg_bin(); } if ($opt_initdir) { - my $repo = Logbuch::HG->new( repo => $opt_initdir ); + my $repo = Logbuch::HG->new(repo => $opt_initdir); - $repo->is_repository() and - die "$ME: directory already initialized, skipping\n"; + $repo->is_repository() + and die "$ME: directory already initialized, skipping\n"; - $repo->init() or - die "E: initialization failed\n"; - - $repo->addremove(); - $repo->commit("initial check in"); + $repo->init() + or die "E: initialization failed\n"; - exit 0; + $repo->addremove(); + $repo->commit("initial check in"); + + exit 0; } my $hg_status_text = ""; if (defined @config::notify_dirs) { - foreach my $dir (@config::notify_dirs) { - -d $dir or next; + foreach my $dir (@config::notify_dirs) { + -d $dir or next; - print "$ME: Checking $dir for modifications\n"; + print "$ME: Checking $dir for modifications\n"; - my $repo = Logbuch::HG->new( repo => $dir ); - $repo->is_repository() or - die "$ME: directory $dir not initialized please call: \n", - " # $ME --init-dir $dir \n"; + my $repo = Logbuch::HG->new(repo => $dir); + $repo->is_repository() + or die "$ME: directory $dir not initialized please call: \n", + " # $ME --init-dir $dir \n"; - $repo->addremove(); - $hg_status_text .= $repo->status(); - } + $repo->addremove(); + $hg_status_text .= $repo->status(); + } } if ($opt_db) { - $Dbh = DBI->connect($DSN, $USER, $PW, {RaiseError => 1}) - or die $DBI::errstr; - END { $Dbh->disconnect() if $Dbh; } + $Dbh = DBI->connect($DSN, $USER, $PW, { RaiseError => 1 }) + or die $DBI::errstr; + END { $Dbh->disconnect() if $Dbh; } } # Temporärfile öffnen @@ -143,102 +145,107 @@ END { unlink $file if $file; } ($fh, $file) = tempfile(DIR => "/tmp"); - my $auto_message = (not $hg_status_text) ? "" : - "\n" - . " Modified config files since last log entry listend below...\n" - . $hg_status_text - . "\n"; + my $auto_message = + (not $hg_status_text) + ? "" + : "\n" + . " Modified config files since last log entry listend below...\n" + . $hg_status_text . "\n"; # Kopftext eintragen - print $fh - "Date: ", scalar(localtime()), "\n", - "User: ", identity(), "\n", - "MailTo: ", mailto(), "\n", - "\n", - " * $opt_message", - "\n", - $auto_message, - "\n", $MAGIC, "\n"; + print $fh "Date: ", scalar(localtime()), "\n", + "User: ", identity(), "\n", + "MailTo: ", mailto(), "\n", + "\n", + " * $opt_message", + "\n", + $auto_message, + "\n", $MAGIC, "\n"; if (!-e $LOG) { - open(X, $_ = ">>$LOG") or die "Can't open $_: $!\n"; - close X; - }; + open(X, $_ = ">>$LOG") or die "Can't open $_: $!\n"; + close X; + } open(IN, $_ = $LOG) or die "Can't open $_: $!\n"; print $fh ; close IN; if (!$opt_message) { - my $stamp = stat($file)->mtime(); - system($EDITOR, "+5", $file); - - if ($stamp == stat($file)->mtime()) { - print STDERR "Nothing changed. Discarding the note.\n"; - unlink $file; - exit 0; - } + my $stamp = stat($file)->mtime(); + system($EDITOR, "+5", $file); + + if ($stamp == stat($file)->mtime()) { + print STDERR "Nothing changed. Discarding the note.\n"; + unlink $file; + exit 0; + } } # Jetzt wie versprochen den (eventuell geänderten Rest) aus der # Temp-Datei wegschneiden { - my ($date, $user, $head, $text, $mailto); - my $pos; + my ($date, $user, $head, $text, $mailto); + my $pos; + + seek $fh, 0, 0; + for ($pos = tell $fh ; defined($_ = <$fh>) ; $pos = tell $fh) { - seek $fh, 0, 0; - for($pos = tell $fh; defined($_ = <$fh>); $pos = tell $fh) { + $head .= "$_" if not $text and /^\S+:/; - $head .= "$_" if not $text and /^\S+:/; + /^Date:\s+(.*)/ and $date = $1, next; + /^User:\s+(.*)/ and $user = $1, next; + /^MailTo:\s(.*)/ and $mailto = $1, next; + last if $_ eq $MAGIC; - /^Date:\s+(.*)/ and $date = $1, next; - /^User:\s+(.*)/ and $user = $1, next; - /^MailTo:\s(.*)/ and $mailto = $1, next; - last if $_ eq $MAGIC; - - $text .= $_ if /\S/ || $text; # somit werden die ersten Leerzeilen übersprungen - } + $text .= $_ + if /\S/ + || $text; # somit werden die ersten Leerzeilen übersprungen + } - $text =~ s/\s*$//s; # Leerzeichen am Ende weg + $text =~ s/\s*$//s; # Leerzeichen am Ende weg - truncate $fh, $pos; - seek $fh, 0, 2; + truncate $fh, $pos; + seek $fh, 0, 2; - if ($opt_db) { - my $sth = $Dbh->prepare(" + if ($opt_db) { + my $sth = $Dbh->prepare(" INSERT INTO log (host, date, user, mailto, text) VALUES(?, now(), ?, ?, ?)"); - $sth->execute(hostname(), $user, $mailto, $text); - print STDERR "Database entry inserted\n"; - } + $sth->execute(hostname(), $user, $mailto, $text); + print STDERR "Database entry inserted\n"; + } - if ($opt_mail and $mailto) { - my $mailer = new Mail::Mailer "sendmail" - or die "Can't create Mailer: $!\n"; + if ($opt_mail and $mailto) { + my $mailer = new Mail::Mailer "sendmail" + or die "Can't create Mailer: $!\n"; - my $subject = (split /\n/, $text)[0]; - $subject =~ s/^\s*\S\s//; # cut the "itemizer" - $subject = encode_qp("Service [". hostname(). "]: $subject\n"); - $subject =~ s/\n(.)/\n\t$1/g; + my $subject = (split /\n/, $text)[0]; + $subject =~ s/^\s*\S\s//; # cut the "itemizer" + $subject = encode_qp("Service [" . hostname() . "]: $subject\n"); + $subject =~ s/\n(.)/\n\t$1/g; - $mailer->open({ - "Content-Type" => "text/plain; charset=utf-8", - "Content-Transfer-Encoding" => "8bit", - "To" => $mailto, - "Subject" => $subject}); - print $mailer $head, "\n", $text; - close $mailer; - print STDERR "Mail sent (to $mailto).\n"; - } + $mailer->open( + { + "Content-Type" => "text/plain; charset=utf-8", + "Content-Transfer-Encoding" => "8bit", + "To" => $mailto, + "Subject" => $subject + } + ); + print $mailer $head, "\n", $text; + close $mailer; + print STDERR "Mail sent (to $mailto).\n"; + } - if (defined @config::notify_dirs) { - foreach my $dir (@config::notify_dirs) { - -d $dir or next; + if (defined @config::notify_dirs) { + foreach my $dir (@config::notify_dirs) { + -d $dir or next; - my $repo = Logbuch::HG->new( repo => $dir ); - $repo->commit(); - } - } + my $repo = Logbuch::HG->new(repo => $dir); + $repo->commit(); + } + } } # Und jetzt das aus der alten Datei dort anhängen @@ -251,32 +258,27 @@ } -sub identity() -{ +sub identity() { my $user = `who am i`; chomp $user; $user .= " [" . ($ENV{IUS_PROFILE} || $ENV{REMOTE_USER} || "-") . "]"; return $user; } -sub hostname() -{ +sub hostname() { my $r = `hostname -f`; chomp($r); return $r; } -sub mailto() -{ +sub mailto() { return join(", ", @config::mailto); } - -sub check_hg_bin() -{ +sub check_hg_bin() { if (not Logbuch::HG::hg_available()) { - die <<'EOF'; + die <<'EOF'; You requested an operation based on hg/mercurial but this tool is not installed! @@ -292,6 +294,5 @@ } } - # vim:sts=4 sw=4 aw ai sm: