Differences between revisions 2 and 6 (spanning 4 versions)
Revision 2 as of 2011-04-20 17:10:09
Size: 2302
Editor: NielsThykier
Comment: Fixed shell script #! line
Revision 6 as of 2011-07-28 16:57:25
Size: 8809
Editor: NielsThykier
Comment: Corrected some info
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from Lintian/HackersGuide
Line 48: Line 49:

== Writing a new check ==

In this tutorial, we will write a sample check called "illegal". This check will issue the tag "illegal-name", if the version binary package is "foo" or "bar". We start by creating a "desc" file for the check, which has to be "checks/illegal.desc":

{{{
Check-Script: illegal
Author: Your Name <you@example.com>
Abbrev: ill
Type: binary
Info: Checks for illegal names

Tag: illegal-version
Severity: important
Certainty: certain
Info: The package version is "foo" or "bar", which is not allowed.
Needs-Info: fields
}}}

Let us go over the fields of the first paragraph:
 * Check-Script (required): Name of the script (same as the filename without the ".desc" extension)
 * Author (optional): Name and email of who (initially) wrote the check
 * Abbrev (required): An abbreviation of the check name; used for (e.g.) lintian -C.
 * Type (required): Comma separated list of the values "binary" (.deb), "udeb" (.udeb), "source" (.dsc) and "changes" (.changes). This determines which types of packages this check applies.
 * Info (required): Short description of what the check does; this will be added to the manpage of lintian.
 * Needs-Info (optional): The list of "collection"s this check needs. We will come back to this later.

One or more "Tag" paragraphs follows the first parapragh. In this example we only have one:
 * Tag (required): name of the tag
 * Severity (required): One of "serious", "important", "normal", "minor", "wishlist" and "pedantic". Together with Certainty, this determines the "code" of the tag. Note the profile used may overrule this value.
 * Certainty (required): One of "certain", "possible" and "wild-guess". Together with Severity, this determine the "code" of the tag.
 * Info (required): The description of the tag (shown with lintian -i, lintian-info -t $tag, etc.)
 * Ref (optional, not used here): Comma separated list of references to a manual, web-page or the likes. Regular URLs and manpages (e.g. lintian(1)) works as expected. There are some named manuals (see data/output/manual-references).
 * Experimental (optional, not used): Either "yes" or "no"; if present and set to "yes", the tag will be an experimental one.

After filling out this file, lintian-info will recognise the "illegal-version" tag.
{{{#!highlight perl
# note lintian-info < 2.5.2 will use "N:" instead of "E:" on the first line
/path/to/lintian.git-dir$ LINTIAN_ROOT='.' frontend/lintian-info -t illegal-version
E: illegal-version
N:
N: The package version is "foo" or "bar", which is not allowed.
N:
N: Severity: important, Certainty: certain
N:
N: Check: illegal, Type: binary
N:
}}}

However, lintian does not know how to emit the tag yet. Time to write the actual check. Open "checks/illegal" and write:

{{{
# This check was written by Your Name <you@example.com>
# copyright and license info etc.
package Lintian::illegal;

use strict;
use warnings;

use Lintian::Tags qw(tag);

sub run {
  my $pkg = shift; # package name
  my $type = shift; # package type - for this check it will always be "binary"
  my $info = shift; # a Lintian::Collect object for the package
                    # - for this check it will be a Lintian::Collect::Binary

  # Fetch a field from the control file in the control.tar.gz from the deb.
  # This also works on udeb, source and changes packages.
  # For source and changes, this will be a field from the ".dsc" and the ".changes" file
  my $version = $info->field('version');
  # Note fields are "undef" if they are not present; do not assume the presence of any field.
  if (defined $version) {
     if ($version eq 'foo' || $version eq 'bar') {
        # Issue the illegal-version tag, giving the actual version as "extra" info.
        tag 'illegal-version', $version;
     }
  }
}

1;

}}}

The rationale for using the $info (Lintian::Collect) object, is that it abstracts away the underlying lab (and allows lintian to better cache things). The Lintian::Collect API is a bit behind (at time of writing) and some checks still have to directly look in the lab. The Lintian::Collect API is based on the information extracted by "collection" scripts; each method in the API will generally have one (or more) lines like this (some times it is inside the method):

{{{
# sub field Needs-Info fields
}}}

This means that any check using this method, must declare a Needs-Info on "fields". The lintian resolver is not smart enough to calculate the indirect dependencies, so check collections/$name.desc for a Needs-Info field, add those to the Needs-Info of the check (repeat as need).

With this lintian will be able to emit the tag, so lets try it on a deb:
{{{
/path/to/lintian.git-dir$ LINTIAN_ROOT='.' frontend/lintian -i --no-override -C illegal /path/to/deb/version-foo.deb
E: deb-format-lzma: illegal-version foo
N:
N: The package version is "foo" or "bar", which is not allowed.
N:
N: Severity: important, Certainty: certain
N:
N: Check: illegal, Type: binary
N:
}}}

Finally, you just need to add the check to a profile. If it should be added to the debian/main profile, just run:
{{{
/path/to/lintian.git-dir$ debian/rules profiles
}}}

Otherwise you may have to alter private/generate-profiles.pl to exclude it from debian/main and add it to the right profile.

Summing it up: In this tutorial, we created a new check by creating a "desc" file with that tag information, a perl module to check and emit the tag and finally we updated the profiles.

== Writing tests (test suite) ==

The test-suite generally consists of building a number of packages, running Lintian on them and comparing the output to the expected results. There are 5 "sub" test suites named "scripts", "debs", "changes", "source" and "tests". The scripts sub suite consists of perl tests used to unit test parts of lintian and will not be covered here. The rest are used to test if Lintian can diagnose tags correctly.

Generally the "tests" subsuite (in t/tests) should be used. It has more features (cmd-line options etc.) and uses templates to fill in the "blanks" (which means you have to write less to make it work). The "source", "changes" and "debs" suites are used to hand-make special packages to test edge-cases (like missing required fields). Please consult t/$suite/README for how to write a test in each of the suites.

Hacking Lintian 101

This document aims to give a quick guide and a few hints to understanding Lintian.

Using Lintian from git

Lintian can be used directly from git; this allows developers to skip the boring build/install phase when testing the development version on real packages. To use lintian directly from git you need to set LINTIAN_ROOT (alternatively --root can be used) to the directory where the git clone is. The following script can be used for this:

   1 #!/bin/sh
   2 LINTIAN_ROOT="<INSERT PATH TO LINTIAN GIT DIR>"
   3 export LINTIAN_ROOT
   4 exec "$LINTIAN_ROOT/frontend/lintian" "$@"

Save that as run-lintian, make it executable, put it in your PATH and you are good to go.

Caveat 1: If LINTIAN_ROOT is not set, lintian will try to use the installed version of lintian, which most likely does not have the changes you would like to test.

Caveat 2: Lintian expects to have a standard locale, currently the version from git just uses the installed locale. This works fine as long as you also have Lintian installed (or if you already have the needed locale). In chroots or on minimum installs you may have to generate such a locale and set LOCPATH. Note, lintian will auto-generate this locale for running its test suite.

Running tests

There are a number of ways to run (parts of) the Lintian test suite. To run the entire test suite, simply use:

$ debian/rules runtests

It is also possible to run only a subset of the tests by using the "onlyrun" variable.

$ debian/rules runtests onlyrun=$test

Here $test can be one of:

  • The name of a test, in which case that specific test is run. This works regardless if the test is in the new or in the legacy test suite.
  • The name of a check, in which case all tests in t/tests that starts with $name will be run. If a legacy test happens to have the same name as a check, then this test is also run.

Finally it is also possible to only check for tests that are designed to trigger (or not trigger) a specific tag. For this use:

$ debian/rules check-tag tag=$tag

In this case, the test suite will run all tests that has $tag in its Test-For or Test-Against field. This cannot be used to run a test in the legacy test suite.

Writing a new check

In this tutorial, we will write a sample check called "illegal". This check will issue the tag "illegal-name", if the version binary package is "foo" or "bar". We start by creating a "desc" file for the check, which has to be "checks/illegal.desc":

Check-Script: illegal
Author: Your Name <you@example.com>
Abbrev: ill
Type: binary
Info: Checks for illegal names

Tag: illegal-version
Severity: important
Certainty: certain
Info: The package version is "foo" or "bar", which is not allowed.
Needs-Info: fields

Let us go over the fields of the first paragraph:

  • Check-Script (required): Name of the script (same as the filename without the ".desc" extension)
  • Author (optional): Name and email of who (initially) wrote the check
  • Abbrev (required): An abbreviation of the check name; used for (e.g.) lintian -C.
  • Type (required): Comma separated list of the values "binary" (.deb), "udeb" (.udeb), "source" (.dsc) and "changes" (.changes). This determines which types of packages this check applies.
  • Info (required): Short description of what the check does; this will be added to the manpage of lintian.
  • Needs-Info (optional): The list of "collection"s this check needs. We will come back to this later.

One or more "Tag" paragraphs follows the first parapragh. In this example we only have one:

  • Tag (required): name of the tag
  • Severity (required): One of "serious", "important", "normal", "minor", "wishlist" and "pedantic". Together with Certainty, this determines the "code" of the tag. Note the profile used may overrule this value.
  • Certainty (required): One of "certain", "possible" and "wild-guess". Together with Severity, this determine the "code" of the tag.
  • Info (required): The description of the tag (shown with lintian -i, lintian-info -t $tag, etc.)
  • Ref (optional, not used here): Comma separated list of references to a manual, web-page or the likes. Regular URLs and manpages (e.g. lintian(1)) works as expected. There are some named manuals (see data/output/manual-references).
  • Experimental (optional, not used): Either "yes" or "no"; if present and set to "yes", the tag will be an experimental one.

After filling out this file, lintian-info will recognise the "illegal-version" tag.

   1 # note lintian-info < 2.5.2 will use "N:" instead of "E:" on the first line
   2 /path/to/lintian.git-dir$ LINTIAN_ROOT='.' frontend/lintian-info -t illegal-version
   3 E: illegal-version
   4 N:
   5 N:   The package version is "foo" or "bar", which is not allowed.
   6 N:
   7 N:   Severity: important, Certainty: certain
   8 N:
   9 N:   Check: illegal, Type: binary
  10 N:

However, lintian does not know how to emit the tag yet. Time to write the actual check. Open "checks/illegal" and write:

# This check was written by Your Name <you@example.com>
#  copyright and license info etc.
package Lintian::illegal;

use strict;
use warnings;

use Lintian::Tags qw(tag);

sub run {
  my $pkg  = shift; # package name
  my $type = shift; # package type - for this check it will always be "binary"
  my $info = shift; # a Lintian::Collect object for the package
                    #  - for this check it will be a Lintian::Collect::Binary

  # Fetch a field from the control file in the control.tar.gz from the deb.
  # This also works on udeb, source and changes packages.
  # For source and changes, this will be a field from the ".dsc" and the ".changes" file
  my $version = $info->field('version');
  # Note fields are "undef" if they are not present; do not assume the presence of any field.
  if (defined $version) {
     if ($version eq 'foo' || $version eq 'bar') {
        # Issue the illegal-version tag, giving the actual version as "extra" info.
        tag 'illegal-version', $version;
     }
  }
}

1;

The rationale for using the $info (Lintian::Collect) object, is that it abstracts away the underlying lab (and allows lintian to better cache things). The Lintian::Collect API is a bit behind (at time of writing) and some checks still have to directly look in the lab. The Lintian::Collect API is based on the information extracted by "collection" scripts; each method in the API will generally have one (or more) lines like this (some times it is inside the method):

# sub field Needs-Info fields

This means that any check using this method, must declare a Needs-Info on "fields". The lintian resolver is not smart enough to calculate the indirect dependencies, so check collections/$name.desc for a Needs-Info field, add those to the Needs-Info of the check (repeat as need).

With this lintian will be able to emit the tag, so lets try it on a deb:

/path/to/lintian.git-dir$  LINTIAN_ROOT='.' frontend/lintian -i --no-override -C illegal /path/to/deb/version-foo.deb
E: deb-format-lzma: illegal-version foo
N: 
N:    The package version is "foo" or "bar", which is not allowed.
N:    
N:    Severity: important, Certainty: certain
N:    
N:    Check: illegal, Type: binary
N: 

Finally, you just need to add the check to a profile. If it should be added to the debian/main profile, just run:

/path/to/lintian.git-dir$ debian/rules profiles

Otherwise you may have to alter private/generate-profiles.pl to exclude it from debian/main and add it to the right profile.

Summing it up: In this tutorial, we created a new check by creating a "desc" file with that tag information, a perl module to check and emit the tag and finally we updated the profiles.

Writing tests (test suite)

The test-suite generally consists of building a number of packages, running Lintian on them and comparing the output to the expected results. There are 5 "sub" test suites named "scripts", "debs", "changes", "source" and "tests". The scripts sub suite consists of perl tests used to unit test parts of lintian and will not be covered here. The rest are used to test if Lintian can diagnose tags correctly.

Generally the "tests" subsuite (in t/tests) should be used. It has more features (cmd-line options etc.) and uses templates to fill in the "blanks" (which means you have to write less to make it work). The "source", "changes" and "debs" suites are used to hand-make special packages to test edge-cases (like missing required fields). Please consult t/$suite/README for how to write a test in each of the suites.