pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/pkgtools/lintpkgsrc/files lintpkgsrc: cleanup: move lo...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/fdc12e5f6c85
branches:  trunk
changeset: 383312:fdc12e5f6c85
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Wed Aug 17 17:26:35 2022 +0000

description:
lintpkgsrc: cleanup: move loading and storing of package data together

diffstat:

 pkgtools/lintpkgsrc/files/lintpkgsrc.pl |  97 ++++++++++++++++----------------
 pkgtools/lintpkgsrc/files/t/packages.t  |   8 +-
 2 files changed, 52 insertions(+), 53 deletions(-)

diffs (173 lines):

diff -r fb84ebc761f0 -r fdc12e5f6c85 pkgtools/lintpkgsrc/files/lintpkgsrc.pl
--- a/pkgtools/lintpkgsrc/files/lintpkgsrc.pl   Wed Aug 17 17:05:14 2022 +0000
+++ b/pkgtools/lintpkgsrc/files/lintpkgsrc.pl   Wed Aug 17 17:26:35 2022 +0000
@@ -1,5 +1,5 @@
 #!@PERL5@
-# $NetBSD: lintpkgsrc.pl,v 1.107 2022/08/16 20:54:35 rillig Exp $
+# $NetBSD: lintpkgsrc.pl,v 1.108 2022/08/17 17:26:35 rillig Exp $
 
 # Written by David Brownlee <abs%netbsd.org@localhost>.
 #
@@ -135,6 +135,51 @@
            : sort { $a->pkgbase cmp $b->pkgbase } values %$self;
 }
 
+sub load($class, $fname) {
+       open(STORE, '<', $fname)
+           or die("Cannot read package data from $fname: $!\n");
+       my ($pkgver);
+       my $self = $class->new();
+       while (defined(my $line = <STORE>)) {
+               chomp($line);
+               if ($line =~ m"^ package \t ([^\t]+) \t ([^\t]+) $"x) {
+                       $pkgver = $self->add($1, $2);
+               } elsif ($line =~ m"^ var \t ([^\t]+) \t (.*) $"x) {
+                       $pkgver->var($1, $2);
+               } elsif ($line =~ m"^ sub ") {
+                       die "Outdated cache format in '$fname'\n";
+               } else {
+                       die "Invalid line '$line' in cache '$fname'\n";
+               }
+       }
+       close(STORE) or die;
+}
+
+sub store($self, $fname) {
+       open(STORE, '>', $fname)
+           or die("Cannot save package data to $fname: $!\n");
+       foreach my $pkgver ($self->pkgvers_all) {
+               my $pkgbase = $pkgver->pkgbase;
+               my $pkgversion = $pkgver->pkgversion;
+
+               $pkgbase =~ /^\S+$/
+                   or die "cannot store package name '$pkgbase'\n";
+               $pkgversion =~ /^\S+$/
+                   or die "cannot store package version '$pkgversion'\n";
+               print STORE "package\t$pkgbase\t$pkgversion\n";
+
+               foreach my $varname (sort $pkgver->vars) {
+                       my $value = $pkgver->var($varname);
+                       $varname =~ /^\S+$/
+                           or die "cannot store variable name '$varname'\n";
+                       $value =~ /^.*$/
+                           or die "cannot store variable value '$value'\n";
+                       print STORE "var\t$varname\t$value\n";
+               }
+       }
+       close(STORE) or die("Cannot save package data to $fname: $!\n");
+}
+
 package main;
 
 # Buildtime configuration
@@ -1010,26 +1055,6 @@
        return $prev_dir;
 }
 
-sub load_pkgdata_from_cache($fname) {
-       open(STORE, '<', $fname)
-           or die("Cannot read pkgsrc store from $fname: $!\n");
-       my ($pkgver);
-       $pkgdata = PkgData->new();
-       while (defined(my $line = <STORE>)) {
-               chomp($line);
-               if ($line =~ m"^ package \t ([^\t]+) \t ([^\t]+) $"x) {
-                       $pkgver = $pkgdata->add($1, $2);
-               } elsif ($line =~ m"^ var \t ([^\t]+) \t (.*) $"x) {
-                       $pkgver->var($1, $2);
-               } elsif ($line =~ m"^ sub ") {
-                       die "Outdated cache format in '$fname'\n";
-               } else {
-                       die "Invalid line '$line' in cache '$fname'\n";
-               }
-       }
-       close(STORE);
-}
-
 # Generate pkgname->category/pkg mapping, optionally check DEPENDS
 #
 sub scan_pkgsrc_makefiles($pkgsrcdir) {
@@ -1037,7 +1062,7 @@
        return if defined $pkgdata; # Already done.
 
        if ($opt{I}) {
-               load_pkgdata_from_cache($opt{I});
+               $pkgdata = PkgData->load($opt{I});
                return;
        }
 
@@ -1234,32 +1259,6 @@
        sort keys %unref_distfiles;
 }
 
-sub store_pkgdata_in_cache($db, $fname) {
-       open(STORE, '>', $fname)
-           or die("Cannot save package data to $fname: $!\n");
-       foreach my $pkgver ($db->pkgvers_all) {
-               my $pkgbase = $pkgver->pkgbase;
-               my $pkgversion = $pkgver->pkgversion;
-
-               $pkgbase =~ /^\S+$/
-                   or die "cannot store package name '$pkgbase'\n";
-               $pkgversion =~ /^\S+$/
-                   or die "cannot store package version '$pkgversion'\n";
-               print STORE "package\t$pkgbase\t$pkgversion\n";
-
-               foreach my $varname (sort $pkgver->vars) {
-                       my $value = $pkgver->var($varname);
-                       $varname =~ /^\S+$/
-                           or die "cannot store variable name '$varname'\n";
-                       $value =~ /^.*$/
-                           or die "cannot store variable value '$value'\n";
-                       print STORE "var\t$varname\t$value\n";
-               }
-       }
-       close(STORE)
-           or die("Cannot save package data to $fname: $!\n");
-}
-
 # Remember to update manual page when modifying option list
 #
 sub usage_and_exit($status) {
@@ -1651,7 +1650,7 @@
 
        if ($opt{E}) {
                scan_pkgsrc_makefiles($pkgsrcdir);
-               store_pkgdata_in_cache($pkgdata, $opt{E});
+               $pkgdata->store($opt{E});
        }
 }
 
diff -r fb84ebc761f0 -r fdc12e5f6c85 pkgtools/lintpkgsrc/files/t/packages.t
--- a/pkgtools/lintpkgsrc/files/t/packages.t    Wed Aug 17 17:05:14 2022 +0000
+++ b/pkgtools/lintpkgsrc/files/t/packages.t    Wed Aug 17 17:26:35 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: packages.t,v 1.16 2022/08/16 20:54:35 rillig Exp $
+# $NetBSD: packages.t,v 1.17 2022/08/17 17:26:36 rillig Exp $
 #
 # Tests for the internal package database, which stores the packages and their
 # versions, and a few variables like DEPENDS and BROKEN.
@@ -104,7 +104,7 @@
 }
 
 # Demonstrate how the package data is stored in the cache file.
-sub test_store_order() {
+sub test_pkgdata_store() {
        my $pkgdata = PkgData->new();
 
        my $pkgbase_1_0 = $pkgdata->add('pkgbase', '1.0');
@@ -120,7 +120,7 @@
        $pkgbase_1_15->var('COMMENT', 'Version 1.15');
 
        my $tmpfile = File::Temp->new();
-       store_pkgdata_in_cache($pkgdata, $tmpfile->filename);
+       $pkgdata->store($tmpfile->filename);
        my $stored = read_file($tmpfile->filename);
 
        # XXX: 1.3nb4 should be sorted before 1.15.
@@ -141,4 +141,4 @@
 test_pkgs();
 test_pkgdata();
 test_package_variables();
-test_store_order();
+test_pkgdata_store();



Home | Main Index | Thread Index | Old Index