SteveNewson
10/1/2012 - 5:08 PM

List all the packages in a Debian / Ubuntu repository using debian-repository-parser

List all the packages in a Debian / Ubuntu repository using debian-repository-parser

You'll need the Ubuntu public key. Use the following to list keys:

sudo apt-key list

That will give you something like:

/etc/apt/trusted.gpg
--------------------
pub   1024D/437D05B5 2004-09-12
uid                  Ubuntu Archive Automatic Signing Key <ftpmaster@ubuntu.com>
sub   2048g/79164387 2004-09-12

And the following to export the public key (you'll need to get the key ID from the list, everything after the forward slash).

sudo apt-key export 437D05B5
  import java.io.ByteArrayOutputStream;
  import com.google.common.io.ByteStreams; // This is for convenience, you could use Commons IO or roll your own

  public void list() throws URISyntaxException {
    ByteArrayOutputStream writer = new ByteArrayOutputStream();
    try {
      ByteStreams.copy(getClass().getClassLoader().getResourceAsStream("ubuntu-public-key.txt"), writer);
    } catch (IOException e) {
      throw new IllegalStateException("Could not copy Ubuntu public key resource", e);
    }

    RepositoryReader reader = new HttpRepositoryReader(new DefaultHttpClient());
    Parser parser = new Parser(new URI("http://archive.ubuntu.com/ubuntu/"), reader, writer.toString());

    Distribution dist = parser.getDistribution("precise");
    parser.listPackageBinaries(dist, "main", "i386", new PackageHandler() {
      @Override
      public void handle(Package thePackage) {
        System.out.println("Package: " + thePackage.getName() + " - verified=" + thePackage.isVerified());
      }
    });
  }