Просмотр исходного кода

Update Dpkg::Control::* to use Dpkg::Interface::Storable

This implies renaming parse_fh() into parse() and parse() into load().
Update all scripts and modules using those methods.
Raphaël Hertzog лет назад: 16
Родитель
Сommit
6c8369aee3

+ 1 - 1
scripts/Dpkg/Changelog/Parse.pm

@@ -144,7 +144,7 @@ sub changelog_parse {
     my (@res, $fields);
     while (1) {
         $fields = Dpkg::Control::Changelog->new();
-        last unless $fields->parse_fh(\*P, _g("output of changelog parser"));
+        last unless $fields->parse(\*P, _g("output of changelog parser"));
 	push @res, $fields;
     }
     close(P) or subprocerr(_g("changelog parser %s"), $parser);

+ 12 - 15
scripts/Dpkg/Control/Hash.pm

@@ -26,10 +26,11 @@ use Dpkg::ErrorHandling;
 # Dpkg::Control::Fields itself (Dpkg::Vendor)
 # That's why field_capitalize is duplicated
 
+use base qw(Dpkg::Interface::Storable);
+
 use overload
     '%{}' => sub { ${$_[0]}->{'fields'} },
-    'eq' => sub { "$_[0]" eq "$_[1]" },
-    '""' => \&output;
+    'eq' => sub { "$_[0]" eq "$_[1]" };
 
 =head1 NAME
 
@@ -136,22 +137,12 @@ sub get_option {
     return $$self->{$k};
 }
 
-=item $c->parse($file)
+=item $c->load($file)
 
 Parse the content of $file. Exits in case of errors. Returns true if some
 fields have been parsed.
 
-=cut
-
-sub parse {
-    my ($self, $file) = @_;
-    open(CDATA, "<", $file) || syserr(_g("cannot read %s"), $file);
-    my $res = $self->parse_fh(\*CDATA, $file);
-    close(CDATA);
-    return $res;
-}
-
-=item $c->parse_fh($fh, $description)
+=item $c->parse($fh, $description)
 
 Parse a control file from the given filehandle. Exits in case of errors.
 $description is used to describe the filehandle, ideally it's a filename
@@ -160,7 +151,7 @@ messages. Returns true if some fields have been parsed.
 
 =cut
 
-sub parse_fh {
+sub parse {
     my ($self, $fh, $desc) = @_;
 
     my $paraborder = 1;
@@ -256,7 +247,13 @@ sub get_custom_field {
     return undef;
 }
 
+=item $c->save($filename)
+
+Write the string representation of the control information to a
+file.
+
 =item my $str = $c->output()
+
 =item "$c"
 
 Get a string representation of the control information. The fields

+ 19 - 24
scripts/Dpkg/Control/Info.pm

@@ -22,6 +22,8 @@ use Dpkg::Control;
 use Dpkg::ErrorHandling;
 use Dpkg::Gettext;
 
+use base qw(Dpkg::Interface::Storable);
+
 =head1 NAME
 
 Dpkg::Control::Info - parse files like debian/control
@@ -37,8 +39,8 @@ syntax than debian/control.
 
 =item $c = Dpkg::Control::Info->new($file)
 
-Create a new Dpkg::Control::Info object for $file. If $file is omitted, it parses
-debian/control. If file is "-", it parses the standard input.
+Create a new Dpkg::Control::Info object for $file. If $file is omitted, it
+loads debian/control. If file is "-", it parses the standard input.
 
 =cut
 
@@ -52,12 +54,12 @@ sub new {
     bless $self, $class;
     if ($arg) {
         if ($arg eq "-") {
-            $self->parse_fh(\*STDIN, _g("<standard input>"));
+            $self->parse(\*STDIN, _g("<standard input>"));
         } else {
-            $self->parse($arg);
+            $self->load($arg);
         }
     } else {
-	$self->parse("debian/control");
+	$self->load("debian/control");
     }
     return $self;
 }
@@ -74,20 +76,11 @@ sub reset {
     $self->{packages} = [];
 }
 
-=item $c->parse($file)
+=item $c->load($file)
 
-Parse the content of $file. Exits in case of errors.
+Load the content of $file. Exits in case of errors.
 
-=cut
-
-sub parse {
-    my ($self, $file) = @_;
-    open(CDATA, "<", $file) || syserr(_g("cannot read %s"), $file);
-    $self->parse_fh(\*CDATA, $file);
-    close(CDATA);
-}
-
-=item $c->parse_fh($fh, $description)
+=item $c->parse($fh, $description)
 
 Parse a control file from the given filehandle. Exits in case of errors.
 $description is used to describe the filehandle, ideally it's a filename
@@ -96,18 +89,18 @@ messages.
 
 =cut
 
-sub parse_fh {
+sub parse {
     my ($self, $fh, $desc) = @_;
     $self->reset();
     my $cdata = Dpkg::Control->new(type => CTRL_INFO_SRC);
-    return if not $cdata->parse_fh($fh, $desc);
+    return if not $cdata->parse($fh, $desc);
     $self->{source} = $cdata;
     unless (exists $cdata->{Source}) {
 	syntaxerr($desc, _g("first block lacks a source field"));
     }
     while (1) {
 	$cdata = Dpkg::Control->new(type => CTRL_INFO_PKG);
-        last if not $cdata->parse_fh($fh, $desc);
+        last if not $cdata->parse($fh, $desc);
 	push @{$self->{packages}}, $cdata;
 	unless (exists $cdata->{Package}) {
 	    syntaxerr($desc, _g("block lacks a package field"));
@@ -166,19 +159,21 @@ sub get_packages {
     return @{$self->{packages}};
 }
 
-=item $c->dump($filehandle)
+=item $c->output($filehandle)
 
 Dump the content into a filehandle.
 
 =cut
 
-sub dump {
+sub output {
     my ($self, $fh) = @_;
-    $self->{source}->output($fh);
+    my $str;
+    $str .= $self->{source}->output($fh);
     foreach my $pkg (@{$self->{packages}}) {
 	print $fh "\n";
-	$pkg->output($fh);
+	$str .= "\n" . $pkg->output($fh);
     }
+    return $str;
 }
 
 =back

+ 1 - 1
scripts/Dpkg/Index.pm

@@ -163,7 +163,7 @@ sub parse {
     my ($self, $fh, $desc) = @_;
     my $item = $self->new_item();
     my $i = 0;
-    while ($item->parse_fh($fh, $desc)) {
+    while ($item->parse($fh, $desc)) {
 	$self->add($item);
 	$item = $self->new_item();
 	$i++;

+ 1 - 3
scripts/Dpkg/Source/Package.pm

@@ -137,10 +137,8 @@ sub initialize {
     }
     close(DSC);
     # Read the fields
-    open(CDATA, "<", $filename) || syserr(_g("cannot open %s"), $filename);
     my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
-    $fields->parse_fh(\*CDATA, sprintf(_g("source control file %s"), $filename));
-    close(CDATA);
+    $fields->load($filename);
     $self->{'fields'} = $fields;
 
     foreach my $f (qw(Source Format Version Files)) {

+ 1 - 3
scripts/Dpkg/Vendor.pm

@@ -60,10 +60,8 @@ sub get_vendor_info(;$) {
     my $vendor = shift || "default";
     my $file = get_vendor_file($vendor);
     return undef unless $file;
-    open(my $fh, "<", $file) || syserr(_g("cannot read %s"), $file);
     my $fields = Dpkg::Control::Hash->new();
-    $fields->parse_fh($fh, $file) || error(_g("%s is empty"), $file);
-    close($fh);
+    $fields->load($file) || error(_g("%s is empty"), $file);
     return $fields;
 }
 

+ 1 - 3
scripts/dpkg-genchanges.pl

@@ -393,12 +393,10 @@ if (!is_binaryonly) {
 
     (my $sversion = $substvars->get('source:Version')) =~ s/^\d+://;
     $dsc= "$uploadfilesdir/${sourcepackage}_${sversion}.dsc";
-    open(CDATA, "<", $dsc) || syserr(_g("cannot open .dsc file %s"), $dsc);
     push(@sourcefiles,"${sourcepackage}_${sversion}.dsc");
 
     my $dsc_fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
-    $dsc_fields->parse_fh(\*CDATA, sprintf(_g("source control file %s"), $dsc)) ||
-        error(_g("%s is empty", $dsc));
+    $dsc_fields->load($dsc) || error(_g("%s is empty", $dsc));
 
     readallchecksums($dsc_fields, \%checksum, \%size);
 

+ 1 - 1
scripts/dpkg-name.pl

@@ -95,7 +95,7 @@ sub getfields($)
     open(CDATA, '-|', "dpkg-deb", "-f", "--", $filename) ||
         syserr(_g("cannot open %s"), $filename);
     my $fields = Dpkg::Control->new(type => CTRL_PKG_DEB);
-    $fields->parse_fh(\*CDATA, sprintf(_g("binary control file %s"), $filename));
+    $fields->parse(\*CDATA, sprintf(_g("binary control file %s"), $filename));
     close(CDATA);
 
     return $fields;

+ 1 - 1
scripts/dpkg-scanpackages.pl

@@ -189,7 +189,7 @@ FILE:
 	my $pid = spawn('exec' => [ "dpkg-deb", "-I", $fn, "control" ],
 			'to_pipe' => \$output);
 	my $fields = Dpkg::Control->new(type => CTRL_INDEX_PKG);
-	$fields->parse_fh($output, $fn)
+	$fields->parse($output, $fn)
 	    or error(_g("couldn't parse control information from %s."), $fn);
 	wait_child($pid, no_check => 1);
 	if ($?) {

+ 1 - 1
scripts/dpkg-scansources.pl

@@ -224,7 +224,7 @@ sub process_dsc {
 
     # Parse ‘.dsc’ file.
     my $fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
-    $fields->parse($file);
+    $fields->load($file);
     $fields->set_options(type => CTRL_INDEX_SRC);
 
     # Get checksums

+ 1 - 1
scripts/t/700_Dpkg_Control.t

@@ -27,7 +27,7 @@ my $datadir = $srcdir . '/t/700_Dpkg_Control';
 my $c = Dpkg::Control::Info->new("$datadir/control-1");
 
 my $io = IO::String->new();
-$c->dump($io);
+$c->output($io);
 my $value = ${$io->string_ref()};
 my $expected = 'Source: mysource
 My-Field-One: myvalue1