pkgsrc-Changes-HG archive

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

[pkgsrc/trunk]: pkgsrc/pkgtools/url2pkg/files pkgtools/url2pkg: refactor to g...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/181f9ffc4e8c
branches:  trunk
changeset: 339649:181f9ffc4e8c
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Fri Sep 13 13:31:39 2019 +0000

description:
pkgtools/url2pkg: refactor to group the subs that modify Makefile lines

diffstat:

 pkgtools/url2pkg/files/url2pkg.pl |  220 +++++++++++++++++++------------------
 pkgtools/url2pkg/files/url2pkg.t  |  119 +++++++++++---------
 2 files changed, 180 insertions(+), 159 deletions(-)

diffs (truncated from 631 to 300 lines):

diff -r 46d937c37474 -r 181f9ffc4e8c pkgtools/url2pkg/files/url2pkg.pl
--- a/pkgtools/url2pkg/files/url2pkg.pl Fri Sep 13 13:13:31 2019 +0000
+++ b/pkgtools/url2pkg/files/url2pkg.pl Fri Sep 13 13:31:39 2019 +0000
@@ -1,5 +1,5 @@
 #! @PERL5@
-# $NetBSD: url2pkg.pl,v 1.72 2019/09/13 06:22:33 rillig Exp $
+# $NetBSD: url2pkg.pl,v 1.73 2019/09/13 13:31:39 rillig Exp $
 #
 
 # Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -64,30 +64,6 @@
        return [$name, $op, $value];
 }
 
-sub read_lines($) {
-       my ($filename) = @_;
-
-       my @lines;
-       open(F, "<", $filename) or return @lines;
-       while (defined(my $line = <F>)) {
-               chomp($line);
-               push(@lines, $line);
-       }
-       close(F) or die;
-       return @lines;
-}
-
-sub write_lines($@) {
-       my ($filename, @lines) = @_;
-
-       open(F, ">", "$filename.tmp") or die;
-       foreach my $line (@lines) {
-               print F "$line\n";
-       }
-       close(F) or die;
-       rename("$filename.tmp", $filename) or die;
-}
-
 sub find_package($) {
        my ($pkgbase) = @_;
 
@@ -96,32 +72,81 @@
        return $candidates[0] =~ s/\Q$pkgsrcdir\E/..\/../r;
 }
 
+sub make(@) {
+       my @args = @_;
+
+       (system { $make } ($make, @args)) == 0 or die;
+}
+
+package Lines;
+
+use constant false => 0;
+use constant true => 1;
+
+sub new($@) {
+       my ($class, @lines) = @_;
+       my $lines = \@lines;
+       bless($lines, $class);
+       return $lines;
+}
+
+sub read_from($$) {
+       my ($class, $filename) = @_;
+
+       my $lines = Lines->new();
+       open(F, "<", $filename) or die;
+       while (defined(my $line = <F>)) {
+               chomp($line);
+               $lines->add($line);
+       }
+       close(F) or die;
+       return $lines;
+}
+
+sub write_to($@) {
+       my ($lines, $filename) = @_;
+
+       open(F, ">", "$filename.tmp") or die;
+       foreach my $line (@$lines) {
+               print F "$line\n";
+       }
+       close(F) or die;
+       rename("$filename.tmp", $filename) or die;
+}
+
+sub add($@) {
+       my ($lines, @lines) = @_;
+
+       push(@$lines, @lines);
+}
+
 # appends the given variable assignments to the lines, aligning the
 # variable values vertically.
-sub lines_add_vars($$) {
-       my ($lines, $vars) = @_;
+sub add_vars($@) {
+       my ($lines, @vars) = @_;
 
-       return if scalar(@$vars) == 0;
+       return if @vars == 0;
 
        my $width = 0;
-       foreach my $var (@$vars) {
+       foreach my $var (@vars) {
                my ($name, $op, $value) = @$var;
                next if $value eq "";
                my $len = (length("$name$op\t") + 7) & -8;
-               $width = ($len > $width) ? $len : $width;
+               $width = $len if $len > $width;
        }
 
-       foreach my $var (@$vars) {
+       foreach my $var (@vars) {
                my ($name, $op, $value) = @$var;
                next if $value eq "";
                my $tabs = "\t" x (($width - length("$name$op") + 7) / 8);
-               push(@$lines, "$name$op$tabs$value");
+               $lines->add("$name$op$tabs$value");
        }
-       push(@$lines, "");
+
+       $lines->add("");
 }
 
 # changes the value of an existing variable in the lines.
-sub lines_set($$$) {
+sub set($$$) {
        my ($lines, $varname, $new_value) = @_;
 
        my $i = 0;
@@ -139,7 +164,7 @@
 }
 
 # appends to the value of an existing variable in the lines.
-sub lines_append($$$) {
+sub append($$$) {
        my ($lines, $varname, $value) = @_;
 
        return if $value eq "";
@@ -161,7 +186,7 @@
 }
 
 # removes a variable assignment from the lines.
-sub lines_remove($$) {
+sub remove($$) {
        my ($lines, $varname) = @_;
 
        my $i = 0;
@@ -178,7 +203,7 @@
 
 # returns the variable value from the only variable assignment, or an empty
 # string.
-sub lines_get($$) {
+sub get($$) {
        my ($lines, $varname) = @_;
 
        my $only_value = "";
@@ -193,10 +218,9 @@
        return $only_value;
 }
 
-
 # removes a variable assignment from the lines if its value is the
 # expected one.
-sub lines_remove_if($$$) {
+sub remove_if($$$) {
        my ($lines, $varname, $expected_value) = @_;
 
        my $i = 0;
@@ -215,7 +239,7 @@
        return false;
 }
 
-sub lines_index($$) {
+sub index($$) {
        my ($lines, $re) = @_;
 
        foreach my $i (0 .. $#$lines) {
@@ -224,11 +248,9 @@
        return -1;
 }
 
-sub make(@) {
-       my @args = @_;
+1;
 
-       (system { $make } ($make, @args)) == 0 or die;
-}
+package main;
 
 # The following adjust_* subroutines are called after the distfiles have
 # been downloaded and extracted. They inspect the extracted files
@@ -443,11 +465,11 @@
 sub adjust_perl_module_homepage($) {
        my ($url) = @_;
 
-       if (lines_get($makefile_lines, "MASTER_SITES") =~ qr"\$\{MASTER_SITE_PERL_CPAN:") {
-               my $homepage = lines_get($makefile_lines, "HOMEPAGE");
+       if ($makefile_lines->get("MASTER_SITES") =~ qr"\$\{MASTER_SITE_PERL_CPAN:") {
+               my $homepage = $makefile_lines->get("HOMEPAGE");
                if ($homepage ne "" && index($url, $homepage) == 0) {
                        my $module_name = $distname =~ s/-v?[0-9].*//r =~ s/-/::/gr;
-                       lines_set($makefile_lines, "HOMEPAGE", "https://metacpan.org/pod/$module_name";);
+                       $makefile_lines->set("HOMEPAGE", "https://metacpan.org/pod/$module_name";);
                }
        }
 }
@@ -661,11 +683,11 @@
        my $pkgname = "$pkgname_prefix\${DISTNAME$pkgname_transform}";
        $pkgname = "" if $pkgname eq "\${DISTNAME}";
 
-       my @lines;
-       push(@lines, "# \$" . "NetBSD\$");
-       push(@lines, "");
+       my $lines = Lines->new();
+       $lines->add("# \$" . "NetBSD\$");
+       $lines->add("");
 
-       lines_add_vars(\@lines, [
+       $lines->add_vars(
                var("GITHUB_PROJECT", "=", $github_project),
                var("DISTNAME", "=", $distname),
                var("PKGNAME", "=", $pkgname),
@@ -674,28 +696,28 @@
                var("GITHUB_RELEASE", "=", $github_release),
                var("EXTRACT_SUFX", "=", $extract_sufx),
                var("DIST_SUBDIR", "=", $dist_subdir),
-       ]);
+       );
 
-       lines_add_vars(\@lines, [
+       $lines->add_vars(
                var("MAINTAINER", "=", get_maintainer()),
                var("HOMEPAGE", "=", $homepage),
                var("COMMENT", "=", "TODO: Short description of the package"),
                var("#LICENSE", "=", "# TODO: (see mk/license.mk)"),
-       ]);
+       );
 
-       push(@lines, "# url2pkg-marker (please do not remove this line.)");
-       push(@lines, ".include \"../../mk/bsd.pkg.mk\"");
+       $lines->add("# url2pkg-marker (please do not remove this line.)");
+       $lines->add(".include \"../../mk/bsd.pkg.mk\"");
 
-       return @lines;
+       return $lines;
 }
 
 sub generate_initial_package($) {
        my ($url) = @_;
 
        rename("Makefile", "Makefile-url2pkg.bak") or do {};
-       write_lines("Makefile", generate_initial_package_Makefile_lines($url));
-       write_lines("PLIST", "\@comment \$" . "NetBSD\$");
-       write_lines("DESCR", ());
+       generate_initial_package_Makefile_lines($url)->write_to("Makefile");
+       Lines->new("\@comment \$" . "NetBSD\$")->write_to("PLIST");
+       Lines->new()->write_to("DESCR");
        run_editor("Makefile", 5);
 
        make("distinfo");
@@ -705,41 +727,30 @@
 sub adjust_lines_python_module($$) {
        my ($lines, $url) = @_;
 
-       my @initial_lines = generate_initial_package_Makefile_lines($url);
-       my @current_lines = read_lines("Makefile");
-
-       my %old;
-       foreach my $line (@initial_lines) {
-               if ($line =~ qr"^(\w+)(\+?=)([ \t]+)([^#\\]*?)(\s*)(#.*|)$") {
-                       my ($varname, $op, $indent, $value, $space_after_value, $comment) = ($1, $2, $3, $4, $5, $6);
+       my $initial_lines = generate_initial_package_Makefile_lines($url);
+       my $current_lines = Lines->read_from("Makefile");
 
-                       if ($op eq "=") {
-                               $old{$varname} = $value;
-                       }
-               }
-       }
-
-       return unless $old{"CATEGORIES"} =~ qr"python";
-       my $pkgbase = $old{"GITHUB_PROJECT"};
-       return unless defined($pkgbase);
+       return unless $initial_lines->get("CATEGORIES") =~ qr"python";
+       my $pkgbase = $initial_lines->get("GITHUB_PROJECT");
+       return if $pkgbase eq "";
        my $pkgbase1 = substr($pkgbase, 0, 1);
-       my $pkgversion_norev = $old{"DISTNAME"} =~ s/^v//r;
+       my $pkgversion_norev = $initial_lines->get("DISTNAME") =~ s/^v//r;
 
        # don't risk to overwrite any changes made by the package developer.
-       if (join('\n', @current_lines) ne join('\n', @initial_lines)) {
+       if (join('\n', @$current_lines) ne join('\n', @$initial_lines)) {
                splice(@$lines, -2, 0, "# TODO: Migrate MASTER_SITES to PYPI");
                return;
        }
 



Home | Main Index | Thread Index | Old Index