summaryrefslogtreecommitdiffstats
path: root/cpan2tgz
blob: 9584a70bcf47b71a6cd5ba0f5ddb0fc8e3fdca95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
173
174
175
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
202
203
204
205
206
207
208
209
210
211
212
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/perl
# 
# cpan2tgz - create slackware packages from cpan distributions 
# 
# Jason Woodward
# woodwardj at jaos dot org
# http://software.jaos.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
use strict;
use warnings;
use Config;
use CPAN;
use Getopt::Long;

our $VERSION = "0.2";

$ENV{PATH} = "/bin:/sbin:/usr/bin:/usr/sbin";
my (%PACKAGE_CACHE_LIST,$recursive,$install_after_building,$ignore_installed_deps);

usage() unless
	GetOptions(
		"recursive" => \$recursive,
		"ignoreinstalled" => \$ignore_installed_deps,
		"install"	=> \$install_after_building,
	);

usage() unless (scalar(@ARGV) > 0);

foreach my $mod (@ARGV){
	do_package($mod);
}

sub do_package {
	my ($module_name) = @_;
	return unless $module_name;

	my $module = CPAN::Shell->expand('Module',$module_name);
	unless($module){
		print STDERR "\nFailed to find module: $module_name","\n";
		exit(1);
	}

	print "\n\nProcessing $module_name...\n\n";
	$PACKAGE_CACHE_LIST{$module->id()} = 1;

	# try to extract some info about the module
	my $pkg_name = "perl-" . lc((split('/',$module->cpan_file))[-1]);
	my $final_pkg_version = (split('-',$pkg_name))[-1];
	(my $final_pkg_name = $pkg_name) =~ s/\-$final_pkg_version//;
	$pkg_name =~ s/\.tar.*?$//;
	if( $module->xs_file() ){
		$pkg_name .= "-" . [split('-',$Config{archname})]->[0];
	}else{
		$pkg_name .= "-noarch";
	}
	$pkg_name .= "-1.tgz";
	$pkg_name = lc($pkg_name);

	my $dest_dir = "/tmp/" . $module->id();

	# do an initial make so that we can get the dep info
	my $pack = $CPAN::META->instance('CPAN::Distribution',$module->cpan_file());
	die "Failed to initialize CPAN::Distribution object for $module_name: $!" unless $pack;
	eval { $pack->make() or die $!; };
	if( $@ ){
		print "make ERROR [$module_name]: $!\n";
		exit(1);
	}

	my @deps;
	if( $ignore_installed_deps ){
		# only get the deps that are not installed
		@deps = grep { m/\w+/; } map { eval "no warnings 'all'; use $_;"; if($@){ $_ } } keys %{$pack->prereq_pm()};
	}else{
		# get all dependencies
		@deps = keys %{$pack->prereq_pm()};
	}
	# look at module prerequisites
	if( @deps > 0 ){
		if($recursive){
			print "\n\nhandling " . scalar(@deps)
				. " deps for $module_name: ",join(", ",@deps),"\n\n";
			sleep(1);
			foreach my $dep ( @deps ){
				local $@;
				next if defined $PACKAGE_CACHE_LIST{$dep};
				print "FIXME: I don't deal with xsloader b/c make ?n/a?\n",next if $dep =~ m/xsloader/i;
				eval "no warnings 'all';use $dep;";
				next if ( $ignore_installed_deps && !$@ );
				do_package($dep);
			}
		}else{
			print "\n\nStopping.\n[", $module->id(), "] requires ", join(", ",@deps),"\n";
			exit(1);
		}
	}

	clear_build_dir($dest_dir);
	
	# here we do steps to cleanup source before we build (per LinuxPackages.net perfect packages instructions)
	system("cd " . $pack->dir() . " && chown -R root.root .");
	system("cd " . $pack->dir() . " && find . -perm 777 -exec chmod 755 {} \\;");
	system("cd " . $pack->dir() . " && find . -perm 555 -exec chmod 755 {} \\;");
	system("cd " . $pack->dir() . " && find . -perm 444 -exec chmod 644 {} \\;");
	system("cd " . $pack->dir() . " && find . -perm 666 -exec chmod 644 {} \\;");
	system("cd " . $pack->dir() . " && find . -perm 664 -exec chmod 644 {} \\;");

	# install to the package build dir
	system("cd " . $pack->dir() . " && make install DESTDIR=$dest_dir");
	die "Failed to install to $dest_dir: $!" unless ( -d $dest_dir );

	# copy documentation to the package build dir
	system("cd $dest_dir && mkdir -p ./usr/doc/$final_pkg_name-" . $module->cpan_version);
	system("cd " . $pack->dir() . " && find . -iregex '.*readme.*' -o -iregex '.*change.*' -o -iregex '.*todo.*' -o -iregex '.*license.*' -o -iregex '.*copying.*' -o -iregex '.*install.*' -o -iregex '.*\.txt' -o -iregex '.*\.html' |xargs -r -iZ cp Z $dest_dir/usr/doc/$final_pkg_name-" . $module->cpan_version . "/");

	# build a shell script to fixup the package like Pat's Perl SlackBuild
	open(my $script_fh,">$dest_dir/build.sh") or die "Failed to open build.sh for writing: $!";
	print $script_fh "cd $dest_dir\n";
	print $script_fh "find . | xargs file | grep 'executable' | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null\n";
	print $script_fh "find . | xargs file | grep 'shared object' | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null\n";
	print $script_fh "find ./usr/share/man/ -name '*.3' -exec gzip -9 {} \\; 2> /dev/null\n";
	print $script_fh "find ./usr/share/man/ -name '*.1' -exec gzip -9 {} \\; 2> /dev/null\n";
	print $script_fh "mv ./usr/share/man ./usr/\n" if( -d "$dest_dir/usr/share/man" );
	print $script_fh "chown -R root.bin ./usr/bin\n" if( -d "$dest_dir/usr/bin" );
	print $script_fh "chmod 755 ./usr/bin/*\n" if( -d "$dest_dir/usr/bin");
	print $script_fh "chmod 644 ./usr/man/man?/*\n" if( -d "$dest_dir/usr/man" );
	print $script_fh "rmdir ./usr/share\n" if( -d "$dest_dir/usr/share" );
	print $script_fh "mkdir ./install\n";
	# generate the doinst.sh to fix perllocal.pod
	print $script_fh <<SCRIPT
PERLLOCALPOD=\`find . -name perllocal.pod\`
cat >./install/doinst.sh <<EOF
#!/bin/sh

cat >> \${PERLLOCALPOD/.\\//} <<PLP

EOF
cat \$PERLLOCALPOD >>install/doinst.sh
echo "PLP" >>install/doinst.sh
rm \$PERLLOCALPOD
SCRIPT
;

	close($script_fh);
	system("cd $dest_dir && sh build.sh");
	system("cd $dest_dir && rm build.sh");

	
	# generate the slack-required file
	open(my $required_fh,">$dest_dir/install/slack-required") or die "Failed to open slack-required for writing: $!";
	print $required_fh "perl\n";
	# use the hashref here so we can get the version
	my $deps = $pack->prereq_pm();
	foreach my $perl_mod_dep ( keys %{$deps} ){
		my $version = $deps->{$perl_mod_dep};
		(my $pmd = $perl_mod_dep) =~ s/\:\:/\-/g;
		if( int($version) > 0 ){
			print $required_fh "perl-$pmd >= $version\n";
		}else{
			print $required_fh "perl-$pmd\n";
		}
	}
	close($required_fh);

	# generate the slack-desc file
	open(my $desc_fh,">$dest_dir/install/slack-desc") or die "Failed to open slack-desc for writing: $!";
	print $desc_fh "\n";
	print $desc_fh "# HOW TO EDIT THIS FILE:\n";
	print $desc_fh "# The \"handy ruler\" below makes it easier to edit a package description.  Line\n";
	print $desc_fh "# up the first '|' above the ':' following the base package name, and the '|'\n";
	print $desc_fh "# on the right side marks the last column you can put a character in.  You must\n";
	print $desc_fh "# make exactly 11 lines for the formatting to be correct.  It's also\n";
	print $desc_fh "# customary to leave one space after the ':'.\n";
	print $desc_fh "\n";
	print $desc_fh "         |-----handy-ruler------------------------------------------------------|\n";
	print $desc_fh "$final_pkg_name: $final_pkg_name " . $module->cpan_version() . " (Perl module)\n";
	print $desc_fh "$final_pkg_name:  \n";
	print $desc_fh "$final_pkg_name:  Packaged by cpan2tgz\n";
	print $desc_fh "$final_pkg_name:  \n";
	print $desc_fh "$final_pkg_name:  cpan2tgz by Jason Woodward <woodwardj\@jaos.org>\n";
	print $desc_fh "$final_pkg_name:  \n";
	print $desc_fh "$final_pkg_name:  \n";
	print $desc_fh "$final_pkg_name:  \n";
	print $desc_fh "$final_pkg_name:  \n";
	print $desc_fh "$final_pkg_name:  http://software.jaos.org/\n";
	print $desc_fh "$final_pkg_name:  \n";
	close($desc_fh);

	# finally, build the package
	system("cd $dest_dir && makepkg -l y -c n /usr/src/$pkg_name");
	die "Failed to build package $pkg_name: $!" unless ( -f "/usr/src/$pkg_name" );

	# install the package
	if( $recursive or $install_after_building ){
		system("installpkg /usr/src/$pkg_name");
	}

	clear_build_dir($dest_dir);

}

sub usage {
	print "Jason Woodward <woodwardj at jaos dot org>\n";
	print "Usage: $0 [option(s)] [modules(s)]\n";
	print "  --recursive        build required modules","\n";
	print "                     this requires installing required modules\n";
	print "                     as it traverses the dependency tree (DANGEROUS)\n";
	print "  --ignoreinstalled  don't build installed required modules","\n";
	print "                     this only works with --recursive\n";
	print "  --install          always install the generated package\n";
	print "                     this is on for --recursive by default\n";

	exit(1);
}

sub clear_build_dir {
	my ($dest_dir) = @_;
	return unless $dest_dir;
	system("rm -r $dest_dir/*") if ( -d $dest_dir );
	system("rmdir $dest_dir") if ( -d $dest_dir );
}

1;

__END__

=head1 NAME

cpan2tgz - create Slackware GNU/Linux packages from CPAN Perl module distributions

=head1 SYNOPSIS

cpan2tgz [option(s)] [module(s)]

options: [--recursive] [--ignoreinstalled] [--install]

=head1 DESCRIPTION

cpan2tgz is a tool to create Slackware GNU/Linux packages from CPAN Perl module distributions.  cpan2tgz was inspired by cpan2rpm.

=head1 OPTIONS

=over

=item B<--recursive>

Download the package only.  Do not install/upgrade.  Packages are downloaded to /var/slapt-get/.   This will build required modules and then install them as it traverses the dependency tree of the specified Perl module.  This can be dangerous.  It might be best to install one at a time.

=item B<--ignoreinstalled>

This option specifies not to build packages for required modules if the module is found to be present and installed on the system.  This only works in conjunction with --recursive.

=item B<--install>

Always install the generated package.  This is the default behavior with --recursive.

=item B<module(s)>

A series of Perl modules to install, ie Data::Dumper

=head1 NOTES

cpan2tgz uses the CPAN Perl module.  A working CPAN/Config.pm is assumed.  If CPAN has not been initialized prior to running cpan2tgz, it will be initialized on the first run.

=head1 AUTHOR

Jason Woodward <woodwardj@jaos.org>

http://software.jaos.org/

=head1 SEE ALSO

installpkg(8), makepkg(8), removepkg(8), pkgtool(8), upgradepkg(8),

=pod OSNAMES

any

=pod SCRIPT CATEGORIES

Unix/System_administration

=cut