# # Copyright 2005 Yuki Fujimura # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # package MT::Plugin::DebianPackages; use strict; use warnings; use MT; use MT::Plugin; use MT::Template::Context; our $VERSION = 1.2; MT->add_plugin( MT::Plugin->new( 'name' => 'DebianPackages', 'description' => 'Shows Debian GNU/Linux repository from Packages file.', 'version' => $VERSION, 'author_name' => 'Yuki Fujimura', 'author_link' => 'http://www.wakhok.ac.jp/~fujimura/', 'doc_link' => 'http://www.wakhok.ac.jp/~fujimura/2005/08/14/025600', ) ); sub _build_packages($$) { my ($context, $packages_file_name) = @_; my $io_class = undef; if ($packages_file_name =~ m/.gz$/) { $io_class = 'IO::Zlib'; } $io_class ||= 'IO::File'; eval qq{ use $io_class; }; return $context->error($@) if $@; my $fn = $io_class->new($packages_file_name, 'r'); return $context->error(sprintf('%s: %s', $packages_file_name, $!)) unless $fn; my @pkg = (); my $last_field = undef; my $current_pkg = {}; while (<$fn>) { chomp; if (m/^((?:\w+|[\-_])*):\s*(.*)/) { ($last_field = $1) =~ s/[\-_]//; $current_pkg->{$last_field} = $2; } elsif (m/^ /) { if ($last_field eq 'Description') { # paragraphs s/^ \.$/\n\n/; $current_pkg->{'.Description'} ||= ''; s/^ // if $current_pkg->{'.Description'} =~ m/(^|\n)$/s; $current_pkg->{'.Description'} .= $_; } elsif ($last_field eq 'Conffiles') { return $context->error('Illegal Package = format') unless m/^ (\S+)\s+(\S+)/; $current_pkg->{'.Conffiles'} ||= []; push(@{$current_pkg->{'.Conffiles'}}, { 'Filename' => $1, 'MD5sum' => $2, }); } else { return $context->error(sprintf('%s: Body field value is not supported', $last_field)); } } elsif ($_ eq '') { push(@pkg, $current_pkg); $current_pkg = {}; } else { return $context->error('Illegal Packages format'); }; } return \@pkg; } MT::Template::Context->add_container_tag('DebianPackages' => sub { my ($context, $args) = @_; return $context->error('Unspecified Packages file.') unless exists $args->{'path'}; my $packages = _build_packages($context, $args->{'path'}); return $context->errstr unless ref($packages); my $builder = $context->stash('builder'); my $tokens = $context->stash('tokens'); my $content = ''; foreach my $i (@$packages) { $context->stash('debian_package', $i); defined(my $out = $builder->build($context, $tokens)) or return $context->error($builder->errstr); $content .= $out; } return $content; }); foreach my $field_name (qw/ Architecture Bugs Conflicts Depends Enhances Essential Filename Installed-Size MD5sum Maintainer Origin Package Pre-Depends Priority Provides Recommends Replaces Section Size Source Suggests Task Version url /) { (my $tag_name = $field_name) =~ s/[\-_]//g; MT::Template::Context->add_tag('Debian' . $tag_name => sub { my ($context, $args) = @_; my $pkg = $context->stash('debian_package'); return $context->error(sprintf('<MTDebian%s> should be used in <MTDebianPackages>', $tag_name)) unless $pkg; return $pkg->{$field_name} || $args->{'unknown'} || 'Unknown'; }); } MT::Template::Context->add_tag('DebianDescription' => sub { my ($context, $args) = @_; my $pkg = $context->stash('debian_package'); return $context->error('<MTDebianDescription> should be used in <MTDebianPackages>') unless $pkg; my $field_name = $args->{'verbose'} ? '.Description' : 'Description'; return $pkg->{$field_name} || $args->{'unknown'} || 'Unknown'; }); MT::Template::Context->add_container_tag('DebianConffiles' => sub { my ($context, $args) = @_; my $pkg = $context->stash('debian_package'); return $context->error('<MTConffiles> should be used in <MTDebianPackages>') unless $pkg; my $conffiles = $pkg->{'.Conffiles'} || []; my $builder = $context->stash('builder'); my $tokens = $context->stash('tokens'); my $content = ''; foreach my $i (@$conffiles) { $context->stash('debian_conffile', $i); defined(my $out = $builder->build($context, $tokens)) or return $context->error($builder->errstr); $content .= $out; } return $content; }); foreach my $field_name (qw/Filename MD5sum/) { (my $tag_name = $field_name) =~ s/[\-_]//g; MT::Template::Context->add_tag('DebianConffile' . $tag_name => sub { my ($context, $args) = @_; my $pkg = $context->stash('debian_conffile'); return $context->error(sprintf('<MT%s> should be used in <MTDebianConffiles>', $tag_name)) unless $pkg; return $pkg->{$field_name} || $args->{'unknown'} || 'Unknown'; }); } 1;