]> git.donarmstrong.com Git - cran2deb.git/blob - trunk/R/build.R
use debuild instead of dpkg-source
[cran2deb.git] / trunk / R / build.R
1
2 build <- function(name,extra_deps,force=F,do_cleanup=T) {
3     # can't, and hence don't need to, build base packages
4     if (name %in% base_pkgs) {
5         return(T)
6     }
7     log_clear()
8     dir <- setup()
9
10     # obtain the Debian version-to-be
11     version <- try(new_build_version(name))
12     if (inherits(version,'try-error')) {
13         error('failed to build in new_build_version: ',name)
14         return(NULL)
15     }
16
17     result <- try((function() {
18         if (!force && !needs_build(name,version)) {
19             notice('skipping build of',name)
20             return(NULL)
21         }
22
23         if (name %in% db_blacklist_packages()) {
24             #fail('package',name,'is blacklisted. consult database for reason.')
25             notice('package',name,'is blacklisted. consult database for reason.')
26             return(NULL)
27         }
28
29         pkg <- prepare_new_debian(prepare_pkg(dir,name),extra_deps)
30         if (pkg$debversion != version) {
31             fail('expected Debian version',version,'not equal to actual version',pkg$debversion)
32         }
33
34         # delete notes of upload
35         file.remove(Sys.glob(file.path(pbuilder_results,'*.upload')))
36
37         notice('R dependencies:',paste(pkg$depends$r,collapse=', '))
38         #if (debug) notice(paste("build_debian(",pkg,") invoked\n",sep=""))
39         build_debian_srcpkg(pkg)
40         #if (debug) notice(paste("build_debian(",pkg,") completed.\n",sep=""))
41
42
43         # upload the package
44 #       notice("Package upload")
45 ##         ret = log_system('umask 002;dput','-c',shQuote(dput_config),'local' ,changesfile(pkg$srcname,pkg$debversion))
46
47 ###     cmd = paste('umask 002; cd /var/www/cran2deb/rep && reprepro -b . include testing', changesfile(pkg$srcname,pkg$debversion),sep=" ")
48 ###         #if (verbose) notice('Executing: ',cmd)
49 ###         ret = log_system(cmd)
50 ###         if (ret != 0) {
51 ###             #fail('upload failed!')
52 ###         notice("Upload failed, ignored.")
53 ###         } else {
54 ###         notice("Upload successful.")
55 ###     }
56 ##         # wait for mini-dinstall to get to work
57 ##         upload_success = FALSE
58 ##         for (i in seq(1,12)) {
59 ##             if (file.exists(file.path(dinstall_archive,'testing',paste(pkg$srcname, '_', pkg$version, '.orig.tar.gz', sep='')))) {
60 ##                 upload_success = TRUE
61 ##                 break
62 ##             }
63 ##             warn(i,'/12: does not exist',file.path(dinstall_archive,'testing',paste(pkg$srcname, '_', pkg$version, '.orig.tar.gz', sep='')))
64
65 ##             Sys.sleep(5)
66 ##         }
67 ##         if (!upload_success) {
68 ##             warn('upload took too long; continuing as normal (some builds may fail temporarily)')
69 ##         }
70         return(pkg$debversion)
71     })())
72     if (do_cleanup) {
73         cleanup(dir)
74     } else {
75         notice('output is in',dir,'. you must clean this up yourself.')
76     }
77     if (is.null(result)) {
78         # nothing was done so escape asap.
79         return(result)
80     }
81
82     # otherwise record progress
83     failed = inherits(result,'try-error')
84     if (failed) {
85         error('failure of',name,'means these packages will fail:'
86                      ,paste(r_dependency_closure(name,forward_arcs=F),collapse=', '))
87     }
88     db_record_build(name, version, log_retrieve(), !failed)
89     return(!failed)
90 }
91
92 needs_build <- function(name,version) {
93     # see if the last build was successful
94     build <- db_latest_build(name)
95     if (!is.null(build) && build$success) {
96         # then something must have changed for us to attempt this
97         # build
98         if (build$r_version == version_upstream(version) &&
99             build$deb_epoch == version_epoch(version) &&
100             build$db_version == db_get_version()) {
101             return(F)
102         }
103     } else {
104         # always rebuild on failure or no record
105         notice('rebuilding',name,': no build record or previous build failed')
106         return(T)
107     }
108     # see if it has already been built *and* successfully uploaded
109     srcname <- pkgname_as_debian(name,binary=F)
110     debname <- pkgname_as_debian(name,binary=T)
111     if (file.exists(changesfile(srcname, version))) {
112         notice('already built',srcname,'version',version)
113         return(F)
114     }
115
116     if (build$r_version != version_upstream(version)) {
117         notice('rebuilding',name,': new upstream version',build$r_version,'(old) vs',version_upstream(version),'(new)')
118     }
119     if (build$deb_epoch != version_epoch(version)) {
120         notice('rebuilding',name,': new cran2deb epoch',build$deb_epoch,'(old) vs',version_epoch(version),'(new)')
121     }
122     if (build$db_version != db_get_version()) {
123         notice('rebuilding',name,': new db version',build$db_version,'(old) vs',db_get_version(),'(new)')
124     }
125     notice(paste("Now deleting ",debname,", ",srcname,".\n",sep=""))
126     rm(debname,srcname)
127     return(T)
128 }
129
130 build_debian_srcpkg <- function(pkg) {
131     wd <- getwd()
132     #notice(paste("Now in path ",wd,"\n",sep=""))
133     setwd(pkg$path)
134     
135     notice('building Debian source package',pkg$debname,paste('(',pkg$debversion,')',sep=''),'in',getwd(),'...')
136
137     
138     cmd = paste('debuild -uc -us -d -S -nc')
139     if (version_revision(pkg$debversion) > 2) {
140       cmd = paste(cmd,'-sd')
141       notice('build should exclude original source')
142     }
143     else {
144       cmd = paste(cmd,'-sa')
145       notice('build should include original source')
146     }
147     notice(paste("Executing ",'"',cmd,'"'," from directory '",getwd(),"'.\n",sep=""))
148     ret = log_system(cmd)
149     setwd(wd)
150     if (ret != 0) {
151         fail('Failed to build package.')
152     }
153     else { # copy the build results into the appropriate location
154       changes.fn <- list.files(path=dirname(pkg$path),pattern="\\.changes$")
155       changes.file <- readLines(file.path(dirname(pkg$path),changes.fn))
156       ## extract out the files that belong to the changes file
157       srcpkg.files <- gsub(".+ ","",changes.file[(which(changes.file=="Files: ")+1):NROW(changes.file)])
158       for (file in c(srcpkg.files,changes.fn)) {
159         file.copy(file.path(dirname(pkg$path),file),file.path(pbuilder_results,file));
160       }
161     }
162     return(ret);
163 }
164
165 build_debian <- function(pkg) {
166     wd <- getwd()
167     #notice(paste("Now in path ",wd,"\n",sep=""))
168     setwd(pkg$path)
169     
170     notice('building Debian package',pkg$debname,paste('(',pkg$debversion,')',sep=''),'in',getwd(),'...')
171
172     cmd = paste('pdebuild --configfile',shQuote(pbuilder_config))
173     if (version_revision(pkg$debversion) > 2) {
174         cmd = paste(cmd,'--debbuildopts','-sd')
175         notice('build should exclude original source')
176     }
177     notice(paste("Executing ",'"',cmd,'"'," from directory '",getwd(),"'.\n",sep=""))
178     ret = log_system(cmd)
179     setwd(wd)
180     if (ret != 0) {
181         fail('Failed to build package.')
182     }
183     return(ret);
184 }
185