2 # set up the working directory
3 tmp <- tempfile('cran2deb')
8 cleanup <- function(dir) {
9 # remove the working directory
10 unlink(dir,recursive=T)
14 download_pkg <- function(dir, pkgname) {
15 # download pkgname into dir, and construct some metadata
17 # record some basic information
19 pkg$date_stamp = Sys.time()
21 pkg$repoURL = available[pkgname,'Repository']
22 pkg$repo = repourl_as_debian(pkg$repoURL)
23 if (!length(grep('^[A-Za-z0-9][A-Za-z0-9+.-]+$',pkg$name))) {
24 fail('Cannot convert package name into a Debian name',pkg$name)
26 pkg$srcname = pkgname_as_debian(pkg$name,binary=F)
27 pkg$debname = pkgname_as_debian(pkg$name,repo=pkg$repo)
28 pkg$version <- available[pkgname,'Version']
30 # see if we have already built this release and uploaded it.
31 debfn <- file.path(pbuilder_results, paste(pkg$srcname, '_', pkg$version, '.orig.tar.gz', sep=''))
32 pkg$need_repack = FALSE
33 if (file.exists(debfn)) {
34 # if so, use the existing archive. this is good for three reasons:
35 # 1. it saves downloading the archive again
36 # 2. the repacking performed below changes the MD5 sum of the archive
37 # which upsets some Debian archive software.
38 # 3. why repack more than once?
39 pkg$archive <- file.path(dir, basename(debfn))
40 file.copy(debfn,pkg$archive)
41 pkg$path = file.path(dir, paste(pkg$srcname ,pkg$version ,sep='-'))
42 notice('using an existing debianized source tarball:',debfn)
44 # see if we have a local mirror in /srv/R
46 if (pkg$repo == 'cran') {
47 localfn = file.path('/srv/R/Repositories/CRAN/src/contrib',paste(pkg$name,'_',pkg$version,'.tar.gz',sep=''))
48 use_local = file.exists(localfn)
49 } else if (pkg$repo == 'bioc') {
50 localfn = file.path('/srv/R/Repositories/Bioconductor/release/bioc/src/contrib',paste(pkg$name,'_',pkg$version,'.tar.gz',sep=''))
51 use_local = file.exists(localfn)
54 fn <- paste(pkgname, '_', pkg$version, '.tar.gz', sep='')
55 archive <- file.path(dir, fn)
58 file.copy(localfn, archive)
60 # use this instead of download.packages as it is more resilient to
61 # dodgy network connections (hello BT 'OpenWorld', bad ISP)
62 url <- paste(available[pkgname,'Repository'], fn, sep='/')
63 # don't log the output -- we don't care!
64 ret <- system(paste('curl','-o',shQuote(archive),'-m 720 --retry 5',shQuote(url)))
66 fail('failed to download',url)
68 # end of download.packages replacement
71 if (length(grep('\\.\\.',archive)) || normalizePath(archive) != archive) {
72 fail('funny looking path',archive)
74 pkg$path = sub("_\\.(zip|tar\\.gz)", ""
75 ,gsub(.standard_regexps()$valid_package_version, ""
78 # this is not a Debian conformant archive
79 pkg$need_repack = TRUE
84 repack_pkg <- function(pkg) {
85 # re-pack into a Debian-named archive with a Debian-named directory.
86 notice('repacking into debian source archive.')
87 debpath = file.path(dirname(pkg$archive)
91 file.rename(pkg$path, debpath)
93 debarchive = file.path(dirname(pkg$archive)
94 ,paste(pkg$srcname,'_'
95 ,pkg$version,'.orig.tar.gz'
98 setwd(dirname(pkg$path))
99 # remove them pesky +x files
100 # BUT EXCLUDE configure and cleanup
101 log_system('find',shQuote(basename(pkg$path))
103 , '! \\( -name configure -o -name cleanup \\)'
104 ,'-exec chmod -x {} \\;')
105 if (file.exists(file.path(basename(pkg$path),'debian'))) {
106 warn('debian/ directory found in tarball! removing...')
107 unlink(file.path(basename(pkg$path),'debian'),recursive=TRUE)
110 log_system('tar -czf',shQuote(debarchive),shQuote(basename(pkg$path)))
112 file.remove(pkg$archive)
113 pkg$archive = debarchive
114 pkg$need_repack = FALSE
118 prepare_pkg <- function(dir, pkgname) {
119 # download and extract an R package named pkgname
121 # based loosely on library/utils/R/packages2.R::install.packages
123 # grab the archive and some metadata
124 pkg <- download_pkg(dir, pkgname)
126 # now extract the archive
127 if (!length(grep('\\.tar\\.gz',pkg$archive))) {
128 fail('archive is not tarball')
132 ret = log_system('tar','xzf',shQuote(pkg$archive))
135 fail('Extraction of archive',pkg$archive,'failed.')
138 # if necessary, repack the archive into Debian-conformant format
139 if (pkg$need_repack) {
140 pkg <- repack_pkg(pkg)
142 if (!file.info(pkg$path)[,'isdir']) {
143 fail(pkg$path,'is not a directory and should be.')
146 # extract the DESCRIPTION file, which contains much metadata
147 pkg$description = read.dcf(file.path(pkg$path,'DESCRIPTION'))
149 # ensure consistency of version numbers
150 if ('Version' %in% names(pkg$description[1,])) {
151 if (pkg$description[1,'Version'] != available[pkg$name,'Version']) {
152 # should never happen since available is the basis upon which the
153 # package is retrieved.
154 error('available version:',available[pkg$name,'Version'])
155 error('package version:',pkg$description[1,'Version'])
156 fail('inconsistency between R package version and cached R version')
160 # note subtly of short circuit operators (no absorption)
161 if (pkg$description[1,'Package'] != pkg$name) {
162 fail('package name mismatch')