From: Markus Armbruster Date: Sun, 28 Apr 2013 13:22:11 +0000 (+0200) Subject: Make mksubj.pl touch subject files only when it needs to change X-Git-Tag: v4.3.31~33 X-Git-Url: http://git.pond.sub.org/?p=empserver;a=commitdiff_plain;h=156930c51505e6a5d53f709229f3998e6ce87bbf Make mksubj.pl touch subject files only when it needs to change Avoids unnecessary reformatting of subject pages again. --- diff --git a/info/mksubj.pl b/info/mksubj.pl index 4e966b123..e178c97a5 100644 --- a/info/mksubj.pl +++ b/info/mksubj.pl @@ -25,7 +25,7 @@ # # --- # -# mksubj.pl: Create the subject index pages +# mksubj.pl: Update the subject index pages # # Known contributors to this file: # Ken Stevens (when it was still info.pl) @@ -33,14 +33,12 @@ # # Usage: mksubj.pl SUBJECT... INFO-FILE... # -# Read the INFO-FILE..., create info/SUBJECT.t for each SUBJECT. +# Read the INFO-FILE..., update info/SUBJECT.t for each SUBJECT. use strict; use warnings; use File::stat; -use Fcntl qw(O_WRONLY O_EXCL O_CREAT); - # The chapters, in order my @Chapters = qw/Introduction Concept Command Server/; @@ -84,7 +82,9 @@ for my $t (sort keys %desc) { parse_see_also($t); } -create_subjects(); +for (keys %Subjects) { + update_subj($_); +} # Parse an info file # Set $filename, $filename{TOPIC}, $long{TOPIC}, $chapter{TOPIC}, @@ -194,22 +194,20 @@ sub set_subject { $largest{$sub} = $topic if length $topic > length $largest{$sub}; } -# Create a Subject.t file -sub create_subj { +# Update a Subject.t file +sub update_subj { my ($subj) = @_; my $fname = "info/$subj.t"; + my $out = ""; my ($any_topic, $any_basic, $any_obsolete, $any_long); - sysopen(SUBJ, $fname, O_WRONLY | O_EXCL | O_CREAT) - or die "Unable to create $fname: $!\n"; - - print SUBJ '.\" DO NOT EDIT THIS FILE. It was automatically generated by mksubj.pl'."\n"; - print SUBJ ".TH Subject \U$subj\n"; + $out .= '.\" DO NOT EDIT THIS FILE. It was automatically generated by mksubj.pl'."\n"; + $out .= ".TH Subject \U$subj\n"; $largest{$subj} =~ s/-/M/g; - print SUBJ ".in \\w'$largest{$subj}XX\\0\\0\\0\\0'u\n"; + $out .= ".in \\w'$largest{$subj}XX\\0\\0\\0\\0'u\n"; for my $chap (@Chapters) { next unless exists $subject{$subj}{$chap}; - print SUBJ ".s1\n"; + $out .= ".s1\n"; for my $topic (split(/\n/, $subject{$subj}{$chap})) { $any_topic = 1; my $flags = ""; @@ -226,40 +224,43 @@ sub create_subj { $any_long = 1; } $flags = sprintf("%-2s", $flags); - print SUBJ ".L \"$topic $flags\"\n"; - print SUBJ "$desc{$topic}\n"; + $out .= ".L \"$topic $flags\"\n"; + $out .= "$desc{$topic}\n"; } } unless ($any_topic) { print STDERR "$0: Subject $subj has no topics\n"; exit 1; } - print SUBJ ".s1\n" + $out .= ".s1\n" . ".in 0\n" . "For info on a particular subject, type \"info \" where is\n" . "one of the subjects listed above.\n"; - print SUBJ "Subjects marked by * are the most important and should be read by new players.\n" + $out .= "Subjects marked by * are the most important and should be read by new players.\n" if $any_basic; - print SUBJ "Subjects marked by + are obsolete.\n" + $out .= "Subjects marked by + are obsolete.\n" if $any_obsolete; - print SUBJ "Unusually long subjects are marked with a !.\n" + $out .= "Unusually long subjects are marked with a !.\n" if $any_long; + + return if (same_contents($fname, $out)); + open(SUBJ, ">$fname") + or die "Can't open $fname for writing: $!"; + print SUBJ $out; close SUBJ; } -# Remove the old Subject.t files and create new ones -sub create_subjects { - my (@subj); +sub same_contents { + my ($fname, $contents) = @_; + local $/; - for (keys %Subjects) { - unlink "info/$_.t"; - } - - @subj = sort keys %subject; - - for my $subj (@subj) { - create_subj($subj); + if (!open(SUBJ, "<$fname")) { + return 0 if ($!{ENOENT}); + die "Can't open $fname for reading: $!"; } + my $old = ; + close SUBJ; + return $contents eq $old; } # Print an integrity error message and exit with code 1