apkbuild-pypi.in 6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/usr/bin/perl

use strict;
use warnings;
use autodie qw(:all);
use feature qw(:5.14);
use LWP::UserAgent;
use LWP::ConnCache;
use JSON;

our $packager = '';
my %pkgmap = ();
my %licenses = ();

my $template = <<'EOF';
16
[% authors %]
17
pkgname=[% pkgname %]
18
_pkgreal=[% pkgreal %]
19 20
pkgver=[% pkgver %]
pkgrel=0
21
pkgdesc="[% pkgdesc %]"
22 23
url="http://packages.python.org/pypi/[% pkgreal %]"
arch="noarch"
24
license="[% license %]"
25 26
pydepends=""
pymakedepends=""
27 28 29 30
depends="python3 $pydepends"
checkdepends="python3-dev"
makedepends="py3-setuptools $pymakedepends"
subpackages=""
31
source="[% source %]"
32
builddir="$srcdir/$_pkgreal-$pkgver"
33 34

build() {
35
	python3 setup.py build
36 37 38
}

package() {
39 40
	PYTHONPATH=$pkgdir`python3 -c "import site; print(site.getsitepackages()[0])"` \
	python3 setup.py install \
41 42
		--prefix=/usr \
		--root="$pkgdir" \
tmpfile's avatar
tmpfile committed
43
		--single-version-externally-managed
44
}
45 46 47 48 49

check() {
	python3 setup.py test
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
EOF

my $ua = LWP::UserAgent->new();
my $json = JSON->new;
$ua->env_proxy;
$ua->conn_cache(LWP::ConnCache->new);

sub read_file {
    my ($filename) = @_;
    open my $fh, '<:utf8', $filename;
    local $/;
    my $text = <$fh>;
    return $text;
}

sub read_assignments_from_file {
    my ($filename) = @_;
67
    return () if ( ! -e $filename );
68 69 70
    my $text = read_file($filename);
    my %sline = $text =~ /^(\w+)\s*=\s*([^\"\n]*)$/mg;
    my %mline = $text =~ /^(\w+)\s*=\s*\"([^\"]*)\"$/mg;
71
    my %hash = ( %sline, %mline );
72

73 74
    my $authors = join("\n", $text =~ /^# Contributor: .*$/mg, $text =~ /^# Maintainer: .*$/mg);
    $hash{'authors'} = $authors if length($authors) > 1;
75

76
    return \%hash;
77 78 79 80 81
}

sub map_pypi_to_apk {
    my ($pypi) = @_;
    return $pkgmap{$pypi} unless !exists($pkgmap{$pypi});
82
    return 'py3-'.lc($pypi);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
}

sub map_license {
    my ($license) = @_;

    $license =~ s/ or / /g;

    return $license;
}

sub get_source {
    my ($distdata) = @_;

    for my $url (@{$distdata->{urls}}) {
        return $url->{url} if $url->{python_version} eq 'source'
    }
    die "Unable to locate sources for $distdata->{name}.\n";
}

sub read_apkbuild {
    return read_assignments_from_file('APKBUILD');
}

sub write_apkbuild {
107
    my ($distdata, $authors) = @_;
108 109

    my %repl = (
110
	authors  => ($authors or "# Contributor: $packager\n# Maintainer: $packager"),
111 112 113 114 115
        pkgname  => map_pypi_to_apk($distdata->{info}{name}),
        pkgreal  => $distdata->{info}{name},
        pkgver   => $distdata->{info}{version},
        source   => get_source($distdata),
        license  => map_license($distdata->{info}{license}),
116
        pkgdesc  => $distdata->{info}{summary},
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    );
    $template =~ s/\[% (.*?) %\]/$repl{$1}/g;

    open my $fh, '>:utf8', 'APKBUILD';
    print {$fh} $template;
    close $fh;

    say "Wrote $repl{pkgname}/APKBUILD";

    return \%repl;
}

sub prepare_tree {
    system('abuild checksum unpack prepare');
}

sub get_data {
    my ($package) = @_;
135
    my $response = $ua->get("https://pypi.python.org/pypi/$package/json");
136 137 138 139 140 141 142 143 144 145
    $response->is_success or die $response->status_line;
    my $distdata = $json->decode($response->decoded_content);

    return $distdata;
}

sub get_deps {
    my ($data) = @_;

    chdir "src/$data->{pkgreal}-$data->{pkgver}";
146
    my $reqs = `python3 ./setup.py --requires`;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

    my @reqs = split /\n/, $reqs;

    foreach my $i (0 .. $#reqs) {
        $reqs[$i] =~ s/(\(|\s+).*$//;
        $reqs[$i] = map_pypi_to_apk($reqs[$i]);
    }

    chdir '../..';

    my $apk = read_file('APKBUILD');

    $reqs = join ' ', @reqs;

    $apk =~ s/pymakedepends=""/pymakedepends="$reqs"/;

    open my $fh, '>:utf8', 'APKBUILD';

    print $fh $apk;

    say "Requires: $reqs";
}

my $abuild_conf = read_assignments_from_file('/etc/abuild.conf');
$packager = $abuild_conf->{PACKAGER} if $abuild_conf->{PACKAGER};

173 174 175
my $user_abuild_conf = read_assignments_from_file($ENV{"HOME"} . "/.abuild/abuild.conf");
$packager = $user_abuild_conf->{PACKAGER} if $user_abuild_conf->{PACKAGER};

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
sub usage {
    say <<'EOF';
Usage: apkbuild-pypi [create <package> | check | recreate | upgrade | update]

In the repository root:
    create <package>: Creates an APKBUILD for <package>

In the package root:
    check           : Reports current & latest version of the package
    recreate        : Recreates the APKBUILD
    upgrade         : Upgrades to the latest version of the package
    update          : Updates APKBUILD metadata
EOF
}

given ($ARGV[0]) {
    when ('create') {
        my $package = $ARGV[1];
        $package or die usage;

        my $distdata = get_data($package);
        my $apkname = map_pypi_to_apk($package);

        mkdir $apkname;
        chdir $apkname;

202
        my $data = write_apkbuild($distdata, undef);
203 204 205 206 207 208
        prepare_tree;

        get_deps($data);
    } when ('recreate') {
        my $apkbuild = read_apkbuild;
        my $distdata = get_data($apkbuild->{_pkgreal});
209
	my $data = write_apkbuild($distdata, $apkbuild->{authors});
210 211

        prepare_tree;
212
        get_deps($data);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    } when ('upgrade') {
        my $apkbuild = read_apkbuild;
        my $distdata = get_data($apkbuild->{_pkgreal});

        my $pkgver = $distdata->{info}{version};

        if ($pkgver ne $apkbuild->{pkgver}) {
            say "Upgrading PyPI package from $apkbuild->{pkgver} to $pkgver";

            my $text = read_file('APKBUILD');

            $text =~ s/^(pkgver)=.*$/$1=$pkgver/mg or
                die "Can't find pkgver line in APKBUILD";
            $text =~ s/^(pkgrel)=.*$/$1=0/mg or
                die "Can't find pkgrel line in APKBUILD";

            open my $fh, '>:utf8', 'APKBUILD';
            print $fh $text;
            close $fh;
        } else {
            say "Already up to date with PyPI";
        }
    } when ('check') {
        my $apkbuild = read_apkbuild;
        my $distdata = get_data($apkbuild->{_pkgreal});

        my $pkgver = $distdata->{info}{version};

        say "$apkbuild->{pkgname}: Latest version: $pkgver Packaged version: $apkbuild->{pkgver}";
        if ($pkgver ne $apkbuild->{pkgver}) {
            exit(1);
        }
    } when ('update') {
        prepare_tree;
    } default {
        die usage;
    }
}