2011-11-03 12:57:19 +00:00
|
|
|
#!/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';
|
2019-08-04 01:28:00 +00:00
|
|
|
[% authors %]
|
2011-11-03 12:57:19 +00:00
|
|
|
pkgname=[% pkgname %]
|
2016-06-12 14:13:14 +00:00
|
|
|
_pkgreal=[% pkgreal %]
|
2011-11-03 12:57:19 +00:00
|
|
|
pkgver=[% pkgver %]
|
|
|
|
pkgrel=0
|
2019-08-04 01:28:00 +00:00
|
|
|
pkgdesc="[% pkgdesc %]"
|
2011-11-03 12:57:19 +00:00
|
|
|
url="http://packages.python.org/pypi/[% pkgreal %]"
|
|
|
|
arch="noarch"
|
2019-08-04 01:28:00 +00:00
|
|
|
license="[% license %]"
|
2011-11-03 12:57:19 +00:00
|
|
|
pydepends=""
|
|
|
|
pymakedepends=""
|
2019-08-04 01:28:00 +00:00
|
|
|
depends="python3 $pydepends"
|
|
|
|
checkdepends="python3-dev"
|
|
|
|
makedepends="py3-setuptools $pymakedepends"
|
|
|
|
subpackages=""
|
2011-11-03 12:57:19 +00:00
|
|
|
source="[% source %]"
|
2016-06-12 14:11:51 +00:00
|
|
|
builddir="$srcdir/$_pkgreal-$pkgver"
|
2011-11-03 12:57:19 +00:00
|
|
|
|
|
|
|
build() {
|
2019-08-04 01:28:00 +00:00
|
|
|
python3 setup.py build
|
2011-11-03 12:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
package() {
|
2019-08-04 01:28:00 +00:00
|
|
|
PYTHONPATH=$pkgdir`python3 -c "import site; print(site.getsitepackages()[0])"` \
|
|
|
|
python3 setup.py install \
|
2011-11-03 12:57:19 +00:00
|
|
|
--prefix=/usr \
|
|
|
|
--root="$pkgdir" \
|
2017-07-11 11:59:19 +00:00
|
|
|
--single-version-externally-managed
|
2011-11-03 12:57:19 +00:00
|
|
|
}
|
2019-08-04 01:28:00 +00:00
|
|
|
|
|
|
|
check() {
|
|
|
|
python3 setup.py test
|
|
|
|
}
|
|
|
|
|
2011-11-03 12:57:19 +00:00
|
|
|
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) = @_;
|
2019-08-04 01:28:00 +00:00
|
|
|
return () if ( ! -e $filename );
|
2011-11-03 12:57:19 +00:00
|
|
|
my $text = read_file($filename);
|
|
|
|
my %sline = $text =~ /^(\w+)\s*=\s*([^\"\n]*)$/mg;
|
|
|
|
my %mline = $text =~ /^(\w+)\s*=\s*\"([^\"]*)\"$/mg;
|
2019-08-04 01:28:00 +00:00
|
|
|
my %hash = ( %sline, %mline );
|
2011-11-03 12:57:19 +00:00
|
|
|
|
2019-08-04 01:28:00 +00:00
|
|
|
my $authors = join("\n", $text =~ /^# Contributor: .*$/mg, $text =~ /^# Maintainer: .*$/mg);
|
|
|
|
$hash{'authors'} = $authors if length($authors) > 1;
|
2011-11-03 12:57:19 +00:00
|
|
|
|
2019-08-04 01:28:00 +00:00
|
|
|
return \%hash;
|
2011-11-03 12:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub map_pypi_to_apk {
|
|
|
|
my ($pypi) = @_;
|
|
|
|
return $pkgmap{$pypi} unless !exists($pkgmap{$pypi});
|
2019-08-04 01:28:00 +00:00
|
|
|
return 'py3-'.lc($pypi);
|
2011-11-03 12:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-08-04 01:28:00 +00:00
|
|
|
my ($distdata, $authors) = @_;
|
2011-11-03 12:57:19 +00:00
|
|
|
|
|
|
|
my %repl = (
|
2019-08-04 01:28:00 +00:00
|
|
|
authors => ($authors or "# Contributor: $packager\n# Maintainer: $packager"),
|
2011-11-03 12:57:19 +00:00
|
|
|
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}),
|
2019-08-04 01:28:00 +00:00
|
|
|
pkgdesc => $distdata->{info}{summary},
|
2011-11-03 12:57:19 +00:00
|
|
|
);
|
|
|
|
$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) = @_;
|
2019-08-04 01:28:00 +00:00
|
|
|
my $response = $ua->get("https://pypi.python.org/pypi/$package/json");
|
2011-11-03 12:57:19 +00:00
|
|
|
$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}";
|
2019-08-04 01:28:00 +00:00
|
|
|
my $reqs = `python3 ./setup.py --requires`;
|
2011-11-03 12:57:19 +00:00
|
|
|
|
|
|
|
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};
|
|
|
|
|
2019-08-04 01:28:00 +00:00
|
|
|
my $user_abuild_conf = read_assignments_from_file($ENV{"HOME"} . "/.abuild/abuild.conf");
|
|
|
|
$packager = $user_abuild_conf->{PACKAGER} if $user_abuild_conf->{PACKAGER};
|
|
|
|
|
2011-11-03 12:57:19 +00:00
|
|
|
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;
|
|
|
|
|
2019-08-04 01:28:00 +00:00
|
|
|
my $data = write_apkbuild($distdata, undef);
|
2011-11-03 12:57:19 +00:00
|
|
|
prepare_tree;
|
|
|
|
|
|
|
|
get_deps($data);
|
|
|
|
} when ('recreate') {
|
|
|
|
my $apkbuild = read_apkbuild;
|
|
|
|
my $distdata = get_data($apkbuild->{_pkgreal});
|
2019-08-04 01:28:00 +00:00
|
|
|
my $data = write_apkbuild($distdata, $apkbuild->{authors});
|
2011-11-03 12:57:19 +00:00
|
|
|
|
|
|
|
prepare_tree;
|
2019-08-04 01:28:00 +00:00
|
|
|
get_deps($data);
|
2011-11-03 12:57:19 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|