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