2015-06-12 01:39:48 +01:00
|
|
|
#!/usr/bin/env Rscript
|
|
|
|
|
2014-11-21 13:23:36 +00:00
|
|
|
library(data.table)
|
|
|
|
library(parallel)
|
|
|
|
cl <- makeCluster(10)
|
|
|
|
|
2015-05-23 01:51:24 +01:00
|
|
|
mirrorType <- commandArgs(trailingOnly=TRUE)[1]
|
2015-06-16 11:58:58 +01:00
|
|
|
stopifnot(mirrorType %in% c("bioc","cran"))
|
2015-02-25 22:43:43 +00:00
|
|
|
|
2015-06-16 11:58:58 +01:00
|
|
|
packagesFile <- paste(mirrorType, 'packages.nix', sep='-')
|
2015-05-23 01:51:24 +01:00
|
|
|
readFormatted <- as.data.table(read.table(skip=6, sep='"', text=head(readLines(packagesFile), -1)))
|
2015-02-25 22:43:43 +00:00
|
|
|
|
2015-06-16 11:58:58 +01:00
|
|
|
mirrorUrls <- list( bioc="http://bioconductor.statistik.tu-dortmund.de/packages/3.2/bioc/src/contrib/"
|
|
|
|
, cran="http://cran.r-project.org/src/contrib/"
|
|
|
|
)
|
|
|
|
mirrorUrl <- mirrorUrls[mirrorType][[1]]
|
|
|
|
knownPackages <- lapply(mirrorUrls, function(url) as.data.table(available.packages(url, filters=c("R_version", "OS_type", "duplicates"))))
|
|
|
|
pkgs <- knownPackages[mirrorType][[1]]
|
|
|
|
setkey(pkgs, Package)
|
|
|
|
knownPackages <- c(unique(do.call("rbind", knownPackages)$Package))
|
|
|
|
knownPackages <- sapply(knownPackages, gsub, pattern=".", replacement="_", fixed=TRUE)
|
|
|
|
|
2014-11-21 13:23:36 +00:00
|
|
|
nixPrefetch <- function(name, version) {
|
2015-02-25 22:43:43 +00:00
|
|
|
prevV <- readFormatted$V2 == name & readFormatted$V4 == version
|
|
|
|
if (sum(prevV) == 1) as.character(readFormatted$V6[ prevV ]) else
|
2015-05-23 01:51:24 +01:00
|
|
|
system(paste0("nix-prefetch-url --type sha256 ", mirrorUrl, name, "_", version, ".tar.gz"), intern=TRUE)
|
2014-11-21 13:23:36 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 11:58:58 +01:00
|
|
|
formatPackage <- function(name, version, sha256, depends, imports, linkingTo) {
|
2014-11-21 13:23:36 +00:00
|
|
|
attr <- gsub(".", "_", name, fixed=TRUE)
|
2015-06-16 11:58:58 +01:00
|
|
|
depends <- paste( if (is.na(depends)) "" else gsub("[ \t\n]+", "", depends)
|
|
|
|
, if (is.na(imports)) "" else gsub("[ \t\n]+", "", imports)
|
|
|
|
, if (is.na(linkingTo)) "" else gsub("[ \t\n]+", "", linkingTo)
|
|
|
|
, sep=","
|
|
|
|
)
|
|
|
|
depends <- unlist(strsplit(depends, split=",", fixed=TRUE))
|
2014-11-21 13:23:36 +00:00
|
|
|
depends <- sapply(depends, gsub, pattern="([^ \t\n(]+).*", replacement="\\1")
|
|
|
|
depends <- sapply(depends, gsub, pattern=".", replacement="_", fixed=TRUE)
|
2015-06-16 11:58:58 +01:00
|
|
|
depends <- depends[depends %in% knownPackages]
|
|
|
|
depends <- paste(sort(unique(depends)), collapse=" ")
|
2014-11-21 13:23:36 +00:00
|
|
|
paste0(attr, " = derive { name=\"", name, "\"; version=\"", version, "\"; sha256=\"", sha256, "\"; depends=[", depends, "]; };")
|
|
|
|
}
|
|
|
|
|
2015-06-16 11:58:58 +01:00
|
|
|
clusterExport(cl, c("nixPrefetch","readFormatted", "mirrorUrl", "knownPackages"))
|
2014-11-21 13:23:36 +00:00
|
|
|
|
2015-05-23 01:51:24 +01:00
|
|
|
pkgs <- as.data.table(available.packages(mirrorUrl, filters=c("R_version", "OS_type", "duplicates")))
|
2014-11-21 13:23:36 +00:00
|
|
|
pkgs <- pkgs[order(Package)]
|
|
|
|
pkgs$sha256 <- parApply(cl, pkgs, 1, function(p) nixPrefetch(p[1], p[2]))
|
|
|
|
|
2015-06-16 11:58:58 +01:00
|
|
|
nix <- apply(pkgs, 1, function(p) formatPackage(p[1], p[2], p[18], p[4], p[5], p[6]))
|
2014-11-21 13:23:36 +00:00
|
|
|
|
2015-05-23 01:51:24 +01:00
|
|
|
cat("# This file is generated from generate-r-packages.R. DO NOT EDIT.\n")
|
2014-11-21 13:23:36 +00:00
|
|
|
cat("# Execute the following command to update the file.\n")
|
|
|
|
cat("#\n")
|
2015-06-16 11:58:58 +01:00
|
|
|
cat(paste("# Rscript generate-r-packages.R", mirrorType, ">new && mv new", packagesFile))
|
2015-05-23 01:51:24 +01:00
|
|
|
cat("\n\n")
|
2014-11-21 13:23:36 +00:00
|
|
|
cat("{ self, derive }: with self; {\n")
|
|
|
|
cat(paste(nix, collapse="\n"), "\n")
|
|
|
|
cat("}\n")
|
|
|
|
|
|
|
|
stopCluster(cl)
|