| 1 | # Copyright © 2014-2015 Guillem Jover <guillem@debian.org> | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||||||||||
| 6 | # (at your option) any later version. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||
| 14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =encoding utf8 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Dpkg::Dist::Files - handle built artifacts to distribute | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | This module provides a class used to parse and write the F<debian/files> | ||||||||||||||||||||||||||||
| 25 | file, as part of the list of built artifacts to include in an upload. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | B<Note>: This is a private module, its API can change at any time. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | package Dpkg::Dist::Files 0.01; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use IO::Dir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | use Dpkg::Gettext; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use Dpkg::ErrorHandling; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | use parent qw(Dpkg::Interface::Storable); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 1 | my ($this, %opts) = @_; | |||||||||||||||||||||||||||
| 44 | 1 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | my $self = { | |||||||||||||||||||||||||||
| 47 | options => [], | ||||||||||||||||||||||||||||
| 48 | files => {}, | ||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | 1 | foreach my $opt (keys %opts) { | |||||||||||||||||||||||||||
| 51 | 0 | $self->{$opt} = $opts{$opt}; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | return $self; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 5 | ▶ | sub reset { | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 5 | $self->{files} = {}; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 33 | ▶ | sub parse_filename { | ||||||||||||||||||||||||||
|
Subroutine
parse_filename: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | 33 | my ($self, $fn) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 33 | my $file; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 33 | ▶ | if ($fn =~ m/^(([-+:.0-9a-z]+)_([^_]+)_([-\w]+)\.([a-z0-9.]+))$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 70 | # Artifact using the common <name>_<version>_<arch>.<type> pattern. | ||||||||||||||||||||||||||||
| 71 | 18 | $file->{filename} = $1; | |||||||||||||||||||||||||||
| 72 | 18 | $file->{package} = $2; | |||||||||||||||||||||||||||
| 73 | 18 | $file->{version} = $3; | |||||||||||||||||||||||||||
| 74 | 18 | $file->{arch} = $4; | |||||||||||||||||||||||||||
| 75 | 18 | $file->{package_type} = $5; | |||||||||||||||||||||||||||
| 76 | } elsif ($fn =~ m/^([-+:.,_0-9a-zA-Z~]+)$/) { | ||||||||||||||||||||||||||||
| 77 | # Artifact with no common pattern, usually called byhand or raw, as | ||||||||||||||||||||||||||||
| 78 | # they might require manual processing on the server side, or custom | ||||||||||||||||||||||||||||
| 79 | # actions per file type. | ||||||||||||||||||||||||||||
| 80 | 14 | $file->{filename} = $1; | |||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 1 | $file = undef; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 33 | return $file; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 7 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 7 | my ($self, $fh, $desc) = @_; | |||||||||||||||||||||||||||
| 90 | 7 | my $count = 0; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 7 | local $_; | |||||||||||||||||||||||||||
| 93 | 7 | binmode $fh; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 7 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 27 | chomp; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 27 | my $file; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 27 | ▶ | if (m/^(\S+) (\S+) (\S+)((?:\s+[0-9a-z-]+=\S+)*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 27 | $file = $self->parse_filename($1); | |||||||||||||||||||||||||||
| 102 | 27 | ▶ | error(g_('badly formed file name in files list file, line %d'), $.) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | unless defined $file; | ||||||||||||||||||||||||||||
| 104 | 27 | $file->{section} = $2; | |||||||||||||||||||||||||||
| 105 | 27 | $file->{priority} = $3; | |||||||||||||||||||||||||||
| 106 | 27 | my $attrs = $4; | |||||||||||||||||||||||||||
| 107 | 27 | $file->{attrs} = { map { split /=/ } split ' ', $attrs }; | |||||||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||||||||
| 109 | 0 | error(g_('badly formed line in files list file, line %d'), $.); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 27 | ▶ | if (defined $self->{files}->{$file->{filename}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | warning(g_('duplicate files list entry for file %s (line %d)'), | ||||||||||||||||||||||||||||
| 114 | 0 | $file->{filename}, $.); | |||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||
| 116 | 27 | $count++; | |||||||||||||||||||||||||||
| 117 | 27 | $self->{files}->{$file->{filename}} = $file; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 7 | return $count; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | sub load_dir { | ||||||||||||||||||||||||||
|
Subroutine
load_dir: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | 1 | my ($self, $dir) = @_; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | my $count = 0; | |||||||||||||||||||||||||||
| 128 | 1 | ▶ | my $dh = IO::Dir->new($dir) or syserr(g_('cannot open directory %s'), $dir); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | ▶ | while (defined(my $file = $dh->read)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 5 | my $pathname = "$dir/$file"; | |||||||||||||||||||||||||||
| 132 | 5 | ▶ | next unless -f $pathname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 3 | $count += $self->load($pathname); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | return $count; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | ▶ | sub get_files { | ||||||||||||||||||||||||||
|
Subroutine
get_files: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | return map { $self->{files}->{$_} } sort keys %{$self->{files}}; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 12 | ▶ | sub get_file { | ||||||||||||||||||||||||||
|
Subroutine
get_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | 12 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 12 | return $self->{files}->{$filename}; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 5 | ▶ | sub add_file { | ||||||||||||||||||||||||||
|
Subroutine
add_file: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 152 | 5 | my ($self, $filename, $section, $priority, %attrs) = @_; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 5 | my $file = $self->parse_filename($filename); | |||||||||||||||||||||||||||
| 155 | 5 | ▶ | error(g_('invalid filename %s'), $filename) unless defined $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 5 | $file->{section} = $section; | |||||||||||||||||||||||||||
| 157 | 5 | $file->{priority} = $priority; | |||||||||||||||||||||||||||
| 158 | 5 | $file->{attrs} = \%attrs; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 5 | $self->{files}->{$filename} = $file; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 5 | return $file; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 1 | ▶ | sub del_file { | ||||||||||||||||||||||||||
|
Subroutine
del_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 166 | 1 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 1 | delete $self->{files}->{$filename}; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 3 | ▶ | sub filter { | ||||||||||||||||||||||||||
|
Subroutine
filter: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 172 | 3 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||
| 173 | 3 | ▶ | my $remove = $opts{remove} // sub { 0 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$opts{"remove"} // sub {
0;
}
MC/DC0%0/2
$opts{"remove"} // sub {
0;
}
$opts{"remove"}
sub {
0;
}
Truth table66%2/3
A // B
A= $opts{"remove"}B= sub {
0;
}
| |||||||||||||||||||||||||||||
| 174 | 3 | ▶ | my $keep = $opts{keep} // sub { 1 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$opts{"keep"} // sub {
1;
}
MC/DC0%0/2
$opts{"keep"} // sub {
1;
}
$opts{"keep"}
sub {
1;
}
Truth table66%2/3
A // B
A= $opts{"keep"}B= sub {
1;
}
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 3 | foreach my $filename (keys %{$self->{files}}) { | |||||||||||||||||||||||||||
| 177 | 15 | my $file = $self->{files}->{$filename}; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 15 | ▶ | if (not $keep->($file) or $remove->($file)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not &$keep($file) or &$remove($file)
MC/DC100%2/2
not &$keep($file) or &$remove($file)
not &$keep($file)
&$remove($file)
Truth table100%3/3
not(A) or B
A= not &$keep($file)B= &$remove($file)
| |||||||||||||||||||||||||||||
| 180 | 9 | delete $self->{files}->{$filename}; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 7 | ▶ | sub output { | ||||||||||||||||||||||||||
|
Subroutine
output: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 7 | my ($self, $fh) = @_; | |||||||||||||||||||||||||||
| 187 | 7 | my $str = ''; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 7 | ▶ | binmode $fh if defined $fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 7 | foreach my $filename (sort keys %{$self->{files}}) { | |||||||||||||||||||||||||||
| 192 | 26 | my $file = $self->{files}->{$filename}; | |||||||||||||||||||||||||||
| 193 | 26 | my $entry = "$filename $file->{section} $file->{priority}"; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 26 | ▶ | if (exists $file->{attrs}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 26 | foreach my $attr (sort keys %{$file->{attrs}}) { | |||||||||||||||||||||||||||
| 197 | 9 | $entry .= " $attr=$file->{attrs}->{$attr}"; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 26 | $entry .= "\n"; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 0 | ▶ | print { $fh } $entry if defined $fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 26 | $str .= $entry; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 7 | return $str; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =head1 CHANGES | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head2 Version 0.xx | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | This is a private module. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =cut | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 1 | 1; | |||||||||||||||||||||||||||