Sun Aug 18 16:18:04 2019 UTC ()
pkgtools/url2pkg: rename helper functions


(rillig)
diff -r1.59 -r1.60 pkgsrc/pkgtools/url2pkg/files/url2pkg.pl
diff -r1.3 -r1.4 pkgsrc/pkgtools/url2pkg/files/url2pkg.t

cvs diff -r1.59 -r1.60 pkgsrc/pkgtools/url2pkg/files/Attic/url2pkg.pl (expand / switch to unified diff)

--- pkgsrc/pkgtools/url2pkg/files/Attic/url2pkg.pl 2019/08/18 16:09:01 1.59
+++ pkgsrc/pkgtools/url2pkg/files/Attic/url2pkg.pl 2019/08/18 16:18:04 1.60
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1#! @PERL5@ 1#! @PERL5@
2# $NetBSD: url2pkg.pl,v 1.59 2019/08/18 16:09:01 rillig Exp $ 2# $NetBSD: url2pkg.pl,v 1.60 2019/08/18 16:18:04 rillig Exp $
3# 3#
4 4
5# Copyright (c) 2010 The NetBSD Foundation, Inc. 5# Copyright (c) 2010 The NetBSD Foundation, Inc.
6# All rights reserved. 6# All rights reserved.
7# 7#
8# This code is derived from software contributed to The NetBSD Foundation 8# This code is derived from software contributed to The NetBSD Foundation
9# by Roland Illig. 9# by Roland Illig.
10# 10#
11# Redistribution and use in source and binary forms, with or without 11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions 12# modification, are permitted provided that the following conditions
13# are met: 13# are met:
14# 1. Redistributions of source code must retain the above copyright 14# 1. Redistributions of source code must retain the above copyright
15# notice, this list of conditions and the following disclaimer. 15# notice, this list of conditions and the following disclaimer.
@@ -115,81 +115,81 @@ sub write_lines($@) { @@ -115,81 +115,81 @@ sub write_lines($@) {
115 print F "$line\n"; 115 print F "$line\n";
116 } 116 }
117 close(F) or die; 117 close(F) or die;
118 rename("$filename.tmp", $filename) or die; 118 rename("$filename.tmp", $filename) or die;
119} 119}
120 120
121sub find_package($) { 121sub find_package($) {
122 my ($pkgbase) = @_; 122 my ($pkgbase) = @_;
123 123
124 my @candidates = <../../*/$pkgbase>; 124 my @candidates = <../../*/$pkgbase>;
125 return scalar(@candidates) == 1 ? $candidates[0] : ""; 125 return scalar(@candidates) == 1 ? $candidates[0] : "";
126} 126}
127 127
128sub update_var_set($$$) { 128sub lines_set($$$) {
129 my ($lines, $varname, $new_value) = @_; 129 my ($lines, $varname, $new_value) = @_;
130 130
131 my $i = 0; 131 my $i = 0;
132 foreach my $line (@$lines) { 132 foreach my $line (@$lines) {
133 if ($line =~ qr"^\Q$varname\E(\+?=)([ \t]+)([^#\\]*?)(\s*)(#.*|)$") { 133 if ($line =~ qr"^\Q$varname\E(\+?=)([ \t]+)([^#\\]*?)(\s*)(#.*|)$") {
134 my ($op, $indent, $old_value, $space_after_value, $comment) = ($1, $2, $3, $4, $5); 134 my ($op, $indent, $old_value, $space_after_value, $comment) = ($1, $2, $3, $4, $5);
135 135
136 $lines->[$i] = "$varname$op$indent$new_value$space_after_value$comment"; 136 $lines->[$i] = "$varname$op$indent$new_value$space_after_value$comment";
137 return true; 137 return true;
138 } 138 }
139 $i++; 139 $i++;
140 } 140 }
141 141
142 return false; 142 return false;
143} 143}
144 144
145# appends the given value to the variable assignment. 145# appends the given value to the variable assignment.
146sub update_var_append($$$) { 146sub lines_append($$$) {
147 my ($lines, $varname, $value) = @_; 147 my ($lines, $varname, $value) = @_;
148 148
149 return if $value eq ""; 149 return if $value eq "";
150 150
151 my $i = 0; 151 my $i = 0;
152 foreach my $line (@$lines) { 152 foreach my $line (@$lines) {
153 if ($line =~ qr"^\Q$varname\E(\+?=)([ \t]+)([^#\\]*)(#.*|)$") { 153 if ($line =~ qr"^\Q$varname\E(\+?=)([ \t]+)([^#\\]*)(#.*|)$") {
154 my ($op, $indent, $old_value, $comment) = ($1, $2, $3, $4); 154 my ($op, $indent, $old_value, $comment) = ($1, $2, $3, $4);
155 155
156 my $before = $old_value =~ qr"\S$" ? " " : ""; 156 my $before = $old_value =~ qr"\S$" ? " " : "";
157 my $after = $comment eq "" ? "" : " "; 157 my $after = $comment eq "" ? "" : " ";
158 $lines->[$i] = "$varname$op$indent$old_value$before$value$after$comment"; 158 $lines->[$i] = "$varname$op$indent$old_value$before$value$after$comment";
159 return true; 159 return true;
160 } 160 }
161 $i++; 161 $i++;
162 } 162 }
163 163
164 return false; 164 return false;
165} 165}
166 166
167sub update_var_remove($$) { 167sub lines_remove($$) {
168 my ($lines, $varname) = @_; 168 my ($lines, $varname) = @_;
169 169
170 my $i = 0; 170 my $i = 0;
171 foreach my $line (@$lines) { 171 foreach my $line (@$lines) {
172 if ($line =~ qr"^\Q$varname\E(\+?=)") { 172 if ($line =~ qr"^\Q$varname\E(\+?=)") {
173 splice(@$lines, $i, 1); 173 splice(@$lines, $i, 1);
174 return true; 174 return true;
175 } 175 }
176 $i++; 176 $i++;
177 } 177 }
178 178
179 return false; 179 return false;
180} 180}
181 181
182sub update_var_remove_if($$$) { 182sub lines_remove_if($$$) {
183 my ($lines, $varname, $expected_value) = @_; 183 my ($lines, $varname, $expected_value) = @_;
184 184
185 my $i = 0; 185 my $i = 0;
186 foreach my $line (@$lines) { 186 foreach my $line (@$lines) {
187 if ($line =~ qr"^\Q$varname\E(\+?=)([ \t]+)([^#\\]*?)(\s*)(#.*|)$") { 187 if ($line =~ qr"^\Q$varname\E(\+?=)([ \t]+)([^#\\]*?)(\s*)(#.*|)$") {
188 my ($op, $indent, $old_value, $space_after_value, $comment) = ($1, $2, $3, $4, $5); 188 my ($op, $indent, $old_value, $space_after_value, $comment) = ($1, $2, $3, $4, $5);
189 189
190 if ($old_value eq $expected_value) { 190 if ($old_value eq $expected_value) {
191 splice(@$lines, $i, 1); 191 splice(@$lines, $i, 1);
192 return true; 192 return true;
193 } 193 }
194 } 194 }
195 $i++; 195 $i++;
@@ -632,32 +632,32 @@ sub adjust_lines_python_module($$) { @@ -632,32 +632,32 @@ sub adjust_lines_python_module($$) {
632 my ($varname, $op, $indent, $value, $space_after_value, $comment) = ($1, $2, $3, $4, $5, $6); 632 my ($varname, $op, $indent, $value, $space_after_value, $comment) = ($1, $2, $3, $4, $5, $6);
633 633
634 if ($op eq "=") { 634 if ($op eq "=") {
635 $old{$varname} = $value; 635 $old{$varname} = $value;
636 } 636 }
637 } 637 }
638 } 638 }
639 639
640 my $pkgbase = $old{"GITHUB_PROJECT"}; 640 my $pkgbase = $old{"GITHUB_PROJECT"};
641 my $pkgbase1 = substr($pkgbase, 0, 1); 641 my $pkgbase1 = substr($pkgbase, 0, 1);
642 my $pkgversion_norev = $old{"DISTNAME"} =~ s/^v//r; 642 my $pkgversion_norev = $old{"DISTNAME"} =~ s/^v//r;
643 643
644 my @tx_lines = @$lines; 644 my @tx_lines = @$lines;
645 if (update_var_remove(\@tx_lines, "GITHUB_PROJECT") 645 if (lines_remove(\@tx_lines, "GITHUB_PROJECT")
646 && update_var_set(\@tx_lines, "DISTNAME", "$pkgbase-$pkgversion_norev") 646 && lines_set(\@tx_lines, "DISTNAME", "$pkgbase-$pkgversion_norev")
647 && update_var_set(\@tx_lines, "PKGNAME", "\${PYPKGPREFIX}-\${DISTNAME}") 647 && lines_set(\@tx_lines, "PKGNAME", "\${PYPKGPREFIX}-\${DISTNAME}")
648 && update_var_set(\@tx_lines, "MASTER_SITES", "\${MASTER_SITE_PYPI:=$pkgbase1/$pkgbase/}") 648 && lines_set(\@tx_lines, "MASTER_SITES", "\${MASTER_SITE_PYPI:=$pkgbase1/$pkgbase/}")
649 && update_var_remove(\@tx_lines, "DIST_SUBDIR") 649 && lines_remove(\@tx_lines, "DIST_SUBDIR")
650 && (update_var_remove_if(\@tx_lines, "EXTRACT_SUFX", ".zip") || true)) { 650 && (lines_remove_if(\@tx_lines, "EXTRACT_SUFX", ".zip") || true)) {
651 651
652 @$lines = @tx_lines; 652 @$lines = @tx_lines;
653 $regenerate_distinfo = true 653 $regenerate_distinfo = true
654 } 654 }
655} 655}
656 656
657sub adjust_package_from_extracted_distfiles($) 657sub adjust_package_from_extracted_distfiles($)
658{ 658{
659 my ($url) = @_; 659 my ($url) = @_;
660 660
661 chomp($abs_wrkdir = `$make show-var VARNAME=WRKDIR`); 661 chomp($abs_wrkdir = `$make show-var VARNAME=WRKDIR`);
662 662
663 # 663 #
@@ -744,27 +744,27 @@ sub adjust_package_from_extracted_distfi @@ -744,27 +744,27 @@ sub adjust_package_from_extracted_distfi
744 add_section(\@lines, \@extra_vars); 744 add_section(\@lines, \@extra_vars);
745 745
746 push(@lines, @bl3_lines); 746 push(@lines, @bl3_lines);
747 push(@lines, map { $_ = ".include \"$_\"" } @includes); 747 push(@lines, map { $_ = ".include \"$_\"" } @includes);
748 748
749 # Copy the rest of the user-edited part of the Makefile. 749 # Copy the rest of the user-edited part of the Makefile.
750 while (defined(my $line = <MF1>)) { 750 while (defined(my $line = <MF1>)) {
751 chomp($line); 751 chomp($line);
752 push(@lines, $line); 752 push(@lines, $line);
753 } 753 }
754 754
755 close(MF1); 755 close(MF1);
756 756
757 update_var_append(\@lines, "CATEGORIES", join(" ", @categories)); 757 lines_append(\@lines, "CATEGORIES", join(" ", @categories));
758 758
759 adjust_lines_python_module(\@lines, $url); 759 adjust_lines_python_module(\@lines, $url);
760 760
761 write_lines("Makefile", @lines); 761 write_lines("Makefile", @lines);
762 762
763 if ($regenerate_distinfo) { 763 if ($regenerate_distinfo) {
764 make("distinfo"); 764 make("distinfo");
765 } 765 }
766} 766}
767 767
768sub main() { 768sub main() {
769 my $url; 769 my $url;
770 770

cvs diff -r1.3 -r1.4 pkgsrc/pkgtools/url2pkg/files/Attic/url2pkg.t (expand / switch to unified diff)

--- pkgsrc/pkgtools/url2pkg/files/Attic/url2pkg.t 2019/08/18 13:59:38 1.3
+++ pkgsrc/pkgtools/url2pkg/files/Attic/url2pkg.t 2019/08/18 16:18:04 1.4
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1# -*- perl -*- 1# -*- perl -*-
2# $NetBSD: url2pkg.t,v 1.3 2019/08/18 13:59:38 rillig Exp $ 2# $NetBSD: url2pkg.t,v 1.4 2019/08/18 16:18:04 rillig Exp $
3 3
4require "url2pkg.pl"; 4require "url2pkg.pl";
5 5
6use Test::More; 6use Test::More;
7 7
8use strict; 8use strict;
9use warnings; 9use warnings;
10 10
11sub test_add_section__simple() { 11sub test_add_section__simple() {
12 my $lines = []; 12 my $lines = [];
13 13
14 add_section($lines, [ 14 add_section($lines, [
15 var("1", "=", "one"), 15 var("1", "=", "one"),
@@ -41,54 +41,54 @@ sub test_add_section__alignment() { @@ -41,54 +41,54 @@ sub test_add_section__alignment() {
41sub test_add_section__operators() { 41sub test_add_section__operators() {
42 my $lines = []; 42 my $lines = [];
43 43
44 add_section($lines, [ 44 add_section($lines, [
45 var("123456", "+=", "value"), 45 var("123456", "+=", "value"),
46 ]); 46 ]);
47 47
48 is_deeply($lines, [ 48 is_deeply($lines, [
49 "123456+=\tvalue", 49 "123456+=\tvalue",
50 "", 50 "",
51 ]); 51 ]);
52} 52}
53 53
54sub test_var_append__not_found() { 54sub test_lines_append__not_found() {
55 my $lines = []; 55 my $lines = [];
56 56
57 update_var_append($lines, "VARNAME", "value"); 57 lines_append($lines, "VARNAME", "value");
58 58
59 is_deeply($lines, []); 59 is_deeply($lines, []);
60} 60}
61 61
62sub test_var_append__only_comment() { 62sub test_lines_append__only_comment() {
63 my $lines = ["VARNAME=\t\t\t# none"]; 63 my $lines = ["VARNAME=\t\t\t# none"];
64 64
65 update_var_append($lines, "VARNAME", "value"); 65 lines_append($lines, "VARNAME", "value");
66 66
67 is_deeply($lines, ["VARNAME=\t\t\tvalue # none"]); 67 is_deeply($lines, ["VARNAME=\t\t\tvalue # none"]);
68} 68}
69 69
70sub test_var_append__value_with_comment() { 70sub test_lines_append__value_with_comment() {
71 my $lines = ["VARNAME=\tvalue # comment"]; 71 my $lines = ["VARNAME=\tvalue # comment"];
72 72
73 update_var_append($lines, "VARNAME", "appended"); 73 lines_append($lines, "VARNAME", "appended");
74 74
75 is_deeply($lines, ["VARNAME=\tvalue appended # comment"]); 75 is_deeply($lines, ["VARNAME=\tvalue appended # comment"]);
76} 76}
77 77
78sub test_var_append__value_without_comment() { 78sub test_lines_append__value_without_comment() {
79 my $lines = ["VARNAME+=\tvalue"]; 79 my $lines = ["VARNAME+=\tvalue"];
80 80
81 update_var_append($lines, "VARNAME", "appended"); 81 lines_append($lines, "VARNAME", "appended");
82 82
83 is_deeply($lines, ["VARNAME+=\tvalue appended"]); 83 is_deeply($lines, ["VARNAME+=\tvalue appended"]);
84} 84}
85 85
86sub test_generate_initial_package_Makefile_lines__GitHub() { 86sub test_generate_initial_package_Makefile_lines__GitHub() {
87 my $url = "https://github.com/org/proj/archive/v1.0.0.tar.gz"; 87 my $url = "https://github.com/org/proj/archive/v1.0.0.tar.gz";
88 88
89 my @lines = generate_initial_package_Makefile_lines($url); 89 my @lines = generate_initial_package_Makefile_lines($url);
90 90
91 is_deeply(\@lines, [ 91 is_deeply(\@lines, [
92 "# \$" . "NetBSD\$", 92 "# \$" . "NetBSD\$",
93 "", 93 "",
94 "GITHUB_PROJECT=\tproj", 94 "GITHUB_PROJECT=\tproj",
@@ -104,23 +104,23 @@ sub test_generate_initial_package_Makefi @@ -104,23 +104,23 @@ sub test_generate_initial_package_Makefi
104 "", 104 "",
105 "# url2pkg-marker (please do not remove this line.)", 105 "# url2pkg-marker (please do not remove this line.)",
106 ".include \"../../mk/bsd.pkg.mk\"" 106 ".include \"../../mk/bsd.pkg.mk\""
107 ]); 107 ]);
108} 108}
109 109
110sub test_all() { 110sub test_all() {
111 my $pkgsrcdir = $ENV{"PKGSRCDIR"} or die; 111 my $pkgsrcdir = $ENV{"PKGSRCDIR"} or die;
112 chdir("$pkgsrcdir/pkgtools/url2pkg") or die; 112 chdir("$pkgsrcdir/pkgtools/url2pkg") or die;
113 113
114 test_add_section__simple(); 114 test_add_section__simple();
115 test_add_section__alignment(); 115 test_add_section__alignment();
116 test_add_section__operators(); 116 test_add_section__operators();
117 test_var_append__not_found(); 117 test_lines_append__not_found();
118 test_var_append__only_comment(); 118 test_lines_append__only_comment();
119 test_var_append__value_with_comment(); 119 test_lines_append__value_with_comment();
120 test_var_append__value_without_comment(); 120 test_lines_append__value_without_comment();
121 test_generate_initial_package_Makefile_lines__GitHub(); 121 test_generate_initial_package_Makefile_lines__GitHub();
122 122
123 done_testing(); 123 done_testing();
124} 124}
125 125
126test_all(); 126test_all();