ctdb/wscript: rework how version number is retrieved
authorAlexander Bokovoy <ab@samba.org>
Wed, 4 Jul 2018 08:05:10 +0000 (11:05 +0300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 5 Sep 2018 04:37:27 +0000 (06:37 +0200)
Using default context functions before waf initialization occured
is prone to error. Postpone calling samba_version.* code until we
got default context initialized.

Signed-off-by: Alexander Bokovoy <ab@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
ctdb/wscript

index 87483e0f71368c6661c3a4694ee3bc4064a2b367..59deb8cce0913b91b1be97f0b18f9f18e0d7066a 100644 (file)
@@ -13,10 +13,9 @@ sys.path.insert(0, top + '/buildtools/wafsamba')
 out = 'bin'
 
 from waflib import Options, Logs, Errors, Context
-import wafsamba, samba_dist
-import samba_utils, samba_version
+import wafsamba
+from wafsamba import samba_dist, samba_utils
 
-env = samba_utils.LOAD_ENVIRONMENT()
 if os.path.isfile('./VERSION'):
     vdir = '.'
 elif os.path.isfile('../VERSION'):
@@ -24,9 +23,6 @@ elif os.path.isfile('../VERSION'):
 else:
     Logs.error("VERSION file not found")
 
-version = samba_version.samba_version_file('%s/VERSION' % vdir, vdir, env)
-VERSION = version.STRING.replace('-', '.')
-
 default_prefix = Options.default_prefix = '/usr/local'
 
 samba_dist.DIST_DIRS('''ctdb:. lib/replace:lib/replace lib/talloc:lib/talloc
@@ -65,6 +61,17 @@ manpages_ceph = [
     'ctdb_mutex_ceph_rados_helper.7',
 ]
 
+VERSION = ''
+
+def get_version():
+    if Context.g_module.VERSION:
+        return Context.g_module.VERSION
+    import samba_version
+    env = samba_utils.LOAD_ENVIRONMENT()
+
+    version = samba_version.samba_version_file('%s/VERSION' % vdir, vdir, env)
+    Context.g_module.VERSION = version.STRING.replace('-', '.')
+    return Context.g_module.VERSION
 
 def options(opt):
     opt.PRIVATE_EXTENSION_DEFAULT('ctdb')
@@ -334,7 +341,7 @@ def gen_ctdb_version(task):
     fp.write('/* This file is auto-generated from waf */\n')
     fp.write('#include "version.h"\n')
     fp.write('\n')
-    fp.write('#define CTDB_VERSION_STRING "%s"\n' % VERSION)
+    fp.write('#define CTDB_VERSION_STRING "%s"\n' % get_version())
     fp.close()
 
 
@@ -350,7 +357,7 @@ def build(bld):
                             target='include/ctdb_version.h',
                             rule=gen_ctdb_version,
                             dep_vars=['VERSION'])
-    t.env.VERSION = VERSION
+    t.env.VERSION = get_version()
 
     bld.env.PKGCONFIGDIR = '${LIBDIR}/pkgconfig'
 
@@ -1115,9 +1122,8 @@ def testonly(ctx):
 
 
 def test(ctx):
-    from waflib import Scripting
-    Scripting.commands.append('build')
-    Scripting.commands.append('testonly')
+    Options.commands.append('build')
+    Options.commands.append('testonly')
 
 
 def autotest(ctx):
@@ -1130,7 +1136,7 @@ def autotest(ctx):
 
 
 def show_version(ctx):
-    print VERSION
+    print get_version()
 
 
 def manpages(ctx):
@@ -1163,7 +1169,7 @@ def distonly(ctx):
     samba_dist.DIST_FILES('ctdb/.distversion:.distversion', extend=True)
 
     t = 'ctdb.spec'
-    sed_expr1 = 's/@VERSION@/%s/g' % VERSION
+    sed_expr1 = 's/@VERSION@/%s/g' % get_version()
     sed_expr2 = 's/@RELEASE@/%s/g' % '1'
     cmd = 'sed -e "%s" -e "%s" packaging/RPM/ctdb.spec.in > %s' % (
         sed_expr1, sed_expr2, t)
@@ -1183,14 +1189,13 @@ def distonly(ctx):
 
 
 def dist():
-    from waflib import Scripting
-    Scripting.commands.append('manpages')
-    Scripting.commands.append('distonly')
+    Options.commands.append('manpages')
+    Options.commands.append('distonly')
 
 
 def rpmonly(ctx):
     opts = os.getenv('RPM_OPTIONS') or ''
-    cmd = 'rpmbuild -ta --clean --rmsource %s ctdb-%s.tar.gz' % (opts, VERSION)
+    cmd = 'rpmbuild -ta --clean --rmsource %s ctdb-%s.tar.gz' % (opts, get_version())
     ret = samba_utils.RUN_COMMAND(cmd)
     if ret != 0:
         print('rpmbuild exited with exit status %d' % ret)
@@ -1198,10 +1203,9 @@ def rpmonly(ctx):
 
 
 def rpm(ctx):
-    from waflib import Scripting
-    Scripting.commands.append('manpages')
-    Scripting.commands.append('distonly')
-    Scripting.commands.append('rpmonly')
+    Options.commands.append('manpages')
+    Options.commands.append('distonly')
+    Options.commands.append('rpmonly')
 
 
 def ctags(ctx):