]> git.donarmstrong.com Git - cran2deb.git/blob - pkg/trunk/R/debianpkg.R
use NULL instead of NA. include git revision and date stamp in build log. generate...
[cran2deb.git] / pkg / trunk / R / debianpkg.R
1 append_build_from_pkg <- function(pkg, builds) {
2     pkg_build <- data.frame(id = -1     # never used
3                            ,package = pkg$name
4                            ,r_version = version_upstream(pkg$debversion)
5                            ,deb_epoch = version_epoch(pkg$debversion)
6                            ,deb_revision = version_revision(pkg$debversion)
7                            ,db_version = db_get_version()
8                            ,date_stamp = pkg$date_stamp
9                            ,git_revision = git_revision
10                            ,success = 1 # never used
11                            ,log = ''    # never used
12                            )
13     return(cbind(data.frame(srcname=pkg$srcname), rbind(builds, pkg_build)))
14 }
15
16 generate_changelog <- function(pkg) {
17     # TODO: ``Writing R extensions'' mentions that a package may also have
18     # {NEWS,ChangeLog} files.
19     builds <- append_build_from_pkg(pkg, db_builds(pkg$name))
20     sapply(rev(rownames(builds)), function(b, changelog) generate_changelog_entry(builds[b,], changelog), pkg$debfile('changelog.in'))
21 }
22
23 generate_changelog_entry <- function(build, changelog) {
24     # TODO: should say 'New upstream release' when necessary
25     debversion <- version_new(build$r_version, build$deb_revision, build$deb_epoch)
26     cat(paste(paste(build$srcname,' (',debversion,') unstable; urgency=low',sep='')
27              ,'' ,paste('  * cran2deb ',build$git_revision
28                        ,' with DB version ',as.integer(build$db_version),'.',sep='')
29              ,'',paste(' --',maintainer,'',build$date_stamp)
30              ,'','','',sep='\n'),file=changelog, append=TRUE)
31 }
32
33 generate_rules <- function(pkg) {
34     cat(paste('#!/usr/bin/make -f'
35              ,paste('debRreposname :=',pkg$repo)
36              ,'include /usr/share/R/debian/r-cran.mk'
37              ,'',sep='\n')
38        ,file=pkg$debfile('rules'))
39     Sys.chmod(pkg$debfile('rules'),'0700')
40 }
41
42 generate_copyright <- function(pkg) {
43     # generate_copyright file; we trust DESCRIPTION
44     writeLines(strwrap(
45         paste('This Debian package of the GNU R package',pkg$name
46              ,'was generated automatically using cran2deb by'
47              ,paste(maintainer,'.',sep='')
48              ,''
49              ,'The original GNU R package is Copyright (C) '
50              # TODO: copyright start date, true copyright date
51              ,format(Sys.time(),'%Y')
52              ,pkg$description[1,'Author']
53              ,'and possibly others.'
54              ,''
55              ,'The original GNU R package is maintained by'
56              ,pkg$description[1,'Maintainer'],'and was obtained from:'
57              ,''
58              ,pkg$repoURL
59              ,''
60              ,''
61              ,'The GNU R package DESCRIPTION offers a'
62              ,'Copyright licenses under the terms of the license:'
63              ,pkg$license,'.  On a Debian GNU/Linux system, common'
64              ,'licenses are included in the directory'
65              ,'/usr/share/common-licenses/.'
66              ,''
67              ,'The DESCRIPTION file for the original GNU R package '
68              ,'can be found in '
69              ,file.path('/usr/lib/R/site-library'
70                    ,pkg$debname
71                    ,'DESCRIPTION'
72                    )
73              ,sep='\n'), width=72), con=pkg$debfile('copyright.in'))
74 }
75
76 prepare_new_debian <- function(pkg,extra_deps) {
77     # generate Debian version and name
78     pkg$date_stamp = format(Sys.time(),'%a, %d %b %Y %H:%M:%S %z')
79     pkg$repo = repourl_as_debian(pkg$repoURL)
80     if (pkg$version != available[pkg$name,'Version']) {
81         # should never happen since available is the basis upon which the
82         # package is retrieved.
83         error('available version:',available[pkg$name,'Version'])
84         error('package version:',pkg$version)
85         fail('inconsistency between R package version and cached R version')
86     }
87     pkg$debversion = new_build_version(pkg$name)
88     if (!length(grep('^[A-Za-z0-9][A-Za-z0-9+.-]+$',pkg$name))) {
89         fail('Cannot convert package name into a Debian name',pkg$name)
90     }
91     pkg$srcname = tolower(pkg$name)
92     pkg$debname = pkgname_as_debian(pkg$name,repo=pkg$repo)
93
94     if (!length(grep('\\.tar\\.gz',pkg$archive))) {
95         fail('archive is not tarball')
96     }
97
98     # re-pack into a Debian-named archive with a Debian-named directory.
99     debpath = file.path(dirname(pkg$archive)
100                    ,paste(pkg$srcname,'-'
101                          ,pkg$version
102                          ,sep=''))
103     file.rename(pkg$path, debpath)
104     pkg$path = debpath
105     debarchive = file.path(dirname(pkg$archive)
106                           ,paste(pkg$srcname,'_'
107                                 ,pkg$version,'.orig.tar.gz'
108                                 ,sep=''))
109     wd <- getwd()
110     setwd(dirname(pkg$path))
111     # remove them pesky +x files
112     log_system('find',shQuote(basename(pkg$path))
113                 ,'-type f -exec chmod -x {} \\;')
114     # tar it all back up
115     log_system('tar -czf',shQuote(debarchive),shQuote(basename(pkg$path)))
116     setwd(wd)
117     file.remove(pkg$archive)
118     pkg$archive = debarchive
119
120     # make the debian/ directory
121     debdir <- file.path(pkg$path,'debian')
122     pkg$debfile <- function(x) { file.path(debdir,x) }
123     unlink(debdir,recursive=T)
124     dir.create(debdir)
125
126     # see if this is an architecture-dependent package.
127     # heuristic: if /src/ exists in pkg$path, then this is an
128     #            architecture-dependent package.
129     # CRAN2DEB.pm is a bit fancier about this but ``Writing R extensions''
130     # says: ``The sources and headers for the compiled code are in src, plus
131     # optionally file Makevars or Makefile.'' It seems unlikely that
132     # architecture independent code would end up here.
133     if (pkg$is_bundle) {
134         # if it's a bundle, check each of the packages
135         pkg$archdep = F
136         for (pkgname in r_bundle_contains(pkg$name)) {
137             pkg$archdep = file.exists(file.path(pkg$path,pkgname,'src'))
138             if (pkg$archdep) {
139                 break
140             }
141         }
142     } else {
143         pkg$archdep = file.exists(file.path(pkg$path,'src'))
144     }
145     pkg$arch <- 'all'
146     if (pkg$archdep) {
147         pkg$arch <- host_arch()
148     }
149
150     pkg$license <- accept_license(pkg)
151     pkg$depends <- get_dependencies(pkg,extra_deps)
152     generate_changelog(pkg)
153     generate_rules(pkg)
154     generate_copyright(pkg)
155     generate_control(pkg)
156
157     # TODO: debian/watch from pkg$repoURL
158
159     # convert text to utf8 (who knows what the original character set is --
160     # let's hope iconv DTRT).
161     for (file in c('control','changelog','copyright')) {
162         log_system('iconv -o ',shQuote(pkg$debfile(file))
163                     ,' -t utf8 '
164                     ,shQuote(pkg$debfile(paste(file,'in',sep='.'))))
165         file.remove(pkg$debfile(paste(file,'in',sep='.')))
166     }
167     return(pkg)
168 }