Debian Codegraph @

tools/update-from-latest-olson

raw
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use autodie qw( :all );
6
7 use File::pushd;
8 use Getopt::Long;
9 use Net::FTP;
10 use Path::Class qw( dir tempdir );
11
12 sub main {
13 my %opts = (
14 download => 1,
15 tests => 1,
16 compile => 1,
17 );
18
19 GetOptions(
20 'download!' => \$opts{download},
21 'dir:s' => \$opts{dir},
22 'tests!' => \$opts{tests},
23 'compile!' => \$opts{compile},
24 );
25
26 my $olson_version;
27 my $dir
28 = $opts{dir}
29 ? dir( $opts{dir} )
30 : tempdir( CLEANUP => 1 );
31
32 if ( $opts{download} ) {
33 $olson_version = _download($dir);
34 }
35 else {
36 ($olson_version) = $dir =~ m{/([^/]+)$};
37 }
38
39 if ( $opts{compile} ) {
40 _compile_tzdata($dir);
41 }
42 _parse_olson( $dir, $olson_version, $opts{tests} );
43 }
44
45 sub _download {
46 my $pushed = pushd(shift);
47
48 my $host = 'ftp.iana.org';
49
50 my $ftp = Net::FTP->new( $host, Passive => 1 )
51 or die "Cannot connect to $host: $@";
52 $ftp->login()
53 or die 'Cannot login: ', $ftp->message;
54
55 my $dir = '/tz/releases';
56 $ftp->cwd($dir)
57 or die "Cannot cwd to $dir: ", $ftp->message;
58
59 my @code;
60 my @data;
61 for my $file ( $ftp->ls() ) {
62 next unless $file =~ /tz(code|data)(\d+)(\w+)\.tar\.gz/;
63
64 if ( $1 eq 'code' ) {
65 push @code, [ $file, $2, $3 ];
66 }
67 else {
68 push @data, [ $file, $2, $3 ];
69 }
70 }
71
72 my $data
73 = ( sort { $b->[1] <=> $a->[1] or $b->[2] cmp $a->[2] } @data )[0]
74 ->[0];
75
76 my $code
77 = ( sort { $b->[1] <=> $a->[1] or $b->[2] cmp $a->[2] } @code )[0]
78 ->[0];
79
80 $ftp->binary();
81
82 my $olson_version;
83 for my $file ( $code, $data ) {
84 print "Getting $file\n";
85 $ftp->get($file)
86 or die "Cannot fetch $file: ", $ftp->message;
87
88 system( 'tar', 'xzf', $file );
89
90 ($olson_version) = $file =~ /(\d\d\d\d\w)/;
91
92 die "Did not retrieve anything from elsie"
93 unless $olson_version;
94 }
95
96 return $olson_version;
97 }
98
99 sub _compile_tzdata {
100 my $dir = shift;
101
102 my $pushed = pushd($dir);
103
104 # The CFLAGS bit is necessary because of an issue introduced in 2017c with
105 # the use of snprintf. This works fine on my machine without the flag but
106 # blows up on Travis.
107 system( 'make', 'CFLAGS=-DHAVE_SNPRINTF' )
108 and die "Cannot run make: $!";
109
110 my $target = $dir->subdir('target');
111 $target->mkpath( 0, 0755 );
112
113 my @files = qw(
114 africa
115 antarctica
116 asia
117 australasia
118 europe
119 northamerica
120 southamerica
121 etcetera
122 factory
123 backward
124 );
125
126 for my $f (@files) {
127 system( './zic', '-d', $target, $f )
128 and die "Cannot run zic on $f";
129 }
130
131 # The rdfind and symlinks bits are copied from the Debian tzdata packages
132 # rules file.
133 system(
134 'rdfind',
135 '-outputname', '/dev/null',
136 '-makesymlinks', 'true',
137 '-removeidentinode', 'false',
138 $target
139 );
140
141 system( qw( symlinks -r -s -c ), $target );
142
143 system(qw( sudo rm -fr /usr/share/zoneinfo ));
144 system( qw( sudo mv ), $target, '/usr/share/zoneinfo' );
145 system(qw( sudo chown -R root:root /usr/share/zoneinfo ));
146 }
147
148 sub _parse_olson {
149 my $dir = shift;
150 my $olson_version = shift;
151 my $generate_tests = shift;
152
153 system(
154 './tools/parse_olson',
155 '--clean',
156 '--version', $olson_version,
157 '--dir', $dir,
158 ) and die "Cannot run parse_olson: $!";
159
160 if ($generate_tests) {
161 print "Generating tests from zdump\n";
162 system('./tools/tests_from_zdump')
163 and die "Cannot run tests_from_zdump: $!";
164 }
165 }
166
167 main();