From 6ff95c7495e66ced931a062169504dfb0af5d7c9 Mon Sep 17 00:00:00 2001 From: Benjamin Schubert Date: Fri, 6 Dec 2019 18:18:55 +0000 Subject: utils.py: Rework `is_main_process` to allow for running the cli in a subprocess This will allow us a better isolation for tests, as we would now be able to run them in subprocesses. --- src/buildstream/_frontend/cli.py | 4 +++- src/buildstream/utils.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/buildstream/_frontend/cli.py b/src/buildstream/_frontend/cli.py index 051f5d7f7..577761033 100644 --- a/src/buildstream/_frontend/cli.py +++ b/src/buildstream/_frontend/cli.py @@ -10,7 +10,7 @@ from .._exceptions import BstError, LoadError, AppError from .._versions import BST_FORMAT_VERSION from .complete import main_bashcomplete, complete_path, CompleteUnhandled from ..types import _CacheBuildTrees, _SchedulerErrorAction -from ..utils import _get_compression, UtilError +from ..utils import _get_compression, _set_as_main_process, UtilError ################################################################## @@ -366,6 +366,8 @@ def cli(context, **kwargs): from .app import App + _set_as_main_process() + # Create the App, giving it the main arguments context.obj = App.create(dict(kwargs)) context.call_on_close(context.obj.cleanup) diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py index b6716a29d..a3cd96d1d 100644 --- a/src/buildstream/utils.py +++ b/src/buildstream/utils.py @@ -56,7 +56,8 @@ _ALIAS_SEPARATOR = ":" _URI_SCHEMES = ["http", "https", "ftp", "file", "git", "sftp", "ssh"] # Main process pid -_MAIN_PID = os.getpid() +# This is expected to be set by bst `cli()` +_MAIN_PID = None # The number of threads in the main process at startup. # This is 1 except for certain test environments (xdist/execnet). @@ -770,6 +771,15 @@ def _pretty_size(size, dec_places=0): return "{size:g}{unit}".format(size=round(psize, dec_places), unit=unit) +# _set_as_main_process() +# +# Mark the current process at the +# +def _set_as_main_process(): + global _MAIN_PID + _MAIN_PID = os.getpid() + + # _is_main_process() # # Return whether we are in the main process or not. -- cgit v1.2.1