merge of '3f2a27262036d4f6ceb75b07aa81c14e4b0fdcf0'
[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 copy # to back up env data
68     import sys
69     import re
70
71     workdir = bb.data.getVar('WORKDIR', d, 1)
72     if not workdir:
73         bb.error("WORKDIR not defined, unable to package")
74         return
75
76     import os # path manipulations
77     outdir = bb.data.getVar('DEPLOY_DIR_DEB', d, 1)
78     if not outdir:
79         bb.error("DEPLOY_DIR_DEB not defined, unable to package")
80         return
81
82     dvar = bb.data.getVar('D', d, 1)
83     if not dvar:
84         bb.error("D not defined, unable to package")
85         return
86     bb.mkdirhier(dvar)
87
88     packages = bb.data.getVar('PACKAGES', d, 1)
89     if not packages:
90         bb.debug(1, "PACKAGES not defined, nothing to package")
91         return
92
93     tmpdir = bb.data.getVar('TMPDIR', d, 1)
94     # Invalidate the packages file
95     if os.access(os.path.join(tmpdir, "stamps", "do_packages"),os.R_OK):
96         os.unlink(os.path.join(tmpdir, "stamps", "do_packages"))
97
98     if packages == []:
99         bb.debug(1, "No packages; nothing to do")
100         return
101
102     for pkg in packages.split():
103         localdata = bb.data.createCopy(d)
104         root = "%s/install/%s" % (workdir, pkg)
105
106         bb.data.setVar('ROOT', '', localdata)
107         bb.data.setVar('ROOT_%s' % pkg, root, localdata)
108         pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, 1)
109         if not pkgname:
110             pkgname = pkg
111         bb.data.setVar('PKG', pkgname, localdata)
112
113         overrides = bb.data.getVar('OVERRIDES', localdata)
114         if not overrides:
115             raise bb.build.FuncFailed('OVERRIDES not defined')
116         overrides = bb.data.expand(overrides, localdata)
117         bb.data.setVar('OVERRIDES', overrides + ':' + pkg, localdata)
118
119         bb.data.update_data(localdata)
120         basedir = os.path.join(os.path.dirname(root))
121
122         pkgoutdir = os.path.join(outdir, bb.data.getVar('PACKAGE_ARCH', localdata, 1))
123         bb.mkdirhier(pkgoutdir)
124
125         os.chdir(root)
126         from glob import glob
127         g = glob('*')
128         try:
129             del g[g.index('DEBIAN')]
130             del g[g.index('./DEBIAN')]
131         except ValueError:
132             pass
133         if not g and bb.data.getVar('ALLOW_EMPTY', localdata) != "1":
134             from bb import note
135             note("Not creating empty archive for %s-%s-%s" % (pkg, bb.data.getVar('PV', localdata, 1), bb.data.getVar('PR', localdata, 1)))
136             continue
137         controldir = os.path.join(root, 'DEBIAN')
138         bb.mkdirhier(controldir)
139         os.chmod(controldir, 0755)
140         try:
141             ctrlfile = file(os.path.join(controldir, 'control'), 'wb')
142             # import codecs
143             # ctrlfile = codecs.open("someFile", "w", "utf-8")
144         except OSError:
145             raise bb.build.FuncFailed("unable to open control file for writing.")
146
147         fields = []
148         pe = bb.data.getVar('PE', d, 1)
149         if pe and int(pe) > 0:
150             fields.append(["Version: %s:%s-%s\n", ['PE', 'PV', 'PR']])
151         else:
152             fields.append(["Version: %s-%s\n", ['PV', 'PR']])
153         fields.append(["Description: %s\n", ['DESCRIPTION']])
154         fields.append(["Section: %s\n", ['SECTION']])
155         fields.append(["Priority: %s\n", ['PRIORITY']])
156         fields.append(["Maintainer: %s\n", ['MAINTAINER']])
157         fields.append(["Architecture: %s\n", ['TARGET_ARCH']])
158         fields.append(["OE: %s\n", ['PN']])
159         fields.append(["Homepage: %s\n", ['HOMEPAGE']])
160
161 #        Package, Version, Maintainer, Description - mandatory
162 #        Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
163
164
165         def pullData(l, d):
166             l2 = []
167             for i in l:
168                 data = bb.data.getVar(i, d, 1)
169                 if data is None:
170                     raise KeyError(f)
171                 if i == 'TARGET_ARCH' and bb.data.getVar('PACKAGE_ARCH', d, 1) == 'all':
172                     data = 'all'
173                 l2.append(data)
174             return l2
175
176         ctrlfile.write("Package: %s\n" % pkgname)
177         # check for required fields
178         try:
179             for (c, fs) in fields:
180                 ctrlfile.write(unicode(c % tuple(pullData(fs, localdata))))
181         except KeyError:
182             (type, value, traceback) = sys.exc_info()
183             ctrlfile.close()
184             raise bb.build.FuncFailed("Missing field for deb generation: %s" % value)
185         # more fields
186
187         bb.build.exec_func("mapping_rename_hook", localdata)
188
189         rdepends = explode_deps(unicode(bb.data.getVar("RDEPENDS", localdata, 1) or ""))
190         rdepends = [dep for dep in rdepends if not '*' in dep]
191         rrecommends = explode_deps(unicode(bb.data.getVar("RRECOMMENDS", localdata, 1) or ""))
192         rrecommends = [rec for rec in rrecommends if not '*' in rec]
193         rsuggests = (unicode(bb.data.getVar("RSUGGESTS", localdata, 1) or "")).split()
194         rprovides = (unicode(bb.data.getVar("RPROVIDES", localdata, 1) or "")).split()
195         rreplaces = (unicode(bb.data.getVar("RREPLACES", localdata, 1) or "")).split()
196         rconflicts = (unicode(bb.data.getVar("RCONFLICTS", localdata, 1) or "")).split()
197         if rdepends:
198             ctrlfile.write(u"Depends: %s\n" % ", ".join(rdepends))
199         if rsuggests:
200             ctrlfile.write(u"Suggests: %s\n" % ", ".join(rsuggests))
201         if rrecommends:
202             ctrlfile.write(u"Recommends: %s\n" % ", ".join(rrecommends))
203         if rprovides:
204             ctrlfile.write(u"Provides: %s\n" % ", ".join(rprovides))
205         if rreplaces:
206             ctrlfile.write(u"Replaces: %s\n" % ", ".join(rreplaces))
207         if rconflicts:
208             ctrlfile.write(u"Conflicts: %s\n" % ", ".join(rconflicts))
209         ctrlfile.close()
210
211         for script in ["preinst", "postinst", "prerm", "postrm"]:
212             scriptvar = bb.data.getVar('pkg_%s' % script, localdata, 1)
213             if not scriptvar:
214                 continue
215             try:
216                 scriptfile = file(os.path.join(controldir, script), 'w')
217             except OSError:
218                 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
219             scriptfile.write("#!/bin/sh\n")
220             scriptfile.write(scriptvar)
221             scriptfile.close()
222             os.chmod(os.path.join(controldir, script), 0755)
223
224         conffiles_str = bb.data.getVar("CONFFILES", localdata, 1)
225         if conffiles_str:
226             try:
227                 conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
228             except OSError:
229                 raise bb.build.FuncFailed("unable to open conffiles for writing.")
230             for f in conffiles_str.split():
231                 conffiles.write('%s\n' % f)
232             conffiles.close()
233
234         os.chdir(basedir)
235         ret = os.system("PATH=\"%s\" fakeroot dpkg-deb -b %s %s" % (bb.data.getVar("PATH", localdata, 1), root, pkgoutdir))
236         if ret != 0:
237             raise bb.build.FuncFailed("dpkg-deb execution failed")
238
239         for script in ["preinst", "postinst", "prerm", "postrm", "control" ]:
240             scriptfile = os.path.join(controldir, script)
241             try:
242                 os.remove(scriptfile)
243             except OSError:
244                 pass
245         try:
246             os.rmdir(controldir)
247         except OSError:
248             pass
249         del localdata
250 }