diff options
-rw-r--r-- | morphlib/__init__.py | 1 | ||||
-rw-r--r-- | morphlib/workspace.py | 104 | ||||
-rw-r--r-- | morphlib/workspace_tests.py | 85 |
3 files changed, 190 insertions, 0 deletions
diff --git a/morphlib/__init__.py b/morphlib/__init__.py index 1ebf972a..6b2a1cd7 100644 --- a/morphlib/__init__.py +++ b/morphlib/__init__.py @@ -74,6 +74,7 @@ import stagingarea import stopwatch import tempdir import util +import workspace import yamlparse diff --git a/morphlib/workspace.py b/morphlib/workspace.py new file mode 100644 index 00000000..5a2eb86b --- /dev/null +++ b/morphlib/workspace.py @@ -0,0 +1,104 @@ +# Copyright (C) 2013 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# =*= License: GPL-2 =*= + + +'''A module to create, query, and manipulate Morph workspaces.''' + + +import os + +import morphlib + + +class WorkspaceDirExists(morphlib.Error): + + def __init__(self, dirname): + self.msg = ( + 'can only initialize empty directory as a workspace: %s' % + dirname) + + +class NotInWorkspace(morphlib.Error): + + def __init__(self, dirname): + self.msg = 'Directory %s is not in a workspace' % dirname + + +class Workspace(object): + + '''A Morph workspace. + + This class should be instantiated with the open() or create() + functions in this module. + + ''' + + def __init__(self, root_directory): + self.root = root_directory + + +def open(dirname): + '''Open an existing workspace. + + The given directory name may be to a subdirectory of the + workspace. This makes it easy to instantiate the Workspace + class even when the user invokes Morph in a subdirectory. + The workspace MUST exist already, or NotInWorkspace is + raised. + + Return a Workspace instance. + + ''' + + root = _find_root(dirname) + if root is None: + raise NotInWorkspace(dirname) + return Workspace(root) + + +def create(dirname): + '''Create a new workspace. + + The given directory must not be inside an existing workspace. + The workspace directory is created, unless it already exists. If it + does exist, it must be empty. Otherwise WorkspaceDirExists is raised. + + ''' + + root = _find_root(dirname) + if root is not None: + raise WorkspaceDirExists(root) + + if os.path.exists(dirname): + if os.listdir(dirname): + raise WorkspaceDirExists(dirname) + else: + os.mkdir(dirname) + os.mkdir(os.path.join(dirname, '.morph')) + return Workspace(dirname) + + +def _find_root(dirname): + '''Find the workspace root directory at or above a given directory.''' + + dirname = os.path.normpath(os.path.abspath(dirname)) + while not os.path.isdir(os.path.join(dirname, '.morph')): + if dirname == '/': + return None + dirname = os.path.dirname(dirname) + return dirname + diff --git a/morphlib/workspace_tests.py b/morphlib/workspace_tests.py new file mode 100644 index 00000000..7837481a --- /dev/null +++ b/morphlib/workspace_tests.py @@ -0,0 +1,85 @@ +# Copyright (C) 2013 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# =*= License: GPL-2 =*= + + +import os +import shutil +import tempfile +import unittest + +import morphlib + + +class WorkspaceTests(unittest.TestCase): + + def setUp(self): + self.tempdir = tempfile.mkdtemp() + self.workspace_dir = os.path.join(self.tempdir, 'workspace') + + def tearDown(self): + shutil.rmtree(self.tempdir) + + def assertIsWorkspace(self, dirname): + self.assertTrue(os.path.isdir(dirname)) + self.assertTrue(os.path.isdir(os.path.join(dirname, '.morph'))) + + def create_it(self): + morphlib.workspace.create(self.workspace_dir) + + def test_creates_workspace(self): + ws = morphlib.workspace.create(self.workspace_dir) + self.assertIsWorkspace(self.workspace_dir) + self.assertEqual(ws.root, self.workspace_dir) + + def test_create_initialises_existing_but_empty_directory(self): + os.mkdir(self.workspace_dir) + ws = morphlib.workspace.create(self.workspace_dir) + self.assertIsWorkspace(self.workspace_dir) + self.assertEqual(ws.root, self.workspace_dir) + + def test_fails_to_create_workspace_when_dir_exists_and_is_not_empty(self): + os.mkdir(self.workspace_dir) + os.mkdir(os.path.join(self.workspace_dir, 'somedir')) + self.assertRaises( + morphlib.workspace.WorkspaceDirExists, + morphlib.workspace.create, self.workspace_dir) + + def test_fails_to_recreate_workspace(self): + # Create it once. + morphlib.workspace.create(self.workspace_dir) + # Creating it again must fail. + self.assertRaises( + morphlib.workspace.WorkspaceDirExists, + morphlib.workspace.create, self.workspace_dir) + + def test_opens_workspace_when_given_its_root(self): + self.create_it() + ws = morphlib.workspace.open(self.workspace_dir) + self.assertEqual(ws.root, self.workspace_dir) + + def test_opens_workspace_when_given_subdirectory(self): + self.create_it() + subdir = os.path.join(self.workspace_dir, 'subdir') + os.mkdir(subdir) + ws = morphlib.workspace.open(subdir) + self.assertEqual(ws.root, self.workspace_dir) + + def test_fails_to_open_workspace_when_no_workspace_anywhere(self): + self.assertRaises( + morphlib.workspace.NotInWorkspace, + morphlib.workspace.open, self.tempdir) + |