]> git.donarmstrong.com Git - cran2deb.git/blob - tags/gsoc_final/R/getrpkg.R
3c7e0af2cfb5b02f023a50bf5262ebc5bf43ab9e
[cran2deb.git] / tags / gsoc_final / 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 = format(Sys.time(),'%a, %d %b %Y %H:%M:%S %z')
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     } else {
43         # use this instead of download.packages as it is more resilient to
44         # dodgy network connections (hello BT 'OpenWorld', bad ISP)
45         fn <- paste(pkgname, '_', pkg$version, '.tar.gz', sep='')
46         url <- paste(available[pkgname,'Repository'], fn, sep='/')
47         archive <- file.path(dir, fn)
48         # don't log the output -- we don't care!
49         ret <- system(paste('curl','-o',shQuote(archive),'-m 720 --retry 5',shQuote(url)))
50         if (ret != 0) {
51             fail('failed to download',url)
52         }
53         # end of download.packages replacement
54
55         if (length(grep('\\.\\.',archive)) || normalizePath(archive) != archive) {
56             fail('funny looking path',archive)
57         }
58         pkg$path = sub("_\\.(zip|tar\\.gz)", ""
59                       ,gsub(.standard_regexps()$valid_package_version, ""
60                       ,archive))
61         pkg$archive = archive
62         # this is not a Debian conformant archive
63         pkg$need_repack = TRUE
64     }
65     return(pkg)
66 }
67
68 repack_pkg <- function(pkg) {
69     # re-pack into a Debian-named archive with a Debian-named directory.
70     debpath = file.path(dirname(pkg$archive)
71                    ,paste(pkg$srcname
72                          ,pkg$version
73                          ,sep='-'))
74     file.rename(pkg$path, debpath)
75     pkg$path = debpath
76     debarchive = file.path(dirname(pkg$archive)
77                           ,paste(pkg$srcname,'_'
78                                 ,pkg$version,'.orig.tar.gz'
79                                 ,sep=''))
80     wd <- getwd()
81     setwd(dirname(pkg$path))
82     # remove them pesky +x files
83     # BUT EXCLUDE configure and cleanup
84     log_system('find',shQuote(basename(pkg$path))
85                 ,'-type f -a '
86                 ,   '! \\( -name configure -o -name cleanup \\)'
87                 ,'-exec chmod -x {} \\;')
88     # tar it all back up
89     log_system('tar -czf',shQuote(debarchive),shQuote(basename(pkg$path)))
90     setwd(wd)
91     file.remove(pkg$archive)
92     pkg$archive = debarchive
93     pkg$need_repack = FALSE
94     return(pkg)
95 }
96
97 prepare_pkg <- function(dir, pkgname) {
98     # download and extract an R package named pkgname
99     # OR the bundle containing pkgname
100
101     # based loosely on library/utils/R/packages2.R::install.packages
102
103     # first a little trick; change pkgname if pkgname is contained in a bundle
104     if (!(pkgname %in% rownames(available))) {
105         bundle <- r_bundle_of(pkgname)
106         if (is.null(bundle)) {
107             fail('package',pkgname,'is unavailable')
108         }
109         pkgname <- bundle
110     }
111
112     # grab the archive and some metadata
113     pkg <- download_pkg(dir, pkgname)
114
115     # now extract the archive
116     if (!length(grep('\\.tar\\.gz',pkg$archive))) {
117         fail('archive is not tarball')
118     }
119     wd <- getwd()
120     setwd(dir)
121     ret = log_system('tar','xzf',shQuote(pkg$archive))
122     setwd(wd)
123     if (ret != 0) {
124         fail('Extraction of archive',pkg$archive,'failed.')
125     }
126
127     # if necessary, repack the archive into Debian-conformant format
128     if (pkg$need_repack) {
129         pkg <- repack_pkg(pkg)
130     }
131     if (!file.info(pkg$path)[,'isdir']) {
132         fail(pkg$path,'is not a directory and should be.')
133     }
134
135     # extract the DESCRIPTION file, which contains much metadata
136     pkg$description = read.dcf(file.path(pkg$path,'DESCRIPTION'))
137
138     # ensure consistency of version numbers
139     if ('Version' %in% names(pkg$description[1,])) {
140         if (pkg$description[1,'Version'] != available[pkg$name,'Version']) {
141             # should never happen since available is the basis upon which the
142             # package is retrieved.
143             error('available version:',available[pkg$name,'Version'])
144             error('package version:',pkg$description[1,'Version'])
145             fail('inconsistency between R package version and cached R version')
146         }
147     }
148
149     pkg$is_bundle = 'Bundle' %in% names(pkg$description[1,])
150     # note subtly of short circuit operators (no absorption)
151     if ((!pkg$is_bundle && pkg$description[1,'Package'] != pkg$name) ||
152         ( pkg$is_bundle && pkg$description[1,'Bundle'] != pkg$name)) {
153         fail('package name mismatch')
154     }
155     return(pkg)
156 }
157