From: Raphael Hertzog Date: Sun, 13 Jan 2008 21:03:28 +0000 (+0100) Subject: Dpkg::Fields: Change set_field_importance() into a method of Dpkg::Fields::Object X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=09ea14d036266288dd1678946fc2631e169db4c4;p=dpkg Dpkg::Fields: Change set_field_importance() into a method of Dpkg::Fields::Object * scripts/Dpkg/Fields.pm: Drop public function set_fields_importance() in favor of a method on Dpkg::Fields::Object. Integrate public function sort_field_by_importance() in the output() method where it was used. * scripts/Dpkg/Changelog.pm, scripts/dpkg-genchanges.pl, scripts/dpkg-gencontrol.pl, scripts/dpkg-source.pl: Updated to use the set_field_importance() method instead of the removed function. * scripts/Dpkg/Control.pm: Update pod documentation to refer to Dpkg::Fields::Object instead of the invalid Dpkg::Cdata::Object. --- diff --git a/ChangeLog b/ChangeLog index 4d1028d0..06799295 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2008-01-13 Raphael Hertzog + + * scripts/Dpkg/Fields.pm: Drop public function + set_fields_importance() in favor of a method on + Dpkg::Fields::Object. Integrate public function + sort_field_by_importance() in the output() method where it was + used. + * scripts/Dpkg/Changelog.pm, scripts/dpkg-genchanges.pl, + scripts/dpkg-gencontrol.pl, scripts/dpkg-source.pl: Updated to use + the set_field_importance() method instead of the removed function. + * scripts/Dpkg/Control.pm: Update pod documentation to refer to + Dpkg::Fields::Object instead of the invalid Dpkg::Cdata::Object. + 2008-01-13 Frank Lichtenheld * scripts/Dpkg/Fields.pm (dump): Allow to omit the diff --git a/scripts/Dpkg/Changelog.pm b/scripts/Dpkg/Changelog.pm index de59c667..18db282f 100644 --- a/scripts/Dpkg/Changelog.pm +++ b/scripts/Dpkg/Changelog.pm @@ -41,7 +41,7 @@ use Dpkg; use Dpkg::Gettext; use Dpkg::ErrorHandling qw(warning report syserr subprocerr); use Dpkg::Cdata; -use Dpkg::Fields qw(set_field_importance); +use Dpkg::Fields; use base qw(Exporter); @@ -418,7 +418,6 @@ BEGIN { Urgency_comment Urgency_lc); tie %CHANGELOG_FIELDS, 'Dpkg::Fields::Object'; %CHANGELOG_FIELDS = map { $_ => 1 } @CHANGELOG_FIELDS; - set_field_importance(@CHANGELOG_FIELDS); @URGENCIES = qw(low medium high critical emergency); my $i = 1; %URGENCIES = map { $_ => $i++ } @URGENCIES; @@ -693,7 +692,7 @@ sub get_dpkg_changes { =head3 parse_changelog($file, $format, $since) Calls "dpkg-parsechangelog -l$file -F$format -v$since" and returns a -Dpkg::Cdata::Object with the values output by the program. +Dpkg::Fields::Object with the values output by the program. =cut sub parse_changelog { @@ -726,6 +725,7 @@ sub new { my ($classname) = @_; tie my %entry, 'Dpkg::Fields::Object'; + tied(%entry)->set_field_importance(@CHANGELOG_FIELDS); my $entry = \%entry; bless $entry, $classname; } diff --git a/scripts/Dpkg/Control.pm b/scripts/Dpkg/Control.pm index b8450d8b..b9e4c941 100644 --- a/scripts/Dpkg/Control.pm +++ b/scripts/Dpkg/Control.pm @@ -99,7 +99,7 @@ sub parse { =item $c->get_source() Returns a reference to a hash containing the fields concerning the -source package. The hash is tied to Dpkg::Cdata::Object. +source package. The hash is tied to Dpkg::Fields::Object. =cut sub get_source { @@ -111,7 +111,7 @@ sub get_source { Returns a reference to a hash containing the fields concerning the binary package numbered $idx (starting at 1). The hash is tied to -Dpkg::Cdata::Object. +Dpkg::Fields::Object. =cut sub get_pkg_by_idx { @@ -122,7 +122,7 @@ sub get_pkg_by_idx { =item $c->get_pkg_by_name($name) Returns a reference to a hash containing the fields concerning the binary -package named $name. The hash is tied to Dpkg::Cdata::Object. +package named $name. The hash is tied to Dpkg::Fields::Object. =cut sub get_pkg_by_name { diff --git a/scripts/Dpkg/Fields.pm b/scripts/Dpkg/Fields.pm index c640bb58..f1b9df39 100644 --- a/scripts/Dpkg/Fields.pm +++ b/scripts/Dpkg/Fields.pm @@ -7,9 +7,8 @@ use Exporter; use Dpkg::Deps qw(@src_dep_fields @pkg_dep_fields); our @ISA = qw(Exporter); -our @EXPORT_OK = qw(capit set_field_importance sort_field_by_importance - %control_src_fields %control_pkg_fields $control_src_field_regex - $control_pkg_field_regex); +our @EXPORT_OK = qw(capit %control_src_fields %control_pkg_fields + $control_src_field_regex $control_pkg_field_regex); our %EXPORT_TAGS = ('list' => [qw(%control_src_fields %control_pkg_fields $control_src_field_regex $control_pkg_field_regex)]); @@ -35,31 +34,6 @@ sub capit { return join '-', @pieces; } -my %fieldimps; - -sub set_field_importance(@) -{ - my @fields = @_; - my $i = 1; - - grep($fieldimps{$_} = $i++, @fields); -} - -sub sort_field_by_importance($$) -{ - my ($a, $b) = @_; - - if (defined $fieldimps{$a} && defined $fieldimps{$b}) { - $fieldimps{$a} <=> $fieldimps{$b}; - } elsif (defined($fieldimps{$a})) { - -1; - } elsif (defined($fieldimps{$b})) { - 1; - } else { - $a cmp $b; - } -} - package Dpkg::Fields::Object; =head1 OTHER OBJECTS @@ -71,8 +45,6 @@ normalizing the name of fields received in keys (using Dpkg::Fields::capit). It also stores the order in which fields have been added in order to be able to dump them in the same order. -You can also dump the content of the hash with tied(%hash)->dump($fh). - =cut use Tie::Hash; our @ISA = qw(Tie::ExtraHash Tie::Hash); @@ -80,10 +52,12 @@ our @ISA = qw(Tie::ExtraHash Tie::Hash); use Dpkg::ErrorHandling qw(internerr syserr); # Import capit -Dpkg::Fields->import('capit', 'sort_field_by_importance'); +Dpkg::Fields->import('capit'); # $self->[0] is the real hash # $self->[1] is an array containing the ordered list of keys +# $self->[2] is an hash describing the relative importance of each field +# (used to sort the output). =head2 Dpkg::Fields::Object->new() @@ -99,7 +73,7 @@ sub new { sub TIEHASH { my $class = shift; - return bless [{}, []], $class; + return bless [{}, [], {}], $class; } sub FETCH { @@ -156,6 +130,12 @@ sub NEXTKEY { return undef; } +=head2 my $str = tied(%hash)->dump() +=head2 tied(%hash)->dump($fh) + +Dump the raw content of the hash either as a string or to a filehandle. + +=cut sub dump { my ($self, $fh) = @_; my $str = ""; @@ -168,9 +148,31 @@ sub dump { return $str; } +=head2 tied(%hash)->set_field_importance(@fields) + +Define the order in which fields will be displayed in the output() method. + +=cut +sub set_field_importance { + my ($self, @fields) = @_; + my $i = 1; + + $self->[2] = {}; + $self->[2]{$_} = $i++ foreach (@fields); +} + +=head2 tied(%hash)->output($fh, $substvars) + +If $fh is defined, print the fields on the $fh filehandle after +substitution of variables defined in the Dpkg::Substvars object. + +Also returns the string of what would printed on the filehandle. + +=cut sub output { my ($self, $fh, $substvars) = @_; my $str = ""; + my $imp = $self->[2]; # Hash of relative importance # Add substvars to refer to other fields if (defined($substvars)) { @@ -179,7 +181,19 @@ sub output { } } - for my $f (sort sort_field_by_importance keys %{$self->[0]}) { + my @keys = sort { + if (defined $imp->{$a} && defined $imp->{$b}) { + $imp->{$a} <=> $imp->{$b}; + } elsif (defined($imp->{$a})) { + -1; + } elsif (defined($imp->{$b})) { + 1; + } else { + $a cmp $b; + } + } keys %{$self->[0]}; + + foreach my $f (@keys) { my $v = $self->[0]->{$f}; if (defined($substvars)) { $v = $substvars->substvars($v); diff --git a/scripts/dpkg-genchanges.pl b/scripts/dpkg-genchanges.pl index 20e546bd..9fb13fc0 100755 --- a/scripts/dpkg-genchanges.pl +++ b/scripts/dpkg-genchanges.pl @@ -11,7 +11,7 @@ use Dpkg::Gettext; use Dpkg::ErrorHandling qw(warning error failure unknown internerr syserr subprocerr usageerr); use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is); -use Dpkg::Fields qw(:list capit set_field_importance sort_field_by_importance); +use Dpkg::Fields qw(:list capit); use Dpkg::Compression; use Dpkg::Control; use Dpkg::Cdata; @@ -480,7 +480,7 @@ for my $f (keys %remove) { delete $fields->{$f}; } -set_field_importance(@changes_fields); $substvars->parse($varlistfile) if -e $varlistfile; +tied(%{$fields})->set_field_importance(@changes_fields); tied(%{$fields})->output(\*STDOUT, $substvars); diff --git a/scripts/dpkg-gencontrol.pl b/scripts/dpkg-gencontrol.pl index beeed4bf..04e7fc36 100755 --- a/scripts/dpkg-gencontrol.pl +++ b/scripts/dpkg-gencontrol.pl @@ -11,7 +11,7 @@ use Dpkg::ErrorHandling qw(warning error failure unknown internerr syserr subprocerr usageerr); use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is); use Dpkg::Deps qw(@pkg_dep_fields %dep_field_type); -use Dpkg::Fields qw(:list capit set_field_importance); +use Dpkg::Fields qw(:list capit); use Dpkg::Control; use Dpkg::Substvars; use Dpkg::Vars; @@ -350,7 +350,7 @@ if (!$stdout) { binmode(STDOUT); } -set_field_importance(@control_fields); +tied(%{$fields})->set_field_importance(@control_fields); tied(%{$fields})->output(\*STDOUT, $substvars); if (!$stdout) { diff --git a/scripts/dpkg-source.pl b/scripts/dpkg-source.pl index 6a712d9b..af4cd575 100755 --- a/scripts/dpkg-source.pl +++ b/scripts/dpkg-source.pl @@ -10,7 +10,7 @@ use Dpkg::ErrorHandling qw(warning warnerror error failure unknown $warnable_error $quiet_warnings); use Dpkg::Arch qw(debarch_eq); use Dpkg::Deps qw(@src_dep_fields %dep_field_type); -use Dpkg::Fields qw(:list capit set_field_importance); +use Dpkg::Fields qw(:list capit); use Dpkg::Compression; use Dpkg::Cdata; use Dpkg::Control; @@ -769,8 +769,8 @@ if ($opmode eq 'build') { open(STDOUT, "> $basenamerev.dsc") || syserr(_g("create %s"), "$basenamerev.dsc"); - set_field_importance(@dsc_fields); $substvars->parse($varlistfile) if -e $varlistfile; + tied(%{$fields})->set_field_importance(@dsc_fields); tied(%{$fields})->output(\*STDOUT, $substvars); if ($ur) {