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