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