]> git.donarmstrong.com Git - cran2deb.git/blob - trunk/R/getrpkg.R
For the largish BioConductor files one has
[cran2deb.git] / trunk / R / getrpkg.R
1
2 curl.maxtime<-60*60  # 60 minutes max download time (some bioconductor packages are truly big and take time)
3 curl.retries<-0      # No retries (connections are commonly good enough)
4
5 setup <- function() {
6     # set up the working directory
7     tmp <- tempfile('cran2deb')
8     dir.create(tmp)
9     return (tmp)
10 }
11
12 cleanup <- function(dir) {
13     # remove the working directory
14     unlink(dir,recursive=T)
15     invisible()
16 }
17
18 download_pkg <- function(dir, pkgname) {
19     # download pkgname into dir, and construct some metadata
20
21     # record some basic information
22     pkg <- pairlist()
23     pkg$date_stamp = Sys.time()
24     pkg$name = pkgname
25     pkg$repoURL = available[pkgname,'Repository']
26     pkg$repo = repourl_as_debian(pkg$repoURL)
27     if (!length(grep('^[A-Za-z0-9][A-Za-z0-9+.-]+$',pkg$name))) {
28         fail('Cannot convert package name into a Debian name',pkg$name)
29     }
30     pkg$srcname = pkgname_as_debian(pkg$name,binary=F)
31     pkg$debname = pkgname_as_debian(pkg$name,repo=pkg$repo)
32     pkg$version <- available[pkgname,'Version']
33
34     # see if we have already built this release and uploaded it.
35     debfn <- file.path(pbuilder_results, paste(pkg$srcname, '_', pkg$version, '.orig.tar.gz', sep=''))
36     pkg$need_repack = FALSE
37     if (file.exists(debfn)) {
38         # if so, use the existing archive. this is good for three reasons:
39         # 1. it saves downloading the archive again
40         # 2. the repacking performed below changes the MD5 sum of the archive
41         #    which upsets some Debian archive software.
42         # 3. why repack more than once?
43         pkg$archive <- file.path(dir, basename(debfn))
44         file.copy(debfn,pkg$archive)
45         pkg$path = file.path(dir, paste(pkg$srcname ,pkg$version ,sep='-'))
46         notice('using an existing debianized source tarball:',debfn)
47     } else {
48         # see if we have a local mirror in /srv/R
49         use_local = FALSE
50         if (pkg$repo == 'cran') {
51             localfn = file.path('/srv/R/Repositories/CRAN/src/contrib',paste(pkg$name,'_',pkg$version,'.tar.gz',sep=''))
52             use_local = file.exists(localfn)
53         } else if (pkg$repo == 'bioc') {
54             localfn = file.path('/srv/R/Repositories/Bioconductor/release/bioc/src/contrib',paste(pkg$name,'_',pkg$version,'.tar.gz',sep=''))
55             use_local = file.exists(localfn)
56         }
57
58         fn <- paste(pkgname, '_', pkg$version, '.tar.gz', sep='')
59         archive <- file.path(dir, fn)
60
61         if (use_local) {
62             file.copy(localfn, archive)
63         } else {
64             # use this instead of download.packages as it is more resilient to
65             # dodgy network connections (hello BT 'OpenWorld', bad ISP)
66             url <- paste(available[pkgname,'Repository'], fn, sep='/')
67             # don't log the output -- we don't care!
68             ret <- system(paste('curl','-o',shQuote(archive),
69                           paste('-m',curl.maxtime,'--retry',curl.retries,sep=' '),
70                           shQuote(url)))
71             if (ret != 0) {
72                 fail('failed to download',url)
73             }
74             # end of download.packages replacement
75         }
76
77         if (length(grep('\\.\\.',archive)) || normalizePath(archive) != archive) {
78             fail('funny looking path',archive)
79         }
80         pkg$path = sub("_\\.(zip|tar\\.gz)", ""
81                       ,gsub(.standard_regexps()$valid_package_version, ""
82                       ,archive))
83         pkg$archive = archive
84         # this is not a Debian conformant archive
85         pkg$need_repack = TRUE
86     }
87     return(pkg)
88 }
89
90 repack_pkg <- function(pkg) {
91     # re-pack into a Debian-named archive with a Debian-named directory.
92     notice('repacking into debian source archive.')
93     debpath = file.path(dirname(pkg$archive)
94                    ,paste(pkg$srcname
95                          ,pkg$version
96                          ,sep='-'))
97     file.rename(pkg$path, debpath)
98     pkg$path = debpath
99     debarchive = file.path(dirname(pkg$archive)
100                           ,paste(pkg$srcname,'_'
101                                 ,pkg$version,'.orig.tar.gz'
102                                 ,sep=''))
103     wd <- getwd()
104     setwd(dirname(pkg$path))
105     # remove them pesky +x files
106     # BUT EXCLUDE configure and cleanup
107     log_system('find',shQuote(basename(pkg$path))
108                 ,'-type f -a '
109                 ,   '! \\( -name configure -o -name cleanup \\)'
110                 ,'-exec chmod -x {} \\;')
111     if (file.exists(file.path(basename(pkg$path),'debian'))) {
112         warn('debian/ directory found in tarball! removing...')
113         unlink(file.path(basename(pkg$path),'debian'),recursive=TRUE)
114     }
115     # tar it all back up
116     log_system('tar -czf',shQuote(debarchive),shQuote(basename(pkg$path)))
117     setwd(wd)
118     file.remove(pkg$archive)
119     pkg$archive = debarchive
120     pkg$need_repack = FALSE
121     return(pkg)
122 }
123
124 prepare_pkg <- function(dir, pkgname) {
125     # download and extract an R package named pkgname
126
127     # based loosely on library/utils/R/packages2.R::install.packages
128
129     # grab the archive and some metadata
130     pkg <- download_pkg(dir, pkgname)
131
132     # now extract the archive
133     if (!length(grep('\\.tar\\.gz',pkg$archive))) {
134         fail('archive is not tarball')
135     }
136     wd <- getwd()
137     setwd(dir)
138     ret = log_system('tar','xzf',shQuote(pkg$archive))
139     setwd(wd)
140     if (ret != 0) {
141         fail('Extraction of archive',pkg$archive,'failed.')
142     }
143
144     # if necessary, repack the archive into Debian-conformant format
145     if (pkg$need_repack) {
146         pkg <- repack_pkg(pkg)
147     }
148     if (!file.info(pkg$path)[,'isdir']) {
149         fail(pkg$path,'is not a directory and should be.')
150     }
151
152     # extract the DESCRIPTION file, which contains much metadata
153     pkg$description = read.dcf(file.path(pkg$path,'DESCRIPTION'))
154
155     # ensure consistency of version numbers
156     if ('Version' %in% names(pkg$description[1,])) {
157         if (pkg$description[1,'Version'] != available[pkg$name,'Version']) {
158             # should never happen since available is the basis upon which the
159             # package is retrieved.
160             error('available version:',available[pkg$name,'Version'])
161             error('package version:',pkg$description[1,'Version'])
162             fail('inconsistency between R package version and cached R version')
163         }
164     }
165
166     # note subtly of short circuit operators (no absorption)
167     if (pkg$description[1,'Package'] != pkg$name) {
168         fail('package name mismatch')
169     }
170     return(pkg)
171 }
172