diff options
author | Don Garrett <dgarrett@google.com> | 2015-10-09 13:27:44 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2015-10-13 05:28:44 -0700 |
commit | 4c2b917d07039d5d023b843d8357b6dd525b8f47 (patch) | |
tree | 3c32eac29c7b92109b890834ffc656c4b83836cf | |
parent | c0c7400713f66a79f15a65f98aa347e7b2cc89d7 (diff) | |
download | chromite-4c2b917d07039d5d023b843d8357b6dd525b8f47.tar.gz |
workspace_lib: Remove this library.
This library partially implemented the Chrome OS Brillo concept of a
workspace. That concept is no longer relevant, so remove the library.
Update assorted users, removing the concept of workspace where it seems
appropriate.
BUG=chromium:530677
TEST=run_tests
Change-Id: Icb43d24428f3c35e89e6c90325cbd03382670ffd
Reviewed-on: https://chromium-review.googlesource.com/305025
Commit-Ready: Don Garrett <dgarrett@chromium.org>
Tested-by: Don Garrett <dgarrett@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
-rw-r--r-- | cli/command.py | 22 | ||||
-rw-r--r-- | cli/command_unittest.py | 46 | ||||
-rw-r--r-- | cli/flash_unittest.py | 3 | ||||
-rw-r--r-- | lib/commandline.py | 41 | ||||
-rw-r--r-- | lib/commandline_unittest.py | 61 | ||||
-rw-r--r-- | lib/cros_test_lib.py | 36 | ||||
-rw-r--r-- | lib/cros_test_lib_unittest.py | 17 | ||||
-rw-r--r-- | lib/operation.py | 20 | ||||
-rw-r--r-- | lib/operation_unittest.py | 76 | ||||
-rw-r--r-- | lib/path_util_unittest.py | 2 | ||||
-rw-r--r-- | lib/toolchain_unittest.py | 4 | ||||
-rw-r--r-- | lib/workspace_lib.py | 329 | ||||
l--------- | lib/workspace_lib_unittest | 1 | ||||
-rw-r--r-- | lib/workspace_lib_unittest.py | 229 | ||||
-rw-r--r-- | scripts/cros_unittest.py | 2 |
15 files changed, 6 insertions, 883 deletions
diff --git a/cli/command.py b/cli/command.py index 7743ae2cb..7f4e4b096 100644 --- a/cli/command.py +++ b/cli/command.py @@ -23,8 +23,6 @@ from chromite.lib import commandline from chromite.lib import cros_build_lib from chromite.lib import cros_import from chromite.lib import cros_logging as logging -from chromite.lib import osutils -from chromite.lib import workspace_lib # Paths for finding and importing subcommand modules. @@ -35,26 +33,6 @@ _SUBCOMMAND_MODULE_PREFIX = 'cros_' _commands = dict() -def SetupFileLogger(filename='brillo.log', log_level=logging.DEBUG): - """Store log messages to a file. - - In case of an error, this file can be made visible to the user. - """ - workspace_path = workspace_lib.WorkspacePath() - if workspace_path is None: - return - path = os.path.join(workspace_path, workspace_lib.WORKSPACE_LOGS_DIR, - filename) - osutils.Touch(path, makedirs=True) - logger = logging.getLogger() - fh = logging.FileHandler(path, mode='w') - fh.setLevel(log_level) - fh.setFormatter( - logging.Formatter(fmt=constants.LOGGER_FMT, - datefmt=constants.LOGGER_DATE_FMT)) - logger.addHandler(fh) - - def UseProgressBar(): """Determine whether the progress bar is to be used or not. diff --git a/cli/command_unittest.py b/cli/command_unittest.py index 33c072567..6ef4d507c 100644 --- a/cli/command_unittest.py +++ b/cli/command_unittest.py @@ -15,10 +15,8 @@ from chromite.cli import command from chromite.lib import commandline from chromite.lib import cros_build_lib_unittest from chromite.lib import cros_import -from chromite.lib import cros_logging as logging from chromite.lib import cros_test_lib from chromite.lib import partial_mock -from chromite.lib import workspace_lib # pylint:disable=protected-access @@ -133,47 +131,3 @@ class CommandTest(cros_test_lib.MockTestCase): self.assertIn('chrome-sdk', cros_commands) self.assertIn('flash', cros_commands) - -class FileLoggerSetupTest(cros_test_lib.WorkspaceTestCase): - """Test that logging to file works correctly.""" - - def setUp(self): - self.CreateWorkspace() - - def testSetupFileLoggerFilename(self): - """Test that the filename and path are correct.""" - patch_handler = self.PatchObject(logging, 'FileHandler', - return_value=logging.StreamHandler()) - command.SetupFileLogger(filename='foo.log') - - # Test that the filename is correct. - patch_handler.assert_called_with( - os.path.join(self.workspace_path, workspace_lib.WORKSPACE_LOGS_DIR, - 'foo.log'), mode='w') - - def testSetupFileLoggerNoFilename(self): - """Test that the filename and path are correct with no arguments.""" - patch_handler = self.PatchObject(logging, 'FileHandler', - return_value=logging.StreamHandler()) - command.SetupFileLogger() - - # Test that the filename is correct. - patch_handler.assert_called_with( - os.path.join(self.workspace_path, workspace_lib.WORKSPACE_LOGS_DIR, - 'brillo.log'), mode='w') - - def testSetupFileLoggerLogLevels(self): - """Test that the logger operates at the right level.""" - command.SetupFileLogger('foo.log', log_level=logging.INFO) - logging.getLogger().setLevel(logging.DEBUG) - logging.debug('debug') - logging.info('info') - logging.notice('notice') - - # Test that the logs are correct. - logs = open( - os.path.join(self.workspace_path, workspace_lib.WORKSPACE_LOGS_DIR, - 'foo.log'), 'r').read() - self.assertNotIn('debug', logs) - self.assertIn('info', logs) - self.assertIn('notice', logs) diff --git a/cli/flash_unittest.py b/cli/flash_unittest.py index 2a7a86866..7a18d4f65 100644 --- a/cli/flash_unittest.py +++ b/cli/flash_unittest.py @@ -19,7 +19,6 @@ from chromite.lib import dev_server_wrapper from chromite.lib import osutils from chromite.lib import partial_mock from chromite.lib import remote_access -from chromite.lib import workspace_lib class RemoteDeviceUpdaterMock(partial_mock.PartialCmdMock): @@ -62,7 +61,6 @@ class RemoteDeviceUpdaterTest(cros_test_lib.MockTempDirTestCase): self.PatchObject(dev_server_wrapper, 'GetUpdatePayloads') self.PatchObject(remote_access, 'CHECK_INTERVAL', new=0) self.PatchObject(remote_access, 'ChromiumOSDevice') - self.PatchObject(workspace_lib, 'WorkspacePath', return_value=None) def testUpdateAll(self): """Tests that update methods are called correctly.""" @@ -141,7 +139,6 @@ class USBImagerTest(cros_test_lib.MockTempDirTestCase): self.PatchObject(os.path, 'exists', return_value=True) self.isgpt_mock = self.PatchObject(flash, '_IsFilePathGPTDiskImage', return_value=True) - self.PatchObject(workspace_lib, 'WorkspacePath', return_value=None) def testLocalImagePathCopy(self): """Tests that imaging methods are called correctly.""" diff --git a/lib/commandline.py b/lib/commandline.py index 4416047ed..d10824488 100644 --- a/lib/commandline.py +++ b/lib/commandline.py @@ -29,7 +29,6 @@ from chromite.lib import gs from chromite.lib import osutils from chromite.lib import path_util from chromite.lib import terminal -from chromite.lib import workspace_lib DEVICE_SCHEME_FILE = 'file' @@ -318,45 +317,6 @@ class DeviceParser(object): raise ValueError('Unknown device scheme "%s" in "%s"' % (scheme, value)) -def NormalizeWorkspacePath(path, default_dir=None, extension=None): - """Normalize a workspace path. - - Converts |path| into a locator and applies |default_dir| and/or - |extension| if specified. - - Args: - path: Relative, absolute, or locator path in the CWD workspace. - default_dir: If |path| does not contain '/', prepend this - directory to the result. - extension: If |path| doesn't end in this extension, append this - extension to the result. - - Returns: - Workspace locator corresponding to the modified |path|. - - Raises: - ValueError: |path| isn't in the workspace. - """ - if default_dir and '/' not in path: - path = os.path.join(default_dir, path) - - if extension: - extension = '.' + extension - if os.path.splitext(path)[1] != extension: - path += extension - - if workspace_lib.IsLocator(path): - return path - - locator = workspace_lib.PathToLocator(path) - if not locator: - # argparse ignores exception messages; log it as well so the user sees it. - error_message = '%s is not in the current workspace.' % path - logging.error(error_message) - raise ValueError(error_message) - return locator - - VALID_TYPES = { 'bool': ParseBool, 'date': ParseDate, @@ -364,7 +324,6 @@ VALID_TYPES = { 'gs_path': NormalizeGSPath, 'local_or_gs_path': NormalizeLocalOrGSPath, 'path_or_uri': NormalizeUri, - 'workspace_path': NormalizeWorkspacePath, } diff --git a/lib/commandline_unittest.py b/lib/commandline_unittest.py index d13c51933..22f13d686 100644 --- a/lib/commandline_unittest.py +++ b/lib/commandline_unittest.py @@ -240,67 +240,6 @@ class DeviceParseTest(cros_test_lib.OutputTestCase): hostname='foo_host') -class NormalizeWorkspacePathTest(cros_test_lib.WorkspaceTestCase): - """Tests for NormalizeWorkspacePath() and associated functions.""" - - def setUp(self): - self.CreateWorkspace() - # By default set the CWD to be the workspace directory. - self.cwd_mock = self.PatchObject(os, 'getcwd') - self.cwd_mock.return_value = self.workspace_path - - def _VerifyNormalized(self, path, expected, **kwargs): - """Verifies tests on NormalizeWorkspacePath(). - - Args: - path: Input path to test. - expected: Expected output. - kwargs: Keyword args for NormalizeWorkspacePath(). - """ - self.assertEqual(expected, - commandline.NormalizeWorkspacePath(path, **kwargs)) - - - def testLocatorConversion(self): - """Tests NormalizeWorkspacePath() conversion to a locator.""" - # Relative paths. - self._VerifyNormalized('a', '//a') - self._VerifyNormalized('a/b', '//a/b') - - # Absolute paths. - self._VerifyNormalized(os.path.join(self.workspace_path, 'a'), '//a') - self._VerifyNormalized(os.path.join(self.workspace_path, 'a', 'b'), '//a/b') - - # Locators should be unchanged. - self._VerifyNormalized('//a', '//a') - self._VerifyNormalized('//a/b', '//a/b') - - # Paths outside the workspace should fail. - for path in ('/', '..'): - with self.assertRaises(ValueError): - commandline.NormalizeWorkspacePath(path) - - def testDefaultDir(self): - """Tests the default_dir parameter.""" - self._VerifyNormalized('a', '//default/a', default_dir='//default') - self._VerifyNormalized('a/b', '//a/b', default_dir='//default') - self._VerifyNormalized('./a', '//a', default_dir='//default') - - def testExtension(self): - """Tests the extension parameter.""" - self._VerifyNormalized('a', '//a.txt', extension='txt') - self._VerifyNormalized('a.bin', '//a.bin.txt', extension='txt') - self._VerifyNormalized('a.txt', '//a.txt', extension='txt') - - def testParser(self): - """Tests adding these types to a parser.""" - parser = commandline.ArgumentParser() - parser.add_argument('path', type='workspace_path') - - options = parser.parse_args(['my_path']) - self.assertEqual('//my_path', options.path) - - class CacheTest(cros_test_lib.MockTempDirTestCase): """Test cache dir default / override functionality.""" diff --git a/lib/cros_test_lib.py b/lib/cros_test_lib.py index 20b3eb50f..d4b68c2a5 100644 --- a/lib/cros_test_lib.py +++ b/lib/cros_test_lib.py @@ -43,7 +43,6 @@ from chromite.lib import remote_access from chromite.lib import retry_util from chromite.lib import terminal from chromite.lib import timeout_util -from chromite.lib import workspace_lib site_config = config_lib.GetConfig() @@ -1566,41 +1565,6 @@ class MockLoggingTestCase(MockTestCase, LoggingTestCase): """Convenience class mixing Logging and Mock.""" -class WorkspaceTestCase(MockTempDirTestCase): - """Test case that adds utilities for using workspaces.""" - - def setUp(self): - """Define variables populated below, mostly to make lint happy.""" - self.workspace_path = None - self.workspace_config = None - self.mock_workspace_path = None - - def CreateWorkspace(self, sdk_version=None): - """Create a fake workspace directory in self.tempdir. - - self.workspace_path points to new workspace path. - self.workspace_config points to workspace config file. - self.mock_workspace_path points to mock of WorkspacePath - - Args: - sdk_version: Mark SDK version as active in workspace. Does NOT mean - it's present in bootstrap. - """ - # Create a workspace, inside our tempdir. - self.workspace_path = os.path.join(self.tempdir, 'workspace') - self.workspace_config = os.path.join( - self.workspace_path, - workspace_lib.WORKSPACE_CONFIG) - osutils.Touch(self.workspace_config, makedirs=True) - - # Define an SDK version for it, if needed. - if sdk_version is not None: - workspace_lib.SetActiveSdkVersion(self.workspace_path, sdk_version) - - # Fake out workspace lookups to find this path. - self.mock_workspace_path = self.PatchObject( - workspace_lib, 'WorkspacePath', return_value=self.workspace_path) - @contextlib.contextmanager def SetTimeZone(tz): """Temporarily set the timezone to the specified value. diff --git a/lib/cros_test_lib_unittest.py b/lib/cros_test_lib_unittest.py index 461dab775..5786a2cc1 100644 --- a/lib/cros_test_lib_unittest.py +++ b/lib/cros_test_lib_unittest.py @@ -18,7 +18,6 @@ from chromite.lib import cros_build_lib_unittest from chromite.lib import osutils from chromite.lib import partial_mock from chromite.lib import timeout_util -from chromite.lib import workspace_lib # pylint: disable=W0212,W0233 @@ -273,19 +272,3 @@ class OutputTestCaseTest(cros_test_lib.OutputTestCase, # Verify that output is actually written to the correct files. self.assertEqual('foo\n', osutils.ReadFile(stdout_path)) self.assertEqual('bar\n', osutils.ReadFile(stderr_path)) - - -class WorkspaceTestCaseTest(cros_test_lib.WorkspaceTestCase): - """Verification for WorkspaceTestCase.""" - - def testCreateWorkspace(self): - """Tests CreateWorkspace().""" - self.CreateWorkspace() - self.assertExists(self.workspace_path) - self.assertEqual(self.workspace_path, workspace_lib.WorkspacePath()) - - def testCreateWorkspaceSdk(self): - """Tests CreateWorkspace() with an SDK version.""" - self.CreateWorkspace(sdk_version='1.2.3') - self.assertEqual('1.2.3', - workspace_lib.GetActiveSdkVersion(self.workspace_path)) diff --git a/lib/operation.py b/lib/operation.py index 785278cbf..ca866a798 100644 --- a/lib/operation.py +++ b/lib/operation.py @@ -24,7 +24,6 @@ except ImportError: # pylint: disable=import-error import queue as Queue import re -import shutil import struct import sys import termios @@ -33,7 +32,6 @@ from chromite.lib import cros_build_lib from chromite.lib import cros_logging as logging from chromite.lib import osutils from chromite.lib import parallel -from chromite.lib import workspace_lib from chromite.lib.terminal import Color # Define filenames for captured stdout and stderr. @@ -76,7 +74,6 @@ class ProgressBarOperation(object): self._stdout_path = None self._stderr_path = None self._progress_bar_displayed = False - self._workspace_path = workspace_lib.WorkspacePath() self._isatty = os.isatty(sys.stdout.fileno()) def _GetTerminalSize(self, fd=pty.STDOUT_FILENO): @@ -174,24 +171,12 @@ class ProgressBarOperation(object): try: with cros_build_lib.OutputCapturer( stdout_path=self._stdout_path, stderr_path=self._stderr_path, - quiet_fail=self._workspace_path is not None): + quiet_fail=False): func(*args, **kwargs) finally: self._queue.put(_BackgroundTaskComplete()) logging.getLogger().setLevel(restore_log_level) - def MoveStdoutStderrFiles(self): - """On failure, move stdout/stderr files to workspace/WORKSPACE_LOGS_DIR.""" - path = os.path.join(self._workspace_path, workspace_lib.WORKSPACE_LOGS_DIR) - # TODO(ralphnathan): Not sure if we need this because it should be done when - # we store the log file for brillo commands. - osutils.SafeMakedirs(path) - osutils.SafeUnlink(os.path.join(path, STDOUT_FILE)) - shutil.move(self._stdout_path, path) - osutils.SafeUnlink(os.path.join(path, STDERR_FILE)) - shutil.move(self._stderr_path, path) - logging.warning('Please look at %s for more information.', path) - # TODO (ralphnathan): Store PID of spawned process. def Run(self, func, *args, **kwargs): """Run func, parse its output, and update the progress bar. @@ -235,9 +220,6 @@ class ProgressBarOperation(object): # touching the progress bar. sys.stdout.write('\n') logging.error('Oops. Something went wrong.') - # Move the stdout/stderr files to a location that the user can access. - if self._workspace_path is not None: - self.MoveStdoutStderrFiles() # Raise the exception so it can be caught again. raise diff --git a/lib/operation_unittest.py b/lib/operation_unittest.py index ea9e184af..01e641db7 100644 --- a/lib/operation_unittest.py +++ b/lib/operation_unittest.py @@ -13,9 +13,7 @@ import sys from chromite.lib import cros_logging as logging from chromite.lib import cros_test_lib from chromite.lib import operation -from chromite.lib import osutils from chromite.lib import parallel -from chromite.lib import workspace_lib class TestWrapperProgressBarOperation(operation.ProgressBarOperation): @@ -40,7 +38,7 @@ class FakeException(Exception): """Fake exception used for testing exception handling.""" -class ProgressBarOperationTest(cros_test_lib.WorkspaceTestCase, +class ProgressBarOperationTest(cros_test_lib.MockTestCase, cros_test_lib.OutputTestCase, cros_test_lib.LoggingTestCase): """Test the Progress Bar Operation class.""" @@ -53,16 +51,6 @@ class ProgressBarOperationTest(cros_test_lib.WorkspaceTestCase, return_value=operation._TerminalSize(100, terminal_width)) self.PatchObject(os, 'isatty', return_value=True) - def _GetStdoutPath(self): - """Return path to the file where stdout is captured.""" - return os.path.join(self.workspace_path, workspace_lib.WORKSPACE_LOGS_DIR, - operation.STDOUT_FILE) - - def _GetStderrPath(self): - """Return path to the file where stderr is captured.""" - return os.path.join(self.workspace_path, workspace_lib.WORKSPACE_LOGS_DIR, - operation.STDERR_FILE) - def _VerifyProgressBar(self, width, percent, expected_shaded, expected_unshaded): """Helper to test progress bar with different percentages and lengths.""" @@ -130,8 +118,8 @@ class ProgressBarOperationTest(cros_test_lib.WorkspaceTestCase, # called once. self.AssertOutputContainsLine('Calling ParseOutput') - def testExceptionHandlingNotInWorkspace(self): - """Test exception handling if not in a workspace.""" + def testExceptionHandling(self): + """Test exception handling.""" def func(): print('foo') print('bar', file=sys.stderr) @@ -152,64 +140,6 @@ class ProgressBarOperationTest(cros_test_lib.WorkspaceTestCase, self.AssertOutputContainsLine('foo') self.AssertOutputContainsLine('bar', check_stderr=True) - def testExceptionHandlingInWorkspace(self): - """Test that stdout/stderr files are moved correctly if in a workspace.""" - def func(): - print('foo') - print('bar', file=sys.stderr) - raise FakeException() - - self.CreateWorkspace() - op = TestWrapperProgressBarOperation() - stdout_file = self._GetStdoutPath() - stderr_file = self._GetStderrPath() - - # Check that the files don't exist before the operation is called. - self.assertNotExists(stdout_file) - self.assertNotExists(stderr_file) - - try: - with cros_test_lib.LoggingCapturer() as logs: - op.Run(func) - except parallel.BackgroundFailure as e: - if not e.HasFailureType(FakeException): - raise e - - # Check that the files have been moved to the right location. - self.assertExists(stdout_file) - self.assertExists(stderr_file) - - # Check that the log message contains the path. - self.AssertLogsContain(logs, self.workspace_path) - - def testExceptionHandlingInWorkspaceFilesAlreadyExist(self): - """Test that old stdout/stderr files are removed from log directory.""" - def func(): - print('foo') - print('bar', file=sys.stderr) - raise FakeException() - - self.CreateWorkspace() - op = TestWrapperProgressBarOperation() - stdout_file = self._GetStdoutPath() - stderr_file = self._GetStderrPath() - osutils.Touch(stdout_file, makedirs=True) - osutils.Touch(stderr_file, makedirs=True) - - # Assert that the files are empty. - self.assertEqual(osutils.ReadFile(stdout_file), '') - self.assertEqual(osutils.ReadFile(stderr_file), '') - - try: - op.Run(func) - except parallel.BackgroundFailure as e: - if not e.HasFailureType(FakeException): - raise e - - # Check that the files contain the right information. - self.assertIn('foo', osutils.ReadFile(stdout_file)) - self.assertIn('bar', osutils.ReadFile(stderr_file)) - def testLogLevel(self): """Test that the log level of the function running is set correctly.""" func_log_level = logging.DEBUG diff --git a/lib/path_util_unittest.py b/lib/path_util_unittest.py index 5d2747801..459997856 100644 --- a/lib/path_util_unittest.py +++ b/lib/path_util_unittest.py @@ -111,7 +111,7 @@ class DetermineCheckoutTest(cros_test_lib.MockTempDirTestCase): path_util.CHECKOUT_TYPE_UNKNOWN, None) -class FindCacheDirTest(cros_test_lib.WorkspaceTestCase): +class FindCacheDirTest(cros_test_lib.MockTempDirTestCase): """Test cache dir specification and finding functionality.""" def setUp(self): diff --git a/lib/toolchain_unittest.py b/lib/toolchain_unittest.py index 45a0658e7..d11c98396 100644 --- a/lib/toolchain_unittest.py +++ b/lib/toolchain_unittest.py @@ -13,7 +13,6 @@ from chromite.lib import cros_build_lib_unittest from chromite.lib import cros_test_lib from chromite.lib import osutils from chromite.lib import toolchain -from chromite.lib import workspace_lib BASE_TOOLCHAIN_CONF = """# The root of all evil is money, err, this config. @@ -46,9 +45,6 @@ EXPECTED_TOOLCHAINS = { class ToolchainTest(cros_test_lib.MockTempDirTestCase): """Tests for lib.toolchain.""" - def setUp(self): - self.PatchObject(workspace_lib, 'WorkspacePath', return_value=self.tempdir) - def testArchForToolchain(self): """Tests that we correctly parse crossdev's output.""" rc_mock = cros_build_lib_unittest.RunCommandMock() diff --git a/lib/workspace_lib.py b/lib/workspace_lib.py deleted file mode 100644 index bdcd98cf6..000000000 --- a/lib/workspace_lib.py +++ /dev/null @@ -1,329 +0,0 @@ -# Copyright 2015 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Utilities for discovering the directories associated with workspaces. - -Workspaces have a variety of important concepts: - -* The bootstrap repository. BOOTSTRAP/chromite/bootstrap is expected to be in -the user's path. Most commands run from here redirect to the active SDK. - -* The workspace directory. This directory (identified by presence of -WORKSPACE_CONFIG), contains code, and is associated with exactly one SDK -instance. It is normally discovered based on CWD. - -* The SDK root. This directory contains a specific SDK version, and is stored in -BOOTSTRAP/sdk_checkouts/<version>. - -This library contains helper methods for finding all of the relevant directories -here. -""" - -from __future__ import print_function - -import json -import os - -from chromite.cbuildbot import constants -from chromite.lib import cros_build_lib -from chromite.lib import osutils - -MAIN_CHROOT_DIR_IN_VM = '/chroots' - -# The presence of this file signifies the root of a workspace. -WORKSPACE_CONFIG = 'workspace-config.json' -WORKSPACE_LOCAL_CONFIG = '.local.json' -WORKSPACE_CHROOT_DIR = '.chroot' -WORKSPACE_IMAGES_DIR = 'build/images' -WORKSPACE_LOGS_DIR = 'build/logs' - -# Prefixes used by locators. -_BOARD_LOCATOR_PREFIX = 'board:' -_WORKSPACE_LOCATOR_PREFIX = '//' - - -class LocatorNotResolved(Exception): - """Given locator could not be resolved.""" - - -class ConfigFileError(Exception): - """Configuration file writing or reading failed.""" - - -def WorkspacePath(workspace_reference_dir=None): - """Returns the path to the current workspace. - - This method works both inside and outside the chroot, though results will - be different. - - Args: - workspace_reference_dir: Any directory inside the workspace. If None, - will use CWD (outside chroot), or bind mount location (inside chroot). - You should normally use the default. - - Returns: - Path to root directory of the workspace (if valid), or None. - """ - if workspace_reference_dir is None: - if cros_build_lib.IsInsideChroot(): - workspace_reference_dir = constants.CHROOT_WORKSPACE_ROOT - else: - workspace_reference_dir = os.getcwd() - - workspace_config = osutils.FindInPathParents( - WORKSPACE_CONFIG, - os.path.abspath(workspace_reference_dir)) - - return os.path.dirname(workspace_config) if workspace_config else None - - -def ChrootPath(workspace_path): - """Returns the path to the chroot associated with the given workspace. - - Each workspace has its own associated chroot. This method returns the chroot - path set in the workspace config if present, or else the default location, - which varies depending on whether or not we run in a VM. - - Args: - workspace_path: Root directory of the workspace (WorkspacePath()). - - Returns: - Path to where the chroot is, or where it should be created. - """ - config_value = GetChrootDir(workspace_path) - if config_value: - # If the config value is a relative path, we base it in the workspace path. - # Otherwise, it is an absolute path and will be returned as is. - return os.path.join(workspace_path, config_value) - - # The default for a VM. - if osutils.IsInsideVm(): - return os.path.join(MAIN_CHROOT_DIR_IN_VM, os.path.basename(workspace_path)) - - # The default for all other cases. - return os.path.join(workspace_path, WORKSPACE_CHROOT_DIR) - - -def SetChrootDir(workspace_path, chroot_dir): - """Set which chroot directory a workspace uses. - - This value will overwrite the default value, if set. This is normally only - used if the user overwrites the default value. This method is NOT atomic. - - Args: - workspace_path: Root directory of the workspace (WorkspacePath()). - chroot_dir: Directory in which this workspaces chroot should be created. - """ - # Read the config, update its chroot_dir, and write it. - config = _ReadLocalConfig(workspace_path) - config['chroot_dir'] = chroot_dir - _WriteLocalConfig(workspace_path, config) - - -def GetChrootDir(workspace_path): - """Get override of chroot directory for a workspace. - - You should normally call ChrootPath so that the default value will be - found if no explicit value has been set. - - Args: - workspace_path: Root directory of the workspace (WorkspacePath()). - - Returns: - version string or None. - """ - # Config should always return a dictionary. - config = _ReadLocalConfig(workspace_path) - - # If version is present, use it, else return None. - return config.get('chroot_dir') - - -def GetActiveSdkVersion(workspace_path): - """Find which SDK version a workspace is associated with. - - This SDK may or may not exist in the bootstrap cache. There may be no - SDK version associated with a workspace. - - Args: - workspace_path: Root directory of the workspace (WorkspacePath()). - - Returns: - version string or None. - """ - # Config should always return a dictionary. - config = _ReadLocalConfig(workspace_path) - - # If version is present, use it, else return None. - return config.get('version') - - -def SetActiveSdkVersion(workspace_path, version): - """Set which SDK version a workspace is associated with. - - This method is NOT atomic. - - Args: - workspace_path: Root directory of the workspace (WorkspacePath()). - version: Version string of the SDK. (Eg. 1.2.3) - """ - # Read the config, update its version, and write it. - config = _ReadLocalConfig(workspace_path) - config['version'] = version - _WriteLocalConfig(workspace_path, config) - - -def _ReadLocalConfig(workspace_path): - """Read a local config for a workspace. - - Args: - workspace_path: Root directory of the workspace (WorkspacePath()). - - Returns: - Local workspace config as a Python dictionary. - """ - try: - return ReadConfigFile(os.path.join(workspace_path, WORKSPACE_LOCAL_CONFIG)) - except IOError: - # If the file doesn't exist, it's an empty dictionary. - return {} - - -def _WriteLocalConfig(workspace_path, config): - """Save out a new local config for a workspace. - - Args: - workspace_path: Root directory of the workspace (WorkspacePath()). - config: New local workspace config contents as a Python dictionary. - """ - WriteConfigFile(os.path.join(workspace_path, WORKSPACE_LOCAL_CONFIG), config) - - -def IsLocator(name): - """Returns True if name is a specific locator.""" - if not name: - raise ValueError('Locator is empty') - return (name.startswith(_WORKSPACE_LOCATOR_PREFIX) - or name.startswith(_BOARD_LOCATOR_PREFIX)) - - -def LocatorToPath(locator): - """Returns the absolute path for this locator. - - Args: - locator: a locator. - - Returns: - The absolute path defined by this locator. - - Raises: - ValueError: If |locator| is invalid. - LocatorNotResolved: If |locator| is valid but could not be resolved. - """ - if locator.startswith(_WORKSPACE_LOCATOR_PREFIX): - workspace_path = WorkspacePath() - if workspace_path is None: - raise LocatorNotResolved( - 'Workspace not found while trying to resolve %s' % locator) - return os.path.join(workspace_path, - locator[len(_WORKSPACE_LOCATOR_PREFIX):]) - - if locator.startswith(_BOARD_LOCATOR_PREFIX): - return os.path.join(constants.SOURCE_ROOT, 'src', 'overlays', - 'overlay-%s' % locator[len(_BOARD_LOCATOR_PREFIX):]) - - raise ValueError('Invalid locator %s' % locator) - - -def PathToLocator(path): - """Converts a path to a locator. - - This does not raise error if the path does not map to a locator. Some valid - (legacy) brick path do not map to any locator: chromiumos-overlay, - private board overlays, etc... - - Args: - path: absolute or relative to CWD path to a workspace object or board - overlay. - - Returns: - The locator for this path if it exists, None otherwise. - """ - workspace_path = WorkspacePath() - path = os.path.abspath(path) - - if workspace_path is None: - return None - - # If path is in the current workspace, return the relative path prefixed with - # the workspace prefix. - if os.path.commonprefix([path, workspace_path]) == workspace_path: - return _WORKSPACE_LOCATOR_PREFIX + os.path.relpath(path, workspace_path) - - # If path is in the src directory of the checkout, this is a board overlay. - # Encode it as board locator. - src_path = os.path.join(constants.SOURCE_ROOT, 'src') - if os.path.commonprefix([path, src_path]) == src_path: - parts = os.path.split(os.path.relpath(path, src_path)) - if parts[0] == 'overlays': - board_name = '-'.join(parts[1].split('-')[1:]) - return _BOARD_LOCATOR_PREFIX + board_name - - return None - - -def LocatorToFriendlyName(locator): - """Returns a friendly name for a given locator. - - Args: - locator: a locator. - """ - if IsLocator(locator) and locator.startswith(_WORKSPACE_LOCATOR_PREFIX): - return locator[len(_WORKSPACE_LOCATOR_PREFIX):].replace('/', '.') - - raise ValueError('Not a valid workspace locator: %s' % locator) - - -def WriteConfigFile(path, config): - """Writes |config| to a file at |path|. - - Configuration files in a workspace should all use the same format - whenever possible. Currently it's JSON, but centralizing config - read/write makes it easier to change when needed. - - Args: - path: path to write. - config: configuration dictionary to write. - - Raises: - ConfigFileError: |config| cannot be written as JSON. - """ - # TODO(dpursell): Add support for comments in config files. - try: - osutils.WriteFile( - path, - json.dumps(config, sort_keys=True, indent=4, separators=(',', ': ')), - makedirs=True) - except TypeError as e: - raise ConfigFileError('Writing config file %s failed: %s', path, e) - - -def ReadConfigFile(path): - """Reads a configuration file at |path|. - - For use with WriteConfigFile(). - - Args: - path: file path. - - Returns: - Result of parsing the JSON file. - - Raises: - ConfigFileError: JSON parsing failed. - """ - try: - return json.loads(osutils.ReadFile(path)) - except ValueError as e: - raise ConfigFileError('%s is not in valid JSON format: %s' % (path, e)) diff --git a/lib/workspace_lib_unittest b/lib/workspace_lib_unittest deleted file mode 120000 index 72196ceea..000000000 --- a/lib/workspace_lib_unittest +++ /dev/null @@ -1 +0,0 @@ -../scripts/wrapper.py
\ No newline at end of file diff --git a/lib/workspace_lib_unittest.py b/lib/workspace_lib_unittest.py deleted file mode 100644 index b5378f930..000000000 --- a/lib/workspace_lib_unittest.py +++ /dev/null @@ -1,229 +0,0 @@ -# Copyright 2015 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Tests for the workspace_lib library.""" - -from __future__ import print_function - -import mock -import os - -from chromite.cbuildbot import constants -from chromite.lib import cros_build_lib -from chromite.lib import cros_test_lib -from chromite.lib import osutils -from chromite.lib import workspace_lib - -# pylint: disable=protected-access - -class WorkspaceLibTest(cros_test_lib.TempDirTestCase): - """Unittests for workspace_lib.py""" - - def setUp(self): - # Define assorted paths to test against. - self.bogus_dir = os.path.join(self.tempdir, 'bogus') - - self.workspace_dir = os.path.join(self.tempdir, 'workspace') - self.workspace_config = os.path.join(self.workspace_dir, - workspace_lib.WORKSPACE_CONFIG) - self.workspace_nested = os.path.join(self.workspace_dir, 'foo', 'bar') - # Create workspace directories and files. - osutils.Touch(self.workspace_config, makedirs=True) - osutils.SafeMakedirs(self.workspace_nested) - - @mock.patch('os.getcwd') - @mock.patch.object(cros_build_lib, 'IsInsideChroot', return_value=False) - def testWorkspacePathOutsideChroot(self, _mock_inside, mock_cwd): - # Set default to a dir outside the workspace. - mock_cwd.return_value = self.bogus_dir - - # Inside the workspace, specified dir. - self.assertEqual(self.workspace_dir, - workspace_lib.WorkspacePath(self.workspace_dir)) - self.assertEqual(self.workspace_dir, - workspace_lib.WorkspacePath(self.workspace_nested)) - - # Outside the workspace, specified dir. - self.assertEqual(None, workspace_lib.WorkspacePath(self.tempdir)) - self.assertEqual(None, workspace_lib.WorkspacePath(self.bogus_dir)) - - # Inside the workspace, default dir. - mock_cwd.return_value = self.workspace_dir - self.assertEqual(self.workspace_dir, workspace_lib.WorkspacePath()) - - mock_cwd.return_value = self.workspace_nested - self.assertEqual(self.workspace_dir, workspace_lib.WorkspacePath()) - - # Outside the workspace, default dir. - mock_cwd.return_value = self.tempdir - self.assertEqual(None, workspace_lib.WorkspacePath()) - - mock_cwd.return_value = self.bogus_dir - self.assertEqual(None, workspace_lib.WorkspacePath()) - - @mock.patch.object(cros_build_lib, 'IsInsideChroot', return_value=True) - def testWorkspacePathInsideChroot(self, _mock_inside): - orig_root = constants.CHROOT_WORKSPACE_ROOT - try: - # Set default to a dir outside the workspace. - constants.CHROOT_WORKSPACE_ROOT = self.bogus_dir - - # Inside the workspace, specified dir. - self.assertEqual(self.workspace_dir, - workspace_lib.WorkspacePath(self.workspace_dir)) - self.assertEqual(self.workspace_dir, - workspace_lib.WorkspacePath(self.workspace_nested)) - - # Outside the workspace, specified dir. - self.assertEqual(None, workspace_lib.WorkspacePath(self.tempdir)) - self.assertEqual(None, workspace_lib.WorkspacePath(self.bogus_dir)) - - # Inside the workspace, default dir. - constants.CHROOT_WORKSPACE_ROOT = self.workspace_dir - self.assertEqual(self.workspace_dir, workspace_lib.WorkspacePath()) - - constants.CHROOT_WORKSPACE_ROOT = self.workspace_nested - self.assertEqual(self.workspace_dir, workspace_lib.WorkspacePath()) - - # Outside the workspace, default dir. - constants.CHROOT_WORKSPACE_ROOT = self.tempdir - self.assertEqual(None, workspace_lib.WorkspacePath()) - - constants.CHROOT_WORKSPACE_ROOT = self.bogus_dir - self.assertEqual(None, workspace_lib.WorkspacePath()) - - finally: - # Restore our constant to it's real value. - constants.CHROOT_WORKSPACE_ROOT = orig_root - - def testChrootPath(self): - # Check the default value. - self.assertEqual(os.path.join(self.workspace_dir, '.chroot'), - workspace_lib.ChrootPath(self.workspace_dir)) - - # Set a new absolute value, check that we get it back. - workspace_lib.SetChrootDir(self.workspace_dir, self.bogus_dir) - self.assertEqual(self.bogus_dir, - workspace_lib.ChrootPath(self.workspace_dir)) - - # Set a new relative path, check that it is properly appended to the - # workspace path. - workspace_lib.SetChrootDir(self.workspace_dir, 'some/path') - self.assertEqual(os.path.join(self.workspace_dir, 'some/path'), - workspace_lib.ChrootPath(self.workspace_dir)) - - @mock.patch.object(osutils, 'IsInsideVm', return_value=True) - def testChrootPathUnderVm(self, _mock_inside_vm): - """Make sure that inside the VM, chroot dir is under /chroots/...""" - self.assertEqual( - os.path.join(workspace_lib.MAIN_CHROOT_DIR_IN_VM, - os.path.basename(self.workspace_dir)), - workspace_lib.ChrootPath(self.workspace_dir) - ) - - def testReadWriteLocalConfig(self): - # Non-existent config should read as an empty dictionary. - config = workspace_lib._ReadLocalConfig(self.workspace_dir) - self.assertEqual({}, config) - - # Write out an empty dict, and make sure we can read it back. - workspace_lib._WriteLocalConfig(self.workspace_dir, {}) - config = workspace_lib._ReadLocalConfig(self.workspace_dir) - self.assertEqual({}, config) - - # Write out a value, and verify we can read it. - workspace_lib._WriteLocalConfig(self.workspace_dir, {'version': 'foo'}) - config = workspace_lib._ReadLocalConfig(self.workspace_dir) - self.assertEqual({'version': 'foo'}, config) - - # Overwrite value, and verify we can read it. - workspace_lib._WriteLocalConfig(self.workspace_dir, {'version': 'bar'}) - config = workspace_lib._ReadLocalConfig(self.workspace_dir) - self.assertEqual({'version': 'bar'}, config) - - def testReadWriteActiveSdkVersion(self): - # If no version is set, value should be None. - version = workspace_lib.GetActiveSdkVersion(self.workspace_dir) - self.assertEqual(None, version) - - # Set value, and make sure we can read it. - workspace_lib.SetActiveSdkVersion(self.workspace_dir, 'foo') - version = workspace_lib.GetActiveSdkVersion(self.workspace_dir) - self.assertEqual('foo', version) - - # Set different value, and make sure we can read it. - workspace_lib.SetActiveSdkVersion(self.workspace_dir, 'bar') - version = workspace_lib.GetActiveSdkVersion(self.workspace_dir) - self.assertEqual('bar', version) - - # Create config with unrelated values, should be same as no config. - workspace_lib._WriteLocalConfig(self.workspace_dir, {'foo': 'bar'}) - version = workspace_lib.GetActiveSdkVersion(self.workspace_dir) - self.assertEqual(None, version) - - # Set version, and make sure it works. - workspace_lib.SetActiveSdkVersion(self.workspace_dir, '1.2.3') - version = workspace_lib.GetActiveSdkVersion(self.workspace_dir) - self.assertEqual('1.2.3', version) - - # Ensure all of config is there afterwords. - config = workspace_lib._ReadLocalConfig(self.workspace_dir) - self.assertEqual({'version': '1.2.3', 'foo': 'bar'}, config) - - @mock.patch('os.getcwd') - @mock.patch.object(cros_build_lib, 'IsInsideChroot', return_value=False) - def testPathToLocator(self, _mock_inside, mock_cwd): - """Tests the path to locator conversion.""" - ws = self.workspace_dir - mock_cwd.return_value = ws - - foo_path = workspace_lib.PathToLocator(os.path.join(ws, 'foo')) - baz_path = workspace_lib.PathToLocator(os.path.join(ws, 'bar', 'foo', - 'baz')) - daisy_path = workspace_lib.PathToLocator(os.path.join(constants.SOURCE_ROOT, - 'src', 'overlays', - 'overlay-daisy')) - some_path = workspace_lib.PathToLocator(os.path.join(constants.SOURCE_ROOT, - 'srcs', 'bar')) - - self.assertEqual('//foo', foo_path) - self.assertEqual('//bar/foo/baz', baz_path) - self.assertEqual('board:daisy', daisy_path) - self.assertEqual(None, some_path) - - def assertReversible(loc): - path = workspace_lib.LocatorToPath(loc) - self.assertEqual(loc, workspace_lib.PathToLocator(path)) - - assertReversible('//foo') - assertReversible('//foo/bar/baz') - assertReversible('board:gizmo') - - -class ConfigurationTest(cros_test_lib.TempDirTestCase): - """Test WriteConfigFile() and ReadConfigFile().""" - - def testWriteReadConfigFile(self): - """Tests WriteConfigFile() then ReadConfigFile().""" - path = os.path.join(self.tempdir, 'foo.json') - config = {'foo': 1, 'bar': 2} - - workspace_lib.WriteConfigFile(path, config) - self.assertDictEqual(config, workspace_lib.ReadConfigFile(path)) - - def testWriteConfigFileInvalid(self): - """Tests writing an invalid configuration file.""" - path = os.path.join(self.tempdir, 'foo.json') - config = Exception() - - with self.assertRaises(workspace_lib.ConfigFileError): - workspace_lib.WriteConfigFile(path, config) - - def testReadConfigFileInvalid(self): - """Tests reading an invalid configuration file.""" - path = os.path.join(self.tempdir, 'foo.json') - osutils.WriteFile(path, 'invalid contents') - - with self.assertRaises(workspace_lib.ConfigFileError): - workspace_lib.ReadConfigFile(path) diff --git a/scripts/cros_unittest.py b/scripts/cros_unittest.py index 7510fcad0..e05ef9056 100644 --- a/scripts/cros_unittest.py +++ b/scripts/cros_unittest.py @@ -13,7 +13,7 @@ from chromite.lib import stats_unittest from chromite.scripts import cros -class RunScriptTest(cros_test_lib.WorkspaceTestCase): +class RunScriptTest(cros_test_lib.MockTempDirTestCase): """Test the main functionality.""" def setUp(self): |