merge of '7393275c6ccce67cadeb49d4afb3459e56edf8a9'
[vuplus_openembedded] / 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     apt_config = bb.data.expand('${STAGING_ETCDIR_NATIVE}/apt/apt.conf', d)
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_backup = os.getenv('APT_CONFIG')
58     os.putenv('APT_CONFIG', apt_config)
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_backup)
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
92     tmpdir = bb.data.getVar('TMPDIR', d, 1)
93
94     if os.access(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"),os.R_OK):
95         os.unlink(os.path.join(tmpdir, "stamps", "DEB_PACKAGE_INDEX_CLEAN"))
96
97     packages = bb.data.getVar('PACKAGES', d, 1)
98     for pkg in packages.split():
99         localdata = bb.data.createCopy(d)
100         pkgdest = bb.data.getVar('PKGDEST', d, 1)
101         root = "%s/%s" % (pkgdest, pkg)
102
103         lf = bb.utils.lockfile(root + ".lock")
104
105         bb.data.setVar('ROOT', '', localdata)
106         bb.data.setVar('ROOT_%s' % pkg, root, localdata)
107         pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, 1)
108         if not pkgname:
109             pkgname = pkg
110         bb.data.setVar('PKG', pkgname, localdata)
111
112         overrides = bb.data.getVar('OVERRIDES', localdata)
113         if not overrides:
114             raise bb.build.FuncFailed('OVERRIDES not defined')
115         overrides = bb.data.expand(overrides, localdata)
116         bb.data.setVar('OVERRIDES', overrides + ':' + pkg, localdata)
117
118         bb.data.update_data(localdata)
119         basedir = os.path.join(os.path.dirname(root))
120
121         pkgoutdir = os.path.join(outdir, bb.data.getVar('PACKAGE_ARCH', localdata, 1))
122         bb.mkdirhier(pkgoutdir)
123
124         os.chdir(root)
125         from glob import glob
126         g = glob('*')
127         try:
128             del g[g.index('DEBIAN')]
129             del g[g.index('./DEBIAN')]
130         except ValueError:
131             pass
132         if not g and bb.data.getVar('ALLOW_EMPTY', localdata) != "1":
133             from bb import note
134             note("Not creating empty archive for %s-%s-%s" % (pkg, bb.data.getVar('PV', localdata, 1), bb.data.getVar('PR', localdata, 1)))
135             bb.utils.unlockfile(lf)
136             continue
137
138         controldir = os.path.join(root, 'DEBIAN')
139         bb.mkdirhier(controldir)
140         os.chmod(controldir, 0755)
141         try:
142             ctrlfile = file(os.path.join(controldir, 'control'), 'wb')
143             # import codecs
144             # ctrlfile = codecs.open("someFile", "w", "utf-8")
145         except OSError:
146             bb.utils.unlockfile(lf)
147             raise bb.build.FuncFailed("unable to open control file for writing.")
148
149         fields = []
150         pe = bb.data.getVar('PE', d, 1)
151         if pe and int(pe) > 0:
152             fields.append(["Version: %s:%s-%s\n", ['PE', 'PV', 'PR']])
153         else:
154             fields.append(["Version: %s-%s\n", ['PV', 'PR']])
155         fields.append(["Description: %s\n", ['DESCRIPTION']])
156         fields.append(["Section: %s\n", ['SECTION']])
157         fields.append(["Priority: %s\n", ['PRIORITY']])
158         fields.append(["Maintainer: %s\n", ['MAINTAINER']])
159         fields.append(["Architecture: %s\n", ['DPKG_ARCH']])
160         fields.append(["OE: %s\n", ['PN']])
161         fields.append(["Homepage: %s\n", ['HOMEPAGE']])
162
163 #        Package, Version, Maintainer, Description - mandatory
164 #        Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
165
166
167         def pullData(l, d):
168             l2 = []
169             for i in l:
170                 data = bb.data.getVar(i, d, 1)
171                 if data is None:
172                     raise KeyError(f)
173                 if i == 'DPKG_ARCH' and bb.data.getVar('PACKAGE_ARCH', d, 1) == 'all':
174                     data = 'all'
175                 l2.append(data)
176             return l2
177
178         ctrlfile.write("Package: %s\n" % pkgname)
179         # check for required fields
180         try:
181             for (c, fs) in fields:
182                 ctrlfile.write(unicode(c % tuple(pullData(fs, localdata))))
183         except KeyError:
184             (type, value, traceback) = sys.exc_info()
185             bb.utils.unlockfile(lf)
186             ctrlfile.close()
187             raise bb.build.FuncFailed("Missing field for deb generation: %s" % value)
188         # more fields
189
190         bb.build.exec_func("mapping_rename_hook", localdata)
191
192         rdepends = explode_deps(unicode(bb.data.getVar("RDEPENDS", localdata, 1) or ""))
193         rdepends = [dep for dep in rdepends if not '*' in dep]
194         rrecommends = explode_deps(unicode(bb.data.getVar("RRECOMMENDS", localdata, 1) or ""))
195         rrecommends = [rec for rec in rrecommends if not '*' in rec]
196         rsuggests = (unicode(bb.data.getVar("RSUGGESTS", localdata, 1) or "")).split()
197         rprovides = (unicode(bb.data.getVar("RPROVIDES", localdata, 1) or "")).split()
198         rreplaces = (unicode(bb.data.getVar("RREPLACES", localdata, 1) or "")).split()
199         rconflicts = (unicode(bb.data.getVar("RCONFLICTS", localdata, 1) or "")).split()
200         if rdepends:
201             ctrlfile.write(u"Depends: %s\n" % ", ".join(rdepends))
202         if rsuggests:
203             ctrlfile.write(u"Suggests: %s\n" % ", ".join(rsuggests))
204         if rrecommends:
205             ctrlfile.write(u"Recommends: %s\n" % ", ".join(rrecommends))
206         if rprovides:
207             ctrlfile.write(u"Provides: %s\n" % ", ".join(rprovides))
208         if rreplaces:
209             ctrlfile.write(u"Replaces: %s\n" % ", ".join(rreplaces))
210         if rconflicts:
211             ctrlfile.write(u"Conflicts: %s\n" % ", ".join(rconflicts))
212         ctrlfile.close()
213
214         for script in ["preinst", "postinst", "prerm", "postrm"]:
215             scriptvar = bb.data.getVar('pkg_%s' % script, localdata, 1)
216             if not scriptvar:
217                 continue
218             try:
219                 scriptfile = file(os.path.join(controldir, script), 'w')
220             except OSError:
221                 bb.utils.unlockfile(lf)
222                 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
223             scriptfile.write("#!/bin/sh\n")
224             scriptfile.write(scriptvar)
225             scriptfile.close()
226             os.chmod(os.path.join(controldir, script), 0755)
227
228         conffiles_str = bb.data.getVar("CONFFILES", localdata, 1)
229         if conffiles_str:
230             try:
231                 conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
232             except OSError:
233                 bb.utils.unlockfile(lf)
234                 raise bb.build.FuncFailed("unable to open conffiles for writing.")
235             for f in conffiles_str.split():
236                 conffiles.write('%s\n' % f)
237             conffiles.close()
238
239         os.chdir(basedir)
240         ret = os.system("PATH=\"%s\" fakeroot dpkg-deb -b %s %s" % (bb.data.getVar("PATH", localdata, 1), root, pkgoutdir))
241         if ret != 0:
242             bb.utils.unlockfile(lf)
243             raise bb.build.FuncFailed("dpkg-deb execution failed")
244
245         for script in ["preinst", "postinst", "prerm", "postrm", "control" ]:
246             scriptfile = os.path.join(controldir, script)
247             try:
248                 os.remove(scriptfile)
249             except OSError:
250                 pass
251         try:
252             os.rmdir(controldir)
253         except OSError:
254             pass
255
256         bb.utils.unlockfile(lf)
257 }
258
259 python () {
260     import bb
261     if bb.data.getVar('PACKAGES', d, True) != '':
262         deps = (bb.data.getVarFlag('do_package_write_deb', 'depends', d) or "").split()
263         deps.append('dpkg-native:do_populate_staging')
264         deps.append('fakeroot-native:do_populate_staging')
265         bb.data.setVarFlag('do_package_write_deb', 'depends', " ".join(deps), d)
266 }
267
268 python do_package_write_deb () {
269     packages = bb.data.getVar('PACKAGES', d, True)
270     if not packages:
271         bb.debug(1, "No PACKAGES defined, nothing to package")
272         return
273
274     bb.build.exec_func("read_subpackage_metadata", d)
275     bb.build.exec_func("do_package_deb", d)
276 }
277 do_package_write_deb[dirs] = "${D}"
278 addtask package_write_deb before do_package_write after do_package
279