Sun Aug 18 16:09:01 2019 UTC ()
pkgtools/url2pkg: migrate from GitHub to PyPI if necessary

Suggested by gdt@ via private mail.


(rillig)
diff -r1.58 -r1.59 pkgsrc/pkgtools/url2pkg/files/url2pkg.pl

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

--- pkgsrc/pkgtools/url2pkg/files/Attic/url2pkg.pl 2019/08/18 13:49:13 1.58
+++ pkgsrc/pkgtools/url2pkg/files/Attic/url2pkg.pl 2019/08/18 16:09:01 1.59
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1#! @PERL5@ 1#! @PERL5@
2# $NetBSD: url2pkg.pl,v 1.58 2019/08/18 13:49:13 rillig Exp $ 2# $NetBSD: url2pkg.pl,v 1.59 2019/08/18 16:09:01 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.
@@ -84,61 +84,134 @@ sub add_section($$) { @@ -84,61 +84,134 @@ sub add_section($$) {
84 my $len = (length("$name$op\t") + 7) & -8; 84 my $len = (length("$name$op\t") + 7) & -8;
85 $width = ($len > $width) ? $len : $width; 85 $width = ($len > $width) ? $len : $width;
86 } 86 }
87 87
88 foreach my $var (@{$vars}) { 88 foreach my $var (@{$vars}) {
89 my ($name, $op, $value) = @$var; 89 my ($name, $op, $value) = @$var;
90 next if $value eq ""; 90 next if $value eq "";
91 my $tabs = "\t" x (($width - length("$name$op") + 7) / 8); 91 my $tabs = "\t" x (($width - length("$name$op") + 7) / 8);
92 push(@$lines, "$name$op$tabs$value"); 92 push(@$lines, "$name$op$tabs$value");
93 } 93 }
94 push(@$lines, ""); 94 push(@$lines, "");
95} 95}
96 96
 97sub read_lines($) {
 98 my ($filename) = @_;
 99
 100 my @lines;
 101 open(F, "<", $filename) or return @lines;
 102 while (defined(my $line = <F>)) {
 103 chomp($line);
 104 push(@lines, $line);
 105 }
 106 close(F) or die;
 107 return @lines;
 108}
 109
97sub write_lines($@) { 110sub write_lines($@) {
98 my ($filename, @lines) = @_; 111 my ($filename, @lines) = @_;
99 112
100 open(F, ">", $filename) or die; 113 open(F, ">", "$filename.tmp") or die;
101 foreach my $line (@lines) { 114 foreach my $line (@lines) {
102 print F "$line\n"; 115 print F "$line\n";
103 } 116 }
104 close(F) or die; 117 close(F) or die;
 118 rename("$filename.tmp", $filename) or die;
105} 119}
106 120
107sub find_package($) { 121sub find_package($) {
108 my ($pkgbase) = @_; 122 my ($pkgbase) = @_;
109 123
110 my @candidates = <../../*/$pkgbase>; 124 my @candidates = <../../*/$pkgbase>;
111 return scalar(@candidates) == 1 ? $candidates[0] : ""; 125 return scalar(@candidates) == 1 ? $candidates[0] : "";
112} 126}
113 127
 128sub update_var_set($$$) {
 129 my ($lines, $varname, $new_value) = @_;
 130
 131 my $i = 0;
 132 foreach my $line (@$lines) {
 133 if ($line =~ qr"^\Q$varname\E(\+?=)([ \t]+)([^#\\]*?)(\s*)(#.*|)$") {
 134 my ($op, $indent, $old_value, $space_after_value, $comment) = ($1, $2, $3, $4, $5);
 135
 136 $lines->[$i] = "$varname$op$indent$new_value$space_after_value$comment";
 137 return true;
 138 }
 139 $i++;
 140 }
 141
 142 return false;
 143}
 144
114# appends the given value to the variable assignment. 145# appends the given value to the variable assignment.
115sub update_var_append($$$) { 146sub update_var_append($$$) {
116 my ($lines, $varname, $value) = @_; 147 my ($lines, $varname, $value) = @_;
117 148
118 return if $value eq ""; 149 return if $value eq "";
119 150
120 my $i = 0; 151 my $i = 0;
121 foreach my $line (@$lines) { 152 foreach my $line (@$lines) {
122 if ($line =~ qr"^\Q$varname\E(\+?=)([ \t]+)([^#\\]*)(#.*|)$") { 153 if ($line =~ qr"^\Q$varname\E(\+?=)([ \t]+)([^#\\]*)(#.*|)$") {
123 my ($op, $indent, $old_value, $comment) = ($1, $2, $3, $4); 154 my ($op, $indent, $old_value, $comment) = ($1, $2, $3, $4);
124 155
125 my $before = $old_value =~ qr"\S$" ? " " : ""; 156 my $before = $old_value =~ qr"\S$" ? " " : "";
126 my $after = $comment eq "" ? "" : " "; 157 my $after = $comment eq "" ? "" : " ";
127 $lines->[$i] = "$varname$op$indent$old_value$before$value$after$comment"; 158 $lines->[$i] = "$varname$op$indent$old_value$before$value$after$comment";
128 return; 159 return true;
129 } 160 }
130 $i++; 161 $i++;
131 } 162 }
 163
 164 return false;
 165}
 166
 167sub update_var_remove($$) {
 168 my ($lines, $varname) = @_;
 169
 170 my $i = 0;
 171 foreach my $line (@$lines) {
 172 if ($line =~ qr"^\Q$varname\E(\+?=)") {
 173 splice(@$lines, $i, 1);
 174 return true;
 175 }
 176 $i++;
 177 }
 178
 179 return false;
 180}
 181
 182sub update_var_remove_if($$$) {
 183 my ($lines, $varname, $expected_value) = @_;
 184
 185 my $i = 0;
 186 foreach my $line (@$lines) {
 187 if ($line =~ qr"^\Q$varname\E(\+?=)([ \t]+)([^#\\]*?)(\s*)(#.*|)$") {
 188 my ($op, $indent, $old_value, $space_after_value, $comment) = ($1, $2, $3, $4, $5);
 189
 190 if ($old_value eq $expected_value) {
 191 splice(@$lines, $i, 1);
 192 return true;
 193 }
 194 }
 195 $i++;
 196 }
 197
 198 return false;
 199}
 200
 201sub make(@) {
 202 my @args = @_;
 203
 204 (system { $make } ($make, @args)) == 0 or die;
132} 205}
133 206
134# The following adjust_* subroutines are called after the distfiles have 207# The following adjust_* subroutines are called after the distfiles have
135# been downloaded and extracted. They inspect the extracted files 208# been downloaded and extracted. They inspect the extracted files
136# and adjust the variable definitions in the package Makefile. 209# and adjust the variable definitions in the package Makefile.
137 210
138# 211#
139# The following variables may be used in the adjust_* subroutines: 212# The following variables may be used in the adjust_* subroutines:
140# 213#
141 214
142# the package name, including the version number. 215# the package name, including the version number.
143my $distname; 216my $distname;
144 217
@@ -182,26 +255,28 @@ my @includes; @@ -182,26 +255,28 @@ my @includes;
182# takes place. 255# takes place.
183my @build_vars; 256my @build_vars;
184 257
185# similar to the @build_vars, but separated by an empty line in 258# similar to the @build_vars, but separated by an empty line in
186# the Makefile, thereby forming the fifth paragraph. 259# the Makefile, thereby forming the fifth paragraph.
187my @extra_vars; 260my @extra_vars;
188 261
189# these are inserted below the second paragraph in the Makefile. 262# these are inserted below the second paragraph in the Makefile.
190my @todos; 263my @todos;
191 264
192# the package name, in case it differs from $distname. 265# the package name, in case it differs from $distname.
193my $pkgname = ""; 266my $pkgname = "";
194 267
 268my $regenerate_distinfo = false;
 269
195# Example: 270# Example:
196# add_dependency("DEPENDS", "package", ">=1", "../../category/package"); 271# add_dependency("DEPENDS", "package", ">=1", "../../category/package");
197# 272#
198sub add_dependency($$$$) { 273sub add_dependency($$$$) {
199 my ($type, $pkgbase, $constraint, $dep_dir) = @_; 274 my ($type, $pkgbase, $constraint, $dep_dir) = @_;
200 275
201 if ($dep_dir ne "" && -f "$dep_dir/buildlink3.mk") { 276 if ($dep_dir ne "" && -f "$dep_dir/buildlink3.mk") {
202 # TODO: add type to bl3_lines (BUILDLINK_DEPENDS) 277 # TODO: add type to bl3_lines (BUILDLINK_DEPENDS)
203 # TODO: add constraint to bl3_lines (BUILDLINK_API_DEPENDS) 278 # TODO: add constraint to bl3_lines (BUILDLINK_API_DEPENDS)
204 push(@bl3_lines, ".include \"$dep_dir/buildlink3.mk\""); 279 push(@bl3_lines, ".include \"$dep_dir/buildlink3.mk\"");
205 return; 280 return;
206 } 281 }
207 282
@@ -327,26 +402,27 @@ sub adjust_python_module() { @@ -327,26 +402,27 @@ sub adjust_python_module() {
327 foreach my $dep_line (@dep_lines) { 402 foreach my $dep_line (@dep_lines) {
328 my ($type, $pkgbase, $constraint) = @$dep_line; 403 my ($type, $pkgbase, $constraint) = @$dep_line;
329 my $dep_dir = find_package("py-$pkgbase"); 404 my $dep_dir = find_package("py-$pkgbase");
330 if ($dep_dir ne "") { 405 if ($dep_dir ne "") {
331 $pkgbase = "py-$pkgbase"; 406 $pkgbase = "py-$pkgbase";
332 } else { 407 } else {
333 $dep_dir = find_package($pkgbase); 408 $dep_dir = find_package($pkgbase);
334 } 409 }
335 410
336 add_dependency($type, $pkgbase, $constraint, $dep_dir); 411 add_dependency($type, $pkgbase, $constraint, $dep_dir);
337 } 412 }
338 413
339 push(@categories, "python"); 414 push(@categories, "python");
 415 push(@includes, "../../lang/python/egg.mk");
340} 416}
341 417
342sub adjust_cargo() { 418sub adjust_cargo() {
343 open(CONF, "<", "$abs_wrksrc/Cargo.lock") or return; 419 open(CONF, "<", "$abs_wrksrc/Cargo.lock") or return;
344 420
345 while (defined(my $line = <CONF>)) { 421 while (defined(my $line = <CONF>)) {
346 # "checksum cargo-package-name cargo-package-version 422 # "checksum cargo-package-name cargo-package-version
347 if ($line =~ m/("checksum)\s(\S+)\s(\S+)/) { 423 if ($line =~ m/("checksum)\s(\S+)\s(\S+)/) {
348 push(@build_vars, var("CARGO_CRATE_DEPENDS", "=", "$2-$3")); 424 push(@build_vars, var("CARGO_CRATE_DEPENDS", "=", "$2-$3"));
349 } 425 }
350 } 426 }
351 close(CONF); 427 close(CONF);
352 428
@@ -520,43 +596,77 @@ sub generate_initial_package_Makefile_li @@ -520,43 +596,77 @@ sub generate_initial_package_Makefile_li
520 ]); 596 ]);
521 597
522 push(@lines, "# url2pkg-marker (please do not remove this line.)"); 598 push(@lines, "# url2pkg-marker (please do not remove this line.)");
523 push(@lines, ".include \"../../mk/bsd.pkg.mk\""); 599 push(@lines, ".include \"../../mk/bsd.pkg.mk\"");
524 600
525 return @lines; 601 return @lines;
526} 602}
527 603
528sub generate_initial_package($) { 604sub generate_initial_package($) {
529 my ($url) = @_; 605 my ($url) = @_;
530 606
531 rename("Makefile", "Makefile-url2pkg.bak") or do {}; 607 rename("Makefile", "Makefile-url2pkg.bak") or do {};
532 write_lines("Makefile", generate_initial_package_Makefile_lines($url)); 608 write_lines("Makefile", generate_initial_package_Makefile_lines($url));
533 
534 write_lines("PLIST", "\@comment \$" . "NetBSD\$"); 609 write_lines("PLIST", "\@comment \$" . "NetBSD\$");
535 
536 write_lines("DESCR", ()); 610 write_lines("DESCR", ());
537 
538 run_editor("Makefile", 5); 611 run_editor("Makefile", 5);
539 612
540 print ("url2pkg> Running \"make distinfo\" ...\n"); 613 make("distinfo");
541 (system { $make } ($make, "distinfo")) == 0 or die; 614 make("extract");
 615}
 616
 617sub adjust_lines_python_module($$) {
 618 my ($lines, $url) = @_;
542 619
543 print ("url2pkg> Running \"make extract\" ...\n"); 620 my @initial_lines = generate_initial_package_Makefile_lines($url);
544 (system { $make } ($make, "extract")) == 0 or die; 621 my @current_lines = read_lines("Makefile");
 622
 623 # don't risk to overwrite any changes by the package developer.
 624 if (join('\n', @current_lines) ne join('\n', @initial_lines)) {
 625 splice(@$lines, -2, 0, "# TODO: Migrate MASTER_SITES to PYPI");
 626 return;
 627 }
 628
 629 my %old;
 630 foreach my $line (@initial_lines) {
 631 if ($line =~ qr"^(\w+)(\+?=)([ \t]+)([^#\\]*?)(\s*)(#.*|)$") {
 632 my ($varname, $op, $indent, $value, $space_after_value, $comment) = ($1, $2, $3, $4, $5, $6);
 633
 634 if ($op eq "=") {
 635 $old{$varname} = $value;
 636 }
 637 }
 638 }
 639
 640 my $pkgbase = $old{"GITHUB_PROJECT"};
 641 my $pkgbase1 = substr($pkgbase, 0, 1);
 642 my $pkgversion_norev = $old{"DISTNAME"} =~ s/^v//r;
 643
 644 my @tx_lines = @$lines;
 645 if (update_var_remove(\@tx_lines, "GITHUB_PROJECT")
 646 && update_var_set(\@tx_lines, "DISTNAME", "$pkgbase-$pkgversion_norev")
 647 && update_var_set(\@tx_lines, "PKGNAME", "\${PYPKGPREFIX}-\${DISTNAME}")
 648 && update_var_set(\@tx_lines, "MASTER_SITES", "\${MASTER_SITE_PYPI:=$pkgbase1/$pkgbase/}")
 649 && update_var_remove(\@tx_lines, "DIST_SUBDIR")
 650 && (update_var_remove_if(\@tx_lines, "EXTRACT_SUFX", ".zip") || true)) {
 651
 652 @$lines = @tx_lines;
 653 $regenerate_distinfo = true
 654 }
545} 655}
546 656
547sub adjust_package_from_extracted_distfiles() 657sub adjust_package_from_extracted_distfiles($)
548{ 658{
549 my ($seen_marker); 659 my ($url) = @_;
550 660
551 chomp($abs_wrkdir = `$make show-var VARNAME=WRKDIR`); 661 chomp($abs_wrkdir = `$make show-var VARNAME=WRKDIR`);
552 662
553 # 663 #
554 # Determine the value of WRKSRC. 664 # Determine the value of WRKSRC.
555 # 665 #
556 my @files = (); 666 my @files = ();
557 opendir(WRKDIR, $abs_wrkdir) or die; 667 opendir(WRKDIR, $abs_wrkdir) or die;
558 while (defined(my $f = readdir(WRKDIR))) { 668 while (defined(my $f = readdir(WRKDIR))) {
559 no if $] >= 5.018, warnings => "experimental::smartmatch"; 669 no if $] >= 5.018, warnings => "experimental::smartmatch";
560 given ($f) { 670 given ($f) {
561 next when qr"^\."; 671 next when qr"^\.";
562 next when "pax_global_header"; 672 next when "pax_global_header";
@@ -584,44 +694,48 @@ sub adjust_package_from_extracted_distfi @@ -584,44 +694,48 @@ sub adjust_package_from_extracted_distfi
584 adjust_cmake(); 694 adjust_cmake();
585 adjust_meson(); 695 adjust_meson();
586 adjust_gconf2_schemas(); 696 adjust_gconf2_schemas();
587 adjust_libtool(); 697 adjust_libtool();
588 adjust_perl_module(); 698 adjust_perl_module();
589 adjust_python_module(); 699 adjust_python_module();
590 adjust_cargo(); 700 adjust_cargo();
591 adjust_pkg_config(); 701 adjust_pkg_config();
592 adjust_po(); 702 adjust_po();
593 adjust_use_languages(); 703 adjust_use_languages();
594 704
595 print("url2pkg> Adjusting the Makefile\n"); 705 print("url2pkg> Adjusting the Makefile\n");
596 706
 707 my $seen_marker = false;
597 my @lines; 708 my @lines;
598 709
599 open(MF1, "<", "Makefile") or die; 710 open(MF1, "<", "Makefile") or die;
600 711
601 # Copy the user-edited part of the Makefile. 712 # Copy the user-edited part of the Makefile.
602 while (defined(my $line = <MF1>)) { 713 while (defined(my $line = <MF1>)) {
603 chomp($line); 714 chomp($line);
604 715
605 if ($line =~ qr"^# url2pkg-marker\b") { 716 if ($line =~ qr"^# url2pkg-marker\b") {
606 $seen_marker = true; 717 $seen_marker = true;
607 last; 718 last;
608 } 719 }
609 push(@lines, $line); 720 push(@lines, $line);
610 721
611 if ($pkgname ne "" && $line =~ qr"^DISTNAME=(\t+)") { 722 if ($pkgname ne "" && $line =~ qr"^DISTNAME=(\t+)") {
612 push(@lines, "PKGNAME=$1$pkgname"); 723 push(@lines, "PKGNAME=$1$pkgname");
613 } 724 }
614 } 725 }
 726 if (!$seen_marker) {
 727 die("$0: ERROR: didn't find the url2pkg marker in the file.\n");
 728 }
615 729
616 if (@todos) { 730 if (@todos) {
617 foreach my $todo (@todos) { 731 foreach my $todo (@todos) {
618 push(@lines, "# TODO: $todo"); 732 push(@lines, "# TODO: $todo");
619 } 733 }
620 push(@lines, ""); 734 push(@lines, "");
621 } 735 }
622 736
623 my @depend_vars; 737 my @depend_vars;
624 push(@depend_vars, map { var("BUILD_DEPENDS", "+=", $_) } @build_depends); 738 push(@depend_vars, map { var("BUILD_DEPENDS", "+=", $_) } @build_depends);
625 push(@depend_vars, map { var("DEPENDS", "+=", $_) } @depends); 739 push(@depend_vars, map { var("DEPENDS", "+=", $_) } @depends);
626 push(@depend_vars, map { var("TEST_DEPENDS", "+=", $_) } @test_depends); 740 push(@depend_vars, map { var("TEST_DEPENDS", "+=", $_) } @test_depends);
627 add_section(\@lines, \@depend_vars); 741 add_section(\@lines, \@depend_vars);
@@ -632,33 +746,32 @@ sub adjust_package_from_extracted_distfi @@ -632,33 +746,32 @@ sub adjust_package_from_extracted_distfi
632 push(@lines, @bl3_lines); 746 push(@lines, @bl3_lines);
633 push(@lines, map { $_ = ".include \"$_\"" } @includes); 747 push(@lines, map { $_ = ".include \"$_\"" } @includes);
634 748
635 # Copy the rest of the user-edited part of the Makefile. 749 # Copy the rest of the user-edited part of the Makefile.
636 while (defined(my $line = <MF1>)) { 750 while (defined(my $line = <MF1>)) {
637 chomp($line); 751 chomp($line);
638 push(@lines, $line); 752 push(@lines, $line);
639 } 753 }
640 754
641 close(MF1); 755 close(MF1);
642 756
643 update_var_append(\@lines, "CATEGORIES", join(" ", @categories)); 757 update_var_append(\@lines, "CATEGORIES", join(" ", @categories));
644 758
645 write_lines("Makefile-url2pkg.new", @lines); 759 adjust_lines_python_module(\@lines, $url);
646 760
647 if ($seen_marker) { 761 write_lines("Makefile", @lines);
648 rename("Makefile-url2pkg.new", "Makefile") or die; 762
649 } else { 763 if ($regenerate_distinfo) {
650 unlink("Makefile-url2pkg.new"); 764 make("distinfo");
651 die("$0: ERROR: didn't find the url2pkg marker in the file.\n"); 
652 } 765 }
653} 766}
654 767
655sub main() { 768sub main() {
656 my $url; 769 my $url;
657 770
658 if (!-f "../../mk/bsd.pkg.mk") { 771 if (!-f "../../mk/bsd.pkg.mk") {
659 die("ERROR: $0 must be run from a package directory (.../pkgsrc/category/package).\n"); 772 die("ERROR: $0 must be run from a package directory (.../pkgsrc/category/package).\n");
660 } 773 }
661 774
662 my @extract_cookie = <w*/.extract_done>; 775 my @extract_cookie = <w*/.extract_done>;
663 if (scalar(@extract_cookie) == 0) { 776 if (scalar(@extract_cookie) == 0) {
664 if (scalar(@ARGV) == 0) { 777 if (scalar(@ARGV) == 0) {
@@ -667,22 +780,22 @@ sub main() { @@ -667,22 +780,22 @@ sub main() {
667 print("\n"); 780 print("\n");
668 exit(0); 781 exit(0);
669 } 782 }
670 chomp($url); 783 chomp($url);
671 } else { 784 } else {
672 $url = shift(@ARGV); 785 $url = shift(@ARGV);
673 } 786 }
674 787
675 generate_initial_package($url); 788 generate_initial_package($url);
676 } else { 789 } else {
677 chomp($distname = `$make show-var VARNAME=DISTNAME`); 790 chomp($distname = `$make show-var VARNAME=DISTNAME`);
678 } 791 }
679 792
680 adjust_package_from_extracted_distfiles(); 793 adjust_package_from_extracted_distfiles($url);
681 794
682 print("\n"); 795 print("\n");
683 print("Remember to run pkglint when you're done.\n"); 796 print("Remember to run pkglint when you're done.\n");
684 print("See ../../doc/pkgsrc.txt to get some help.\n"); 797 print("See ../../doc/pkgsrc.txt to get some help.\n");
685 print("\n"); 798 print("\n");
686} 799}
687 800
688main() unless caller(); 801main() unless caller();