Dpkg/Source/BinaryFiles.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 190 CC 9 SCAR 22.8
CC
9
Cov
0%
detect_binary_files
30.0
load_allowed_binaries
12.0
binary_is_allowed
6.0
CRAP 9.8
1 # Copyright © 2008-2011 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2008-2015 Guillem Jover <guillem@debian.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 Dpkg::Source::BinaryFiles - manipulate debian/source/include-binaries files
22  
23 =head1 DESCRIPTION
24  
25 This module provides a class to handle the F<debian/source/include-binaries>
26 file.
27  
28 B<Note>: This is a private module, its API can change at any time.
29  
30 =cut
31  
32 package Dpkg::Source::BinaryFiles 0.01;
33  
34 use v5.36;
35  
36 use Cwd;
37 use File::Path qw(make_path);
38 use File::Spec;
39 use File::Find;
40  
41 use Dpkg::ErrorHandling;
42 use Dpkg::Gettext;
43 use Dpkg::Source::Functions qw(is_binary);
44  
45 sub new {
46 my ($this, $dir) = @_;
47 my $class = ref($this) || $this;
48  
49 my $self = {
50 dir => $dir,
51 allowed_binaries => {},
52 seen_binaries => {},
53 include_binaries_path =>
54 File::Spec->catfile($dir, 'debian', 'source', 'include-binaries'),
55 };
56 bless $self, $class;
57 $self->load_allowed_binaries();
58 return $self;
59 }
60  
61 sub new_binary_found {
62 my ($self, $path) = @_;
63  
64 $self->{seen_binaries}{$path} = 1;
65 }
66  
67 sub load_allowed_binaries {
68 my $self = shift;
69 my $incbin_file = $self->{include_binaries_path};
70  
71 if (-f $incbin_file) {
72 open my $incbin_fh, '<', $incbin_file
73 or syserr(g_('cannot read %s'), $incbin_file);
74 while (<$incbin_fh>) {
75 chomp;
76 s/^\s*//;
77 s/\s*$//;
78 next if /^#/ or length == 0;
79 $self->{allowed_binaries}{$_} = 1;
80 }
81 close $incbin_fh;
82 }
83 }
84  
85 sub binary_is_allowed {
86 my ($self, $path) = @_;
87  
88 return 1 if exists $self->{allowed_binaries}{$path};
89 return 0;
90 }
91  
92 sub update_debian_source_include_binaries {
93 my $self = shift;
94  
95 my @unknown_binaries = $self->get_unknown_binaries();
96 return unless scalar @unknown_binaries;
97  
98 my $incbin_file = $self->{include_binaries_path};
99 make_path(File::Spec->catdir($self->{dir}, 'debian', 'source'));
100 open my $incbin_fh, '>>', $incbin_file
101 or syserr(g_('cannot write %s'), $incbin_file);
102 foreach my $binary (@unknown_binaries) {
103 print { $incbin_fh } "$binary\n";
104 info(g_('adding %s to %s'), $binary, 'debian/source/include-binaries');
105 $self->{allowed_binaries}{$binary} = 1;
106 }
107 close $incbin_fh;
108 }
109  
110 sub get_unknown_binaries {
111 my $self = shift;
112  
113 return grep { not $self->binary_is_allowed($_) } $self->get_seen_binaries();
114 }
115  
116 sub get_seen_binaries {
117 my $self = shift;
118 my @seen = sort keys %{$self->{seen_binaries}};
119  
120 return @seen;
121 }
122  
123 sub detect_binary_files {
124 my ($self, %opts) = @_;
125  
126 my $unwanted_binaries = 0;
127 my $check_binary = sub {
128 if (-f and is_binary($_)) {
129 my $fn = File::Spec->abs2rel($_, $self->{dir});
130 $self->new_binary_found($fn);
131 unless ($opts{include_binaries} or $self->binary_is_allowed($fn)) {
132 errormsg(g_('unwanted binary file: %s'), $fn);
133 $unwanted_binaries++;
134 }
135 }
136 };
137 my $exclude_glob = '{' .
138 join(',', map { s/,/\\,/rg } @{$opts{exclude_globs}}) .
139 '}';
140 my $filter_ignore = sub {
141 # Filter out files that are not going to be included in the debian
142 # tarball due to ignores.
143 my %exclude;
144 my $reldir = File::Spec->abs2rel($File::Find::dir, $self->{dir});
145 my $cwd = getcwd();
146 # Apply the pattern both from the top dir and from the inspected dir.
147 chdir $self->{dir}
148 or syserr(g_("cannot change directory to '%s'"), $self->{dir});
149 $exclude{$_} = 1 foreach glob $exclude_glob;
150 chdir $cwd or syserr(g_("cannot change directory to '%s'"), $cwd);
151 chdir $File::Find::dir
152 or syserr(g_("cannot change directory to '%s'"), $File::Find::dir);
153 $exclude{$_} = 1 foreach glob $exclude_glob;
154 chdir $cwd or syserr(g_("cannot change directory to '%s'"), $cwd);
155 my @result;
156 foreach my $fn (@_) {
157 unless (exists $exclude{$fn} or exists $exclude{"$reldir/$fn"}) {
158 push @result, $fn;
159 }
160 }
161 return @result;
162 };
163 my $scan_binaries = {
164 wanted => $check_binary,
165 preprocess => $filter_ignore,
166 no_chdir => 1,
167 };
168 find($scan_binaries, File::Spec->catdir($self->{dir}, 'debian'));
169 error(P_('detected %d unwanted binary file (add it in ' .
170 'debian/source/include-binaries to allow its inclusion).',
171 'detected %d unwanted binary files (add them in ' .
172 'debian/source/include-binaries to allow their inclusion).',
173 $unwanted_binaries), $unwanted_binaries)
174 if $unwanted_binaries;
175 }
176  
177 =head1 CHANGES
178  
179 =head2 Version 0.xx
180  
181 This is a private module.
182  
183 =cut
184  
185 1;