This file is indexed.

/usr/bin/dh-make-R is in dh-r 20180403.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env perl

use feature say;
use strict;
use Debian::Debhelper::Buildsystem::R qw(parse_deps);
use Dpkg::Control;
use Dpkg::Deps qw(deps_concat deps_parse);
use Getopt::Long;
use Pod::Usage;

sub maybe_write {
    my $fname = shift;
    my $content = shift;
    if ( -e $fname ) {
        say "W: $fname already exists, not rewriting";
        return 0;
    } else {
        open(my $fh, ">", $fname);
        say $fh $content;
        close $fh;
        say "I: writing $fname";
        return 1;
    }
}


my $opt_team = '';
my $opt_repo = '';
my $opt_help = 0;
my $opt_tests = '';

GetOptions('team=s' => \$opt_team, 'repo=s' => \$opt_repo,
           'help|?' => \$opt_help, 'tests=s' => \$opt_tests);
pod2usage(1) if $opt_help;

( -e "DESCRIPTION") or die "No DESCRIPTION file, is this an R package?";
my $desc = Dpkg::Control->new(type => Dpkg::Control::CTRL_UNKNOWN);
$desc->load("DESCRIPTION");

my $repo = "other";
if ($opt_repo) {
    $repo = $opt_repo;
    say "I: Using repo=$repo from --repo";
} elsif (length $desc->{Repository}) {
    # this appears to be set ("CRAN") for packages originating from CRAN,
    # but is not set for bioconductor, nor for packages direct from upstream
    $repo = $desc->{Repository};
    say "I: Using repo=$repo from DESCRIPTION::Repository";
} elsif (length $desc->{biocViews}) {
    # however, biocViews is (presumably) only going to be set for bioconductor
    # packages, so nonzero should identify
    $repo = "BIOC";
    say "I: Using repo=$repo due to existence of DESCRIPTION::biocViews";
} else {
    say "I: Using repo=$repo by default";
}

my $debname = "r-" . lc $repo . "-" . lc $desc->{Package};
say "I: Package source and binary name: $debname";

my @aptavail = qx/grep-aptavail -P -s Package -n -e ^r-/;
my %apthash;
@apthash{@aptavail} = ();

my $rdepends = deps_concat(Debian::Debhelper::Buildsystem::R::parse_depends("Depends", $desc->{Depends}, \%apthash));
my $rrecommends = deps_concat(Debian::Debhelper::Buildsystem::R::parse_depends("Recommends", $desc->{Recommends}, \%apthash));
my $rsuggests = deps_concat(Debian::Debhelper::Buildsystem::R::parse_depends("Suggests", $desc->{Suggests}, \%apthash));
my $rimports = deps_concat(Debian::Debhelper::Buildsystem::R::parse_depends("Imports", $desc->{Imports}, \%apthash));
my $builddeps;
if ( $rdepends ) {
    $builddeps = deps_concat("debhelper (>= 11~)", "dh-r", "r-base-dev", $rdepends, $rimports);
} else {
    $builddeps = deps_concat("debhelper (>= 11~)", "dh-r", "r-base-dev", $rimports);
}

say "I: Package depends: $rdepends";
say "I: Package imports: $rimports";
say "I: Package recommends: $rrecommends";
say "I: Package suggests: $rsuggests";
say "I: Package build-depends: $builddeps";

my $year = 1900 + (localtime)[5];
my $compiled = lc $desc->{NeedsCompilation} eq "yes";
my $arch =  $compiled ? "any" : "all";
if ($compiled) {
    say "W: This package NeedsCompilation: there are probably extra dependencies required";
    say "W: Package SystemRequirements: '$desc->{SystemRequirements}'";
}

my $longdesc = $desc->{Description};
$longdesc =~ s/^\s*/ /gm;

my $homepage = "";
if ($repo eq "CRAN") {
    $homepage = "https://cran.r-project.org/package=$desc->{Package}";
} elsif ($repo eq "BIOC") {
    $homepage = "https://bioconductor.org/packages/$desc->{Package}/";
} elsif ($repo eq "other") {
    $homepage = $desc->{URL};
}

my $maintainer = "$ENV{DEBFULLNAME} <$ENV{DEBEMAIL}>";
my $uploaders = "";
my $vcs = "";
my $vcsbrowser = "";

if ($opt_team eq "science") {
    say "I: Generating maintainers, uploaders, VCS entries for debian-science team";
    $uploaders = $maintainer;
    $maintainer = "Debian Science Maintainers <debian-science-maintainers\@lists.alioth.debian.org>";
    $vcsbrowser = "https://salsa.debian.org/r-pkg-team/$debname";
    $vcs = "https://salsa.debian.org/r-pkg-team/$debname.git";
} elsif ($opt_team eq "med") {
    say "I: Generating maintainers, uploaders, VCS entries for debian-med team";
    $uploaders = $maintainer;
    $maintainer = "Debian Med Packaging Team <debian-med-packaging\@lists.alioth.debian.org>";
    $vcsbrowser = "https://salsa.debian.org/r-pkg-team/$debname";
    $vcs = "https://salsa.debian.org/r-pkg-team/$debname.git";
}


if (! -d "debian" ) {
    mkdir "debian";
    mkdir "debian/source";
    mkdir "debian/tests";
    say "I: Creating debian/";
}

my $dcontrol = <<"EOF";
Source: $debname
Maintainer: $maintainer
Uploaders: $uploaders
Section: gnu-r
Testsuite: autopkgtest-pkg-r
Priority: optional
Build-Depends: $builddeps
Standards-Version: 4.1.3
Vcs-Browser: $vcsbrowser
Vcs-Git: $vcs
Homepage: $homepage

Package: $debname
Architecture: $arch
Depends: \${R:Depends}, \${shlibs:Depends}, \${misc:Depends}
Recommends: \${R:Recommends}
Suggests: \${R:Suggests}
Description: $desc->{Title}
$longdesc
EOF

maybe_write("debian/control", $dcontrol);
maybe_write("debian/compat", "11");
maybe_write("debian/source/format", "3.0 (quilt)");

my $drules = <<"EOF";
#!/usr/bin/make -f

%:
\tdh \$\@ --buildsystem R
EOF

maybe_write("debian/rules", $drules);
chmod 0755, "debian/rules";

my $dwatch = "version=4\n";
if ($repo eq "CRAN") {
    $dwatch .= "https://cran.r-project.org/src/contrib/" . $desc->{Package} . '_([-\d.]*)\.tar\.gz';
} elsif ($repo eq "BIOC") {
    $dwatch .= 'opts=downloadurlmangle=s?^(.*)\.\.?https:$1packages/release/bioc? \\' . "\nhttps://bioconductor.org/packages/release/bioc/html/$desc->{Package}.html .*/$desc->{Package}_(.*).tar.gz";
} elsif ($repo eq "other") {
    $dwatch .= "FIXME: $homepage";
}

maybe_write("debian/watch", $dwatch);

my $dcopy = <<"EOF";
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: $desc->{Package}
Upstream-Contact: $desc->{Maintainer}
Source: $homepage

Files: *
Copyright: (FIXME: year) $desc->{Author}
License: $desc->{License}
Comment: $desc->{Copyright}

Files: debian/*
Copyright: $year $ENV{DEBFULLNAME} <$ENV{DEBEMAIL}>
License: $desc->{License}
Comment: Same license as upstream package

License: $desc->{License}
  FIXME: insert the appropriate license text
EOF

maybe_write("debian/copyright", $dcopy);

if ( -e "debian/changelog") {
    say "W: debian/changelog already exists, not rewriting";
} else {
    qx/dch --create --package $debname -v $desc->{Version}-1 "Initial release (closes: #xxxxxx)"/;
    say "I: Writing debian/changelog";
}

my $test_generic = <<'EOF';
#!/bin/sh -e

for testfile in tests/*.R; do
    echo "BEGIN TEST $testfile"
    LC_ALL=C R --no-save < $testfile
done
EOF

my $test_run_unit_test = <<"EOF";
#!/bin/sh -e

pkgname=$desc->{Package}
debname=$debname

if [ "\$AUTOPKGTEST_TMP" = "" ] ; then
    AUTOPKGTEST_TMP=`mktemp -d /tmp/\${debname}-test.XXXXXX`
    trap "rm -rf \$AUTOPKGTEST_TMP" 0 INT QUIT ABRT PIPE TERM
fi
cd \$AUTOPKGTEST_TMP
cp -a /usr/share/doc/\$debname/tests/* \$AUTOPKGTEST_TMP
gunzip -r *
for testfile in *.R; do
    echo "BEGIN TEST \$testfile"
    LC_ALL=C R --no-save < \$testfile
done
EOF

my $test_bioc = <<"EOF";
#!/bin/sh -e

LC_ALL=C R --no-save -e 'BiocGenerics:::testPackage("$desc->{Package}")'
EOF

my $test_vignette = <<"EOF";
#!/bin/sh -e

for vignette in \$(find vignettes -iname '*.rnw' -or -iname '*.rmd'); do
    echo "BEGIN VIGNETTE \$vignette"
    LC_ALL=C R CMD Sweave \$vignette
done
EOF

my %autopkgtests;

# detect common test dependencies
my $testpkgs = "@,";
if ($rsuggests =~ /testthat/) {
    $testpkgs .= " r-cran-testthat,";
}
if ($rsuggests =~ /runit/) {
    $testpkgs .= " r-cran-runit,";
}

if ($opt_tests =~ /generic/) {
    maybe_write("debian/tests/generic", $test_generic);
    $autopkgtests{"generic"} = $testpkgs;
}
if ($opt_tests =~ /run-unit-test/) {
    maybe_write("debian/tests/run-unit-test", $test_run_unit_test);
    maybe_write("debian/docs", "debian/tests/run-unit-test\ntests");
    $autopkgtests{"run-unit-test"} = $testpkgs;
}
if ($opt_tests =~ /bioc/) {
    maybe_write("debian/tests/bioc", $test_bioc);
    $autopkgtests{"bioc"} = $testpkgs . " r-bioc-biocgenerics";
}
if ($opt_tests =~ /vignette/) {
    maybe_write("debian/tests/vignette", $test_vignette);
    if ($rsuggests =~ /knitr/) {
        $autopkgtests{"vignette"} = "@, r-cran-knitr";
    } else {
        $autopkgtests{"vignette"} = "@";
    }
}

my $testcontrol = "";
foreach my $key (keys %autopkgtests) {
    $testcontrol .= "Tests: $key\n";
    $testcontrol .= "Depends: $autopkgtests{$key}\n";
    $testcontrol .= "Restrictions: allow-stderr\n\n";
}

maybe_write("debian/tests/control", $testcontrol);


__END__

=head1 NAME

dh-make-R - Generates a debian/ directory skeleton for R packages

=head1 SYNOPSIS

dh-make-R [options]

 Options:
    --help
    --team TEAM          Set packaging team
    --repo REPO          Override R repository detection
    --tests TEST1,TEST2  Generate autopkgtest templates

=head1 OPTIONS

=over 8

=item B<--help>

Print this help message.

=item B<--team>

Generate default maintainer, uploaders, VCS URLs for the given packaging team.
Understood values are "science" and "med" for debian-science and debian-med
respectively.

=item B<--repo>

Set the R repository (used in the package name), overriding automatic detection.

=item B<--tests>

Comma-separated list of test templates to generate. Understood values are
"generic" (run tests/*.R), "run-unit-test" (installs tests as documentation,
can be run outside autopkgtest), "bioc" (runs BiocGenerics::testPackage) and
"vignette" (attempts to rebuild documentation).

=back

=head1 DESCRIPTION

B<dh-make-R> should be run from the root of an unpacked R tarball (ie, the
directory containing DESCRIPTION). Values from this are used to generate
default control, compat, copyright, rules, source/format and watch files.