瀏覽代碼

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.
Raphael Hertzog 18 年之前
父節點
當前提交
09ea14d036
共有 7 個文件被更改,包括 72 次插入45 次删除
  1. 13 0
      ChangeLog
  2. 3 3
      scripts/Dpkg/Changelog.pm
  3. 3 3
      scripts/Dpkg/Control.pm
  4. 47 33
      scripts/Dpkg/Fields.pm
  5. 2 2
      scripts/dpkg-genchanges.pl
  6. 2 2
      scripts/dpkg-gencontrol.pl
  7. 2 2
      scripts/dpkg-source.pl

+ 13 - 0
ChangeLog

@@ -1,3 +1,16 @@
+2008-01-13  Raphael Hertzog  <hertzog@debian.org>
+
+	* 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  <djpig@debian.org>
 2008-01-13  Frank Lichtenheld  <djpig@debian.org>
 
 
 	* scripts/Dpkg/Fields.pm (dump): Allow to omit the
 	* scripts/Dpkg/Fields.pm (dump): Allow to omit the

+ 3 - 3
scripts/Dpkg/Changelog.pm

@@ -41,7 +41,7 @@ use Dpkg;
 use Dpkg::Gettext;
 use Dpkg::Gettext;
 use Dpkg::ErrorHandling qw(warning report syserr subprocerr);
 use Dpkg::ErrorHandling qw(warning report syserr subprocerr);
 use Dpkg::Cdata;
 use Dpkg::Cdata;
-use Dpkg::Fields qw(set_field_importance);
+use Dpkg::Fields;
 
 
 use base qw(Exporter);
 use base qw(Exporter);
 
 
@@ -418,7 +418,6 @@ BEGIN {
                            Urgency_comment Urgency_lc);
                            Urgency_comment Urgency_lc);
     tie %CHANGELOG_FIELDS, 'Dpkg::Fields::Object';
     tie %CHANGELOG_FIELDS, 'Dpkg::Fields::Object';
     %CHANGELOG_FIELDS = map { $_ => 1 } @CHANGELOG_FIELDS;
     %CHANGELOG_FIELDS = map { $_ => 1 } @CHANGELOG_FIELDS;
-    set_field_importance(@CHANGELOG_FIELDS);
     @URGENCIES = qw(low medium high critical emergency);
     @URGENCIES = qw(low medium high critical emergency);
     my $i = 1;
     my $i = 1;
     %URGENCIES = map { $_ => $i++ } @URGENCIES;
     %URGENCIES = map { $_ => $i++ } @URGENCIES;
@@ -693,7 +692,7 @@ sub get_dpkg_changes {
 =head3 parse_changelog($file, $format, $since)
 =head3 parse_changelog($file, $format, $since)
 
 
 Calls "dpkg-parsechangelog -l$file -F$format -v$since"  and returns a
 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
 =cut
 sub parse_changelog {
 sub parse_changelog {
@@ -726,6 +725,7 @@ sub new {
     my ($classname) = @_;
     my ($classname) = @_;
 
 
     tie my %entry, 'Dpkg::Fields::Object';
     tie my %entry, 'Dpkg::Fields::Object';
+    tied(%entry)->set_field_importance(@CHANGELOG_FIELDS);
     my $entry = \%entry;
     my $entry = \%entry;
     bless $entry, $classname;
     bless $entry, $classname;
 }
 }

+ 3 - 3
scripts/Dpkg/Control.pm

@@ -99,7 +99,7 @@ sub parse {
 =item $c->get_source()
 =item $c->get_source()
 
 
 Returns a reference to a hash containing the fields concerning the
 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
 =cut
 sub get_source {
 sub get_source {
@@ -111,7 +111,7 @@ sub get_source {
 
 
 Returns a reference to a hash containing the fields concerning the binary
 Returns a reference to a hash containing the fields concerning the binary
 package numbered $idx (starting at 1). The hash is tied to
 package numbered $idx (starting at 1). The hash is tied to
-Dpkg::Cdata::Object.
+Dpkg::Fields::Object.
 
 
 =cut
 =cut
 sub get_pkg_by_idx {
 sub get_pkg_by_idx {
@@ -122,7 +122,7 @@ sub get_pkg_by_idx {
 =item $c->get_pkg_by_name($name)
 =item $c->get_pkg_by_name($name)
 
 
 Returns a reference to a hash containing the fields concerning the binary
 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
 =cut
 sub get_pkg_by_name {
 sub get_pkg_by_name {

+ 47 - 33
scripts/Dpkg/Fields.pm

@@ -7,9 +7,8 @@ use Exporter;
 use Dpkg::Deps qw(@src_dep_fields @pkg_dep_fields);
 use Dpkg::Deps qw(@src_dep_fields @pkg_dep_fields);
 
 
 our @ISA = qw(Exporter);
 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
 our %EXPORT_TAGS = ('list' => [qw(%control_src_fields %control_pkg_fields
 			$control_src_field_regex $control_pkg_field_regex)]);
 			$control_src_field_regex $control_pkg_field_regex)]);
 
 
@@ -35,31 +34,6 @@ sub capit {
     return join '-', @pieces;
     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;
 package Dpkg::Fields::Object;
 
 
 =head1 OTHER OBJECTS
 =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
 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.
 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
 =cut
 use Tie::Hash;
 use Tie::Hash;
 our @ISA = qw(Tie::ExtraHash 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);
 use Dpkg::ErrorHandling qw(internerr syserr);
 
 
 # Import capit
 # Import capit
-Dpkg::Fields->import('capit', 'sort_field_by_importance');
+Dpkg::Fields->import('capit');
 
 
 # $self->[0] is the real hash
 # $self->[0] is the real hash
 # $self->[1] is an array containing the ordered list of keys
 # $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()
 =head2 Dpkg::Fields::Object->new()
 
 
@@ -99,7 +73,7 @@ sub new {
 
 
 sub TIEHASH  {
 sub TIEHASH  {
     my $class = shift;
     my $class = shift;
-    return bless [{}, []], $class;
+    return bless [{}, [], {}], $class;
 }
 }
 
 
 sub FETCH {
 sub FETCH {
@@ -156,6 +130,12 @@ sub NEXTKEY {
     return undef;
     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 {
 sub dump {
     my ($self, $fh) = @_;
     my ($self, $fh) = @_;
     my $str = "";
     my $str = "";
@@ -168,9 +148,31 @@ sub dump {
     return $str;
     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 {
 sub output {
     my ($self, $fh, $substvars) = @_;
     my ($self, $fh, $substvars) = @_;
     my $str = "";
     my $str = "";
+    my $imp = $self->[2]; # Hash of relative importance
 
 
     # Add substvars to refer to other fields
     # Add substvars to refer to other fields
     if (defined($substvars)) {
     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};
         my $v = $self->[0]->{$f};
         if (defined($substvars)) {
         if (defined($substvars)) {
             $v = $substvars->substvars($v);
             $v = $substvars->substvars($v);

+ 2 - 2
scripts/dpkg-genchanges.pl

@@ -11,7 +11,7 @@ use Dpkg::Gettext;
 use Dpkg::ErrorHandling qw(warning error failure unknown internerr syserr
 use Dpkg::ErrorHandling qw(warning error failure unknown internerr syserr
                            subprocerr usageerr);
                            subprocerr usageerr);
 use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is);
 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::Compression;
 use Dpkg::Control;
 use Dpkg::Control;
 use Dpkg::Cdata;
 use Dpkg::Cdata;
@@ -480,7 +480,7 @@ for my $f (keys %remove) {
     delete $fields->{$f};
     delete $fields->{$f};
 }
 }
 
 
-set_field_importance(@changes_fields);
 $substvars->parse($varlistfile) if -e $varlistfile;
 $substvars->parse($varlistfile) if -e $varlistfile;
+tied(%{$fields})->set_field_importance(@changes_fields);
 tied(%{$fields})->output(\*STDOUT, $substvars);
 tied(%{$fields})->output(\*STDOUT, $substvars);
 
 

+ 2 - 2
scripts/dpkg-gencontrol.pl

@@ -11,7 +11,7 @@ use Dpkg::ErrorHandling qw(warning error failure unknown internerr syserr
                            subprocerr usageerr);
                            subprocerr usageerr);
 use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is);
 use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is);
 use Dpkg::Deps qw(@pkg_dep_fields %dep_field_type);
 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::Control;
 use Dpkg::Substvars;
 use Dpkg::Substvars;
 use Dpkg::Vars;
 use Dpkg::Vars;
@@ -350,7 +350,7 @@ if (!$stdout) {
     binmode(STDOUT);
     binmode(STDOUT);
 }
 }
 
 
-set_field_importance(@control_fields);
+tied(%{$fields})->set_field_importance(@control_fields);
 tied(%{$fields})->output(\*STDOUT, $substvars);
 tied(%{$fields})->output(\*STDOUT, $substvars);
 
 
 if (!$stdout) {
 if (!$stdout) {

+ 2 - 2
scripts/dpkg-source.pl

@@ -10,7 +10,7 @@ use Dpkg::ErrorHandling qw(warning warnerror error failure unknown
                            $warnable_error $quiet_warnings);
                            $warnable_error $quiet_warnings);
 use Dpkg::Arch qw(debarch_eq);
 use Dpkg::Arch qw(debarch_eq);
 use Dpkg::Deps qw(@src_dep_fields %dep_field_type);
 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::Compression;
 use Dpkg::Cdata;
 use Dpkg::Cdata;
 use Dpkg::Control;
 use Dpkg::Control;
@@ -769,8 +769,8 @@ if ($opmode eq 'build') {
     open(STDOUT, "> $basenamerev.dsc") ||
     open(STDOUT, "> $basenamerev.dsc") ||
         syserr(_g("create %s"), "$basenamerev.dsc");
         syserr(_g("create %s"), "$basenamerev.dsc");
 
 
-    set_field_importance(@dsc_fields);
     $substvars->parse($varlistfile) if -e $varlistfile;
     $substvars->parse($varlistfile) if -e $varlistfile;
+    tied(%{$fields})->set_field_importance(@dsc_fields);
     tied(%{$fields})->output(\*STDOUT, $substvars);
     tied(%{$fields})->output(\*STDOUT, $substvars);
 
 
     if ($ur) {
     if ($ur) {