blib/lib/Doit/Locale.pm

filter: Statementstmt 18.0% 13 / 72 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 62.5% 5 / 8 Podpod 0.0% 0 / 3 totaltotal 12.6% 18 / 142 SCAR 61.6
CC
25
Cov
11%
locale_enable_locale
462.0
__ANON__
30.0
__ANON__
2.0
CRAP 472.0
« blib/lib/Doit/User.pm ↑ Summary blib/lib/Doit/Deb.pm »
1 # -*- perl -*-
2  
3 #
4 # Author: Slaven Rezic
5 #
6 # Copyright (C) 2017,2018,2020 Slaven Rezic. All rights reserved.
7 # This package is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 #
10 # Mail: slaven@rezic.de
11 # WWW: http://www.rezic.de/eserte/
12 #
13  
14 package Doit::Locale;
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 our $VERSION = '0.024';
19  
20 1 ▶use Doit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
23 1 ▶sub functions { qw(locale_enable_locale) }
Subroutine functions: called CC=1 SCAR=0.0
Pod undocumented
24  
25 sub locale_enable_locale {
26 0 ▶ my($self, $locale) = @_;
Subroutine locale_enable_locale: not called CC=21 SCAR=61.4
Pod undocumented
27 0 my %locale;
28 0 ▶ if (ref $locale eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $locale eq 'ARRAY') { } 0 0 0
29 0 %locale = map{($_,1)} @$locale;
30 } else {
31 0 %locale = ($locale => 1);
32 }
33  
34 ######################################################################
35 # Is locale already present?
36 my $is_locale_present = sub {
37 0 ▶ open my $fh, '-|', 'locale', '-a'
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", "locale", "-a" or &error("Error while running 'locale -a': $!") 0 0 0
38 or error "Error while running 'locale -a': $!";
39 0 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
40 0 chomp;
41 0 ▶ if ($locale{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locale{$_}) 0 0 0
42 0 return 1;
43 }
44 }
45 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or &error("Error while running 'locale -a': $!") 0 0 0
46 or error "Error while running 'locale -a': $!";
47 0 return 0;
48 0 };
49  
50 0 ▶ if ($is_locale_present->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$is_locale_present()) 0 0 0
51 0 return 0; # no change
52 }
53  
54 ######################################################################
55 # locale-gen (e.g. Ubuntu 12.03)
56 0 ▶ if (-x "/usr/sbin/locale-gen" && !-e "/etc/locale.gen") {
Branch: 1 branch
BranchTrueFalseTotal
if (-x "/usr/sbin/locale-gen" and not -e "/etc/locale.gen") 0 0 0
Condition0%0/3
-x "/usr/sbin/locale-gen" and not -e "/etc/locale.gen"
!ll&&!rl&&r
000
MC/DC0%0/2
-x "/usr/sbin/locale-gen" and not -e "/etc/locale.gen"
-x "/usr/sbin/locale-gen" not -e "/etc/locale.gen"
Truth table0%0/3
A and not(B)
A= -x "/usr/sbin/locale-gen"B= not -e "/etc/locale.gen"
ABresult
0X0
100
111
57 0 $self->system('locale-gen', $locale->[0]);
58 0 return 1;
59 }
60  
61 ######################################################################
62 # /etc/locale.gen (e.g. Debian and Debian-like)
63 0 ▶ if (-e "/etc/locale.gen") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "/etc/locale.gen") 0 0 0
64 0 my $all_locales = '(' . join('|', map { quotemeta $_ } keys %locale) . ')';
65 my $changes = $self->change_file("/etc/locale.gen",
66 {match => qr{^#\s+$all_locales(\s|$)},
67 0 ▶ action => sub { $_[0] =~ s{^#\s+}{}; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 },
69 0 );
70 0 ▶ if (!$changes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($changes) 0 0 0
71 0 error "Cannot find prepared locale '$locale' in /etc/locale.gen";
72 }
73 0 $self->system('locale-gen');
74 0 return 1;
75 }
76  
77 ######################################################################
78 # localedef (e.g RedHat, CentOS)
79 0 ▶ if (-x "/usr/bin/localedef" && -e "/etc/redhat-release") {
Branch: 1 branch
BranchTrueFalseTotal
if (-x "/usr/bin/localedef" and -e "/etc/redhat-release") 0 0 0
Condition0%0/3
-x "/usr/bin/localedef" and -e "/etc/redhat-release"
!ll&&!rl&&r
000
MC/DC0%0/2
-x "/usr/bin/localedef" and -e "/etc/redhat-release"
-x "/usr/bin/localedef" -e "/etc/redhat-release"
Truth table0%0/3
A and B
A= -x "/usr/bin/localedef"B= -e "/etc/redhat-release"
ABresult
0X0
100
111
80 # It also exists on Debian-based systems, but works differently there.
81 0 my $use_glibc_langpack;
82 0 ▶ if (open my $fh, "/etc/redhat-release") {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "/etc/redhat-release") 0 0 0
83 0 my $line = <$fh>;
84 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Fedora release (\d+) / and $1 >= 28 or $line =~ /^CentOS Linux release (\d+)/ and $1 >= 8) 0 0 0
Condition0%0/9
$line =~ /^Fedora release (\d+) / and $1 >= 28 or $line =~ /^CentOS Linux release (\d+)/ and $1 >= 8
l!l&&r!l&&!r
000
$line =~ /^Fedora release (\d+) / and $1 >= 28
!ll&&!rl&&r
000
$line =~ /^CentOS Linux release (\d+)/ and $1 >= 8
!ll&&!rl&&r
000
MC/DC0%0/4
$line =~ /^Fedora release (\d+) / and $1 >= 28 or $line =~ /^CentOS Linux release (\d+)/ and $1 >= 8
$line =~ /^Fedora release (\d+) / $1 >= 28 $line =~ /^CentOS Linux release (\d+)/ $1 >= 8
Truth table0%0/7
A and B or C and D
A= $line =~ /^Fedora release (\d+) /B= $1 >= 28C= $line =~ /^CentOS Linux release (\d+)/D= $1 >= 8
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
85 ($line =~ /^Fedora release (\d+) / && $1 >= 28) # XXX since when we should take this path?
86 || ($line =~ /^CentOS Linux release (\d+)/ && $1 >= 8)
87 ) {
88 0 $use_glibc_langpack = 1;
89 }
90 }
91 TRY_LOCALE: {
92 0 my @errors;
93 0 ▶ if ($use_glibc_langpack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_glibc_langpack) 0 0 0
94 0 ▶ if ((keys(%locale))[0] =~ m{^([^_]+)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ((keys %locale)[0] =~ /^([^_]+)/) 0 0 0
95 0 my $lang = $1;
96 # XXX requires a previous add_component("rpm"); should be done automatically!
97 0 my $package = 'glibc-langpack-'.$lang;
98 0 eval { $self->rpm_install_packages($package) };
99 0 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
100 0 last TRY_LOCALE;
101 }
102 0 push @errors, "Installing $package failed: $@";
103 }
104 }
105 0 for my $try_locale (sort keys %locale) {
106 0 ▶ if (my($lang_country, $charset) = $try_locale =~ m{^(.*)\.(.*)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $try_locale =~ /^(.*)\.(.*)$/) { } 0 0 0
107 0 my $stderr;
108 0 eval { $self->open3({errref => \$stderr}, '/usr/bin/localedef', '-c', '-i', $lang_country, '-f', $charset, $try_locale) };
109 0 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
110 0 last TRY_LOCALE;
111 }
112 # an error, but maybe successful?
113 0 ▶ if ($is_locale_present->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$is_locale_present()) 0 0 0
114 0 last TRY_LOCALE;
115 }
116 0 push @errors, "Can't add '$try_locale': $stderr";
117 } else {
118 0 push @errors, "Can't parse '$try_locale' as lang_COUNTRY.charset";
119 }
120 }
121 0 error "Can't install locale. Errors:\n" . join("\n", @errors);
122 }
123 0 return 1;
124 }
125  
126 ######################################################################
127 # not implemented elsewhere
128 0 error "Don't know how to enable locales on this system";
129 }
130  
131 1 1;
132  
133 __END__
« blib/lib/Doit/User.pm ↑ Summary blib/lib/Doit/Deb.pm »