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