/usr/share/perl5/Argonaut/Libraries/Packages.pm is in argonaut-common 1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | #######################################################################
#
# Argonaut::Libraries::Packages -- get and parse Debian Packages.
#
# Copyright (C) 2011-2016 FusionDirectory project
#
# Author: Côme BERNIGAUD
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
#######################################################################
package Argonaut::Libraries::Packages;
use strict;
use warnings;
use 5.008;
use MIME::Base64;
use Path::Class;
use Net::LDAP;
use File::Path;
use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error);
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
use LWP::Simple;
use Encode qw(encode);
use XML::SAX::RPMHandler;
use XML::SAX::RPMRepomdHandler;
use XML::SAX;
use Argonaut::Libraries::Common qw(:ldap :config);
BEGIN
{
use Exporter ();
use vars qw(@EXPORT_OK @ISA $VERSION);
$VERSION = '2011-04-11';
@ISA = qw(Exporter);
@EXPORT_OK = qw(get_repolines get_packages_info store_packages_file cleanup_and_extract);
}
=pod
=item get_repolines
Get repolines from ldap
=cut
sub get_repolines {
my ($mac,$cn) = @_;
my $config = argonaut_read_config;
my ($ldap,$ldap_base) = argonaut_ldap_handle($config);
my $mesg;
if(defined $mac) {
$mesg = $ldap->search(
base => $ldap_base,
filter => "(&(objectClass=FAIrepositoryServer)(macAddress=$mac))",
attrs => [ 'FAIrepository' ] );
} elsif(defined $cn) {
$mesg = $ldap->search(
base => $ldap_base,
filter => "(&(objectClass=FAIrepositoryServer)(cn=$cn))",
attrs => [ 'FAIrepository' ] );
} else {
$mesg = $ldap->search(
base => $ldap_base,
filter => "(&(objectClass=FAIrepositoryServer))",
attrs => [ 'FAIrepository' ] );
}
$mesg->code && die "Error while searching repositories :".$mesg->error;
$ldap->unbind();
my @repolines = ();
foreach my $entry ($mesg->entries()) {
foreach my $repoline ($entry->get_value('FAIrepository')) {
my ($uri,$parent,$release,$sections,$install,$local,$archs,$dist,$pathmask) = split('\|',$repoline);
my $sections_array = [split(',',$sections)];
if ($install eq 'update') {
foreach my $section (@$sections_array) {
if ($section !~ m/^updates/) {
$section = "updates/$section";
}
}
}
my $repo = {
'line' => $repoline,
'uri' => $uri,
'parent' => $parent,
'release' => $release,
'sections' => $sections_array,
'installrepo' => $install,
'localmirror' => ($local eq "local"),
'archs' => [split(',',$archs)],
'dist' => $dist,
'pathmask' => $pathmask
};
push @repolines, $repo;
}
}
return @repolines;
}
=item get_packages_info
Get packages list with all requested attrs.
Uses the Packages file from the server for that.
If no mac is provided, all servers (for the specified release) in the ldap are checked.
=cut
sub get_packages_info {
my ($packages_folder,$mac,$release,$attrs,$filters,$from,$to) = @_;
if((defined $from) && ($from < 0)) {
undef $from;
}
if((defined $to) && ($to < 0)) {
undef $to;
}
my @filters_temp = grep { $_ ne '' } @{$filters};
if(@filters_temp) {
$filters = \@filters_temp;
} else {
undef $filters;
}
push @{$attrs},'PACKAGE' if (not (grep {uc($_) eq 'PACKAGE'} @{$attrs}));
#~ push @{$attrs},'VERSION' if (not (grep {uc($_) eq 'VERSION'} @{$attrs}));
my @repolines = get_repolines($mac);
my $package_indice = 0;
my $distributions = {};
mkpath($packages_folder);
foreach my $repo (@repolines) {
if(defined($release) && ($repo->{'release'} ne $release)) {
next;
}
if ($repo->{'dist'} eq 'debian') {
parse_package_list_debian($packages_folder,\$package_indice,$distributions,$repo,$attrs,$filters,$from,$to);
} elsif ($repo->{'dist'} eq 'centos') {
parse_package_list_centos($packages_folder,\$package_indice,$distributions,$repo,$attrs,$filters,$from,$to);
}
}
foreach my $key (keys(%{$distributions})) {
my @tmp = values(%{$distributions->{$key}});
$distributions->{$key} = \@tmp;
}
return $distributions;
}
sub parse_package_list_centos {
my ($packages_folder,$package_indice,$distributions,$repo,$attrs,$filters,$from,$to) = @_;
my $uri = $repo->{'uri'};
my $localuri = $uri;
$localuri =~ s/^http:\/\///;
my $handler = XML::SAX::RPMHandler->new(
undef,
{
'name' => 'PACKAGE',
'description' => sub {
my ($package, undef, $data, $attrs) = @_;
$package->{'DESCRIPTION'} = encode_base64(encode('utf8',$data));
},
'version' => sub {
my ($package, undef, $data, $attrs) = @_;
$package->{'VERSION'} = $attrs->{'{}ver'}->{'Value'}.'-'.$attrs->{'{}rel'}->{'Value'};
}
},
$filters,
$from,
$to,
$$package_indice
);
my $parser = XML::SAX::ParserFactory->parser(
Handler => $handler
);
foreach my $section (@{$repo->{'sections'}}) {
if(!defined $distributions->{$repo->{'release'}."/$section"}) {
$distributions->{$repo->{'release'}."/$section"} = {};
}
$handler->{packages} = $distributions->{$repo->{'release'}."/$section"};
foreach my $arch (@{$repo->{'archs'}}) {
my $relpath = $repo->{'pathmask'};
$relpath =~ s/%RELEASE%/$repo->{'release'}/i;
$relpath =~ s/%SECTION%/$section/i;
$relpath =~ s/%ARCH%/$arch/i;
my $primary_file = "$packages_folder/$localuri/".$relpath."/primary.xml";
eval {
$parser->parse_uri($primary_file);
};
if ($@ && ($@ !~ m/^LIMIT_REACHED/)) {
die $@;
}
}
}
$$package_indice = $handler->{indice};
}
sub parse_package_list_debian {
my ($packages_folder,$package_indice,$distributions,$repo,$attrs,$filters,$from,$to) = @_;
my $uri = $repo->{'uri'};
my $localuri = $uri;
$localuri =~ s/^http:\/\///;
my $localmirror = $repo->{'localmirror'};
if(!$localmirror && ((grep {uc($_) eq 'TEMPLATE'} @{$attrs}) || (grep {uc($_) eq 'HASTEMPLATE'} @{$attrs}))) {
push @{$attrs},'FILENAME' if (not (grep {uc($_) eq 'FILENAME'} @{$attrs}));
}
foreach my $section (@{$repo->{'sections'}}) {
if(!defined $distributions->{$repo->{'release'}."/$section"}) {
$distributions->{$repo->{'release'}."/$section"} = {};
}
my $packages = $distributions->{$repo->{'release'}."/$section"};
foreach my $arch (@{$repo->{'archs'}}) {
my $packages_filepath = "$packages_folder/$localuri/dists/".$repo->{'release'}."/$section/binary-$arch/Packages";
my $packages_file;
open ($packages_file, q{<}, $packages_filepath) or next;
my $parsed = {};
while (<$packages_file>) {
if (/^$/) {
# Empty line means this package info lines are over
$$package_indice++;
if((! defined $from) || ($$package_indice>$from)) {
if($localmirror) {
# If it's a local mirror, it's supposed to run the debconf crawler and have the template extracted
# So we just download it (if it's not there, we assume there is no template for this package)
if (grep {uc($_) eq 'TEMPLATE'} @{$attrs}) {
my $template = get("$uri/debconf.d/".$repo->{'release'}."/$section/".$parsed->{'PACKAGE'});
if(defined $template) {
$parsed->{'HASTEMPLATE'} = 1;
$parsed->{'TEMPLATE'} = $template;
}
} elsif (grep {uc($_) eq 'HASTEMPLATE'} @{$attrs}) {
if(head("$uri/debconf.d/".$repo->{'release'}."/$section/".$parsed->{'PACKAGE'})) {
$parsed->{'HASTEMPLATE'} = 1;
}
}
} else {
# If it's not a local mirror, we just download the package, we'll extract the template later
if ((grep {uc($_) eq 'TEMPLATE'} @{$attrs}) || (grep {uc($_) eq 'HASTEMPLATE'} @{$attrs})) {
my $filedir = $parsed->{'FILENAME'};
$filedir =~ s/[^\/]+$//;
mkpath($packages_folder."/".$filedir);
mirror("$uri/".$parsed->{'FILENAME'},$packages_folder."/".$parsed->{'FILENAME'});
}
}
$packages->{$parsed->{'PACKAGE'}} = $parsed;
}
$parsed = {};
if((! defined $to) || ($$package_indice<$to)) {
next;
} else {
last;
}
}
if (my ($key, $value) = m/^(.*): (.*)/) {
if((defined $filters) && (uc($key) eq "PACKAGE")) {
my $match = 0;
foreach my $filter (@{$filters}) {
if($value =~ /$filter/) {
$match = 1;
last;
}
}
if($match == 0) {
while(<$packages_file>) {
if (/^$/) {
last;
}
}
$parsed = {};
next;
}
}
if (grep {uc($_) eq uc($key)} @{$attrs}) {
if (uc($key) eq 'DESCRIPTION') {
$parsed->{'DESCRIPTION'} = encode_base64($value);
} elsif ((uc($key) eq 'PACKAGE') && (defined $packages->{$value}) && !(grep {uc($_) eq 'VERSION'} @{$attrs})) {
# We already have the info on this package and version was not asked, skip to next one.
while(<$packages_file>) {
if (/^$/) {
last;
}
}
$parsed = {};
next;
} elsif ((uc($key) eq 'VERSION') && (defined $packages->{$parsed->{'PACKAGE'}}->{'VERSION'})) {
# We already have the info on this package and this is the version, add it to the list and then skip to next one
my @versions = split(',',$packages->{$parsed->{'PACKAGE'}}->{'VERSION'});
if (!(grep {uc($_) eq uc($value)} @versions)) {
push @versions, $value;
}
$packages->{$parsed->{'PACKAGE'}}->{'VERSION'} = join(',',@versions);
while(<$packages_file>) {
if (/^$/) {
last;
}
}
$parsed = {};
next;
} else {
$parsed->{uc($key)} = $value;
}
}
}
else {
s/ //;
s/^\.$//;
my $body = $_;
if(grep {uc($_) eq uc('BODY')} @{$attrs}) {
$parsed->{'BODY'} .= $body;
}
}
}
close($packages_file);
}
if((defined $to) && ($$package_indice>$to)) {
last;
}
if(!$localmirror && ((grep {uc($_) eq 'TEMPLATE'} @{$attrs}) || (grep {uc($_) eq 'HASTEMPLATE'} @{$attrs}))) {
# If it's not a local mirror and templates where asked, we still need to extract and store them
my $distribs = {};
my @tmp = values(%{$packages});
$distribs->{$repo->{'release'}."/$section"} = \@tmp;
cleanup_and_extract($packages_folder,$distribs);
foreach my $key (keys(%{$packages})) {
if(defined $packages->{$key}->{'TEMPLATE'}) {
next;
}
my $filename = $packages_folder."/debconf.d/".$repo->{'release'}."/$section/".$packages->{$key}->{'PACKAGE'};
if(-f $filename) {
$packages->{$key}->{'HASTEMPLATE'} = 1;
if(grep {uc($_) eq 'TEMPLATE'} @{$attrs}) {
$packages->{$key}->{'TEMPLATE'} = file($filename)->slurp();
}
}
}
}
}
}
=item store_packages_file
Store and extract the Packages file from the repositories.
=cut
sub store_packages_file {
my ($packages_folder,$mac,$release) = @_;
my @repolines = get_repolines($mac);
my @errors;
foreach my $repo (@repolines) {
if(defined($release) && ($repo->{'release'} ne $release)) {
next;
}
my $repo_errors;
if ($repo->{'dist'} eq 'debian') {
$repo_errors = store_package_list_debian($packages_folder,$repo);
} elsif ($repo->{'dist'} eq 'centos') {
$repo_errors = store_package_list_centos($packages_folder,$repo);
}
@errors = (@errors, @$repo_errors);
}
return \@errors;
}
sub store_package_list_centos {
my ($packages_folder,$repo) = @_;
my $uri = $repo->{'uri'};
my $dir = $uri;
my @errors;
$dir =~ s/^http:\/\///;
$dir = "$packages_folder/$dir";
my $parser = XML::SAX::ParserFactory->parser(
Handler => XML::SAX::RPMRepomdHandler->new()
);
foreach my $section (@{$repo->{'sections'}}) {
foreach my $arch (@{$repo->{'archs'}}) {
my $relpath = $repo->{'pathmask'};
$relpath =~ s/%RELEASE%/$repo->{'release'}/i;
$relpath =~ s/%ARCH%/$arch/i;
$relpath =~ s/%SECTION%/$section/i;
mkpath($dir.$relpath."/repodata");
my $res = mirror($uri.$relpath."repodata/repomd.xml" => $dir.$relpath."repodata/repomd.xml");
if(is_error($res)) {
push @errors,"Could not download $uri".$relpath."repodata/repomd.xml: $res";
next;
}
$parser->parse_uri($dir.$relpath."repodata/repomd.xml");
my $primary = $parser->{Handler}->{result};
$res = mirror($uri."/$relpath/".$primary => $dir."/$relpath/".$primary);
if(is_error($res)) {
push @errors,"Could not download $uri"."/$relpath/".$primary.": $res";
next;
}
gunzip ($dir."/$relpath/".$primary => $dir."/$relpath/primary.xml")
or push @errors,"could not extract Packages file : $GunzipError";
}
}
return \@errors;
}
sub store_package_list_debian {
my ($packages_folder,$repo) = @_;
my $uri = $repo->{'uri'};
my $dir = $uri;
my @errors;
$dir =~ s/^http:\/\///;
$dir = "$packages_folder/$dir";
foreach my $section (@{$repo->{'sections'}}) {
my $relpath = "dists/".$repo->{'release'}."/$section";
foreach my $arch (@{$repo->{'archs'}}) {
my $packages_file = "/$relpath/binary-$arch/Packages";
mkpath("$dir/$relpath/binary-$arch/");
my $res = mirror($uri.$packages_file.".bz2" => $dir.$packages_file.".bz2");
if(is_error($res)) {
my $res2 = mirror($uri.$packages_file.".gz" => $dir.$packages_file.".gz");
if(is_error($res2)) {
push @errors,"Could not download $uri".$packages_file.".bz2 : $res";
push @errors,"Could not download $uri".$packages_file.".gz : $res2";
} else {
gunzip ($dir.$packages_file.".gz" => $dir.$packages_file)
or push @errors,"could not extract Packages file : $GunzipError";
}
} else {
bunzip2 ($dir.$packages_file.".bz2" => $dir.$packages_file)
or push @errors,"could not extract Packages file : $Bunzip2Error";
}
}
}
return \@errors;
}
=item cleanup_and_extract
Extract templates from packages.
=cut
sub cleanup_and_extract {
my ($servdir,$distribs) = @_;
my $file;
my $tmpdir = "/tmp";
mkpath($tmpdir);
while (my ($distsection,$packages) = each(%{$distribs})) {
my $outdir = "$servdir/debconf.d/$distsection";
mkpath($outdir);
foreach my $package (@{$packages}) {
if ((-f "$outdir/".$package->{'PACKAGE'}) || (-f "$outdir/".$package->{'PACKAGE'}.'-NOTEMPLATE')) {
next;
}
system( "dpkg -e '$servdir/".$package->{'FILENAME'}."' '$tmpdir/DEBIAN'" );
if( -f "$tmpdir/DEBIAN/templates" ) {
my $tmpl = encode_base64(file("$tmpdir/DEBIAN/templates")->slurp());
open ($file,q{>},"$outdir/".$package->{'PACKAGE'}) or die "cannot open file";
print $file $tmpl;
close ($file);
unlink ("$tmpdir/DEBIAN/templates");
} else {
open ($file,q{>},"$outdir/".$package->{'PACKAGE'}.'-NOTEMPLATE') or die "cannot open file";
print $file "1\n";
close ($file);
}
}
}
unlink("$tmpdir/DEBIAN");
return;
}
1;
__END__
# vim:ts=2:sw=2:expandtab:shiftwidth=2:syntax:paste
|