2 # cran2deb turns R packages in to debs, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 1352 2009-01-25 02:04:38Z don $
17 cran2deb - turn a cran package into a rudimentary Debian package
24 --debug, -d debugging level (Default 0)
25 --help, -h display this help
26 --man, -m display manual
34 Debug verbosity. (Default 0)
38 Display brief usage information.
53 use POSIX qw(strftime);
55 use Text::Wrap qw(wrap);
58 use vars qw($DEBUG $VERSION);
62 my %options = (debug => 0,
68 'debug|d+','help|h|?','man|m');
70 pod2usage() if $options{help};
71 pod2usage({verbose=>2}) if $options{man};
73 $DEBUG = $options{debug};
77 # push @USAGE_ERRORS,"You must pass something";
80 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
83 my $DEBVERPREPEND = "0.r2d.";
85 # function for lowcasing package names:
90 $name =~ tr/[A-Z]/[a-z]/;
91 $name =~ s/(\d*)-(\d)/$1\.$2/;
93 #print "lowerCase: $oldname -> $name\n";
98 my ( $pkg, $maintainer ) = @_;
99 my $repository = $pkg->{Repository};
100 my $name = $pkg->{DebName};
102 my $file = $pkg->{BuildDir} . "/debian/rules";
103 my $fh = IO::File->new($file,'w') or die "unable to open $file for writing: $!";
105 print "generating rules ...\n" if $DEBUG;
107 my $year = strftime '%Y',gmtime;
108 my $now = strftime '%a %b %e %Y', gmtime;
112 # debian/rules file for the Debian/GNU Linux package \'r-$repository-$name\'
113 # Copyright 2004-$year by $maintainer
115 # automatically generated on $now by cran2deb (DLA mod)
117 debRreposname=$repository
118 extraInstallFlags=--no-html --no-latex
119 include /usr/share/R/debian/r-cran.mk
120 include /usr/share/cdbs/1/rules/dpatch.mk
121 include /usr/share/dpatch/dpatch.make
123 # if the package is a binary packages, we need to clean.
124 if ( $pkg->{isBinary} ) {
126 "clean::\n\trm -f `find src -name \"*.o\" -o -name \"*.so\"`\n\n";
130 chmod( 0755, $file ) or die "Cannot chmod $file: $!\n";
135 if ( !exists( $pkg->{isBinary} ) ) {
136 if (-e "$pkg->{BuildDir}/src")
138 $pkg->{isBinary} = 1;
141 $pkg->{isBinary} = 0;
147 my ( $pkg, $maintainer) = @_;
148 my $repository = $pkg->{Repository};
150 #print Dumper(\$pkg);
151 my $file = $pkg->{BuildDir} . "/debian/control";
152 my $control = IO::File->new($file,'w') or die "unable to open $file for writing: $!";
153 print "generating control ...\n" if $DEBUG;
157 if ( $pkg->{isBinary} ) {
158 $pkg->{arch} = "any";
159 $pkg->{archdeps} = "\${shlibs:Depends}, ";
162 $pkg->{arch} = "all";
163 $pkg->{archdeps} = "";
167 $dps = join( ", ", @{ $pkg->{debiandependencies} } )
168 if ( defined $pkg->{debiandependencies} );
173 $bdps = join( ", ", @{ $pkg->{debianbuilddependencies} } )
174 if ( defined $pkg->{debianbuilddependencies} );
176 $bdps = ", " . $bdps;
179 print {$control} "Source: $pkg->{DebName}\n";
180 print {$control} "Section: "
182 ( "" eq $pkg->{section} or "main" eq $pkg->{section} )
186 print {$control} "Priority: optional\n"
187 . "Maintainer: $maintainer\n"
188 . "Standards-Version: 3.7.0\n"
189 . "Build-Depends$indep: r-base-dev (>= 2.6.0), debhelper (>> 4.0.0), cdbs"
192 #$Text::Wrap::columns = 720; # No wrapping of the URL
193 if ( exists( $pkg->{URL} ) ) { # URL from DESCRIPTION:
194 print {$control} "Homepage: " . $pkg->{URL} . "\n";
197 print {$control} "\n"
199 . $pkg->{DebNamePackage} . "\n"
201 . $pkg->{arch} . "\n"
204 . " r-base-core (>= 2.6.0) $dps\n";
206 my $p = $pkg->{Package};
209 # print {$control} "Recommends: "
210 # . join( ", ", @{ $main::globalstuff{recommends}{$p} } ) . "\n"
211 # if exists( $main::globalstuff{recommends}{$p} );
212 # print {$control} "Suggests: "
213 # . join( ", ", @{ $main::globalstuff{suggests}{$p} } ) . "\n"
214 # if exists( $main::globalstuff{suggests}{$p} );
216 local $Text::Wrap::columns = 72; # wrap the Description per Debian Policy
218 if ( length( $pkg->{Title} ) < 50 ) {
219 print {$control} "Description: GNU R package \"$pkg->{Title}\"\n";
222 print {$control} "Description: GNU R package \""
223 . substr( $pkg->{Title}, 0, 50 ), "...\"\n";
224 print {$control} wrap( " ", " ", $pkg->{Title}, ".\n" );
229 if ( !defined( $pkg->{Contains} ) ) {
230 $desc = $pkg->{Description};
233 $desc = $pkg->{BundleDescription};
237 print {$control} wrap( " ", " ", $desc, "\n" );
239 print {$control} " .\n"
241 "Author" . ( $pkg->{Author} =~ /,|(\sand\s)/ ? "s" : "" ) . ": ",
242 $pkg->{Author}, "\n" )
243 if exists( $pkg->{Author} );
245 print {$control} wrap( " ", " ", "Date: ", $pkg->{Date}, "\n" )
246 if exists( $pkg->{Date} );
248 if ( exists( $pkg->{Tag} ) ) {
249 if ( defined( $pkg->{Tag} ) ) {
250 print {$control} "Tag: " . $pkg->{Tag} . "\n";
256 sub write_copyright {
257 my ( $pkg, $maintainer ) = @_;
258 my $repository = $pkg->{Repository};
261 if ( !defined( ( $pkg->{License} ) ) ) {
262 $license = "unknown";
265 $license = $pkg->{License};
268 my $file = $pkg->{BuildDir} . "/debian/copyright";
269 my $copyright = IO::File->new($file,'w') or die "unable to open $file for writing: $!";
270 print "generating copyright ...\n" if $DEBUG;
271 print {$copyright} <<EOT;
272 This is the Debian GNU/Linux $pkg->{DebName} package of $pkg->{Package}.
273 It was written by $pkg->{Author}.
275 This package was created by $maintainer
276 using the automated build script cran2deb version $VERSION. Cran2deb
277 is a modified and extended version of Albrecht Gebhardt's build script
278 http://www.math.uni-klu.ac.at/~agebhard/build-R-contrib-debs.pl
279 The package sources were downloaded from
280 http://cran.us.r-project.org/src/contrib/
281 and should also be available on any other mirror of http://cran.r-project.org.
283 The package was renamed from its upstream name \'$pkg->{Package}\' to \'r-cran-$pkg->{DebName}\'
284 to fit the pattern of CRAN packages for R.
286 The Copyright, as asserted in the DESCRIPTION file, is:
289 On a Debian GNU/Linux system, common licenses are included in the directory
290 /usr/share/common-licenses/.
292 For reference, the upstream DESCRIPTION can be found at
293 /usr/lib/R/site-library/$pkg->{Package}/DESCRIPTION
300 my ( $pkg, $maintainer ) = @_;
302 my $file = $pkg->{BuildDir} . "/debian/README.Debian";
303 my $readme = IO::File->new($file,'w') or die "unable to open $file for writing: $!";
304 print "generating README.Debian ...\n" if $DEBUG;
305 my $date = strftime "%a, %e %b %Y %H:%M:%S %z", localtime;
306 my $repository = 'Unknown';
307 my $repository_url = 'an unknown location';
308 if ($pkg->{Repository} =~ /bioc/) {
309 $repository = 'BioConductor project';
310 $repository_url = 'http://www.bioconductor.org/';
312 elsif ($pkg->{Repository} =~ /omegahat/) {
313 $repository = 'Omegahat';
314 $repository_url = 'http://www.omegahat.org/';
317 $repository = 'Comprehensive R Archive Network';
318 $repository_url = 'http://cran.r-project.org/';
320 print {$readme} <<EOF;
321 $pkg->{DebName} for Debian
323 This Debian package was created from sources on the
324 $repository site, accessible at
327 The package was built using the script cran2deb, which was derived from
328 http://www.math.uni-klu.ac.at/~agebhard/build-R-contrib-debs.pl
329 by Albrecht Gebhard. This script is now maintained by the pkg-bioc
331 http://alioth.debian.org/projects/pkg-bioc/
333 Since the packaging was performed in a semi-automated setup, one should
334 not expect the Debian packages to match those in quality that are
335 accessible via the main Debian distribution. At the same time we
336 express our belief that an automated build can closely match the
337 installation via methods intrinsic to R in actuality with a considerably
338 lower administrative burden particularly for a larger number of machines.
340 All users are welcomed to join in and aid improving on the packages
341 or their documentation.
348 sub write_changelog {
349 my ( $pkg, $maintainer) = @_;
351 my $file = $pkg->{BuildDir} . "/debian/changelog";
353 print "generating changelog ...\n" if $DEBUG;
355 my $changelog = IO::File->new($file,'w') or die "unable to open $file for writing: $!";
357 my $date = POSIX::strftime "%a, %e %b %Y %H:%M:%S %z", localtime;
358 print {$changelog} <<EOT;
359 $pkg->{DebName} ($pkg->{DebRelease}) unstable; urgency=low
361 * mechanically generated using cran2deb for $maintainer
370 if (not -e 'DESCRIPTION') {
371 die "Doesn't appear to be an R package";
374 # read in description file
375 my $description_fh = IO::File->new('DESCRIPTION','r') or
376 die "Unable to open DESCRIPTION for reading: $!";
377 my $description = '';
380 $description = <$description_fh>;
382 $description =~ s/^\#[^\n]+//g;
383 $description =~ s/\n\s+//g;
384 my %description = map {/^([^:]+):\s+(.+)/?(lc($1),$2):()} split /\n/, $description;
387 $pkg{BuildDir} = getcwd;
388 $pkg{Repository} = 'cran';
389 $pkg{DebName} = 'r-cran-'.lc($description{package});
390 $pkg{DebNamePackage} = $pkg{DebName};
391 $pkg{Package} = $description{package};
392 $pkg{Author} = $description{author};
393 $pkg{DebRelease} = $description{version}.'-1';
394 $pkg{Title} = $description{title};
395 $pkg{Description} = defined $description{contains}?$description{bundledescription}:$description{description};
396 $pkg{License} = $description{license};
397 $pkg{section} = 'main';
398 mkdir('debian') if not -d 'debian';
399 my $maint = 'Don Armstrong <don@debian.org>';
401 write_control(\%pkg,$maint);
402 write_copyright(\%pkg,$maint);
403 write_rules(\%pkg,$maint);
404 write_readme(\%pkg,$maint);
405 write_changelog(\%pkg,$maint);