From a5ff465d9225ad894435f95ec03f6ee21968fcb1 Mon Sep 17 00:00:00 2001 From: Chandan Singh Date: Sun, 22 Apr 2018 03:16:03 +0100 Subject: _project.py: Allow running bst commands from subdirectories of project root When initializing the project, BuildStream will continue searching for project.conf in parent directories in case it is not found in the current directory. Fixes #368. --- buildstream/_project.py | 31 ++++++++++++++++++++-- tests/format/project.py | 15 +++++++++++ .../format/project/project-from-subdir/manual.bst | 1 + .../project/project-from-subdir/project.conf | 4 +++ .../project-from-subdir/subdirectory/README | 1 + 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/format/project/project-from-subdir/manual.bst create mode 100644 tests/format/project/project-from-subdir/project.conf create mode 100644 tests/format/project/project-from-subdir/subdirectory/README diff --git a/buildstream/_project.py b/buildstream/_project.py index 5344e954e..25ffaf6d2 100644 --- a/buildstream/_project.py +++ b/buildstream/_project.py @@ -40,6 +40,9 @@ from ._workspaces import Workspaces # The separator we use for user specified aliases _ALIAS_SEPARATOR = ':' +# Project Configuration file +_PROJECT_CONF_FILE = 'project.conf' + # HostMount() # @@ -75,7 +78,7 @@ class Project(): self.name = None # The project directory - self.directory = os.path.abspath(directory) + self.directory = self._ensure_project_dir(directory) # Absolute path to where elements are loaded from within the project self.element_path = None @@ -211,7 +214,7 @@ class Project(): def _load(self): # Load builtin default - projectfile = os.path.join(self.directory, "project.conf") + projectfile = os.path.join(self.directory, _PROJECT_CONF_FILE) config = _yaml.load(_site.default_project_config) # Load project local config and override the builtin @@ -458,3 +461,27 @@ class Project(): # paths are passed in relative to the project, but must be absolute origin_dict['path'] = os.path.join(self.directory, origin_dict['path']) destination.append(origin_dict) + + # _ensure_project_dir() + # + # Returns path of the project directory, if a configuration file is found + # in given directory or any of its parent directories. + # + # Args: + # directory (str) - directory from where the command was invoked + # + # Raises: + # LoadError if project.conf is not found + # + def _ensure_project_dir(self, directory): + directory = os.path.abspath(directory) + while not os.path.isfile(os.path.join(directory, _PROJECT_CONF_FILE)): + parent_dir = os.path.dirname(directory) + if directory == parent_dir: + raise LoadError( + LoadErrorReason.MISSING_PROJECT_CONF, + '{} not found in current directory or any of its parent directories' + .format(_PROJECT_CONF_FILE)) + directory = parent_dir + + return directory diff --git a/tests/format/project.py b/tests/format/project.py index b8e411351..9d595981b 100644 --- a/tests/format/project.py +++ b/tests/format/project.py @@ -54,6 +54,21 @@ def test_load_default_project(cli, datafiles): assert (env['TERM'] == "dumb") +@pytest.mark.datafiles(os.path.join(DATA_DIR)) +def test_load_project_from_subdir(cli, datafiles): + project = os.path.join(datafiles.dirname, datafiles.basename, 'project-from-subdir') + result = cli.run( + project=project, + cwd=os.path.join(project, 'subdirectory'), + args=['show', '--format', '%{env}', 'manual.bst']) + result.assert_success() + + # Read back some of our project defaults from the env + env = _yaml.load_data(result.output) + assert (env['USER'] == "tomjon") + assert (env['TERM'] == "dumb") + + @pytest.mark.datafiles(os.path.join(DATA_DIR)) def test_override_project_path(cli, datafiles): project = os.path.join(datafiles.dirname, datafiles.basename, "overridepath") diff --git a/tests/format/project/project-from-subdir/manual.bst b/tests/format/project/project-from-subdir/manual.bst new file mode 100644 index 000000000..4d7f70266 --- /dev/null +++ b/tests/format/project/project-from-subdir/manual.bst @@ -0,0 +1 @@ +kind: manual diff --git a/tests/format/project/project-from-subdir/project.conf b/tests/format/project/project-from-subdir/project.conf new file mode 100644 index 000000000..fd3134c58 --- /dev/null +++ b/tests/format/project/project-from-subdir/project.conf @@ -0,0 +1,4 @@ +# Basic project configuration that doesnt override anything +# + +name: pony diff --git a/tests/format/project/project-from-subdir/subdirectory/README b/tests/format/project/project-from-subdir/subdirectory/README new file mode 100644 index 000000000..b32d37708 --- /dev/null +++ b/tests/format/project/project-from-subdir/subdirectory/README @@ -0,0 +1 @@ +This directory is used to test running commands from a project subdirectory. -- cgit v1.2.1