1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
package Boxer::World::Reclass;
=encoding UTF-8
=head1 NAME
Boxer::World::Reclass - software as serialized by reclass
=cut
use v5.20;
use utf8;
use Role::Commons -all;
use feature 'signatures';
use namespace::autoclean 0.16;
use autodie;
use YAML::XS;
use List::MoreUtils qw(uniq);
use Hash::Merge qw(merge);
use Try::Tiny;
use Moo;
use MooX::StrictConstructor;
extends qw(Boxer::World);
use Types::Standard qw( ArrayRef InstanceOf Maybe );
use Boxer::Types qw( ClassDir NodeDir Suite );
use Boxer::Part::Reclass;
use Boxer::World::Flat;
use strictures 2;
no warnings "experimental::signatures";
=head1 VERSION
Version v1.4.3
=cut
our $VERSION = "v1.4.3";
=head1 DESCRIPTION
Outside the box is a world of software.
B<Boxer::World::Reclass> is a class describing a collection of software
available for installation into (or as) an operating system.
=head1 SEE ALSO
L<Boxer>.
=cut
has suite => (
is => 'ro',
isa => Suite,
required => 1,
);
has classdir => (
is => 'lazy',
isa => ClassDir,
coerce => 1,
required => 1,
);
sub _build_classdir ($self)
{
if ( $self->data ) {
return $self->data->child('classes');
}
return;
}
has nodedir => (
is => 'lazy',
isa => NodeDir,
coerce => 1,
required => 1,
);
sub _build_nodedir ($self)
{
if ( $self->data ) {
return $self->data->child('nodes');
}
return;
}
has parts => (
is => 'lazy',
isa => ArrayRef [ InstanceOf ['Boxer::Part::Reclass'] ],
init_arg => undef,
);
# process only matching types, and skip duplicates is arrays
my $merge_spec = {
'SCALAR' => {
'SCALAR' => sub { $_[0] },
'ARRAY' => sub { die 'bad input data' },
'HASH' => sub { die 'bad input data' },
},
'ARRAY' => {
'SCALAR' => sub { die 'bad input data' },
'ARRAY' => sub { [ uniq @{ $_[0] }, @{ $_[1] } ] },
'HASH' => sub { die 'bad input data' },
},
'HASH' => {
'SCALAR' => sub { die 'bad input data' },
'ARRAY' => sub { die 'bad input data' },
'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
},
};
Hash::Merge::add_behavior_spec($merge_spec);
sub _build_parts ($self)
{
my $classdata = $self->classdir->visit(
sub ( $path, $state ) {
return if $path->is_dir;
return unless ( $path->basename =~ /\.yml$/ );
my $yaml = Load( $path->slurp_utf8 );
my $class = $path->relative( $self->classdir ) =~ tr/\//./r
=~ s/\.yml$//r =~ s/\.init$//r;
$state->{$class} = $yaml;
},
{ recurse => 1 },
);
my $nodedata = $self->nodedir->visit(
sub ( $path, $state ) {
return if $path->is_dir;
return unless ( $path->basename =~ /\.yml$/ );
my $yaml = Load( $path->slurp_utf8 );
my $node = $path->basename(qr/\.yml$/);
$state->{$node} = $yaml;
},
);
my @parts;
for ( sort keys %{$nodedata} ) {
my %params = ();
my @classes
= $nodedata->{$_}{classes}
? @{ $nodedata->{$_}{classes} }
: ();
while ( my $next = shift @classes ) {
unless ( $classdata->{$next} ) {
$self->_logger->debug(
"Ignoring missing class $next for node $_.");
next;
}
if ( $classdata->{$next}{classes} and !$params{_seen}{$next} ) {
$params{_seen}{$next} = 1;
unshift @classes, @{ $classdata->{$next}{classes} }, $next;
next;
}
%params = %{ merge( \%params, $classdata->{$next}{parameters} ) }
if $classdata->{$next}{parameters};
}
delete $params{_seen};
%params = %{ merge( \%params, $nodedata->{$_}{parameters} ) }
if $nodedata->{$_}{parameters};
push @parts,
Boxer::Part::Reclass->new(
id => $_,
epoch => $self->suite,
%params,
);
}
return [@parts];
}
sub list_parts ($self)
{
return map { $_->id } @{ $self->parts };
}
sub get_part ( $self, $id )
{
unless ( @{ $self->parts } ) {
$self->_logger->error("No parts exist.");
return;
}
foreach ( @{ $self->parts } ) {
if ( $_->id eq $id ) {
return $_;
}
}
$self->_logger->error("Part \"$id\" does not exist.");
return;
}
my $pos = 1;
my @section_order = qw(
Administration
Service
Console
Desktop
Language
Framework
Task
Hardware
);
my %section_order = map { $_ => $pos++ } @section_order;
sub map ( $self, $node_id, $nonfree )
{
my $node = $self->get_part($node_id);
my %desc;
my @section_keys = sort {
( $section_order{$a} // 1000 ) <=> ( $section_order{$b} // 1000 )
|| $a cmp $b
} keys %{ $node->{doc} };
foreach my $key (@section_keys) {
my $headline = $node->{doc}{$key}{headline}[0] || $key;
if (( $node->{pkg} and $node->{doc}{$key}{pkg} )
or ( $nonfree
and $node->{'pkg-nonfree'}
and $node->{doc}{$key}{'pkg-nonfree'} )
)
{
push @{ $desc{pkg} }, "# $headline";
if ( $node->{pkg} ) {
foreach ( @{ $node->{doc}{$key}{pkg} } ) {
push @{ $desc{pkg} }, "# * $_";
}
}
if ( $nonfree and $node->{'pkg-nonfree'} ) {
foreach ( @{ $node->{doc}{$key}{'pkg-nonfree'} } ) {
push @{ $desc{pkg} }, "# * [non-free] $_";
}
}
}
if ( $node->{tweak} and $node->{doc}{$key}{tweak} ) {
push @{ $desc{tweak} }, "# $headline";
foreach ( @{ $node->{doc}{$key}{tweak} } ) {
push @{ $desc{tweak} }, "# * $_";
}
}
}
my $pkgdesc
= defined( $desc{pkg} )
? join( "\n", @{ $desc{pkg} } )
: '';
my $tweakdesc
= defined( $desc{tweak} )
? join( "\n", @{ $desc{tweak} } )
: '';
my @pkg = try { @{ $node->{pkg} } }
catch {
$self->_logger->warning('No packages resolved');
return ();
};
my @pkgauto = try { @{ $node->{'pkg-auto'} } }
catch {
$self->_logger->warning('No package auto-markings resolved');
return ();
};
my @pkgavoid = try { @{ $node->{'pkg-avoid'} } }
catch {
$self->_logger->warning('No package avoidance resolved');
return ();
};
my @tweak = try { @{ $node->{tweak} } }
catch {
$self->_logger->warning('No tweaks resolved');
return ();
};
if ($nonfree) {
push @pkg, @{ $node->{'pkg-nonfree'} } if ( $node->{'pkg-nonfree'} );
push @pkgauto, @{ $node->{'pkg-nonfree-auto'} }
if ( $node->{'pkg-nonfree-auto'} );
}
chomp(@tweak);
return Boxer::World::Flat->new(
node => $node_id,
epoch => $node->epoch,
pkgs => \@pkg,
pkgs_auto => \@pkgauto,
pkgs_avoid => \@pkgavoid,
tweaks => \@tweak,
pkgdesc => $pkgdesc,
tweakdesc => $tweakdesc,
nonfree => $nonfree, # TODO: unset if none resolved
);
}
=head1 AUTHOR
Jonas Smedegaard C<< <dr@jones.dk> >>.
=cut
our $AUTHORITY = 'cpan:JONASS';
=head1 COPYRIGHT AND LICENCE
Copyright © 2013-2016 Jonas Smedegaard
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
=cut
1;
|