-
Notifications
You must be signed in to change notification settings - Fork 26
/
country.pl
executable file
·144 lines (119 loc) · 3.44 KB
/
country.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/perl
use strict;
use warnings;
use PVE::Tools;
use JSON qw(from_json to_json);
# Generates a
#
# - country code => name/kmap/mirror
# - name => country code
#
# mapping for each defined country
my sub generate_country_mappings {
my ($country_codes, $defmap, $mirrors) = @_;
my ($countries, $countryhash) = ({}, {});
foreach my $cc (sort keys %$country_codes) {
my $name = $country_codes->{$cc};
my $kmap = $defmap->{$cc} || '';
my $mirror = $mirrors->{$cc} || '';
$countries->{$cc} = {
name => $name,
kmap => $kmap,
mirror => $mirror,
};
$countryhash->{lc($name)} = $cc;
}
return ($countries, $countryhash);
}
# we need mappings for X11, console, and kvm vnc
# LC(-LC)? => [DESC, kvm, console, X11, X11variant]
my sub generate_keymaps {
my ($country_codes) = @_;
my ($kmap, $kmaphash) = ({}, {});
my $keymaps = PVE::Tools::kvmkeymaps();
foreach my $km (sort keys %$keymaps) {
my ($name, $kvm, $console, $x11, $x11var) = @{$keymaps->{$km}};
if ($km =~m/^([a-z][a-z])-([a-z][a-z])$/i) {
defined ($country_codes->{$2}) || die "undefined country code '$2'";
} else {
defined ($country_codes->{$km}) || die "undefined country code '$km'";
}
$x11var = '' if !defined ($x11var);
$kmap->{$km} = {
name => $name,
kvm => $kvm,
console => $console,
x11 => $x11,
x11var => $x11var,
};
$kmaphash->{$name} = $km;
}
return ($kmap, $kmaphash);
}
my sub parse_zoneinfo {
my ($countries) = @_;
my $zonefn = "/usr/share/zoneinfo/zone.tab";
open (my $ZONE_TAB_FH, '<', "$zonefn") || die "unable to open '$zonefn' - $!\n";
my ($zones, $cczones) = ({}, {});
while (defined (my $line = <$ZONE_TAB_FH>)) {
next if $line =~ m/^\s*(?:#|$)/;
if ($line =~ m|^([A-Z][A-Z])\s+\S+\s+(([^/]+)/\S+)\s|) {
my $cc = lc($1);
$cczones->{$cc}->{$2} = 1;
$countries->{$cc}->{zone} = $2 if !defined ($countries->{$cc}->{zone});
$zones->{$2} = 1;
}
}
close ($ZONE_TAB_FH);
return ($zones, $cczones);
}
# country codes from:
my $country_codes_file = "/usr/share/iso-codes/json/iso_3166-1.json";
my $iso_3166_codes = from_json(PVE::Tools::file_get_contents($country_codes_file, 64 * 1024));
my $country_codes = { map { lc($_->{'alpha_2'}) => $_->{'common_name'} // $_->{'name'} } @{$iso_3166_codes->{'3166-1'}} };
my $defmap = {
'us' => 'en-us',
'be' => 'fr-be',
'br' => 'pt-br',
'ca' => 'en-us',
'dk' => 'dk',
'nl' => 'en-us', # most Dutch people use US layout
'fi' => 'fi',
'fr' => 'fr',
'de' => 'de',
'at' => 'de',
'hu' => 'hu',
'is' => 'is',
'it' => 'it',
'va' => 'it',
'jp' => 'jp',
'lt' => 'lt',
'mk' => 'mk',
'no' => 'no',
'pl' => 'pl',
'pt' => 'pt',
'si' => 'si',
'es' => 'es',
'gi' => 'es',
'ch' => 'de-ch',
'gb' => 'en-gb',
'lu' => 'fr-ch',
'li' => 'de-ch',
};
my $mirrors = PVE::Tools::debmirrors();
foreach my $cc (keys %$mirrors) {
die "undefined country code '$cc'" if !defined ($country_codes->{$cc});
}
my ($countries, $countryhash) = generate_country_mappings($country_codes, $defmap, $mirrors);
my ($kmap, $kmaphash) = generate_keymaps($country_codes);
my ($zones, $cczones) = parse_zoneinfo($countries);
my $locale_info = {
country => $countries,
countryhash => $countryhash,
kmap => $kmap,
kmaphash => $kmaphash,
zones => $zones,
cczones => $cczones,
};
my $json = to_json($locale_info, { utf8 => 1, canonical => 1 });
print $json;