You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by ro...@apache.org on 2020/12/29 13:39:17 UTC

[buildstream] 03/17: meson: Set version number at configure time

This is an automated email from the ASF dual-hosted git repository.

root pushed a commit to branch sam/meson
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit ca2e53ffc2e666881fae06f76572487cf9b60769
Author: Sam Thursfield <sa...@codethink.co.uk>
AuthorDate: Thu Dec 14 15:09:57 2017 +0000

    meson: Set version number at configure time
    
    This saves us from importing pkg_resources just to find the version
    number, which saves between 0 and many seconds of startup time
    (see https://github.com/pypa/setuptools/issues/510)
    
    We use the setuptools_scm module to calculate a version number as
    before, so the version numbers should be exactly the same as before.
---
 buildstream/__init__.py                   |  1 +
 buildstream/_frontend/main.py             |  8 ++------
 buildstream/_frontend/widget.py           |  6 ++----
 buildstream/{__init__.py => config.py.in} | 13 ++++---------
 buildstream/meson.build                   | 13 +++++++++----
 buildstream/utils.py                      |  5 ++---
 meson.build                               | 12 +++++++++++-
 7 files changed, 31 insertions(+), 27 deletions(-)

diff --git a/buildstream/__init__.py b/buildstream/__init__.py
index cde1b43..c49a88f 100644
--- a/buildstream/__init__.py
+++ b/buildstream/__init__.py
@@ -25,3 +25,4 @@ from .source import Source, SourceError, Consistency
 from .element import Element, ElementError, Scope
 from .buildelement import BuildElement
 from .scriptelement import ScriptElement
+from .config import VERSION
diff --git a/buildstream/_frontend/main.py b/buildstream/_frontend/main.py
index 117d148..d1707f2 100644
--- a/buildstream/_frontend/main.py
+++ b/buildstream/_frontend/main.py
@@ -20,13 +20,12 @@
 import os
 import sys
 import click
-import pkg_resources  # From setuptools
 from contextlib import contextmanager
 from blessings import Terminal
 from click import UsageError
 
 # Import buildstream public symbols
-from .. import Scope
+from .. import Scope, VERSION
 
 # Import various buildstream internals
 from .._context import Context
@@ -42,9 +41,6 @@ from .. import _yaml
 from . import Profile, LogLine, Status
 from .complete import main_bashcomplete, complete_path, CompleteUnhandled
 
-# Some globals resolved for default arguments in the cli
-build_stream_version = pkg_resources.require("buildstream")[0].version
-
 
 ##################################################################
 #            Override of click's main entry point                #
@@ -140,7 +136,7 @@ click.BaseCommand.main = override_main
 #                          Main Options                          #
 ##################################################################
 @click.group(context_settings=dict(help_option_names=['-h', '--help']))
-@click.version_option(version=build_stream_version)
+@click.version_option(version=VERSION)
 @click.option('--config', '-c',
               type=click.Path(exists=True, dir_okay=False, readable=True),
               help="Configuration file to use")
diff --git a/buildstream/_frontend/widget.py b/buildstream/_frontend/widget.py
index 83befd3..a57f926 100644
--- a/buildstream/_frontend/widget.py
+++ b/buildstream/_frontend/widget.py
@@ -24,11 +24,10 @@ from contextlib import ExitStack
 from mmap import mmap
 
 import click
-import pkg_resources
 from ruamel import yaml
 
 from . import Profile
-from .. import Element, Scope, Consistency
+from .. import Element, Scope, Consistency, VERSION
 from .. import _yaml
 from .._exceptions import ImplError
 from .._message import MessageType
@@ -423,12 +422,11 @@ class LogLine(Widget):
         context = pipeline.context
         project = pipeline.project
         starttime = datetime.datetime.now()
-        bst = pkg_resources.require("buildstream")[0]
         text = ''
 
         # Main invocation context
         text += '\n'
-        text += self.content_profile.fmt("BuildStream Version {}\n".format(bst.version), bold=True)
+        text += self.content_profile.fmt("BuildStream Version {}\n".format(VERSION), bold=True)
         values = OrderedDict()
         values["Session Start"] = starttime.strftime('%A, %d-%m-%Y at %H:%M:%S')
         values["Project"] = "{} ({})".format(project.name, project.directory)
diff --git a/buildstream/__init__.py b/buildstream/config.py.in
similarity index 64%
copy from buildstream/__init__.py
copy to buildstream/config.py.in
index cde1b43..e061be8 100644
--- a/buildstream/__init__.py
+++ b/buildstream/config.py.in
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 #
-#  Copyright (C) 2016 Codethink Limited
+#  Copyright (C) 2017 Codethink Limited
 #
 #  This program is free software; you can redistribute it and/or
 #  modify it under the terms of the GNU Lesser General Public
@@ -16,12 +16,7 @@
 #  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 #
 #  Authors:
-#        Tristan Van Berkom <tr...@codethink.co.uk>
+#        Sam Thursfield <sa...@codethink.co.uk>
 
-# Plugin auther facing APIs
-from .sandbox import Sandbox, SandboxFlags
-from .plugin import Plugin
-from .source import Source, SourceError, Consistency
-from .element import Element, ElementError, Scope
-from .buildelement import BuildElement
-from .scriptelement import ScriptElement
+
+VERSION = '@package_version@'
diff --git a/buildstream/meson.build b/buildstream/meson.build
index b7c7683..2bd4a23 100644
--- a/buildstream/meson.build
+++ b/buildstream/meson.build
@@ -36,12 +36,17 @@ sources = [
     '_yaml.py',
 ]
 
+config = configure_file(
+    input: 'config.py.in',
+    output: 'config.py',
+    configuration: cdata)
+
+install_data(
+    sources + [config],
+    install_dir: join_paths(python_site_packages_dir, 'buildstream'))
+
 main = configure_file(
     input: 'main.py.in',
     output: 'bst',
     configuration: cdata,
     install_dir: get_option('bindir'))
-
-install_data(
-    sources,
-    install_dir: join_paths(python_site_packages_dir, 'buildstream'))
diff --git a/buildstream/utils.py b/buildstream/utils.py
index 30c79ab..d263bf7 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -36,9 +36,9 @@ import subprocess
 import tempfile
 from contextlib import contextmanager
 
-import pkg_resources
 import psutil
 
+from . import VERSION
 from . import _signals
 from . import _yaml
 from ._exceptions import ProgramNotFoundError
@@ -408,8 +408,7 @@ def get_bst_version():
        (int): The major version
        (int): The minor version
     """
-    package = pkg_resources.require("buildstream")[0]
-    versions = package.version.split('.')[:2]
+    versions = VERSION.split('.')[:2]
 
     return (int(versions[0]), int(versions[1]))
 
diff --git a/meson.build b/meson.build
index 0e0fa78..d1eae78 100644
--- a/meson.build
+++ b/meson.build
@@ -1,4 +1,4 @@
-project('buildstream', version: '0.1')
+project('buildstream')
 
 if get_option('check_backend') == 'auto'
   platform = host_machine.system()
@@ -72,10 +72,20 @@ if backend == 'linux'
 endif
 
 
+# Detect version from Git using setuptools_scm.
+package_version_result = run_command(python, '-c', 'import setuptools_scm; print(setuptools_scm.get_version())')
+if package_version_result.returncode() != 0
+  error('Failed to detect package version: @0@'.format(package_version_result.stderr().strip()))
+endif
+package_version = package_version_result.stdout().strip()
+message('Detected package version @0@'.format(package_version))
+
+
 python_name = 'python' + python_version
 python_site_packages_dir = join_paths(get_option('prefix'), get_option('libdir'), python_name, 'site-packages')
 
 cdata = configuration_data()
+cdata.set('package_version', package_version)
 cdata.set('pythondir', python_site_packages_dir)
 
 subdir('buildstream')