-
Notifications
You must be signed in to change notification settings - Fork 0
/
hwaf-dist.py
285 lines (235 loc) · 8.13 KB
/
hwaf-dist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# -*- python -*-
# stdlib imports
import os
import os.path as osp
import shutil
import sys
# waf imports ---
import waflib.Options
import waflib.Utils
import waflib.Logs as msg
import waflib.Build
import waflib.Context
from waflib.Configure import conf
_heptooldir = osp.dirname(osp.abspath(__file__))
def options(ctx):
grp = ctx.add_option_group('bdist-rpm options')
grp.add_option(
"--rpm-pkg-ver",
default=None,
help="Sets the version of the RPM package",
)
grp.add_option(
"--rpm-pkg-rev",
default=None,
help="Sets the revision of the RPM package",
)
grp.add_option(
"--rpm-pkg-name",
default=None,
help="Sets the name of the RPM package",
)
grp.add_option(
"--rpm-pkg-url",
default=None,
help="Sets the url of the RPM package home",
)
grp.add_option(
"--rpm-pkg-cfg",
default=None,
help="Configure options to pass to the RPM configure/make/make-install step",
)
return
def configure(ctx):
ctx.env.HWAF_BDIST_APPNAME = getattr(waflib.Context.g_module, "APPNAME", "noname")
ctx.env.HWAF_BDIST_VERSION = getattr(waflib.Context.g_module, "VERSION", "0.0.1")
return
### ---------------------------------------------------------------------------
g_rpm_spec_tmpl = '''\
%%define __spec_install_post %%{nil}
%%define debug_package %%{nil}
%%define __os_install_post %%{_dbpath}/brp-compress
%%define _topdir %(HWAF_BDIST_RPMBUILDROOT)s/rpmbuild
%%define _tmppath %%{_topdir}/tmp
%%define _hwaf_topdir %(PREFIX)s
Summary: hepwaf generated RPM for %(HWAF_BDIST_APPNAME)s
Name: %(RPM_PKG_NAME)s
Version: %(RPM_PKG_VER)s
Release: %(RPM_PKG_REV)s
License: BSD
Group: Science
SOURCE0 : %%{name}-%%{version}.tar.gz
URL: %(HWAF_BDIST_URL)s
BuildRoot: %%{_tmppath}/%%{name}-%%{version}-%%{release}-root
%%description
%%{summary}
%%prep
%%setup -q
%%build
./waf \
configure \
--prefix=%(PREFIX)s \
--destdir=%%{buildroot} \
%(RPM_PKG_CFG)s \
build
%%install
rm -rf %%{buildroot}
mkdir -p %%{buildroot}
./waf install --destdir=%%{buildroot}
%%clean
rm -rf %%{buildroot}
%%files
%%defattr(-,root,root,-)
%%{_hwaf_topdir}/*
'''
### ---------------------------------------------------------------------------
class BdistRpmCmd(waflib.Configure.ConfigurationContext):
cmd = 'bdist-rpm'
def init_dirs(self, *k, **kw):
super(BdistRpmCmd, self).init_dirs(*k, **kw)
self.tmp = self.bldnode.make_node('bdist_rpm_tmp_dir')
try:
shutil.rmtree(self.tmp.abspath())
except:
pass
if os.path.exists(self.tmp.abspath()):
self.fatal('Could not remove the temporary directory %r' % self.tmp)
self.tmp.mkdir()
self.rpmbuildroot = self.tmp.make_node('rpmbuild')
self.rpmbuildroot.mkdir()
for d in 'RPMS SRPMS BUILD SOURCES SPECS tmp'.split():
self.rpmbuildroot.make_node(d).mkdir()
pass
self.options.destdir = self.tmp.abspath()
def execute(self, *k, **kw):
# make sure we start from fresh...
waflib.Context.create_context('clean').execute()
###
back = self.options.destdir
try: super(BdistRpmCmd, self).execute(*k, **kw)
finally: self.options.destdir = back
self.find_program(
"rpmbuild",
var='RPMBUILD',
mandatory=True,
)
pkg_ver = self.options.rpm_pkg_ver
if not pkg_ver:
pkg_ver = getattr(waflib.Context.g_module, "VERSION", "0.0.1")
pass
self.env.RPM_PKG_VER = pkg_ver
pkg_rev = self.options.rpm_pkg_rev
if not pkg_rev:
pkg_rev = "1"
pass
self.env.RPM_PKG_REV = pkg_rev
pkg_name = self.options.rpm_pkg_name
if not pkg_name:
pkg_name = getattr(waflib.Context.g_module, "APPNAME", "noname")
pass
self.env.RPM_PKG_NAME = pkg_name
pkg_url = self.options.rpm_pkg_url
if not pkg_url:
pkg_url = "http://cern.ch"
pass
self.env.HWAF_BDIST_URL = pkg_url
pkg_cfg = self.options.rpm_pkg_cfg
if not pkg_cfg:
pkg_cfg = ""
pass
self.env.RPM_PKG_CFG = pkg_cfg
self.env.HWAF_BDIST_RPMBUILDROOT = self.tmp.abspath()
for k in dict(self.env).keys():
if k.startswith(('RPM','HWAF_')):
self.declare_runtime_env(k)
pass
pass
self.start_msg("building rpm package")
rpm_name = "%s-%s-%s" % (self.env.RPM_PKG_NAME,
self.env.RPM_PKG_VER,
self.env.RPM_PKG_REV)
self.end_msg(rpm_name)
from waflib import Scripting
ctx = Scripting.Dist()
ctx.base_name = '%s-%s' % (self.env.RPM_PKG_NAME,
self.env.RPM_PKG_VER,)
ctx.arch_name = '%s-%s.tar.gz' % (self.env.RPM_PKG_NAME,
self.env.RPM_PKG_VER,)
ctx.algo = 'tar.gz'
ctx.execute()
# copy the tarball to SOURCES
src_arch = self.path.find_node(ctx.arch_name)
rpm_arch = self.rpmbuildroot.find_node('SOURCES').make_node(ctx.arch_name)
shutil.copy(src=src_arch.abspath(),
dst=rpm_arch.abspath())
# create spec-file
spec = self.rpmbuildroot.find_node('SPECS').make_node(
self.env.RPM_PKG_NAME+".spec")
spec.write(g_rpm_spec_tmpl % dict(self.env))
env = self._get_env_for_subproc()
cmd = [self.env.RPMBUILD, '-ba', spec.path_from(self.rpmbuildroot)]
try:
rc=self.cmd_and_log(
cmd,
stdout=sys.stdout,
stderr=sys.stderr,
cwd=self.rpmbuildroot.abspath(),
output=waflib.Context.BOTH,
env=env,
)
except Exception as e:
self.fatal(e.stdout)
pass
# fetch back the RPM
arch = 'x86_64'
if self.is_32b():arch = 'i686'
rpm_name = "%s.%s.rpm" % (rpm_name, arch)
rpm = self.rpmbuildroot.find_node('RPMS').find_node(arch).find_node(rpm_name)
dst_rpm = self.path.make_node(rpm_name)
shutil.copy(src=rpm.abspath(),
dst=dst_rpm.abspath())
shutil.rmtree(self.tmp.abspath())
msg.info('New RPM created: %s' % dst_rpm)
return rc
pass
waflib.Context.g_module.__dict__['_bdist_rpm'] = BdistRpmCmd
### ---------------------------------------------------------------------------
class BDist(waflib.Build.InstallContext):
'''creates an archive containing the project binaries'''
cmd = 'bdist'
def init_dirs(self, *k, **kw):
super(BDist, self).init_dirs(*k, **kw)
self.tmp = self.bldnode.make_node('bdist_tmp_dir')
try:
shutil.rmtree(self.tmp.abspath())
except:
pass
if os.path.exists(self.tmp.abspath()):
self.fatal('Could not remove the temporary directory %r' % self.tmp)
self.tmp.mkdir()
self.options.destdir = self.tmp.abspath()
def execute(self, *k, **kw):
back = self.options.destdir
try:
super(BDist, self).execute(*k, **kw)
finally:
self.options.destdir = back
files = self.tmp.ant_glob('**')
# we could mess with multiple inheritance but this is probably unnecessary
from waflib import Scripting
ctx = Scripting.Dist()
project_name = self.env.HWAF_BDIST_APPNAME
variant = self.env.CMTCFG
version = self.env.HWAF_BDIST_VERSION
ctx.arch_name = '%s-%s-%s.tar.gz' % (project_name,
variant,
version)
ctx.algo = 'tar.gz'
ctx.files = files
ctx.tar_prefix = ''
ctx.base_path = self.tmp
ctx.archive()
shutil.rmtree(self.tmp.abspath())
return
pass # class BDist
waflib.Context.g_module.__dict__['_bdist'] = BDist