chiark / gitweb /
Do not include approved client info in master-info
[ypp-sc-tools.db-test.git] / yarrg / update-master-info
1 #!/usr/bin/perl -w
2 #
3 # MAIN ADMINISTRATIVE DATABASE UPDATE TOOL
4 #
5 #  Usage:
6 #     ./update-master-info [-d RSYNCDESTDIR] [-O OCEAN ... | -a]
7 #
8 #  Usual usages:
9 #
10 #    After editing source-info.txt to add commodities, or
11 #    changing db-idempotent-update (eg to change the schema):
12 #         ./update-master-info -d ~ftp/users/ijackson/yarrg/
13 #    This will update everything except the ocean topologies.
14 #
15 #    To take account of new islands, or to fix a mistake in
16 #    assigning island(s) to archipelago(es), or to add support
17 #    for a new ocean:
18 #         ./update-master-info -d ~ftp/users/ijackson/yarrg/ -O Midnight
19 #
20 #    After a fix to a mistake on a YPPedia chart page, correcting
21 #    only inter-island-distances:
22 #         ./update-master-info -O Midnight
23 #
24 #    After changing the algorithms for topology determination
25 #    or YPPedia scraping:
26 #         ./update-master-info -a
27 #    and check that nothing unexpected changes.
28 #
29
30 # This is part of ypp-sc-tools, a set of third-party tools for assisting
31 # players of Yohoho Puzzle Pirates.
32 #
33 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
34 #
35 # This program is free software: you can redistribute it and/or modify
36 # it under the terms of the GNU General Public License as published by
37 # the Free Software Foundation, either version 3 of the License, or
38 # (at your option) any later version.
39 #
40 # This program is distributed in the hope that it will be useful,
41 # but WITHOUT ANY WARRANTY; without even the implied warranty of
42 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43 # GNU General Public License for more details.
44 #
45 # You should have received a copy of the GNU General Public License
46 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
47 #
48 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
49 # are used without permission.  This program is not endorsed or
50 # sponsored by Three Rings.
51
52 use strict (qw(vars));
53 use DBI;
54 use Commods;
55
56 $ENV{'LC_CTYPE'}= 'en_GB.UTF-8';
57
58 sub full ($) {
59     my ($ocean) = @_;
60     quick($ocean);
61     print "## updating topology of $ocean\n";
62     system('./yppedia-chart-parser',$ocean); die "$ocean $?" if $?;
63     print "\n";
64 }
65
66 sub quick ($) {
67     my ($ocean) = @_;
68     print STDERR "## updating schema and commodities for $ocean\n";
69     system('./db-idempotent-populate',$ocean); die $? if $?;
70 }
71
72 my $rsyncdir;
73
74 sub process_some_info ($$$) {
75     my ($v,$df,$sfn) = @_;
76     my $sf= new IO::File $sfn or die "$sfn $!";
77
78     my $h;
79     while (<$sf>) {
80         chomp; s/\s+$//;
81         next if m/^\s*\#/ || !m/\S/;
82         if (m/^\S.*/) {
83             $h= $&;
84         }
85         die "$_ ?" unless defined $h;
86         if ($h =~ m/^commods|^\%[a-z]\b/) {
87             s/\t.*//;
88         }
89         if ($v<2) {
90             next if $h =~ m/^nocommods/;
91         }
92         next if $sfn =~ m/source-info/ && $h =~ m/^ocean\b/;
93         next if $h =~ m/^client\b/;
94
95         print $df $_, "\n" or die $!;
96     }
97
98     $sf->error and die $!;
99 }
100
101 sub update_master_info () {
102     foreach my $v (1..$masterinfoversion) {
103         my $dfnl= sprintf "master-info%s.txt", ($v>1 ? "-v$v" : '');
104         print STDERR "installing new $dfnl...\n";
105     
106         my $dfn= "$rsyncdir/$dfnl";
107         my $df= new IO::File "$dfn.tmp", 'w' or die "$dfn.tmp $!";
108
109         process_some_info($v,$df, 'source-info.txt');
110         foreach my $ocean (sort keys %oceans) {
111             process_some_info($v,$df, '_ocean-'.(lc $ocean).'.txt');
112         }
113
114         close $df or die $!;
115         rename "$dfn.tmp", "$dfn" or die $!;
116     }
117 }
118
119
120 my @specoceans;
121 my $alloceans;
122
123 sub optarg () {
124     return $_ if length;
125     die unless @ARGV;
126     return scalar shift @ARGV;
127 }
128
129 while (@ARGV && $ARGV[0] =~ m/^-/) {
130     $_= shift @ARGV;
131     last if m/^--?$/;
132     while (m/^-./) {
133         if (s/^-d//) {
134             die if defined $rsyncdir;
135             $rsyncdir= optarg();
136         } elsif (s/^-O//) {
137             push @specoceans, optarg();
138         } elsif (s/^-a//) {
139             die if $alloceans;
140             $alloceans=1;
141         } else {
142             die "$_ ?";
143         }
144     }
145 }
146 die if @ARGV;
147
148 die if @specoceans && $alloceans;
149
150 parse_info_serverside();
151
152 if (@specoceans) {
153     print "### full update of specified oceans ...\n";
154     foreach my $ocean (@specoceans) {
155         die "$ocean ?" unless defined $oceans{$ocean};
156         full($ocean);
157     }
158 } elsif ($alloceans) {
159     print "### full (inc.topology) update of all oceans ...\n";
160     foreach my $ocean (sort keys %oceans) {
161         full($ocean);
162     }
163 } else {
164     print "### quick (no topology) update only (of all oceans) ...\n";
165     foreach my $ocean (sort keys %oceans) {
166         quick($ocean);
167     }
168 }
169
170 if (defined $rsyncdir) {
171     print "### master-info update ...\n";
172     update_master_info();
173 }