]> git.pond.sub.org Git - empserver/commitdiff
Make mksubj.pl touch subject files only when it needs to change
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 28 Apr 2013 13:22:11 +0000 (15:22 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Wed, 8 May 2013 04:57:57 +0000 (06:57 +0200)
Avoids unnecessary reformatting of subject pages again.

info/mksubj.pl

index 4e966b123e6434d3de62f5f0c728012f9416fea7..e178c97a59473aff84b37de17062edfe905321fb 100644 (file)
@@ -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)
 #
 #   Known contributors to this file:
 #      Ken Stevens (when it was still info.pl)
 #
 # Usage: mksubj.pl SUBJECT... INFO-FILE...
 #
 #
 # 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 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/;
 
 # 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);
 }
 
     parse_see_also($t);
 }
 
-create_subjects();
+for (keys %Subjects) {
+    update_subj($_);
+}
 
 # Parse an info file
 # Set $filename, $filename{TOPIC}, $long{TOPIC}, $chapter{TOPIC},
 
 # 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};
 }
 
     $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 ($subj) = @_;
     my $fname = "info/$subj.t";
+    my $out = "";
     my ($any_topic, $any_basic, $any_obsolete, $any_long);
 
     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;
     $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};
     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 = "";
        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);
                $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;
     }
        }
     }
     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 <subject>\" where <subject> is\n"
        . "one of the subjects listed above.\n";
        . ".in 0\n"
        . "For info on a particular subject, type \"info <subject>\" where <subject> 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;
        if $any_basic;
-    print SUBJ "Subjects marked by + are obsolete.\n"
+    $out .= "Subjects marked by + are obsolete.\n"
        if $any_obsolete;
        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;
        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;
 }
 
     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 = <SUBJ>;
+    close SUBJ;
+    return $contents eq $old;
 }
 
 # Print an integrity error message and exit with code 1
 }
 
 # Print an integrity error message and exit with code 1