Magpie/Action/Sort.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Magpie/Action/DWIM.pm ↑ Summary Magpie/Action/Sort/Package.pm »
1 #
2 # This file is part of App-Magpie
3 #
4 # This software is copyright (c) 2011 by Jerome Quelin.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 1 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package App::Magpie::Action::Sort;
14 # ABSTRACT: sort command implementation
15 $App::Magpie::Action::Sort::VERSION = '2.010';
16 1 ▶use List::AllUtils qw{ part uniq };
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 0 ▶use App::Magpie::Action::Sort::Package;
Subroutine BEGIN: called CC=1 SCAR=2.6
21 use App::Magpie::URPM;
22  
23 with 'App::Magpie::Role::Logging';
24  
25  
26 # -- public methods
27  
28  
29 sub run {
30 my ($self, $opts) = @_;
31  
32 # default input/output files
33 my $in = $opts->{input} eq "-" ? *STDIN : path($opts->{input})->openr;
34 my $out = $opts->{output} eq "-" ? *STDOUT : path($opts->{output})->openw;
35  
36 # get list of packages to sort
37 my @unsorted = $in->getlines;
38 chomp( @unsorted );
39 $self->log( scalar(@unsorted) . " packages to sort" );
40 @unsorted = uniq @unsorted;
41 $self->log( scalar(@unsorted) . " unique packages to sort" );
42  
43 # fetch list of all provides for the packages to be sorted
44 my $urpm = App::Magpie::URPM->instance;
45 my %provides;
46 @provides{
47 map { $_->provides_nosense }
48 map { $urpm->packages($_) }
49 @unsorted
50 } = ();
51  
52 # create the package list
53 $self->log( "fetching packages requires & provides" );
54 my @packages;
55 my %seen;
56 foreach my $name ( @unsorted ) {
57 next if $seen{$name}++;
58 # create and store the package
59 my @pkgs = $urpm->packages($name);
60 my $pkg = App::Magpie::Action::Sort::Package->new(name=>$name);
61 push @packages, $pkg;
62  
63 # fetch package provides
64 my %pkg_provides;
65 @pkg_provides{ map { $_->provides_nosense } @pkgs } = ();
66 $pkg->add_provides( keys %pkg_provides );
67 # $self->log_debug( "$name provides: @{$pkg->provides}" );
68  
69 # fetch package requires
70 my @requires =
71 grep { ! exists $pkg_provides{$_} }
72 grep { exists $provides{$_} }
73 map { $_->requires_nosense }
74 @pkgs;
75 # $self->log_debug( "$name requires: @requires" );
76 $pkg->add_requires(@requires);
77 }
78  
79 # really sort the list by requires/provides this time
80 my $iteration = 1;
81 @packages = sort { $a->name cmp $b->name } @packages;
82 while ( scalar(@packages) ) {
83 # extract packages with no requires
84 my ($remaining, $this_round) = part { $_->has_no_requires } @packages;
85  
86 # check if we're in a dead-lock
87 if ( not defined $this_round ) {
88 $self->log( "remaining packages: " . join ",", map {$_->name} @$remaining );
89 $self->log_fatal(
90 "no package available this round - aborting " .
91 "(remaining: ". scalar(@$remaining) . ")"
92 );
93 }
94 $self->log( "iteration $iteration: " .scalar(@$this_round) . " packages" );
95 $out->print( "$_\n" ) for map { $_->name } @$this_round;
96  
97 $remaining //= [];
98 @packages = @$remaining;
99 $_->rm_requires( map { $_->provides } @$this_round ) for @$remaining;
100 $iteration++;
101 }
102 }
103  
104  
105  
106 1;
107  
108 __END__
« Magpie/Action/DWIM.pm ↑ Summary Magpie/Action/Sort/Package.pm »