Repository navigation
Expand file tree
/
Copy pathcran2pkgfile.R
More file actions
224 lines (189 loc) · 6.24 KB
/
Copy pathcran2pkgfile.R
File metadata and controls
224 lines (189 loc) · 6.24 KB
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
#!/usr/bin/env Rscript
# input arguments order
args <- commandArgs(trailingOnly = TRUE)
module <- args[1]
cliopt <- args[2]
depth <- args[3]
rds_path <- args[4]
conf_file <- args[5]
# get cranrepo and maintainer information; don't hard code it
source(paste0(conf_file))
# set cran repo
options(repos = c(CRAN = cranrepo.url))
# This must be generated in advance by repos2db.R
pkgsdb <- readRDS(paste0(rds_path, "pkgsdb.rds"))
# is a package on CRAN or BioC?
on.cran <- function(x) {
result <- try(pkgsdb[x, "Package"], silent = T)
if (inherits(result, 'try-error')) {
return(NULL)
}
return(result)
}
# stop already if input does not exist
if (is.null(on.cran(module))) {
stop(paste0("'", module, "'", " cannot be found on CRAN or Bioconductor.", "\n"))
}
# leave only deps available on CRAN
cran.available <- function(x){
y <- c()
for (i in x) {
j <- on.cran(i)
y <- c(y, j)
}
return(y)
}
# prepare deps
names.only <- function(x){
x <- gsub("[\r\n]", " ", x)
x <- gsub(",", ", ", x)
x <- strsplit(x, ", ")
# remove within brackets
y <- c()
for (i in x[[1]]) {
i <- gsub(" ", "", as.character(i))
j <- gsub("\\s*\\([^\\)]+\\)","",as.character(i))
y <- c(y, j)
}
# in case deps are just comma-separated (depa,depb)
y <- strsplit(y, ",")
return(y)
}
# Depends on
depends.on <- function(package = NULL,
pkgs.db = NULL){
depends <- names.only(pkgs.db[package, "Depends"])
imports <- names.only(pkgs.db[package, "Imports"])
linking <- names.only(pkgs.db[package, "LinkingTo"])
dependencies <- c(depends, imports, linking)
y <- sort(unique(cran.available(dependencies)))
return(y)
}
# Optional
optional <- function(package = NULL,
pkgs.db = NULL){
optional <- names.only(pkgs.db[package, "Suggests"])
y <- sort(cran.available(optional))
return(y)
}
# Upstream repo
upstream <- function(package = NULL,
pkgs.db = NULL){
upstream <- pkgs.db[package, "Repository"]
upstream <- unlist(strsplit(upstream, "\\/"))[3]
if(upstream == "cloud.r-project.org"){
u <- "CRAN"
}else if(upstream == "bioconductor.org"){
u <- "BioC"
} else {
u <- "UNKNOWN"
}
return(u)
}
# This is able to retrieve deps of multiple packages
depsofdeps <- function(modules = NULL, pkgsdb = NULL, opts = NULL){
deep <- c(modules)
for (i in deep) {
d <- depends.on(i, pkgsdb)
if (isTRUE(opts)) {
o <- optional(i, pkgsdb)
d <- unique(c(d, o))
}
deep <- c(deep, d)
deep <- sort(unique(deep))
}
return(deep)
}
# This runs depsofdeps recursively with a number of iterations, deep resolving deps
# TODO: make this more intelligent to stop automatically when all deps are resolved.
deepdeps <- function(package = NULL,
pkgs.db = NULL,
iterations = 5,
opts = NULL){
# start with just the one
deep <- depsofdeps(package, pkgsdb)
for (n in 1:iterations) {
deep <- depsofdeps(deep, pkgsdb = pkgsdb, opts)
nam <- paste0("deep.", n)
assign(nam, deep)
deep <- depsofdeps(deep, pkgsdb = pkgsdb, opts)
}
return(deep)
}
# prepare for "Depends on" or "Optional" rows in the Pkgfile
pkgfile.style <- function(x){
y <- c()
for (i in x) {
j <- paste0("r4-", tolower(i))
y <- c(y, j)
}
return(y)
}
# try to get the url right for the Pkgfile URL field
get.url <- function(x){
if (x == paste0(bioc.url, bioc.version, "/bioc/src/contrib") |
x == paste0(bioc.url, bioc.version, "/data/annotation/src/contrib") |
x == paste0(bioc.url, bioc.version, "/data/experiment/src/contrib") |
x == paste0(bioc.url, bioc.version, "/workflows/src/contrib") |
x == paste0(bioc.url, bioc.version, "/books/src/contrib")) {
module.url <- "https://bioconductor.org/packages/"
}else{
module.url <- "https://cran.r-project.org/web/packages/"
}
return(module.url)
}
# Write the Pkgfile
pkgfile.write <- function(module = NULL){
maintainer <- maintainer.info
# Preserve upper case, as well as original version format of the module
modules.dep <- depends.on(module, pkgsdb)
modules.opt <- optional(module, pkgsdb)
modules.ver <- pkgsdb[module, "Version"]
# polish for Pkgfile's fields
pkgfile.dsc <- paste("R-package", module)
pkgfile.url <- paste0(get.url(pkgsdb[module, "Repository"]), module)
pkgfile.mnt <- maintainer
pkgfile.dep <- gsub(",", "", gsub("\\.", "-", toString(pkgfile.style(modules.dep))))
pkgfile.opt <- gsub(",", "", gsub("\\.", "-", toString(pkgfile.style(modules.opt))))
pkgfile.nam <- paste0("r4-", tolower(gsub("\\.", "-", module)))
pkgfile.ver <- gsub("-", "\\.", modules.ver)
if (modules.ver == pkgfile.ver) {
modules.ver <- "${version}"
}
pkgfile.rel <- "1"
pkgfile.src <- paste0(pkgsdb[module, "Repository"], "/", module, "_", modules.ver, ".tar.gz")
pkgfile <- paste0("# Description: ", pkgfile.dsc, "\n",
"# URL: ", pkgfile.url, "\n",
"# Maintainer: ", pkgfile.mnt, "\n",
"# Depends on: r ", pkgfile.dep, "\n",
"# Optional: ", pkgfile.opt, "\n",
"\n",
"name=", pkgfile.nam, "\n",
"version=", pkgfile.ver, "\n",
"release=", pkgfile.rel, "\n",
"source=", "(", pkgfile.src, ")", "\n",
"\n",
"build() {", "\n",
"\t", "cd ", module, "\n",
"\t", "mkdir -p $PKG/usr/lib/R/library", "\n",
"\t", "R CMD INSTALL . -l $PKG/usr/lib/R/library", "\n",
"}")
dir.create(pkgfile.nam)
write(pkgfile, paste0(pkgfile.nam, "/", "Pkgfile"))
cat(" [", upstream(module, pkgsdb), "]", "Created port for", module, ":", pkgfile.nam, "\n")
}
# write the ports
if (cliopt == "-r" | cliopt == "--recursive") {
modules.all <- deepdeps(module, pkgsdb, depth, opts = FALSE)
for (d in modules.all){
pkgfile.write(d)
}
}else if(cliopt == "-ro" | cliopt == "--recursive-opt") {
cat("=== Generating optional dependencies recursively, as well. Be patient.", "\n")
modules.all <- deepdeps(module, pkgsdb, depth, opts = TRUE)
for (d in modules.all){
pkgfile.write(d)
}
}else{
pkgfile.write(module)
}