Arepa/Repository.pm

filter: Statementstmt 12.3% 25 / 203 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 30.7% 8 / 26 Podpod 93.3% 14 / 15 totaltotal 13.9% 47 / 338 CC 48 SCAR 35.1
CC
48
Cov
9%
get_distributions
90.0
_execute_reprepro
72.0
get_source_package_information
42.0
CRAP 33.6
« Arepa/Web/Auth.pm ↑ Summary Arepa/Web/Incoming.pm »
1 package Arepa::Repository;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IO::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Parse::Debian::PackageDesc;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Arepa::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Arepa::PackageDb;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
14 0 my ($class, $config_path) = @_;
15  
16 0 my $config = Arepa::Config->new($config_path);
17 0 my $self = bless {
18 config_path => $config_path,
19 config => $config,
20 package_db => Arepa::PackageDb->new($config->get_key('package_db')),
21 }, $class;
22  
23 0 return $self;
24 }
25  
26 0 ▶sub config_key_exists {
Subroutine config_key_exists: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my ($self, $key) = @_;
28 0 return $self->{config}->key_exists($key);
29 }
30  
31 0 ▶sub get_config_key {
Subroutine get_config_key: not called CC=1 SCAR=6.9
Pod documented
32 0 my ($self, $key) = @_;
33 0 return $self->{config}->get_key($key);
34 }
35  
36 0 ▶sub get_distributions {
Subroutine get_distributions: not called CC=9 SCAR=45.0
Pod documented
37 0 my ($self) = @_;
38  
39 0 my $repository_config_file = $self->get_config_key('repository:path');
40 0 my $distributions_config_file = "$repository_config_file/conf/distributions";
41 0 ▶ open F, $distributions_config_file or croak "Can't open configuration file ";
Branch: 1 branch
BranchTrueFalseTotal
open F, $distributions_config_file or croak("Can't open configuration file ") 0 0 0
42 0 my ($line, $repo_attrs, @repos);
43 0 ▶ while ($line = <F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline F)) 0 0 0
44 0 ▶ if ($line =~ /^\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*$/) { } 0 0 0
elsif ($line =~ /^([^:]+):\s+(.+)/i) { } 0 0 0
45 0 ▶ push @repos, $repo_attrs if (ref($repo_attrs) && %$repo_attrs);
Branch: 1 branch
BranchTrueFalseTotal
if ref $repo_attrs and %$repo_attrs 0 0 0
Condition0%0/3
ref $repo_attrs and %$repo_attrs
!ll&&!rl&&r
000
MC/DC0%0/2
ref $repo_attrs and %$repo_attrs
ref $repo_attrs %$repo_attrs
Truth table0%0/3
A and B
A= ref $repo_attrsB= %$repo_attrs
ABresult
0X0
100
111
46 0 $repo_attrs = {};
47 }
48 elsif ($line =~ /^([^:]+):\s+(.+)/i) {
49 0 $repo_attrs->{lc($1)} = $2;
50 }
51 }
52 0 ▶ push @repos, $repo_attrs if (ref($repo_attrs) && %$repo_attrs);
Branch: 1 branch
BranchTrueFalseTotal
if ref $repo_attrs and %$repo_attrs 0 0 0
Condition0%0/3
ref $repo_attrs and %$repo_attrs
!ll&&!rl&&r
000
MC/DC0%0/2
ref $repo_attrs and %$repo_attrs
ref $repo_attrs %$repo_attrs
Truth table0%0/3
A and B
A= ref $repo_attrsB= %$repo_attrs
ABresult
0X0
100
111
53 0 close F;
54 0 return @repos;
55 }
56  
57 0 ▶sub get_architectures {
Subroutine get_architectures: not called CC=4 SCAR=30.0
Pod documented
58 0 my ($self) = @_;
59  
60 0 my @archs;
61 0 foreach my $repo ($self->get_distributions) {
62 0 foreach my $arch (split(/\s+/, $repo->{architectures})) {
63 0 ▶ push @archs, $arch unless grep { $arch eq $_ }
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$arch eq $_;} @archs 0 0 0
64 @archs;
65 }
66 }
67 0 return @archs;
68 }
69  
70 0 ▶sub insert_source_package {
Subroutine insert_source_package: not called CC=5 SCAR=31.2
Pod documented
71 0 my ($self, $dsc_file, $distro, %user_opts) = @_;
72  
73 2 ▶ use Parse::Debian::PackageDesc;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 0 my $parsed_dsc = Parse::Debian::PackageDesc->new($dsc_file);
75 0 my @archs = $parsed_dsc->architecture;
76 0 ▶ my $arch = (scalar @archs > 1) ? 'any' : $archs[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @archs > 1 ? : 0 0 0
77 0 my %args = (name => $parsed_dsc->name,
78 full_version => $parsed_dsc->version,
79 architecture => $arch,
80 distribution => $distro);
81  
82 0 ▶ if (exists $user_opts{comments}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $user_opts{"comments"}) 0 0 0
83 0 $args{comments} = $user_opts{comments};
84 0 delete $user_opts{comments};
85 }
86  
87 0 my $canonical_distro = $distro;
88 0 ▶ if ($user_opts{canonical_distro}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_opts{"canonical_distro"}) 0 0 0
89 0 $canonical_distro = $user_opts{canonical_distro};
90 0 delete $user_opts{canonical_distro};
91 }
92  
93 0 my $r = $self->_execute_reprepro('includedsc',
94 $canonical_distro,
95 $dsc_file,
96 %user_opts);
97 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 0 0
98 0 return $self->{package_db}->insert_source_package(%args);
99 }
100 else {
101 0 return 0;
102 }
103 }
104  
105 0 ▶sub insert_binary_package {
Subroutine insert_binary_package: not called CC=1 SCAR=6.9
Pod documented
106 0 my ($self, $deb_file, $distro) = @_;
107  
108 0 return $self->_execute_reprepro('includedeb',
109 $distro,
110 $deb_file);
111 }
112  
113 0 ▶sub _shell_escape {
Subroutine _shell_escape: not called CC=2 SCAR=17.9
114 0 my ($self, $arg) = @_;
115 0 ▶ if (defined $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 0 0 0
116 0 $arg =~ s/'/\\'/go;
117 0 return "'$arg'";
118 }
119  
120 0 return "";
121 }
122  
123 0 ▶sub last_cmd_output {
Subroutine last_cmd_output: not called CC=1 SCAR=6.9
Pod documented
124 0 my ($self) = @_;
125 0 $self->{last_cmd_output};
126 }
127  
128 0 ▶sub _execute_reprepro {
Subroutine _execute_reprepro: not called CC=8 SCAR=42.8
129 0 my ($self, $mode, $distro, $file_path, %extra_args) = @_;
130  
131 0 my $repo_path = $self->get_config_key("repository:path");
132 0 $mode = $self->_shell_escape($mode);
133 0 $distro = $self->_shell_escape($distro);
134 0 $file_path = $self->_shell_escape($file_path);
135 # Extra arguments
136 0 my $extra = "";
137 0 foreach my $arg (keys %extra_args) {
138 0 ▶ if ($arg eq 'section') {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg eq 'section') { } 0 0 0
elsif ($arg eq 'priority') { } 0 0 0
139 0 $extra .= " --section " . $self->_shell_escape($extra_args{$arg});
140 }
141 elsif ($arg eq 'priority') {
142 0 $extra .= " --priority " . $self->_shell_escape($extra_args{$arg})
143 }
144 else {
145 0 croak "Don't know anything about argument '$arg'";
146 }
147 }
148 # GNUPG home directory
149 0 ▶ if ($self->config_key_exists('web_ui:gpg_homedir')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config_key_exists("web_ui:gpg_homedir")) 0 0 0
150 0 my $gpg_homedir = $self->get_config_key('web_ui:gpg_homedir');
151 0 ▶ if (defined $gpg_homedir && $gpg_homedir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gpg_homedir and $gpg_homedir) 0 0 0
Condition0%0/3
defined $gpg_homedir and $gpg_homedir
!ll&&!rl&&r
000
MC/DC0%0/2
defined $gpg_homedir and $gpg_homedir
defined $gpg_homedir $gpg_homedir
Truth table0%0/3
A and B
A= defined $gpg_homedirB= $gpg_homedir
ABresult
0X0
100
111
152 0 $extra .= " --gnupghome '$gpg_homedir'";
153 }
154 }
155  
156 0 my $cmd = "reprepro -b$repo_path $extra $mode $distro $file_path 2>&1";
157 0 my $umask = umask;
158 0 umask($umask & 0707); # Always allow group permissions
159 0 $self->{last_cmd_output} = `$cmd`;
160 0 my $status = $?;
161 0 umask $umask;
162 0 ▶ if ($status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 0) { } 0 0 0
163 0 return 1;
164 }
165 else {
166 0 print STDERR "Reprepro command failed: '$cmd'\n";
167 0 return 0;
168 }
169 }
170  
171 0 ▶sub get_package_list {
Subroutine get_package_list: not called CC=4 SCAR=30.0
Pod documented
172 0 my ($self) = @_;
173  
174 0 my %pkg_list;
175 0 my $repo_path = $self->get_config_key("repository:path");
176 0 foreach my $codename (map { $_->{codename} } $self->get_distributions) {
177 0 my $cmd = "reprepro -b$repo_path list $codename";
178 0 open PIPE, "$cmd |";
179 0 ▶ while (<PIPE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline PIPE)) 0 0 0
180 0 my ($distro, $comp, $arch, $pkg_name, $pkg_version) =
181 /(.+)\|(.+)\|(.+): ([^ ]+) (.+)/;
182 0 ▶ $pkg_list{$pkg_name}->{"$distro/$comp"}->{$pkg_version} ||= [];
Condition0%0/2
$pkg_list{$pkg_name}{"$distro/$comp"}{$pkg_version} ||= []
l!l
00
MC/DC0%0/1
$pkg_list{$pkg_name}{"$distro/$comp"}{$pkg_version} ||= []
$pkg_list{$pkg_name}{"$distro/$comp"}{$pkg_version}
Truth table0%0/2
A
A= $pkg_list{$pkg_name}{"$distro/$comp"}{$pkg_version}
Aresult
11
00
183 0 push @{$pkg_list{$pkg_name}->{"$distro/$comp"}->{$pkg_version}},
184 $arch;
185 }
186 0 close PIPE;
187 }
188 0 return %pkg_list;
189 }
190  
191 0 ▶sub get_source_package_information {
Subroutine get_source_package_information: not called CC=6 SCAR=37.4
Pod documented
192 0 my ($self, $package_name, $distro) = @_;
193  
194 0 my $repo_path = $self->get_config_key('repository:path');
195 0 my $sources_file_path = File::Spec->catfile($repo_path,
196 'dists',
197 $distro,
198 'main',
199 'source',
200 'Sources.gz');
201  
202 0 my $fh = new IO::Zlib;
203 0 my $current_pkg = "";
204 0 my %props;
205 0 ▶ if ($fh->open($sources_file_path, "rb")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->open($sources_file_path, "rb")) 0 0 0
206 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
207 0 ▶ if (/^Package: (.+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^Package: (.+)/) { } 0 0 0
elsif ($current_pkg eq $package_name) { } 0 0 0
208 0 $current_pkg = $1;
209 }
210 elsif ($current_pkg eq $package_name) {
211 0 ▶ if (/^([^:]+): (.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([^:]+): (.+)/) 0 0 0
212 0 $props{lc($1)} = $2;
213 }
214 }
215 }
216 0 $fh->close;
217 }
218  
219 0 return %props;
220 }
221  
222 0 ▶sub get_binary_package_information {
Subroutine get_binary_package_information: not called CC=5 SCAR=34.0
Pod documented
223 0 my ($self, $package_name, $distro, $arch) = @_;
224  
225 0 my $repo_path = $self->get_config_key('repository:path');
226 0 my $packages_file_path = File::Spec->catfile($repo_path,
227 'dists',
228 $distro,
229 'main',
230 'binary-' . $arch,
231 'Packages');
232  
233 0 my $current_pkg = "";
234 0 my %props;
235 0 open F, $packages_file_path;
236 0 ▶ while (<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 0 0 0
237 0 ▶ if (/^Package: (.+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^Package: (.+)/) { } 0 0 0
elsif ($current_pkg eq $package_name) { } 0 0 0
238 0 $current_pkg = $1;
239 }
240 elsif ($current_pkg eq $package_name) {
241 0 ▶ if (/^([^:]+): (.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([^:]+): (.+)/) 0 0 0
242 0 $props{lc($1)} = $2;
243 }
244 }
245 }
246 0 close F;
247  
248 0 return %props;
249 }
250  
251 0 ▶sub _all_names_for_distro {
Subroutine _all_names_for_distro: not called CC=2 SCAR=17.9
252 0 my ($self, %properties) = @_;
253  
254 0 my @aliases = ($properties{codename});
255 0 ▶ if (defined $properties{suite}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $properties{"suite"}) 0 0 0
256 0 push @aliases, $properties{suite};
257 }
258 0 return @aliases;
259 }
260  
261 0 ▶sub add_distribution {
Subroutine add_distribution: not called CC=5 SCAR=34.0
Pod documented
262 0 my ($self, %properties) = @_;
263  
264 0 my $repository_path = $self->get_config_key('repository:path');
265 0 my $distributions_config_file = "$repository_path/conf/distributions";
266  
267  
268 0 ▶ if (! defined $properties{codename}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $properties{"codename"}) 0 0 0
269 0 return 0;
270 }
271 # Duplicate names of any kind
272 0 my @new_distro_names = $self->_all_names_for_distro(%properties);
273 0 my @existing_distro_names = map { $self->_all_names_for_distro(%$_) }
274 $self->get_distributions;
275 0 foreach my $distro_name (@new_distro_names) {
276 0 ▶ if (grep { $_ eq $distro_name } @existing_distro_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $distro_name;} @existing_distro_names) 0 0 0
277 0 return 0;
278 }
279 }
280  
281 # Everything seems alright, serialise the distribution properties
282 my $serialised_distro = join("\n",
283 0 map { ucfirst($_) . ": $properties{$_}" }
284 keys %properties);
285  
286 0 ▶ open F, ">>$distributions_config_file" or do {
Branch: 1 branch
BranchTrueFalseTotal
open F, ">>$distributions_config_file" or do { print STDERR "Can't open $distributions_config_file for writing\n"; return 0 } 0 0 0
287 0 print STDERR "Can't open $distributions_config_file for writing\n";
288 0 return 0;
289 };
290 0 print F <<EOD;
291  
292 $serialised_distro
293 EOD
294 0 close F;
295  
296 # Now, update the repository with the new distro
297 0 $self->_execute_reprepro('export', $properties{codename});
298 }
299  
300 0 ▶sub sign_distribution {
Subroutine sign_distribution: not called CC=2 SCAR=17.9
Pod documented
301 0 my ($self, $distro_name) = @_;
302  
303 0 my $repo_path = $self->get_config_key('repository:path');
304 0 my $release_file_path = File::Spec->catfile($repo_path,
305 "dists",
306 $distro_name,
307 "Release");
308 0 unlink "$release_file_path.gpg";
309  
310 0 my $extra_options = "";
311 0 ▶ if ($self->config_key_exists('repository:signature:id')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config_key_exists("repository:signature:id")) 0 0 0
312 0 my $key_id = $self->get_config_key('repository:signature:id');
313 0 $extra_options = " -u $key_id";
314 }
315 0 my $gpg_cmd = "gpg --batch -abs $extra_options -o $release_file_path.gpg $release_file_path >/dev/null";
316  
317 0 return (system($gpg_cmd) == 0);
318 }
319  
320 0 ▶sub sync_remote {
Subroutine sync_remote: not called CC=3 SCAR=24.8
Pod documented
321 0 my ($self) = @_;
322  
323 0 my $repo_path = $self->get_config_key('repository:path');
324 0 ▶ if ($self->config_key_exists('repository:remote_path')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config_key_exists("repository:remote_path")) 0 0 0
325 0 my $remote_repo_path = $self->get_config_key('repository:remote_path');
326 0 my $rsync_cmd = "rsync -avz --delete $repo_path $remote_repo_path";
327 0 ▶ if (system($rsync_cmd) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (system($rsync_cmd) == 0) { } 0 0 0
328 0 return 1;
329 }
330 else {
331 0 print STDERR "Command was '$rsync_cmd'\n";
332 0 return 0;
333 }
334 }
335 0 return 0;
336 }
337  
338 0 ▶sub is_synced {
Subroutine is_synced: not called CC=5 SCAR=34.0
Pod documented
339 0 my ($self) = @_;
340  
341 0 my $repo_path = $self->get_config_key('repository:path');
342 0 ▶ if ($self->config_key_exists('repository:remote_path')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config_key_exists("repository:remote_path")) 0 0 0
343 0 my $remote_repo_path = $self->get_config_key('repository:remote_path');
344 0 my $rsync_cmd = "rsync -avz --delete --dry-run --out-format='AREPA_CHANGE %i' $repo_path $remote_repo_path";
345 0 my $changes = 0;
346  
347 0 open RSYNCOUTPUT, "$rsync_cmd |";
348 0 ▶ while (<RSYNCOUTPUT>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline RSYNCOUTPUT)) 0 0 0
349 0 ▶ next unless /^AREPA_CHANGE/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^AREPA_CHANGE/ 0 0 0
350 0 ▶ if (/^AREPA_CHANGE [^.]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^AREPA_CHANGE [^.]/) 0 0 0
351 0 $changes = 1;
352 }
353 }
354 0 close RSYNCOUTPUT;
355  
356 0 return (! $changes);
357 }
358 0 return 0;
359 }
360  
361 2 1;
362  
363 __END__
« Arepa/Web/Auth.pm ↑ Summary Arepa/Web/Incoming.pm »