Tue Aug 16 19:07:53 2022 UTC ()
lintpkgsrc: split remove_distfiles into manageable pieces


(rillig)
diff -r1.99 -r1.100 pkgsrc/pkgtools/lintpkgsrc/files/lintpkgsrc.pl

cvs diff -r1.99 -r1.100 pkgsrc/pkgtools/lintpkgsrc/files/lintpkgsrc.pl (expand / switch to unified diff)

--- pkgsrc/pkgtools/lintpkgsrc/files/lintpkgsrc.pl 2022/08/16 18:58:00 1.99
+++ pkgsrc/pkgtools/lintpkgsrc/files/lintpkgsrc.pl 2022/08/16 19:07:53 1.100
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1#!@PERL5@ 1#!@PERL5@
2# $NetBSD: lintpkgsrc.pl,v 1.99 2022/08/16 18:58:00 rillig Exp $ 2# $NetBSD: lintpkgsrc.pl,v 1.100 2022/08/16 19:07:53 rillig Exp $
3 3
4# Written by David Brownlee <abs@netbsd.org>. 4# Written by David Brownlee <abs@netbsd.org>.
5# 5#
6# Caveats: 6# Caveats:
7# The 'Makefile parsing' algorithm used to obtain package versions and 7# The 'Makefile parsing' algorithm used to obtain package versions and
8# DEPENDS information is geared towards speed rather than perfection, 8# DEPENDS information is geared towards speed rather than perfection,
9# though it has gotten somewhat better over time, it only parses the 9# though it has gotten somewhat better over time, it only parses the
10# simpler Makefile conditionals. 10# simpler Makefile conditionals.
11# 11#
12# TODO: Handle fun DEPENDS like avifile-devel with 12# TODO: Handle fun DEPENDS like avifile-devel with
13# {qt2-designer>=2.2.4,qt2-designer-kde>=2.3.1nb1} 13# {qt2-designer>=2.2.4,qt2-designer-kde>=2.3.1nb1}
14 14
15use v5.36; 15use v5.36;
@@ -1375,112 +1375,121 @@ sub debug_parse_makefiles(@args) { @@ -1375,112 +1375,121 @@ sub debug_parse_makefiles(@args) {
1375} 1375}
1376 1376
1377sub check_distfiles($pkgsrcdir, $pkgdistdir) { 1377sub check_distfiles($pkgsrcdir, $pkgdistdir) {
1378 my @unref_distfiles = scan_pkgsrc_distfiles_vs_distinfo( 1378 my @unref_distfiles = scan_pkgsrc_distfiles_vs_distinfo(
1379 $pkgsrcdir, $pkgdistdir, $opt{o}, $opt{m}); 1379 $pkgsrcdir, $pkgdistdir, $opt{o}, $opt{m});
1380 1380
1381 return unless $opt{r}; 1381 return unless $opt{r};
1382 verbose("Unlinking unreferenced distfiles\n"); 1382 verbose("Unlinking unreferenced distfiles\n");
1383 foreach my $distfile (@unref_distfiles) { 1383 foreach my $distfile (@unref_distfiles) {
1384 unlink("$pkgdistdir/$distfile"); 1384 unlink("$pkgdistdir/$distfile");
1385 } 1385 }
1386} 1386}
1387 1387
 1388# looking for files that are downloaded on the current system
 1389# but do not belong to any currently installed package i.e. orphaned
 1390sub remove_orphaned_distfiles($dldistfiles, $pkgdistfiles, $pkgdistdir) {
 1391 my $found = 0;
 1392 my @orphan;
 1393 foreach my $dldf (@$dldistfiles) {
 1394 foreach my $pkgdf (@$pkgdistfiles) {
 1395 if ($dldf eq $pkgdf) {
 1396 $found = 1;
 1397 }
 1398 }
 1399 if ($found != 1) {
 1400 push @orphan, $dldf;
 1401 print "Orphaned file: $dldf\n";
 1402 }
 1403 $found = 0;
 1404 }
 1405
 1406 if ($opt{r}) {
 1407 chdir_or_fail("$pkgdistdir");
 1408 verbose("Unlinking 'orphaned' distfiles\n");
 1409 foreach my $distfile (@orphan) {
 1410 unlink($distfile)
 1411 }
 1412 }
 1413}
 1414
 1415# looking for files that are downloaded on the current system
 1416# but belong to a currently installed package i.e. parented
 1417sub remove_parented_distfiles($dldistfiles, $pkgdistfiles, $pkgdistdir) {
 1418 my $found = 0;
 1419 my @parent;
 1420 foreach my $pkgdf (sort @$pkgdistfiles) {
 1421 foreach my $dldf (@$dldistfiles) {
 1422 if ($pkgdf eq $dldf) {
 1423 $found = 1;
 1424 }
 1425 }
 1426 if ($found == 1) {
 1427 push @parent, $pkgdf;
 1428 print "Parented file: $pkgdf\n";
 1429 }
 1430 $found = 0;
 1431 }
 1432
 1433 if ($opt{r}) {
 1434 chdir_or_fail("$pkgdistdir");
 1435 verbose("Unlinking 'parented' distfiles\n");
 1436 foreach my $distfile (@parent) {
 1437 unlink($distfile);
 1438 }
 1439 }
 1440}
 1441
1388sub remove_distfiles($pkgsrcdir, $pkgdistdir) { 1442sub remove_distfiles($pkgsrcdir, $pkgdistdir) {
1389 my @pkgs = list_installed_packages(); 1443 my @installed_pkgnames = list_installed_packages();
1390 scan_pkgsrc_makefiles($pkgsrcdir); 1444 scan_pkgsrc_makefiles($pkgsrcdir);
1391 1445
1392 # list the installed packages and the directory they live in 1446 # list the installed packages and the directory they live in
1393 my @installed; 1447 my @installed_pkgvers;
1394 foreach my $pkgname (sort @pkgs) { 1448 foreach my $pkgname (sort @installed_pkgnames) {
1395 if ($pkgname =~ /^([^*?[]+)-([\d*?[].*)/) { 1449 if ($pkgname !~ /^ ([^*?[]+) - ([\d*?[].*) /x) {
1396 foreach my $pkgver ($pkgdb->pkgvers_by_pkgbase($1)) { 1450 warn "Invalid installed package name: $pkgname";
1397 next if $pkgver->var('dir') =~ /-current/; 1451 next;
1398 push @installed, $pkgver; 1452 }
1399 last; 1453
1400 } 1454 foreach my $pkgver ($pkgdb->pkgvers_by_pkgbase($1)) {
 1455 next if $pkgver->var('dir') =~ /-current/;
 1456 push @installed_pkgvers, $pkgver;
 1457 last;
1401 } 1458 }
1402 } 1459 }
1403 1460
1404 # distfiles belonging to the currently installed packages 1461 # distfiles belonging to the currently installed packages
1405 my (%distfiles, @pkgdistfiles); 1462 my (%distfiles, @pkgdistfiles);
1406 foreach my $pkgver (sort @installed) { 1463 foreach my $pkgver (sort @installed_pkgvers) {
1407 my $pkgpath = $pkgver->var('dir'); 1464 my $pkgpath = $pkgver->var('dir');
1408 foreach my $entry (load_distinfo("$pkgsrcdir/$pkgpath")) { 1465 foreach my $entry (load_distinfo("$pkgsrcdir/$pkgpath")) {
1409 my $distfile = $entry->{distfile}; 1466 my $distfile = $entry->{distfile};
1410 next if $distfile =~ /^patch-[\w.+\-]+$/; 1467 next if $distfile =~ /^patch-[\w.+\-]+$/;
1411 next if defined $distfiles{$distfile}; 1468 next if defined $distfiles{$distfile};
1412 $distfiles{$distfile}->{name} = $distfile; 1469 $distfiles{$distfile}->{name} = $distfile;
1413 push @pkgdistfiles, $distfile; 1470 push @pkgdistfiles, $distfile;
1414 } 1471 }
1415 } 1472 }
1416 1473
1417 # distfiles downloaded on the current system 1474 # distfiles downloaded on the current system
1418 my @dldistfiles = sort grep { $_ ne 'pkg-vulnerabilities' } 1475 my @dldistfiles = sort grep { $_ ne 'pkg-vulnerabilities' }
1419 listdir("$pkgdistdir", undef); 1476 listdir("$pkgdistdir", undef);
1420 1477
1421 if ($opt{y}) { 1478 $opt{y} and remove_orphaned_distfiles(
1422 # looking for files that are downloaded on the current system 1479 \@dldistfiles, \@pkgdistfiles, $pkgdistdir);
1423 # but do not belong to any currently installed package i.e. orphaned 
1424 my $found = 0; 
1425 my @orphan; 
1426 foreach my $dldf (@dldistfiles) { 
1427 foreach my $pkgdf (@pkgdistfiles) { 
1428 if ($dldf eq $pkgdf) { 
1429 $found = 1; 
1430 } 
1431 } 
1432 if ($found != 1) { 
1433 push @orphan, $dldf; 
1434 print "Orphaned file: $dldf\n"; 
1435 } 
1436 $found = 0; 
1437 } 
1438 1480
1439 if ($opt{r}) { 1481 $opt{z} and remove_parented_distfiles(
1440 chdir_or_fail("$pkgdistdir"); 1482 \@dldistfiles, \@pkgdistfiles, $pkgdistdir);
1441 verbose("Unlinking 'orphaned' distfiles\n"); 
1442 foreach my $distfile (@orphan) { 
1443 unlink($distfile) 
1444 } 
1445 } 
1446 } 
1447 
1448 if ($opt{z}) { 
1449 # looking for files that are downloaded on the current system 
1450 # but belong to a currently installed package i.e. parented 
1451 my $found = 0; 
1452 my @parent; 
1453 foreach my $pkgdf (sort @pkgdistfiles) { 
1454 foreach my $dldf (@dldistfiles) { 
1455 if ($pkgdf eq $dldf) { 
1456 $found = 1; 
1457 } 
1458 } 
1459 if ($found == 1) { 
1460 push @parent, $pkgdf; 
1461 print "Parented file: $pkgdf\n"; 
1462 } 
1463 $found = 0; 
1464 } 
1465 
1466 if ($opt{r}) { 
1467 chdir_or_fail("$pkgdistdir"); 
1468 verbose("Unlinking 'parented' distfiles\n"); 
1469 foreach my $distfile (@parent) { 
1470 unlink($distfile); 
1471 } 
1472 } 
1473 } 
1474} 1483}
1475 1484
1476sub list_broken_packages($pkgsrcdir) { 1485sub list_broken_packages($pkgsrcdir) {
1477 scan_pkgsrc_makefiles($pkgsrcdir); 1486 scan_pkgsrc_makefiles($pkgsrcdir);
1478 foreach my $pkgver ($pkgdb->pkgvers_all) { 1487 foreach my $pkgver ($pkgdb->pkgvers_all) {
1479 my $broken = $pkgver->var('BROKEN'); 1488 my $broken = $pkgver->var('BROKEN');
1480 next unless $broken; 1489 next unless $broken;
1481 print $pkgver->pkgname . ": $broken\n"; 1490 print $pkgver->pkgname . ": $broken\n";
1482 } 1491 }
1483} 1492}
1484 1493
1485# List obsolete or NO_BIN_ON_FTP/RESTRICTED prebuilt packages 1494# List obsolete or NO_BIN_ON_FTP/RESTRICTED prebuilt packages
1486# 1495#