#!/usr/bin/perl
#
# update-python-projects -- update Lintian data about Python project names
#
# Copyright © 2011, 2012, 2013 Jakub Wilk
#
# This program is free software.  It is distributed under the terms of the GNU
# General Public License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, you can find it on the World Wide Web at
# http://www.gnu.org/copyleft/gpl.html, or write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

use strict;
use warnings;

use v5.10; # for the // operator

use File::Basename qw(dirname);
use POSIX qw(strftime);

chdir(dirname($0) . '/..') or die $!;

my $mirror = $ENV{'DEB_MIRROR'} // 'http://http.debian.net/debian';
my @data;
$data[2] = {};
$data[3] = {};

my @urls = map { "$mirror/dists/$_/main/Contents-i386.gz" } qw(unstable experimental);
open(my $fh, "wget -q -O - @urls | zcat |") or die $!;
while (<$fh>) {
    if (m{^
        (?: (
            usr/lib/pyshared/python2\.\d+ | # python-support >= 0.90
            usr/share/pyshared | # python-support >= 0.90, python-central, dh_python2
            usr/lib/python-support/[^/]+/python2\.\d+ | # python-support < 0.90
            usr/share/python-support/[^/]+ | # python-support < 0.90
            usr/lib/python2\.\d+/(?:site|dist)-packages # python-central, dh_python2
        ) | (
            usr/lib/python3/dist-packages
        )) /
        (?: .*/ )? # FIXME: check if the directory is actually within sys.path
        ([.\w]+)-[^/]*\.egg-info
        (?: /.+ )?
        \s+
        (\S+)
        $
    }x) {
        my $ver = 2 + (defined $2);
        my $project_name = lc $3;
        my @package_names = split /,/, $4;
        map { s,.*/,, } @package_names;
        $data[$ver]->{$project_name} //= {};
        $data[$ver]->{$project_name}->{$_} = 1 foreach @package_names;
    }
}
close($fh) or die $!;
$data[2]->{'setuptools'} //= $data[2]->{'distribute'};
$data[3]->{'setuptools'} //= $data[3]->{'distribute'};
$data[2]->{'wsgiref'} = {'python (>= 2.5)' => 1, 'python-wsgiref' => 1};
$data[3]->{'wsgiref'} = {'python3' => 1};
$data[2]->{'argparse'} = {'python (>= 2.7)' => 1, 'python-argparse' => 1};
$data[2]->{'python'} = {'python' => 1};
$data[3]->{'python'} = {'python3' => 1};

my $date = strftime('%Y-%m-%d', gmtime);

for my $ver (2..3) {
    my $python = sprintf 'Python %d.X', $ver;
    open(my $fh, '>', sprintf('data/python%d-projects', $ver)) or die $!;
    print $fh <<EOF ;
# This file contains mapping between $python project names (as used by
# setuptools) and Debian package names.
# It was automatically generated by private/update-python-projects.
#
# Last update: $date

EOF
    foreach my $project_name (sort keys %{$data[$ver]}) {
        my @package_names = keys %{$data[$ver]->{$project_name}};
        print $fh "$project_name||" . join(' | ', sort @package_names) . "\n";
    }
    close($fh) or die $!;
}

# Local Variables:
# indent-tabs-mode: nil
# End:
# vim: syntax=perl sw=4 sts=4 sr et
