/
check.R
3506 lines (3283 loc) · 155 KB
/
check.R
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File src/library/tools/R/check.R
# Part of the R package, http://www.R-project.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
# (at your option) 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 General Public License for more details.
#
# A copy of the GNU General Public License is available at
# http://www.r-project.org/Licenses/
###- R based engine for R CMD check
## R developers can use this to debug the function by running it
## directly as tools:::.check_packages(args), where the args should
## be what commandArgs(TRUE) would return, that is a character vector
## of (space-delimited) terms that would be passed to R CMD checks.
## Used for INSTALL and Rd2pdf
run_Rcmd <- function(args, out = "", env = "")
{
if(.Platform$OS.type == "windows")
system2(file.path(R.home("bin"), "Rcmd.exe"), args, out, out)
else
system2(file.path(R.home("bin"), "R"), c("CMD", args), out, out,
env = env)
}
R_runR <- function(cmd = NULL, Ropts = "", env = "",
stdout = TRUE, stderr = TRUE, stdin = NULL,
arch = "")
{
if (.Platform$OS.type == "windows") {
## workaround Windows problem with input = cmd
if (!is.null(cmd)) {
## In principle this should escape \
Rin <- tempfile("Rin"); on.exit(unlink(Rin)); writeLines(cmd, Rin)
} else Rin <- stdin
suppressWarnings(system2(if(nzchar(arch)) file.path(R.home(), "bin", arch, "Rterm.exe")
else file.path(R.home("bin"), "Rterm.exe"),
c(Ropts, paste("-f", Rin)), stdout, stderr, env = env))
} else {
suppressWarnings(system2(file.path(R.home("bin"), "R"),
c(if(nzchar(arch)) paste0("--arch=", arch), Ropts),
stdout, stderr, stdin, input = cmd, env = env))
}
}
setRlibs <- function(lib0 = "", pkgdir = ".", suggests = FALSE,
libdir = NULL, self = FALSE, self2 = TRUE)
{
WINDOWS <- .Platform$OS.type == "windows"
flink <- function(from, to) {
res <- if(WINDOWS) {
if(TRUE) Sys.junction(from, to)
else file.copy(from, to, recursive = TRUE)
} else file.symlink(from, to)
if (!res) stop("cannot link from ", from)
}
pi <- .split_description(.read_description(file.path(pkgdir, "DESCRIPTION")))
thispkg <- unname(pi$DESCRIPTION["Package"])
## We need to make some assumptions about layout: this version
## asssumes .Library contains standard and recommended packages
## and nothing else.
tmplib <- tempfile("RLIBS_")
dir.create(tmplib)
## Since this is under the session directory and only contains
## symlinks and dummies (hence will be small) we never clean it up.
test_recommended <-
config_val_to_logical(Sys.getenv("_R_CHECK_NO_RECOMMENDED_", "FALSE"))
if(test_recommended) {
## Now add dummies for recommended packages (removed later if declared)
recommended <- .get_standard_package_names()$recommended
## grDevices has :: to KernSmooth
## stats has ::: to Matrix, Matrix depends on lattice
## which gives false positives in MASS and Rcpp
## codetools is really part of tools
exceptions <- "codetools"
if (thispkg %in% c("MASS", "Rcpp"))
exceptions <- c(exceptions, "Matrix", "lattice")
if (thispkg %in%
c("Modalclust", "aroma.core", "iWebPlots",
"openair", "oce", "pcalg", "tileHMM"))
exceptions <- c(exceptions, "KernSmooth")
recommended <- recommended[!recommended %in% exceptions]
for(pkg in recommended) {
if(pkg == thispkg) next
dir.create(pd <- file.path(tmplib, pkg))
file.copy(file.path(.Library, pkg, "DESCRIPTION"), pd)
## to make sure find.package throws an error:
close(file(file.path(pd, "dummy_for_check"), "w"))
}
}
deps <- unique(c(names(pi$Depends), names(pi$Imports), names(pi$LinkingTo),
if(suggests) names(pi$Suggests)))
if(length(libdir) && self2) flink(file.path(libdir, thispkg), tmplib)
## .Library is not necessarily canonical, but the .libPaths version is.
lp <- .libPaths()
poss <- c(lp[length(lp)], .Library)
already <- thispkg
more <- unique(deps[!deps %in% already]) # should not depend on itself ...
while(length(more)) {
m0 <- more; more <- character()
for (pkg in m0) {
if (test_recommended) {
if (pkg %in% recommended) unlink(file.path(tmplib, pkg), TRUE)
## hard-code dependencies for now.
if (pkg == "mgcv")
unlink(file.path(tmplib, c("Matrix", "lattice", "nlme")), TRUE)
if (pkg == "Matrix") unlink(file.path(tmplib, "lattice"), TRUE)
if (pkg == "class") unlink(file.path(tmplib, "MASS"), TRUE)
if (pkg == "nlme") unlink(file.path(tmplib, "lattice"), TRUE)
}
where <- find.package(pkg, quiet = TRUE)
## here we assume that dependencies from .Library are also in .Library
if(length(where) && !(dirname(where) %in% poss)) {
flink(where, tmplib)
pi <- readRDS(file.path(where, "Meta", "package.rds"))
more <- c(more, names(pi$Depends), names(pi$Imports),
names(pi$LinkingTo))
}
}
already <- c(already, m0)
more <- unique(more[!more %in% already])
}
if (self) flink(normalizePath(pkgdir), tmplib)
# print(dir(tmplib))
rlibs <- tmplib
if (nzchar(lib0)) rlibs <- c(lib0, rlibs)
rlibs <- paste(rlibs, collapse = .Platform$path.sep)
c(paste("R_LIBS", rlibs, sep = "="),
if(WINDOWS) " R_ENVIRON_USER='no_such_file'" else "R_ENVIRON_USER=''",
if(WINDOWS) " R_LIBS_USER='no_such_dir'" else "R_LIBS_USER=''",
if(WINDOWS) " R_LIBS_SITE='no_such_dir'" else "R_LIBS_SITE=''")
}
###- The main function for "R CMD check" {currently extends all the way to the end-of-file}
.check_packages <- function(args = NULL)
{
WINDOWS <- .Platform$OS.type == "windows"
## this requires on Windows: file.exe (optional)
wrapLog <- function(...) {
text <- paste(..., collapse = " ")
## strwrap expects paras separated by blank lines.
## Perl's wrap split on \n
text <- strsplit(text, "\n", useBytes = TRUE)[[1L]]
printLog(Log, paste(strwrap(text), collapse = "\n"), "\n")
}
## Used for
## .check_packages_used
## .check_packages_used_in_examples
## .check_packages_used_in_tests
## .check_packages_used_in_vignettes
## checkS3methods
## checkReplaceFuns
## checkFF
## .check_code_usage_in_package (with full set)
## .check_T_and_F (with full set)
## .check_dotInternal (with full set)
## undoc, codoc, codocData, codocClasses
## checkDocFiles, checkDocStyle
## The default set of packages here are as they are because
## .get_S3_generics_as_seen_from_package needs utils,graphics,stats
## Used by checkDocStyle (which needs the generic visible) and checkS3methods.
R_runR2 <-
if(WINDOWS) {
function(cmd,
env = "R_DEFAULT_PACKAGES=utils,grDevices,graphics,stats")
{
out <- R_runR(cmd, R_opts2, env)
## pesky gdata ....
grep("^(ftype: not found|File type)", out,
invert = TRUE, value = TRUE)
}
} else
function(cmd,
env = "R_DEFAULT_PACKAGES='utils,grDevices,graphics,stats'")
{
out <- R_runR(cmd, R_opts2, env)
if (R_check_suppress_RandR_message)
grep('^Xlib: *extension "RANDR" missing on display', out,
invert = TRUE, value = TRUE)
else out
}
dir.exists <- function(x) !is.na(isdir <- file.info(x)$isdir) & isdir
td0 <- Inf # updated below
print_time <- function(t1, t2, Log)
{
td <- t2 - t1
if(td[3L] < td0) return()
td2 <- if (td[3L] > 600) {
td <- td/60
if(WINDOWS) sprintf(" [%dm]", round(td[3L]))
else sprintf(" [%dm/%dm]", round(sum(td[-3L])), round(td[3L]))
} else {
if(WINDOWS) sprintf(" [%ds]", round(td[3L]))
else sprintf(" [%ds/%ds]", round(sum(td[-3L])), round(td[3L]))
}
cat(td2)
if (!is.null(Log) && Log$con > 0L) cat(td2, file = Log$con)
}
parse_description_field <- function(desc, field, default=TRUE)
{
tmp <- desc[field]
if (is.na(tmp)) default
else switch(tmp,
"yes"=, "Yes" =, "true" =, "True" =, "TRUE" = TRUE,
"no" =, "No" =, "false" =, "False" =, "FALSE" = FALSE,
default)
}
check_pkg <- function(pkg, pkgname, pkgoutdir, startdir, libdir, desc,
is_base_pkg, is_rec_pkg, subdirs, extra_arch)
{
## pkg is the argument we received from the main loop.
## pkgdir is the corresponding absolute path,
checkingLog(Log, "package directory")
setwd(startdir)
pkg <- sub("/$", "", pkg)
if (dir.exists(pkg)) {
setwd(pkg) ## wrap in try()?
pkgdir <- getwd()
resultLog(Log, "OK")
} else {
errorLog(Log, "Package directory ", sQuote(pkg), "does not exist.")
do_exit(1L)
}
haveR <- dir.exists("R") && !extra_arch
if (!extra_arch) {
allfiles <- check_file_names()
if (R_check_permissions) check_permissions(allfiles)
check_meta() # Check DESCRIPTION meta-information.
check_top_level()
check_indices()
check_subdirectories(haveR, subdirs)
## Check R code for non-ASCII chars which
## might be syntax errors in some locales.
if (!is_base_pkg && haveR && R_check_ascii_code) check_non_ASCII()
} # end of !extra_arch
## Check we can actually load the package: base is always loaded
if (do_install && pkgname != "base") {
if (this_multiarch) {
Log$stars <<- "**"
for (arch in inst_archs) {
printLog(Log, "* loading checks for arch ", sQuote(arch), "\n")
check_loading(arch)
}
Log$stars <<- "*"
} else {
check_loading()
}
}
if (haveR) {
check_R_code() # unstated dependencies, S3 methods, replacement, foreign
check_R_files(is_rec_pkg) # codetools etc
}
check_Rd_files(haveR)
check_data() # 'data' dir and sysdata.rda
if (!is_base_pkg && dir.exists("src") && !extra_arch) check_src_dir()
if(do_install &&
dir.exists("src") &&
length(so_symbol_names_table)) # suitable OS
check_sos()
miss <- file.path("inst", "doc", c("Rplots.ps", "Rplots.pdf"))
if (any(f <- file.exists(miss))) {
checkingLog(Log, "for left-overs from vignette generation")
warningLog(Log)
printLog(Log,
paste(" file", paste(sQuote(miss[f]), collapse = ", "),
"will not be installed: please remove it\n"))
}
if (dir.exists("inst/doc")) {
if (R_check_doc_sizes) check_doc_size()
else if (as_cran)
warningLog(Log, "'qpdf' is needed for checks on size reduction of PDFs")
}
if (dir.exists("inst/doc") && do_install) check_doc_contents()
setwd(pkgoutdir)
## Run the examples: this will be skipped if installation was
if (dir.exists(file.path(libdir, pkgname, "help"))) {
run_examples()
} else if (dir.exists(file.path(pkgdir, "man"))) {
checkingLog(Log, "examples")
resultLog(Log, "SKIPPED")
}
## Run the package-specific tests.
tests_dir <- file.path(pkgdir, "tests")
if (dir.exists(tests_dir) && # trackObjs has only *.Rin
length(dir(tests_dir, pattern = "\\.(R|Rin)$")))
run_tests()
## Check package vignettes.
setwd(pkgoutdir)
run_vignettes(desc)
} ## end{ check_pkg }
check_file_names <- function()
{
## Check for portable file names.
checkingLog(Log, "for portable file names")
## Build list of exclude patterns.
ignore <- get_exclude_patterns()
ignore_file <- ".Rbuildignore"
if (file.exists(ignore_file))
ignore <- c(ignore, readLines(ignore_file))
## Ensure that the names of the files in the package are valid
## for at least the supported OS types. Under Unix, we
## definitely cannot have '/'. Under Windows, the control
## characters as well as " * : < > ? \ | (i.e., ASCII
## characters 1 to 31 and 34, 36, 58, 60, 62, 63, 92, and 124)
## are or can be invalid. (In addition, one cannot have
## one-character file names consisting of just ' ', '.', or
## '~'.) Based on information by Uwe Ligges, Duncan Murdoch,
## and Brian Ripley.
## In addition, Windows does not allow the following DOS type
## device names (by themselves or with possible extensions),
## see e.g.
## http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp
## http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#naming_conventions
## and http://en.wikipedia.org/wiki/Filename (which as of
## 2007-04-22 is wrong about claiming that COM0 and LPT0 are
## disallowed):
##
## CON: Keyboard and display
## PRN: System list device, usually a parallel port
## AUX: Auxiliary device, usually a serial port
## NUL: Bit-bucket device
## CLOCK$: System real-time clock
## COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9:
## Serial communications ports 1-9
## LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9:
## parallel printer ports 1-9
## In addition, the names of help files get converted to HTML
## file names and so should be valid in URLs. We check that
## they are ASCII and do not contain %, which is what is known
## to cause troubles.
allfiles <- dir(".", all.files = TRUE,
full.names = TRUE, recursive = TRUE)
allfiles <- c(allfiles, unique(dirname(allfiles)))
allfiles <- sub("^./", "", allfiles)
ignore_re <- paste0("(", paste(ignore, collapse = "|"), ")")
allfiles <- grep(ignore_re, allfiles, invert = TRUE, value = TRUE)
bad_files <- allfiles[grepl("[[:cntrl:]\"*/:<>?\\|]",
basename(allfiles))]
is_man <- grepl("man$", dirname(allfiles))
bad <- sapply(strsplit(basename(allfiles[is_man]), ""),
function(x) any(grepl("[^ -~]|%", x)))
if (length(bad))
bad_files <- c(bad_files, (allfiles[is_man])[bad])
bad <- tolower(basename(allfiles))
## remove any extension(s) (see 'Writing R Extensions')
bad <- sub("[.].*", "", bad)
bad <- grepl("^(con|prn|aux|clock[$]|nul|lpt[1-9]|com[1-9])$", bad)
bad_files <- c(bad_files, allfiles[bad])
if (length(bad_files)) {
errorLog(Log)
wrapLog("Found the following file(s) with non-portable file names:\n")
printLog(Log, .format_lines_with_indent(bad_files), "\n")
wrapLog("These are not valid file names",
"on all R platforms.\n",
"Please rename the files and try again.\n",
"See section 'Package structure'",
"in the 'Writing R Extensions' manual.\n")
do_exit(1L)
}
## Next check for name clashes on case-insensitive file systems
## (that is on Windows and (by default) on Mac OS X).
dups <- unique(allfiles[duplicated(tolower(allfiles))])
if (length(dups)) {
errorLog(Log)
wrapLog("Found the following file(s) with duplicate lower-cased file names:\n")
printLog(Log, .format_lines_with_indent(dups), "\n")
wrapLog("File names must not differ just by case",
"to be usable on all R platforms.\n",
"Please rename the files and try again.\n",
"See section 'Package structure'",
"in the 'Writing R Extensions' manual.\n")
do_exit(1L)
}
## NB: the omission of ' ' is deliberate.
non_ASCII_files <-
allfiles[grepl("[^-A-Za-z0-9._!#$%&+,;=@^(){}\'[\\]]", #
basename(allfiles), perl = TRUE)]
if (length(non_ASCII_files)) {
warningLog(Log)
wrapLog("Found the following file(s) with non-portable file names:\n")
printLog(Log, .format_lines_with_indent(non_ASCII_files), "\n")
wrapLog("These are not fully portable file names.\n",
"See section 'Package structure'",
"in the 'Writing R Extensions' manual.\n")
} else resultLog(Log, "OK")
allfiles
}
check_permissions <- function(allfiles)
{
checkingLog(Log, "for sufficient/correct file permissions")
## This used to be much more 'aggressive', requiring that dirs
## and files have mode >= 00755 and 00644, respectively (with
## an error if not), and that files know to be 'text' have
## mode 00644 (with a warning if not). We now only require
## that dirs and files have mode >= 00700 and 00400,
## respectively, and try to fix insufficient permission in the
## INSTALL code (Unix only).
##
## In addition, we check whether files 'configure' and
## 'cleanup' exists in the top-level directory but are not
## executable, which is most likely not what was intended.
## Phase A. Directories at least 700, files at least 400.
bad_files <- character()
## allfiles <- dir(".", all.files = TRUE,
## full.names = TRUE, recursive = TRUE)
## allfiles <- sub("^./", "", allfiles)
if(length(allfiles)) {
mode <- file.info(allfiles)$mode
bad_files <- allfiles[(mode & "400") < as.octmode("400")]
}
if(length(alldirs <- unique(dirname(allfiles)))) {
mode <- file.info(alldirs)$mode
bad_files <- c(bad_files,
alldirs[(mode & "700") < as.octmode("700")])
}
if (length(bad_files)) {
errorLog(Log)
wrapLog("Found the following files with insufficient permissions:\n")
printLog(Log, .format_lines_with_indent(bad_files), "\n")
wrapLog("Permissions should be at least 700 for directories and 400 for files.\nPlease fix permissions and try again.\n")
do_exit(1L)
}
## Phase B. Top-level scripts 'configure' and 'cleanup'
## should really be mode at least 500, or they will not be
## necessarily be used (or should we rather change *that*?)
bad_files <- character()
for (f in c("configure", "cleanup")) {
if (!file.exists(f)) next
mode <- file.info(f)$mode
if ((mode & "500") < as.octmode("500"))
bad_files <- c(bad_files, f)
}
if (length(bad_files)) {
warningLog(Log)
wrapLog("The following files should most likely be executable (for the owner):\n")
printLog(Log, .format_lines_with_indent(bad_files), "\n")
printLog(Log, "Please fix their permissions\n")
} else resultLog(Log, "OK")
}
check_meta <- function()
{
## If we just installed the package (via R CMD INSTALL), we already
## validated most of the package DESCRIPTION metadata. Otherwise,
## let us be defensive about this ...
checkingLog(Log, "DESCRIPTION meta-information")
dfile <- if (is_base_pkg) "DESCRIPTION.in" else "DESCRIPTION"
## FIXME: this does not need to be run in another process
Rcmd <- sprintf("tools:::.check_package_description(\"%s\")", dfile)
out <- R_runR(Rcmd, R_opts2, "R_DEFAULT_PACKAGES=NULL")
if (length(out)) {
errorLog(Log)
printLog(Log, paste(out, collapse="\n"), "\n")
do_exit(1L)
}
any <- FALSE
## Check the encoding.
Rcmd <- sprintf("tools:::.check_package_description_encoding(\"%s\")", dfile)
out <- R_runR(Rcmd, R_opts2, "R_DEFAULT_PACKAGES=NULL")
if (length(out)) {
warningLog(Log)
any <- TRUE
printLog(Log, paste(out, collapse="\n"), "\n")
}
## Check the license.
## For base packages, the DESCRIPTION.in files have non-canonical
## License: Part of R @VERSION@
## entries because these really are a part of R: hence, skip the
## check.
check_license <- if (!is_base_pkg) {
Check_license <- Sys.getenv("_R_CHECK_LICENSE_", NA)
if(is.na(Check_license)) {
## The check code conditionalizes *output* on _R_CHECK_LICENSE_.
Sys.setenv('_R_CHECK_LICENSE_' = "TRUE")
TRUE
} else config_val_to_logical(Check_license)
} else FALSE
if (!identical(check_license, FALSE)) {
Rcmd <- sprintf("tools:::.check_package_license(\"%s\", \"%s\")",
dfile, pkgdir)
## FIXME: this does not need to be run in another process
out <- R_runR(Rcmd, R_opts2, "R_DEFAULT_PACKAGES=NULL")
if (length(out)) {
if (check_license == "maybe") {
if (!any) warningLog(Log)
} else if (any(grepl("^(Standardizable: FALSE|Invalid license file pointers:)",
out))) {
if (!any) warningLog(Log)
} else {
if (!any) noteLog(Log)
}
any <- TRUE
printLog(Log, paste(out, collapse="\n"), "\n")
}
}
## Check Authors@R in case it was not checked as part of
## .check_package_description().
db <- .read_description(dfile)
if(!is.na(aar <- db["Authors@R"]) &&
!is.na(db["Author"]) &&
!is.na(db["Maintainer"])) {
out <- .check_package_description_authors_at_R_field(aar)
if(length(out)) {
if(!any) noteLog(Log)
any <- TRUE
.show_check_package_description_authors_at_R_field_results(out)
}
}
if (!any) resultLog(Log, "OK")
}
check_top_level <- function()
{
checkingLog(Log, "top-level files")
topfiles <- Sys.glob(c("install.R", "R_PROFILE.R"))
if (length(topfiles)) {
warningLog(Log)
printLog(Log, .format_lines_with_indent(topfiles), "\n")
wrapLog("These files are defunct.",
"See manual 'Writing R Extensions'.\n")
} else resultLog(Log, "OK")
}
check_indices <- function()
{
## Check index information.
checkingLog(Log, "index information")
any <- FALSE
if (file.exists("INDEX") &&
!length(readLines("INDEX", warn = FALSE))) {
any <- TRUE
warningLog(Log, "Empty file 'INDEX'.")
}
if (dir.exists("demo")) {
index <- file.path("demo", "00Index")
if (!file.exists(index) ||
!length(readLines(index, warn = FALSE))) {
if(!any) warningLog(Log)
any <- TRUE
printLog(Log,
sprintf("Empty or missing file %s.\n",
sQuote(index)))
} else {
Rcmd <- "options(warn=1)\ntools:::.check_demo_index(\"demo\")\n"
## FIXME: this does not need to be run in another process
out <- R_runR(Rcmd, R_opts2, "R_DEFAULT_PACKAGES=NULL")
if(length(out)) {
if(!any) warningLog(Log)
any <- TRUE
printLog(Log, paste(c(out, ""), collapse="\n"))
}
}
}
if (dir.exists(file.path("inst", "doc"))) {
Rcmd <- "options(warn=1)\ntools:::.check_vignette_index(\"inst/doc\")\n"
## FIXME: this does not need to be run in another process
out <- R_runR(Rcmd, R_opts2, "R_DEFAULT_PACKAGES=NULL")
if(length(out)) {
if(!any) warningLog(Log)
any <- TRUE
printLog(Log, paste(c(out, ""), collapse="\n"))
}
}
if (any)
wrapLog("See the information on INDEX files and package",
"subdirectories in the chapter 'Creating R packages'",
"of the 'Writing R Extensions' manual.\n")
else resultLog(Log, "OK")
}
check_subdirectories <- function(haveR, subdirs)
{
checkingLog(Log, "package subdirectories")
any <- FALSE
if (haveR && !length(list_files_with_type("R", "code"))) {
haveR <- FALSE
warningLog(Log, "Found directory 'R' with no source files.")
any <- TRUE
}
if (R_check_subdirs_nocase) {
## Argh. We often get submissions where 'R' comes out as 'r',
## or 'man' comes out as 'MAN', and we've just ran into 'DATA'
## instead of 'data' (2007-03-31). Maybe we should warn about
## this unconditionally ...
## <FIXME>
## Actually, what we should really do is check whether there is
## any directory with lower-cased name matching a lower-cased
## name of a standard directory, while differing in name.
## </FIXME>
## Watch out for case-insensitive file systems
if ("./r" %in% list.dirs(recursive = FALSE)) {
if (!any) warningLog(Log)
any <- TRUE
printLog(Log, "Found subdirectory 'r'.\n",
"Most likely, this should be 'R'.\n")
}
if ("./MAN" %in% list.dirs(recursive = FALSE)) {
if (!any) warningLog(Log)
any <- TRUE
printLog(Log, "Found subdirectory 'MAN'.\n",
"Most likely, this should be 'man'.\n")
}
if ("./DATA" %in% list.dirs(recursive = FALSE)) {
if (!any) warningLog(Log)
any <- TRUE
printLog(Log, "Found subdirectory 'DATA'.\n",
"Most likely, this should be 'data'.\n")
}
}
all_dirs <- list.dirs(".")
## several packages have had check dirs in the sources, e.g.
## ./languageR/languageR.Rcheck
## ./locfdr/man/locfdr.Rcheck
## ./clustvarsel/inst/doc/clustvarsel.Rcheck
## ./bicreduc/OldFiles/bicreduc.Rcheck
## ./waved/man/waved.Rcheck
## ./waved/..Rcheck
ind <- grepl("\\.Rcheck$", all_dirs)
if(any(ind)) {
if(!any) warningLog(Log)
any <- TRUE
printLog(Log,
"Found the following directory(s) with ",
"names of check directories:\n",
.format_lines_with_indent(all_dirs[ind]),
"\n",
"Most likely, these were included erroneously.\n")
}
## Several packages had leftover Rd2dvi build directories in
## their sources
ind <- grepl("^\\.Rd2(dvi|pdf)", basename(all_dirs))
if(any(ind)) {
if(!any) warningLog(Log)
any <- TRUE
printLog(Log,
"Found the following directory(s) with ",
"names of Rd2pdf build directories:\n",
.format_lines_with_indent(all_dirs[ind]),
"\n",
"Most likely, these were included erroneously.\n")
}
if(!is_base_pkg && (istar || R_check_vc_dirs)) {
## Packages also should not contain version control subdirs
## provided that we check a .tar.gz or know we unpacked one.
ind <- basename(all_dirs) %in% .vc_dir_names
if(any(ind)) {
if(!any) warningLog(Log)
any <- TRUE
printLog(Log,
"Found the following directory(s) with ",
"names of version control directories:\n",
.format_lines_with_indent(all_dirs[ind]),
"\n",
"These should not be in a package tarball.\n")
}
}
if (subdirs != "no") {
Rcmd <- "tools:::.check_package_subdirs(\".\")\n"
## We don't run this in the C locale, as we only require
## certain filenames to start with ASCII letters/digits, and not
## to be entirely ASCII.
out <- R_runR(Rcmd, R_opts2, "R_DEFAULT_PACKAGES=NULL")
if(length(out)) {
if(!any) warningLog(Log)
any <- TRUE
printLog(Log, paste(c(out, ""), collapse="\n"))
wrapLog("Please remove or rename the files.\n",
"See section 'Package subdirectories'",
"in the 'Writing R Extensions' manual.\n")
}
}
## Subdirectory 'data' without data sets?
if (dir.exists("data") &&
!length(list_files_with_type("data", "data"))) {
if (!any) warningLog(Log)
any <- TRUE
printLog(Log, "Subdirectory 'data' contains no data sets.\n")
}
## Subdirectory 'demo' without demos?
if (dir.exists("demo") &&
!length(list_files_with_type("demo", "demo"))) {
if (!any) warningLog(Log)
any <- TRUE
printLog(Log, "Subdirectory 'demo' contains no demos.\n")
}
## Subdirectory 'exec' without files?
if (dir.exists("exec") && !length(dir("exec"))) {
if (!any) warningLog(Log)
any <- TRUE
printLog(Log, "Subdirectory 'exec' contains no files.\n")
}
## Subdirectory 'inst' without files?
if (dir.exists("inst") && !length(dir("inst", recursive = TRUE))) {
if (!any) warningLog(Log)
any <- TRUE
printLog(Log, "Subdirectory 'inst' contains no files.\n")
}
## Subdirectory 'src' without sources?
if (dir.exists("src")) {
## <NOTE>
## If there is a Makefile (or a Makefile.win), we cannot assume
## that source files have the predefined extensions.
## </NOTE>
if (!any(file.exists(file.path("src",
c("Makefile", "Makefile.win"))))) {
if (!length(dir("src", pattern = "\\.([cfmM]|cc|cpp|f90|f95|mm)"))) {
if (!any) warningLog(Log)
printLog(Log, "Subdirectory 'src' contains no source files.\n")
any <- TRUE
}
}
}
## Do subdirectories of 'inst' interfere with R package system
## subdirectories?
if (dir.exists("inst")) {
## These include pre-2.10.0 ones
R_system_subdirs <-
c("Meta", "R", "data", "demo", "exec", "libs",
"man", "help", "html", "latex", "R-ex")
allfiles <- dir("inst", full.names = TRUE)
alldirs <- allfiles[file.info(allfiles)$isdir]
suspect <- basename(alldirs) %in% R_system_subdirs
if (any(suspect)) {
## check they are non-empty
suspect <- alldirs[suspect]
suspect <- suspect[sapply(suspect, function(x) {
length(dir(x, all.files = TRUE)) > 2L
})]
if (length(suspect)) {
if (!any) warningLog(Log)
any <- TRUE
wrapLog("Found the following non-empty",
"subdirectories of 'inst' also",
"used by R:\n")
printLog(Log, paste(c(suspect, ""), collapse="\n"))
wrapLog("It is recommended not to interfere",
"with package subdirectories used by R.\n")
}
}
}
## Valid NEWS.Rd?
nfile <- file.path("inst", "NEWS.Rd")
if(file.exists(nfile)) {
## Catch all warning and error messages.
## We use the same construction in at least another place,
## so maybe factor out a common utility function
## .try_catch_all_warnings_and_errors
## eventually.
## For testing package NEWS.Rd files, we really need a real
## QC check function eventually ...
.warnings <- NULL
.error <- NULL
withCallingHandlers(tryCatch(.build_news_db_from_package_NEWS_Rd(nfile),
error = function(e)
.error <<- conditionMessage(e)),
warning = function(e) {
.warnings <<- c(.warnings,
conditionMessage(e))
invokeRestart("muffleWarning")
})
msg <- c(.warnings, .error)
if(length(msg)) {
if(!any) warningLog(Log)
any <- TRUE
printLog(Log, "Problems with news in 'inst/NEWS.Rd':\n")
printLog(Log,
paste(" ",
unlist(strsplit(msg, "\n", fixed = TRUE)),
sep = "", collapse = "\n"),
"\n")
}
}
## Valid CITATION metadata?
if (file.exists(file.path("inst", "CITATION"))) {
Rcmd <- "tools:::.check_citation(\"inst/CITATION\")\n"
out <- R_runR(Rcmd, R_opts2, "R_DEFAULT_PACKAGES=utils")
if(length(out)) {
if(!any) warningLog(Log)
any <- TRUE
printLog(Log,
"Invalid citation information in 'inst/CITATION':\n")
printLog(Log, .format_lines_with_indent(out), "\n")
}
}
## CITATION files in non-standard places?
## Common problems: rather than inst/CITATION, have
## CITATION
## CITATION.txt
## inst/doc/CITATION
## Of course, everything in inst is justifiable, so only give a
## note for now.
files <- dir(".", pattern = "^CITATION.*", recursive = TRUE)
files <- files[file_path_sans_ext(basename(files)) == "CITATION" &
files != file.path("inst", "CITATION")]
if(length(files)) {
if(!any) noteLog(Log)
any <- TRUE
wrapLog("Found the following CITATION file(s) in a non-standard place:\n")
printLog(Log, .format_lines_with_indent(files), "\n")
wrapLog("Most likely 'inst/CITATION' should be used instead.\n")
}
if(!any) resultLog(Log, "OK")
}
check_non_ASCII <- function()
{
checkingLog(Log, "R files for non-ASCII characters")
out <- R_runR("tools:::.check_package_ASCII_code('.')",
R_opts2, "R_DEFAULT_PACKAGES=NULL")
if (length(out)) {
warningLog(Log)
wrapLog("Found the following files with",
"non-ASCII characters:\n")
printLog(Log, .format_lines_with_indent(out), "\n")
wrapLog("Portable packages must use only ASCII",
"characters in their R code,\n",
"except perhaps in comments.\n",
"Use \\uxxxx escapes for other characters.\n")
} else resultLog(Log, "OK")
checkingLog(Log, "R files for syntax errors")
Rcmd <- "options(warn=1);tools:::.check_package_code_syntax(\"R\")"
out <- R_runR(Rcmd, R_opts2, "R_DEFAULT_PACKAGES=NULL")
if (any(grepl("^Error", out))) {
errorLog(Log)
printLog(Log, paste(c(out, ""), collapse = "\n"))
do_exit(1L)
} else if (length(out)) {
warningLog(Log)
printLog(Log, paste(c(out, ""), collapse = "\n"))
} else resultLog(Log, "OK")
}
check_R_code <- function()
{
if (!is_base_pkg) {
checkingLog(Log, "for unstated dependencies in R code")
if (do_install) {
Rcmd <- paste("options(warn=1, showErrorCalls=FALSE)\n",
sprintf("tools:::.check_packages_used(package = \"%s\")\n", pkgname))
out <- R_runR2(Rcmd, "R_DEFAULT_PACKAGES=NULL")
if (length(out)) {
warningLog(Log)
printLog(Log, paste(c(out, ""), collapse = "\n"))
wrapLog(msg_DESCRIPTION)
} else resultLog(Log, "OK")
} else {
## this needs to read the package code, and will fail on
## syntax errors such as non-ASCII code.
Rcmd <- paste("options(warn=1, showErrorCalls=FALSE)\n",
sprintf("tools:::.check_packages_used(dir = \"%s\")\n", pkgdir))
out <- R_runR(Rcmd, R_opts2, "R_DEFAULT_PACKAGES=NULL")
if (length(out)) {
warningLog(Log)
printLog(Log, paste(c(out, ""), collapse = "\n"))
wrapLog(msg_DESCRIPTION)
} else resultLog(Log, "OK")
}
}
## Check whether methods have all arguments of the corresponding
## generic.
checkingLog(Log, "S3 generic/method consistency")
Rcmd <- paste("options(warn=1)\n",
"options(expressions=1000)\n",
if (do_install)
sprintf("tools::checkS3methods(package = \"%s\")\n", pkgname)
else
sprintf("tools::checkS3methods(dir = \"%s\")\n", pkgdir))
out <- R_runR2(Rcmd)
if (length(out)) {
warningLog(Log)
printLog(Log, paste(c(out, ""), collapse = "\n"))
wrapLog("See section 'Generic functions and methods'",
"of the 'Writing R Extensions' manual.\n")
} else resultLog(Log, "OK")
## Check whether replacement functions have their final argument
## named 'value'.
checkingLog(Log, "replacement functions")
Rcmd <- paste("options(warn=1)\n",
if (do_install)
sprintf("tools::checkReplaceFuns(package = \"%s\")\n", pkgname)
else
sprintf("tools::checkReplaceFuns(dir = \"%s\")\n", pkgdir))
out <- R_runR2(Rcmd)
if (length(out)) {
## <NOTE>
## We really want to stop if we find offending replacement
## functions. But we cannot use error() because output may
## contain warnings ...
warningLog(Log)
## </NOTE>
printLog(Log, paste(c(out, ""), collapse = "\n"))
wrapLog("The argument of a replacement function",
"which corresponds to the right hand side",
"must be named 'value'.\n")
} else resultLog(Log, "OK")
## Check foreign function calls.
## The neverending story ...
## For the time being, allow to turn this off by setting the environment
## variable _R_CHECK_FF_CALLS_ to an empty value.
if (nzchar(Sys.getenv("_R_CHECK_FF_CALLS_", "true"))) {
checkingLog(Log, "foreign function calls")
Rcmd <- paste("options(warn=1)\n",
if (do_install)
sprintf("tools::checkFF(package = \"%s\")\n", pkgname)
else
sprintf("tools::checkFF(dir = \"%s\")\n", pkgdir))
out <- R_runR2(Rcmd)
if (length(out)) {
warningLog(Log)
printLog(Log, paste(c(out, ""), collapse = "\n"))
wrapLog("See the chapter 'System and foreign language interfaces'",
"of the 'Writing R Extensions' manual.\n")
} else resultLog(Log, "OK")
}
}
check_R_files <- function(is_rec_pkg)
{
checkingLog(Log, "R code for possible problems")
if (!is_base_pkg) {
Rcmd <- "options(warn=1);tools:::.check_package_code_shlib(\"R\")"
out <- R_runR(Rcmd, R_opts2, "R_DEFAULT_PACKAGES=NULL")
if (length(out)) {
errorLog(Log)