apkbuild-cpan.in 10.7 KB
Newer Older
1 2 3 4 5
#!/usr/bin/perl
# apk add perl-libwww perl-json

use strict;
use warnings;
6 7 8 9
use 5.016;
use feature "switch";
no if $] >= 5.018, warnings => "experimental::smartmatch";

10 11 12 13 14 15 16
use LWP::UserAgent;
use LWP::ConnCache;
use CPAN::Meta;
use Module::CoreList;
use JSON;

my $license_mappings = {
17 18 19
	"perl_5" => "GPL-1.0-or-later Artistic-1.0-Perl",
	"artistic_2" => "Artistic-2.0",
	"gpl_3" => "GPL-3.0-only",
20 21 22 23 24 25 26 27
};

my $package_mappings = {
	"LWP" => "perl-libwww",
	"TermReadKey" => "perl-term-readkey",
};
our $packager = "";
my $template = <<'EOF';
28 29
# Automatically generated by apkbuild-cpan, template 2
[% authors %]
30 31 32 33 34
pkgname=[% pkgname %]
_pkgreal=[% pkgreal %]
pkgver=[% pkgver %]
pkgrel=0
pkgdesc="Perl module for [% pkgreal %]"
Timothy Legge's avatar
Timothy Legge committed
35
url="https://metacpan.org/release/[% pkgreal %]/"
36
arch="noarch"
37
license="GPL-1.0-or-later Artistic-1.0-Perl"
38 39
cpandepends=""
cpanmakedepends=""
40
cpancheckdepends=""
41 42
depends="$cpandepends"
makedepends="perl-dev $cpanmakedepends"
43
checkdepends="$cpancheckdepends"
44 45
subpackages="$pkgname-doc"
source="[% source %]"
46
builddir="$srcdir/$_pkgreal-$pkgver"
47 48 49 50 51

build() {
	:
}

52
check() {
53 54 55
	:
}

56
package() {
57 58 59
	:
}

60 61 62 63 64 65 66
EOF

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

67
sub read_file {
68 69 70 71 72 73 74 75
	my ($filename) = @_;
	local $/;
	open my $fh, "<", $filename or die "could not open $filename: $!";
	return <$fh>;
}

sub read_assignments_from_file {
	my ($filename) = @_;
76
	return () if ( ! -e $filename );
77 78 79 80
	my $text = read_file($filename);
	my %sline = $text =~ /^(\w+)\s*=\s*([^\"\n]*)$/mg;
	my %mline = $text =~ /^(\w+)\s*=\s*\"([^\"]*)\"$/mg;
	my %hash = ( %sline, %mline );
81 82 83 84

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

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	return \%hash;
}

sub map_cpan_to_apk {
	my ($cpan_distrib) = @_;
	return $package_mappings->{$cpan_distrib} if exists $package_mappings->{$cpan_distrib};
	# most packages are named according to the
	# distribution name
	return 'perl-' . lc $cpan_distrib;
}

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

sub write_apkbuild {
101
	my ($distdata, $authors, $moddata) = @_;
102

103
	my $cpanid = $distdata->{id};
104
	$cpanid = substr($cpanid, 0, 1) . "/" . substr($cpanid, 0, 2) . "/$cpanid";
105

106
	my %repl = (
107
		authors  => ($authors or "# Contributor: $packager\n# Maintainer: $packager"),
108
		pkgname  => map_cpan_to_apk($moddata->{distribution}),
109 110
		pkgreal  => $moddata->{distribution},
		pkgver  => $moddata->{version},
111
		source   => $moddata->{download_url} =~ s/$moddata->{version}/\$pkgver/r,
112
		pkgdesc  => $distdata->{abstract},
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	);
	$template =~ s/\[% (.*?) %\]/$repl{$1}/g;

	open my $fh, '>', "APKBUILD" or die;
	print $fh $template;
	close $fh;

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

sub parse_deps {
	my ($reqs) = @_;
	my $distfiles = {};
	my $response;
	my $deps = "";

	for my $module ($reqs->required_modules) {
130 131
		if (Module::CoreList->is_core($module)) {
			my $perlver = Module::CoreList->first_release($module);
132 133 134 135 136 137
			say "$module is part of core perl since $perlver.";
			next;
		}
		next if $module eq 'perl';

		# map module name to package name
Timothy Legge's avatar
Timothy Legge committed
138
		$response = $ua->get("https://fastapi.metacpan.org/module/$module");
139 140 141
		$response->is_success or die $response->status_line;
		my $moddata = $json->decode($response->decoded_content);
		$moddata->{error} and die "Error trying to locate $module: $moddata->{error}\n";
Timothy Legge's avatar
Timothy Legge committed
142
		$distfiles->{$moddata->{distribution}} = $moddata;
143 144 145
	}

	# map package names to alpine packages
146
	foreach ( keys %{ $distfiles } ) {
147
		$response = $ua->get("https://fastapi.metacpan.org/release/$_");
148 149 150 151 152 153 154
		$response->is_success or die $response->status_line;
		my $distdata = $json->decode($response->decoded_content);
		$distdata->{error} and die "Error trying to locate $_: $distdata->{error}\n";

		my $pkgname = map_cpan_to_apk($distdata->{name});
		$deps .= "$pkgname ";
	}
155 156
	$deps =~ s/\h+/ /g;
	$deps =~ s/ $//;
157 158 159 160 161 162 163 164 165 166 167 168 169
	return $deps;

}

sub prepare_tree {
	system("abuild checksum unpack prepare") == 0 or
		die "abuild checksum failed";
}

sub update_functions {
	my $apkbuild = read_apkbuild;
	my $metaprefix = "src/" . $apkbuild->{'_pkgreal'} . "-" . $apkbuild->{'pkgver'} . "/";
	my $build_func;
170
	my $check_func;
171 172 173 174 175 176
	my $package_func;

	my $text = read_file "APKBUILD";
	if (-e "$metaprefix/Build.PL" ) {
		$build_func = <<'EOF';
build() {
177
	export CFLAGS=$(perl -MConfig -E 'say $Config{ccflags}')
178
	perl Build.PL installdirs=vendor
179
	./Build
180 181 182 183
}
EOF
		$package_func = <<'EOF';
package() {
tmpfile's avatar
tmpfile committed
184
	./Build install destdir="$pkgdir"
185 186
	find "$pkgdir" \( -name perllocal.pod -o -name .packlist \) -delete
}
187 188 189 190 191
EOF
		$check_func = <<'EOF';
check() {
	./Build test
}
192 193 194 195
EOF
	} else {
		$build_func = <<'EOF';
build() {
196
	export CFLAGS=$(perl -MConfig -E 'say $Config{ccflags}')
197
	PERL_MM_USE_DEFAULT=1 perl -I. Makefile.PL INSTALLDIRS=vendor
198
	make
199 200 201 202
}
EOF
		$package_func = <<'EOF';
package() {
tmpfile's avatar
tmpfile committed
203
	make DESTDIR="$pkgdir" install
204 205
	find "$pkgdir" \( -name perllocal.pod -o -name .packlist \) -delete
}
206 207 208 209 210 211
EOF
		$check_func = <<'EOF';
check() {
	export CFLAGS=$(perl -MConfig -E 'say $Config{ccflags}')
	make test
}
212 213 214 215 216 217 218
EOF
	}

	$text =~ s/^build\(\) \{.*?^\}\n/$build_func/smg or
		die "Can't replace build function APKBUILD";
	$text =~ s/^package\(\) \{.*?^\}\n/$package_func/smg or
		die "Can't replace package function APKBUILD";
219 220
	$text =~ s/^check\(\) \{.*?^\}\n/$check_func/smg or
		die "Can't replace check function APKBUILD";
221 222 223 224 225 226 227

	open my $fh, '>', "APKBUILD" or die;
	print $fh $text;
	close $fh;
}

sub do_depends {
228

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	my $apkbuild = read_apkbuild;
	my $metaprefix = "src/" . $apkbuild->{'_pkgreal'} . "-" . $apkbuild->{'pkgver'} . "/";
	my $meta;

	foreach my $metafile ("MYMETA.json", "META.json", "MYMETA.yml", "META.yml") {
		if (-e "$metaprefix$metafile") {
			say "Using meta information from $metafile";
			$meta = CPAN::Meta->load_file("$metaprefix$metafile");
			last;
		}
	}
	die "No dependency meta file found" unless $meta;

	my $abstract = $meta->abstract;
	say "Abstract: $abstract";

	my $license = join " ", map {$license_mappings->{$_} or $_} $meta->license;
	say "License: $license";

	my $deps = parse_deps $meta->effective_prereqs->requirements_for('runtime', 'requires');
	say "CPAN deps: $deps";
250
	say "Recommend: " . parse_deps $meta->effective_prereqs->requirements_for('runtime', 'recommends');
251

252
	my $makedeps = parse_deps($meta->effective_prereqs->requirements_for('build', 'requires'), $meta->effective_prereqs->requirements_for('build', 'recommends'));
253
	say "CPAN build deps: $makedeps";
254 255 256 257 258
	say "CPAN requires: " . parse_deps($meta->effective_prereqs->requirements_for('build', 'requires'));
	say "CPAN recommds: " . parse_deps($meta->effective_prereqs->requirements_for('build', 'recommends'));

	my $checkdeps = parse_deps($meta->effective_prereqs->requirements_for('test', 'requires'), $meta->effective_prereqs->requirements_for('test', 'recommends'));
	say "CPAN check deps: $makedeps";
259 260

	my $text = read_file "APKBUILD";
261
	if ($abstract && $abstract ne 'unknown') {
262 263 264
		$text =~ s/^pkgdesc=\"([^\"]*)\"$/pkgdesc=\"$abstract\"/mg or
			die "Can't find cpandepends line in APKBUILD";
	}
265 266 267 268
	if (length(`find $metaprefix -name '*.xs'`)) {
		$text =~ s/^arch=\"([^\"]*)\"$/arch="all"/mg or
			die "Can't find arch line in APKBUILD";
	}
269 270 271 272
	if ($license ne 'unknown') {
		$text =~ s/^license=\"([^\"]*)\"$/license=\"$license\"/mg or
			die "Can't find license line in APKBUILD";
	}
273 274 275 276
	$text =~ s/^cpandepends=\"([^\"]*)\"$/cpandepends=\"$deps\"/mg or
		die "Can't find cpandepends line in APKBUILD";
	$text =~ s/^cpanmakedepends=\"([^\"]*)\"$/cpanmakedepends=\"$makedeps\"/mg or
		die "Can't find cpanmakedepends line in APKBUILD";
277 278
	$text =~ s/^cpancheckdepends=\"([^\"]*)\"$/cpancheckdepends=\"$checkdeps\"/mg or
		die "Can't find cpancheckdepends line in APKBUILD";
279 280 281 282 283 284

	open my $fh, '>', "APKBUILD" or die;
	print $fh $text;
	close $fh;
}

285 286
sub get_data {
	my $apkbuild = read_apkbuild;
287
	$apkbuild->{_pkgreal} or die "Not apkbuild-cpan generated APKBUILD";
Timothy Legge's avatar
Timothy Legge committed
288
	my $response = $ua->get("https://fastapi.metacpan.org/release/$apkbuild->{_pkgreal}");
289 290 291 292
	$response->is_success or die $response->status_line;
	my $distdata = $json->decode($response->decoded_content);
	$distdata->{error} and die "Error trying to locate $apkbuild->{_pkgreal}: $distdata->{error}\n";

293 294 295 296 297 298
	$response = $ua->get("https://fastapi.metacpan.org/module/$distdata->{main_module}");
	$response->is_success or die $response->status_line;
	my $moddata = $json->decode($response->decoded_content);
	$moddata->{error} and die "Error trying to locate $distdata->{main_module}: $moddata->{error}\n";

	return ($apkbuild, $distdata, $moddata);
299 300
}

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

304 305 306
my $user_abuild_conf = read_assignments_from_file($ENV{"HOME"} . "/.abuild/abuild.conf");
$packager = $user_abuild_conf->{PACKAGER} if $user_abuild_conf->{PACKAGER};

307 308 309 310 311 312
given ( $ARGV[0] ) {
	when ("create") {
		my $module = $ARGV[1];
		my $response;
		$module or die "Module name is a mandatory argument";

Timothy Legge's avatar
Timothy Legge committed
313
		$response = $ua->get("https://fastapi.metacpan.org/module/$module");
314 315 316 317
		$response->is_success or die $response->status_line;
		my $moddata = $json->decode($response->decoded_content);
		$moddata->{error} and die "Error trying to locate $module: $moddata->{error}\n";

Timothy Legge's avatar
Timothy Legge committed
318
		$response = $ua->get("https://fastapi.metacpan.org/release/$moddata->{distribution}");
319 320 321 322
		$response->is_success or die $response->status_line;
		my $distdata = $json->decode($response->decoded_content);
		$distdata->{error} and die "Error trying to locate $module: $distdata->{error}\n";

Timothy Legge's avatar
Timothy Legge committed
323
		my $apkname = map_cpan_to_apk $distdata->{metadata}{name};
324 325
		mkdir $apkname;
		chdir $apkname;
326
		write_apkbuild($distdata, undef, $moddata);
327 328 329 330 331
		prepare_tree;
		update_functions;
		do_depends;
	}
	when ("recreate") {
332 333
		my ($apkbuild, $distdata, $moddata) = get_data;
		write_apkbuild($distdata, $apkbuild->{authors}, $moddata);
334 335 336 337 338
		prepare_tree;
		update_functions;
		do_depends;
	}
	when ("upgrade") {
339
		my ($apkbuild, $distdata, $moddata) = get_data;
340

341
		my $pkgver = $moddata->{version};
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
		if ($pkgver != $apkbuild->{pkgver}) {
			say "Upgrading CPAN module from $apkbuild->{pkgver} to $pkgver";

			my $text = read_file "APKBUILD";
			$text =~ s/^pkgver=(.*)$/pkgver=$pkgver/mg or
				die "Can't find pkgver line in APKBUILD";
			$text =~ s/^pkgrel=(.*)$/pkgrel=0/mg;
			open my $fh, '>', "APKBUILD" or die;
			say $fh $text;
			close $fh;

			prepare_tree;
			do_depends;
		} else {
			say "Up-to-data with CPAN";
		}
	}
359
	when ('check') {
360
		my ($apkbuild, $distdata, $moddata) = get_data;
361
		my $pkgver = $moddata->{version};
362 363 364 365
		say "$apkbuild->{pkgname}: Latest version: $pkgver Packaged version: $apkbuild->{pkgver}";
		if ($pkgver ne $apkbuild->{pkgver}) {
			exit(1);
		}
366
	}
367 368 369 370 371
	when ("update") {
		prepare_tree;
		do_depends;
	}
	default {
372
		say "Usage: apkbuild-cpan [create <Module::Name> | check | recreate | update | upgrade]";
373 374 375
		exit;
	}
}