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