diff options
-rw-r--r-- | buildstream/_project.py | 31 | ||||
-rw-r--r-- | tests/format/project.py | 15 | ||||
-rw-r--r-- | tests/format/project/project-from-subdir/manual.bst | 1 | ||||
-rw-r--r-- | tests/format/project/project-from-subdir/project.conf | 4 | ||||
-rw-r--r-- | tests/format/project/project-from-subdir/subdirectory/README | 1 |
5 files changed, 50 insertions, 2 deletions
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 @@ -55,6 +55,21 @@ def test_load_default_project(cli, datafiles): @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") result = cli.run(project=project, args=[ 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. |