package_ipk.bbclass/package_deb.bbclass: Use bitbake locking functions
[openembedded.git] / classes / package_deb.bbclass
1 #
2 # Copyright 2006-2007 OpenedHand Ltd.
3 #
4
5 inherit package
6
7 BOOTSTRAP_EXTRA_RDEPENDS += "dpkg"
8 DISTRO_EXTRA_RDEPENDS += "dpkg"
9 IMAGE_PKGTYPE ?= "deb"
10
11 # Map TARGET_ARCH to Debian's ideas about architectures
12 DPKG_ARCH ?= "${TARGET_ARCH}" 
13 DPKG_ARCH_x86 ?= "i386"
14 DPKG_ARCH_i486 ?= "i386"
15 DPKG_ARCH_i586 ?= "i386"
16 DPKG_ARCH_i686 ?= "i386"
17 DPKG_ARCH_pentium ?= "i386"
18
19 python package_deb_fn () {
20     from bb import data
21     bb.data.setVar('PKGFN', bb.data.getVar('PKG',d), d)
22 }
23
24 addtask package_deb_install
25 python do_package_deb_install () {
26     import os, sys
27     pkg = bb.data.getVar('PKG', d, 1)
28     pkgfn = bb.data.getVar('PKGFN', d, 1)
29     rootfs = bb.data.getVar('IMAGE_ROOTFS', d, 1)
30     debdir = bb.data.getVar('DEPLOY_DIR_DEB', d, 1)
31     stagingdir = bb.data.getVar('STAGING_DIR', d, 1)
32     stagingbindir = bb.data.getVar('STAGING_BINDIR_NATIVE', d, 1)
33     tmpdir = bb.data.getVar('TMPDIR', d, 1)
34
35     if None in (pkg,pkgfn,rootfs):
36         raise bb.build.FuncFailed("missing variables (one or more of PKG, PKGFN, IMAGE_ROOTFS)")
37     try:
38         if not os.exists(rootfs):
39             os.makedirs(rootfs)
40         os.chdir(rootfs)
41     except OSError:
42         raise bb.build.FuncFailed(str(sys.exc_value))
43
44     # update packages file
45     (exitstatus, output) = commands.getstatusoutput('dpkg-scanpackages %s > %s/Packages' % (debdir, debdir))
46     if (exitstatus != 0 ):
47         raise bb.build.FuncFailed(output)
48
49     f = open(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"), "w")
50     f.close()
51
52     # NOTE: this env stuff is racy at best, we need something more capable
53     # than 'commands' for command execution, which includes manipulating the
54     # env of the fork+execve'd processs
55
56     # Set up environment
57     apt_config = os.getenv('APT_CONFIG')
58     os.putenv('APT_CONFIG', os.path.join(stagingdir, 'etc', 'apt', 'apt.conf'))
59     path = os.getenv('PATH')
60     os.putenv('PATH', '%s:%s' % (stagingbindir, os.getenv('PATH')))
61
62     # install package
63     commands.getstatusoutput('apt-get update')
64     commands.getstatusoutput('apt-get install -y %s' % pkgfn)
65
66     # revert environment
67     os.putenv('APT_CONFIG', apt_config)
68     os.putenv('PATH', path)
69 }
70
71 python do_package_deb () {
72     import sys, re, copy
73
74     workdir = bb.data.getVar('WORKDIR', d, 1)
75     if not workdir:
76         bb.error("WORKDIR not defined, unable to package")
77         return
78
79     import os # path manipulations
80     outdir = bb.data.getVar('DEPLOY_DIR_DEB', d, 1)
81     if not outdir:
82         bb.error("DEPLOY_DIR_DEB not defined, unable to package")
83         return
84
85     dvar = bb.data.getVar('D', d, 1)
86     if not dvar:
87         bb.error("D not defined, unable to package")
88         return
89     bb.mkdirhier(dvar)
90
91     packages = bb.data.getVar('PACKAGES', d, 1)
92     if not packages:
93         bb.debug(1, "PACKAGES not defined, nothing to package")
94         return
95
96     tmpdir = bb.data.getVar('TMPDIR', d, 1)
97
98     if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
99         os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
100
101     if packages == []:
102         bb.debug(1, "No packages; nothing to do")
103         return
104
105     for pkg in packages.split():
106         localdata = bb.data.createCopy(d)
107         root = "%s/install/%s" % (workdir, pkg)
108
109         lf = bb.utils.lockfile(root + ".lock")
110
111         bb.data.setVar('ROOT', '', localdata)
112         bb.data.setVar('ROOT_%s' % pkg, root, localdata)
113         pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, 1)
114         if not pkgname:
115             pkgname = pkg
116         bb.data.setVar('PKG', pkgname, localdata)
117
118         overrides = bb.data.getVar('OVERRIDES', localdata)
119         if not overrides:
120             raise bb.build.FuncFailed('OVERRIDES not defined')
121         overrides = bb.data.expand(overrides, localdata)
122         bb.data.setVar('OVERRIDES', overrides + ':' + pkg, localdata)
123
124         bb.data.update_data(localdata)
125         basedir = os.path.join(os.path.dirname(root))
126
127         pkgoutdir = os.path.join(outdir, bb.data.getVar('PACKAGE_ARCH', localdata, 1))
128         bb.mkdirhier(pkgoutdir)
129
130         os.chdir(root)
131         from glob import glob
132         g = glob('*')
133         try:
134             del g[g.index('DEBIAN')]
135             del g[g.index('./DEBIAN')]
136         except ValueError:
137             pass
138         if not g and bb.data.getVar('ALLOW_EMPTY', localdata) != "1":
139             from bb import note
140             note("Not creating empty archive for %s-%s-%s" % (pkg, bb.data.getVar('PV', localdata, 1), bb.data.getVar('PR', localdata, 1)))
141             bb.utils.unlockfile(lf)
142             continue
143
144         controldir = os.path.join(root, 'DEBIAN')
145         bb.mkdirhier(controldir)
146         os.chmod(controldir, 0755)
147         try:
148             ctrlfile = file(os.path.join(controldir, 'control'), 'wb')
149             # import codecs
150             # ctrlfile = codecs.open("someFile", "w", "utf-8")
151         except OSError:
152             bb.utils.unlockfile(lf)
153             raise bb.build.FuncFailed("unable to open control file for writing.")
154
155         fields = []
156         pe = bb.data.getVar('PE', d, 1)
157         if pe and int(pe) > 0:
158             fields.append(["Version: %s:%s-%s\n", ['PE', 'PV', 'PR']])
159         else:
160             fields.append(["Version: %s-%s\n", ['PV', 'PR']])
161         fields.append(["Description: %s\n", ['DESCRIPTION']])
162         fields.append(["Section: %s\n", ['SECTION']])
163         fields.append(["Priority: %s\n", ['PRIORITY']])
164         fields.append(["Maintainer: %s\n", ['MAINTAINER']])
165         fields.append(["Architecture: %s\n", ['DPKG_ARCH']])
166         fields.append(["OE: %s\n", ['PN']])
167         fields.append(["Homepage: %s\n", ['HOMEPAGE']])
168
169 #        Package, Version, Maintainer, Description - mandatory
170 #        Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
171
172
173         def pullData(l, d):
174             l2 = []
175             for i in l:
176                 data = bb.data.getVar(i, d, 1)
177                 if data is None:
178                     raise KeyError(f)
179                 if i == 'DPKG_ARCH' and bb.data.getVar('PACKAGE_ARCH', d, 1) == 'all':
180                     data = 'all'
181                 l2.append(data)
182             return l2
183
184         ctrlfile.write("Package: %s\n" % pkgname)
185         # check for required fields
186         try:
187             for (c, fs) in fields:
188                 ctrlfile.write(unicode(c % tuple(pullData(fs, localdata))))
189         except KeyError:
190             (type, value, traceback) = sys.exc_info()
191             bb.utils.unlockfile(lf)
192             ctrlfile.close()
193             raise bb.build.FuncFailed("Missing field for deb generation: %s" % value)
194         # more fields
195
196         bb.build.exec_func("mapping_rename_hook", localdata)
197
198         rdepends = explode_deps(unicode(bb.data.getVar("RDEPENDS", localdata, 1) or ""))
199         rdepends = [dep for dep in rdepends if not '*' in dep]
200         rrecommends = explode_deps(unicode(bb.data.getVar("RRECOMMENDS", localdata, 1) or ""))
201         rrecommends = [rec for rec in rrecommends if not '*' in rec]
202         rsuggests = (unicode(bb.data.getVar("RSUGGESTS", localdata, 1) or "")).split()
203         rprovides = (unicode(bb.data.getVar("RPROVIDES", localdata, 1) or "")).split()
204         rreplaces = (unicode(bb.data.getVar("RREPLACES", localdata, 1) or "")).split()
205         rconflicts = (unicode(bb.data.getVar("RCONFLICTS", localdata, 1) or "")).split()
206         if rdepends:
207             ctrlfile.write(u"Depends: %s\n" % ", ".join(rdepends))
208         if rsuggests:
209             ctrlfile.write(u"Suggests: %s\n" % ", ".join(rsuggests))
210         if rrecommends:
211             ctrlfile.write(u"Recommends: %s\n" % ", ".join(rrecommends))
212         if rprovides:
213             ctrlfile.write(u"Provides: %s\n" % ", ".join(rprovides))
214         if rreplaces:
215             ctrlfile.write(u"Replaces: %s\n" % ", ".join(rreplaces))
216         if rconflicts:
217             ctrlfile.write(u"Conflicts: %s\n" % ", ".join(rconflicts))
218         ctrlfile.close()
219
220         for script in ["preinst", "postinst", "prerm", "postrm"]:
221             scriptvar = bb.data.getVar('pkg_%s' % script, localdata, 1)
222             if not scriptvar:
223                 continue
224             try:
225                 scriptfile = file(os.path.join(controldir, script), 'w')
226             except OSError:
227                 bb.utils.unlockfile(lf)
228                 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
229             scriptfile.write("#!/bin/sh\n")
230             scriptfile.write(scriptvar)
231             scriptfile.close()
232             os.chmod(os.path.join(controldir, script), 0755)
233
234         conffiles_str = bb.data.getVar("CONFFILES", localdata, 1)
235         if conffiles_str:
236             try:
237                 conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
238             except OSError:
239                 bb.utils.unlockfile(lf)
240                 raise bb.build.FuncFailed("unable to open conffiles for writing.")
241             for f in conffiles_str.split():
242                 conffiles.write('%s\n' % f)
243             conffiles.close()
244
245         os.chdir(basedir)
246         ret = os.system("PATH=\"%s\" fakeroot dpkg-deb -b %s %s" % (bb.data.getVar("PATH", localdata, 1), root, pkgoutdir))
247         if ret != 0:
248             bb.utils.unlockfile(lf)
249             raise bb.build.FuncFailed("dpkg-deb execution failed")
250
251         for script in ["preinst", "postinst", "prerm", "postrm", "control" ]:
252             scriptfile = os.path.join(controldir, script)
253             try:
254                 os.remove(scriptfile)
255             except OSError:
256                 pass
257         try:
258             os.rmdir(controldir)
259         except OSError:
260             pass
261
262         bb.utils.unlockfile(lf)
263 }
264
265 python () {
266     import bb
267     if bb.data.getVar('PACKAGES', d, True) != '':
268         bb.data.setVarFlag('do_package_write_deb', 'depends', 'dpkg-native:do_populate_staging fakeroot-native:do_populate_staging', d)
269 }
270
271 python do_package_write_deb () {
272         bb.build.exec_func("read_subpackage_metadata", d)
273         bb.build.exec_func("do_package_deb", d)
274 }
275 do_package_write_deb[dirs] = "${D}"
276 addtask package_write_deb before do_package_write after do_package
277