2018-06-17 01:25:59 +02:00
|
|
|
|
#! /usr/bin/env nix-shell
|
|
|
|
|
#! nix-shell -i perl -p perl perlPackages.XMLSimple
|
2013-07-25 23:50:14 +02:00
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
use List::Util qw(min);
|
|
|
|
|
use XML::Simple qw(:strict);
|
2013-07-30 11:45:54 +02:00
|
|
|
|
use Getopt::Long qw(:config gnu_getopt);
|
2013-07-25 23:50:14 +02:00
|
|
|
|
|
2013-07-30 11:45:54 +02:00
|
|
|
|
# Parse the command line.
|
|
|
|
|
my $path = "<nixpkgs>";
|
2013-07-25 23:50:14 +02:00
|
|
|
|
my $filter = "*";
|
2013-07-30 11:45:54 +02:00
|
|
|
|
my $maintainer;
|
2013-07-25 23:50:14 +02:00
|
|
|
|
|
2013-07-30 11:45:54 +02:00
|
|
|
|
sub showHelp {
|
|
|
|
|
print <<EOF;
|
|
|
|
|
Usage: $0 [--package=NAME] [--maintainer=REGEXP] [--file=PATH]
|
2013-07-25 23:50:14 +02:00
|
|
|
|
|
2013-07-30 11:45:54 +02:00
|
|
|
|
Check Nixpkgs for common errors/problems.
|
2013-07-25 23:50:14 +02:00
|
|
|
|
|
2013-07-30 11:45:54 +02:00
|
|
|
|
-p, --package filter packages by name (default is ‘*’)
|
|
|
|
|
-m, --maintainer filter packages by maintainer (case-insensitive regexp)
|
|
|
|
|
-f, --file path to Nixpkgs (default is ‘<nixpkgs>’)
|
2013-07-25 23:50:14 +02:00
|
|
|
|
|
2013-07-30 11:45:54 +02:00
|
|
|
|
Examples:
|
|
|
|
|
\$ nixpkgs-lint -f /my/nixpkgs -p firefox
|
|
|
|
|
\$ nixpkgs-lint -f /my/nixpkgs -m eelco
|
|
|
|
|
EOF
|
|
|
|
|
exit 0;
|
2013-07-25 23:50:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-30 11:45:54 +02:00
|
|
|
|
GetOptions("package|p=s" => \$filter,
|
|
|
|
|
"maintainer|m=s" => \$maintainer,
|
|
|
|
|
"file|f=s" => \$path,
|
|
|
|
|
"help" => sub { showHelp() }
|
2014-03-31 19:17:17 +02:00
|
|
|
|
) or exit 1;
|
2013-07-30 11:45:54 +02:00
|
|
|
|
|
|
|
|
|
# Evaluate Nixpkgs into an XML representation.
|
2020-08-02 16:56:22 +02:00
|
|
|
|
my $xml = `nix-env -f '$path' --arg overlays '[]' -qa '$filter' --xml --meta --drv-path`;
|
2013-07-30 11:45:54 +02:00
|
|
|
|
die "$0: evaluation of ‘$path’ failed\n" if $? != 0;
|
|
|
|
|
|
|
|
|
|
my $info = XMLin($xml, KeyAttr => { 'item' => '+attrPath', 'meta' => 'name' }, ForceArray => 1, SuppressEmpty => '' ) or die "cannot parse XML output";
|
|
|
|
|
|
2013-07-25 23:50:14 +02:00
|
|
|
|
# Check meta information.
|
|
|
|
|
print "=== Package meta information ===\n\n";
|
2013-07-30 16:51:32 +02:00
|
|
|
|
my $nrBadNames = 0;
|
2013-07-25 23:50:14 +02:00
|
|
|
|
my $nrMissingMaintainers = 0;
|
2013-09-02 22:13:01 +02:00
|
|
|
|
my $nrMissingPlatforms = 0;
|
2013-07-25 23:50:14 +02:00
|
|
|
|
my $nrMissingDescriptions = 0;
|
|
|
|
|
my $nrBadDescriptions = 0;
|
|
|
|
|
my $nrMissingLicenses = 0;
|
|
|
|
|
|
|
|
|
|
foreach my $attr (sort keys %{$info->{item}}) {
|
|
|
|
|
my $pkg = $info->{item}->{$attr};
|
|
|
|
|
|
|
|
|
|
my $pkgName = $pkg->{name};
|
2013-07-30 16:51:32 +02:00
|
|
|
|
my $pkgVersion = "";
|
|
|
|
|
if ($pkgName =~ /(.*)(-[0-9].*)$/) {
|
|
|
|
|
$pkgName = $1;
|
|
|
|
|
$pkgVersion = $2;
|
|
|
|
|
}
|
2013-07-25 23:50:14 +02:00
|
|
|
|
|
|
|
|
|
# Check the maintainers.
|
|
|
|
|
my @maintainers;
|
|
|
|
|
my $x = $pkg->{meta}->{maintainers};
|
|
|
|
|
if (defined $x && $x->{type} eq "strings") {
|
|
|
|
|
@maintainers = map { $_->{value} } @{$x->{string}};
|
|
|
|
|
} elsif (defined $x->{value}) {
|
|
|
|
|
@maintainers = ($x->{value});
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-30 11:45:54 +02:00
|
|
|
|
if (defined $maintainer && scalar(grep { $_ =~ /$maintainer/i } @maintainers) == 0) {
|
|
|
|
|
delete $info->{item}->{$attr};
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 23:50:14 +02:00
|
|
|
|
if (scalar @maintainers == 0) {
|
|
|
|
|
print "$attr: Lacks a maintainer\n";
|
|
|
|
|
$nrMissingMaintainers++;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-02 22:13:01 +02:00
|
|
|
|
# Check the platforms.
|
|
|
|
|
if (!defined $pkg->{meta}->{platforms}) {
|
|
|
|
|
print "$attr: Lacks a platform\n";
|
|
|
|
|
$nrMissingPlatforms++;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-30 16:51:32 +02:00
|
|
|
|
# Package names should not be capitalised.
|
|
|
|
|
if ($pkgName =~ /^[A-Z]/) {
|
|
|
|
|
print "$attr: package name ‘$pkgName’ should not be capitalised\n";
|
|
|
|
|
$nrBadNames++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($pkgVersion eq "") {
|
|
|
|
|
print "$attr: package has no version\n";
|
|
|
|
|
$nrBadNames++;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 23:50:14 +02:00
|
|
|
|
# Check the license.
|
|
|
|
|
if (!defined $pkg->{meta}->{license}) {
|
|
|
|
|
print "$attr: Lacks a license\n";
|
|
|
|
|
$nrMissingLicenses++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Check the description.
|
|
|
|
|
my $description = $pkg->{meta}->{description}->{value};
|
|
|
|
|
if (!$description) {
|
|
|
|
|
print "$attr: Lacks a description\n";
|
|
|
|
|
$nrMissingDescriptions++;
|
|
|
|
|
} else {
|
|
|
|
|
my $bad = 0;
|
|
|
|
|
if ($description =~ /^\s/) {
|
|
|
|
|
print "$attr: Description starts with whitespace\n";
|
|
|
|
|
$bad = 1;
|
|
|
|
|
}
|
|
|
|
|
if ($description =~ /\s$/) {
|
|
|
|
|
print "$attr: Description ends with whitespace\n";
|
|
|
|
|
$bad = 1;
|
|
|
|
|
}
|
|
|
|
|
if ($description =~ /\.$/) {
|
|
|
|
|
print "$attr: Description ends with a period\n";
|
|
|
|
|
$bad = 1;
|
|
|
|
|
}
|
|
|
|
|
if (index(lc($description), lc($attr)) != -1) {
|
|
|
|
|
print "$attr: Description contains package name\n";
|
|
|
|
|
$bad = 1;
|
|
|
|
|
}
|
|
|
|
|
$nrBadDescriptions++ if $bad;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-30 16:51:32 +02:00
|
|
|
|
|
2013-07-25 23:50:14 +02:00
|
|
|
|
print "\n";
|
|
|
|
|
|
|
|
|
|
# Find packages that have the same name.
|
|
|
|
|
print "=== Package name collisions ===\n\n";
|
|
|
|
|
|
2013-07-30 11:45:54 +02:00
|
|
|
|
my %pkgsByName;
|
|
|
|
|
|
|
|
|
|
foreach my $attr (sort keys %{$info->{item}}) {
|
|
|
|
|
my $pkg = $info->{item}->{$attr};
|
|
|
|
|
#print STDERR "attr = $attr, name = $pkg->{name}\n";
|
|
|
|
|
$pkgsByName{$pkg->{name}} //= [];
|
|
|
|
|
push @{$pkgsByName{$pkg->{name}}}, $pkg;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 23:50:14 +02:00
|
|
|
|
my $nrCollisions = 0;
|
|
|
|
|
foreach my $name (sort keys %pkgsByName) {
|
|
|
|
|
my @pkgs = @{$pkgsByName{$name}};
|
|
|
|
|
|
|
|
|
|
# Filter attributes that are aliases of each other (e.g. yield the
|
|
|
|
|
# same derivation path).
|
|
|
|
|
my %drvsSeen;
|
|
|
|
|
@pkgs = grep { my $x = $drvsSeen{$_->{drvPath}}; $drvsSeen{$_->{drvPath}} = 1; !defined $x } @pkgs;
|
|
|
|
|
|
|
|
|
|
# Filter packages that have a lower priority.
|
2013-07-30 12:11:40 +02:00
|
|
|
|
my $highest = min (map { $_->{meta}->{priority}->{value} // 0 } @pkgs);
|
|
|
|
|
@pkgs = grep { ($_->{meta}->{priority}->{value} // 0) == $highest } @pkgs;
|
2013-07-25 23:50:14 +02:00
|
|
|
|
|
|
|
|
|
next if scalar @pkgs == 1;
|
|
|
|
|
|
|
|
|
|
$nrCollisions++;
|
|
|
|
|
print "The following attributes evaluate to a package named ‘$name’:\n";
|
|
|
|
|
print " ", join(", ", map { $_->{attrPath} } @pkgs), "\n\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print "=== Bottom line ===\n";
|
|
|
|
|
print "Number of packages: ", scalar(keys %{$info->{item}}), "\n";
|
2013-07-30 16:51:32 +02:00
|
|
|
|
print "Number of bad names: $nrBadNames\n";
|
2013-07-25 23:50:14 +02:00
|
|
|
|
print "Number of missing maintainers: $nrMissingMaintainers\n";
|
2013-09-02 22:13:01 +02:00
|
|
|
|
print "Number of missing platforms: $nrMissingPlatforms\n";
|
2013-07-25 23:50:14 +02:00
|
|
|
|
print "Number of missing licenses: $nrMissingLicenses\n";
|
|
|
|
|
print "Number of missing descriptions: $nrMissingDescriptions\n";
|
|
|
|
|
print "Number of bad descriptions: $nrBadDescriptions\n";
|
|
|
|
|
print "Number of name collisions: $nrCollisions\n";
|