]> git.donarmstrong.com Git - cran2deb.git/blob - trunk/R/getrpkg.R
9c87c379460d28ecbc990241cb3fd287cadbd9e9
[cran2deb.git] / trunk / R / getrpkg.R
1 setup <- function() {
2     # set up the working directory
3     tmp <- tempfile('cran2deb')
4     dir.create(tmp)
5     return (tmp)
6 }
7
8 cleanup <- function(dir) {
9     # remove the working directory
10     unlink(dir,recursive=T)
11     invisible()
12 }
13
14 download_pkg <- function(dir, pkgname) {
15     # download pkgname into dir, and construct some metadata
16
17     # record some basic information
18     pkg <- pairlist()
19     pkg$date_stamp = Sys.time()
20     pkg$name = pkgname
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)
25     }
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']
29
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)
43     } else {
44         # see if we have a local mirror in /srv/R
45         use_local = FALSE
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)
52         }
53
54         fn <- paste(pkgname, '_', pkg$version, '.tar.gz', sep='')
55         archive <- file.path(dir, fn)
56
57         if (use_local) {
58             file.copy(localfn, archive)
59         } else {
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)))
65             if (ret != 0) {
66                 fail('failed to download',url)
67             }
68             # end of download.packages replacement
69         }
70
71         if (length(grep('\\.\\.',archive)) || normalizePath(archive) != archive) {
72             fail('funny looking path',archive)
73         }
74         pkg$path = sub("_\\.(zip|tar\\.gz)", ""
75                       ,gsub(.standard_regexps()$valid_package_version, ""
76                       ,archive))
77         pkg$archive = archive
78         # this is not a Debian conformant archive
79         pkg$need_repack = TRUE
80     }
81     return(pkg)
82 }
83
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)
88                    ,paste(pkg$srcname
89                          ,pkg$version
90                          ,sep='-'))
91     file.rename(pkg$path, debpath)
92     pkg$path = debpath
93     debarchive = file.path(dirname(pkg$archive)
94                           ,paste(pkg$srcname,'_'
95                                 ,pkg$version,'.orig.tar.gz'
96                                 ,sep=''))
97     wd <- getwd()
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))
102                 ,'-type f -a '
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)
108     }
109     # tar it all back up
110     log_system('tar -czf',shQuote(debarchive),shQuote(basename(pkg$path)))
111     setwd(wd)
112     file.remove(pkg$archive)
113     pkg$archive = debarchive
114     pkg$need_repack = FALSE
115     return(pkg)
116 }
117
118 prepare_pkg <- function(dir, pkgname) {
119     # download and extract an R package named pkgname
120
121     # based loosely on library/utils/R/packages2.R::install.packages
122
123     # grab the archive and some metadata
124     pkg <- download_pkg(dir, pkgname)
125
126     # now extract the archive
127     if (!length(grep('\\.tar\\.gz',pkg$archive))) {
128         fail('archive is not tarball')
129     }
130     wd <- getwd()
131     setwd(dir)
132     ret = log_system('tar','xzf',shQuote(pkg$archive))
133     setwd(wd)
134     if (ret != 0) {
135         fail('Extraction of archive',pkg$archive,'failed.')
136     }
137
138     # if necessary, repack the archive into Debian-conformant format
139     if (pkg$need_repack) {
140         pkg <- repack_pkg(pkg)
141     }
142     if (!file.info(pkg$path)[,'isdir']) {
143         fail(pkg$path,'is not a directory and should be.')
144     }
145
146     # extract the DESCRIPTION file, which contains much metadata
147     pkg$description = read.dcf(file.path(pkg$path,'DESCRIPTION'))
148
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')
157         }
158     }
159
160     # note subtly of short circuit operators (no absorption)
161     if (pkg$description[1,'Package'] != pkg$name) {
162         fail('package name mismatch')
163     }
164     return(pkg)
165 }
166