Phillip Smyth pushed to branch mac_fixes at BuildStream / buildstream
Commits:
- 
1a28cea3
by knownexus at 2018-09-27T12:54:47Z
- 
02590a5b
by knownexus at 2018-09-27T12:54:53Z
- 
49a9f42c
by James Ennis at 2018-09-27T12:54:53Z
6 changed files:
- + buildstream/_platform/darwin.py
- buildstream/_platform/linux.py
- buildstream/_platform/platform.py
- buildstream/_platform/unix.py
- buildstream/sandbox/__init__.py
- buildstream/utils.py
Changes:
| 1 | +#
 | |
| 2 | +#  Copyright (C) 2017 Codethink Limited
 | |
| 3 | +#  Copyright (C) 2018 Bloomberg Finance LP
 | |
| 4 | +#
 | |
| 5 | +#  This program is free software; you can redistribute it and/or
 | |
| 6 | +#  modify it under the terms of the GNU Lesser General Public
 | |
| 7 | +#  License as published by the Free Software Foundation; either
 | |
| 8 | +#  version 2 of the License, or (at your option) any later version.
 | |
| 9 | +#
 | |
| 10 | +#  This library is distributed in the hope that it will be useful,
 | |
| 11 | +#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 | |
| 12 | +#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 | |
| 13 | +#  Lesser General Public License for more details.
 | |
| 14 | +#
 | |
| 15 | +#  You should have received a copy of the GNU Lesser General Public
 | |
| 16 | +#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 | |
| 17 | + | |
| 18 | +import os
 | |
| 19 | +import resource
 | |
| 20 | + | |
| 21 | +from .._exceptions import PlatformError
 | |
| 22 | +from ..sandbox import SandboxDummy
 | |
| 23 | + | |
| 24 | +from . import Platform
 | |
| 25 | + | |
| 26 | + | |
| 27 | +class Darwin(Platform):
 | |
| 28 | + | |
| 29 | +    # This value comes from OPEN_MAX in syslimits.h
 | |
| 30 | +    OPEN_MAX = 10240
 | |
| 31 | + | |
| 32 | +    def __init__(self):
 | |
| 33 | + | |
| 34 | +        super().__init__()
 | |
| 35 | + | |
| 36 | +    def create_sandbox(self, *args, **kwargs):
 | |
| 37 | +        return SandboxDummy(*args, **kwargs)
 | |
| 38 | + | |
| 39 | +    def check_sandbox_config(self, config):
 | |
| 40 | +        # Accept all sandbox configs as it's irrelevant with the dummy sandbox (no Sandbox.run).
 | |
| 41 | +        return True
 | |
| 42 | + | |
| 43 | +    def get_cpu_count(self, cap=None):
 | |
| 44 | +        if cap < os.cpu_count():
 | |
| 45 | +            return cap
 | |
| 46 | +        else:
 | |
| 47 | +            return os.cpu_count()
 | |
| 48 | + | |
| 49 | +    def set_resource_limits(self, soft_limit=OPEN_MAX, hard_limit=None):
 | |
| 50 | +        super().set_resource_limits(soft_limit) | 
| ... | ... | @@ -23,7 +23,7 @@ import subprocess | 
| 23 | 23 |  from .. import _site
 | 
| 24 | 24 |  from .. import utils
 | 
| 25 | 25 |  from .._message import Message, MessageType
 | 
| 26 | -from ..sandbox import SandboxBwrap, SandboxDummy
 | |
| 26 | +from ..sandbox import SandboxDummy
 | |
| 27 | 27 |  | 
| 28 | 28 |  from . import Platform
 | 
| 29 | 29 |  | 
| ... | ... | @@ -48,6 +48,7 @@ class Linux(Platform): | 
| 48 | 48 |          if not self._local_sandbox_available():
 | 
| 49 | 49 |              return SandboxDummy(*args, **kwargs)
 | 
| 50 | 50 |          else:
 | 
| 51 | +            from ..sandbox._sandboxbwrap import SandboxBwrap
 | |
| 51 | 52 |              # Inform the bubblewrap sandbox as to whether it can use user namespaces or not
 | 
| 52 | 53 |              kwargs['user_ns_available'] = self._user_ns_available
 | 
| 53 | 54 |              kwargs['die_with_parent_available'] = self._die_with_parent_available
 | 
| ... | ... | @@ -37,19 +37,22 @@ class Platform(): | 
| 37 | 37 |  | 
| 38 | 38 |      @classmethod
 | 
| 39 | 39 |      def _create_instance(cls):
 | 
| 40 | -        if sys.platform.startswith('linux'):
 | |
| 41 | -            backend = 'linux'
 | |
| 42 | -        else:
 | |
| 43 | -            backend = 'unix'
 | |
| 44 | - | |
| 45 | 40 |          # Meant for testing purposes and therefore hidden in the
 | 
| 46 | 41 |          # deepest corners of the source code. Try not to abuse this,
 | 
| 47 | 42 |          # please?
 | 
| 48 | 43 |          if os.getenv('BST_FORCE_BACKEND'):
 | 
| 49 | 44 |              backend = os.getenv('BST_FORCE_BACKEND')
 | 
| 45 | +        elif sys.platform.startswith('linux'):
 | |
| 46 | +            backend = 'linux'
 | |
| 47 | +        elif sys.platform.startswith('darwin'):
 | |
| 48 | +            backend = 'darwin'
 | |
| 49 | +        else:
 | |
| 50 | +            backend = 'unix'
 | |
| 50 | 51 |  | 
| 51 | 52 |          if backend == 'linux':
 | 
| 52 | 53 |              from .linux import Linux as PlatformImpl
 | 
| 54 | +        elif backend == 'darwin':
 | |
| 55 | +            from .darwin import Darwin as PlatformImpl
 | |
| 53 | 56 |          elif backend == 'unix':
 | 
| 54 | 57 |              from .unix import Unix as PlatformImpl
 | 
| 55 | 58 |          else:
 | 
| ... | ... | @@ -20,7 +20,6 @@ | 
| 20 | 20 |  import os
 | 
| 21 | 21 |  | 
| 22 | 22 |  from .._exceptions import PlatformError
 | 
| 23 | -from ..sandbox import SandboxChroot
 | |
| 24 | 23 |  | 
| 25 | 24 |  from . import Platform
 | 
| 26 | 25 |  | 
| ... | ... | @@ -39,6 +38,7 @@ class Unix(Platform): | 
| 39 | 38 |              raise PlatformError("Root privileges are required to run without bubblewrap.")
 | 
| 40 | 39 |  | 
| 41 | 40 |      def create_sandbox(self, *args, **kwargs):
 | 
| 41 | +        from ..sandbox._sandboxchroot import SandboxChroot
 | |
| 42 | 42 |          return SandboxChroot(*args, **kwargs)
 | 
| 43 | 43 |  | 
| 44 | 44 |      def check_sandbox_config(self, config):
 | 
| ... | ... | @@ -18,7 +18,5 @@ | 
| 18 | 18 |  #        Tristan Maat <tristan maat codethink co uk>
 | 
| 19 | 19 |  | 
| 20 | 20 |  from .sandbox import Sandbox, SandboxFlags
 | 
| 21 | -from ._sandboxchroot import SandboxChroot
 | |
| 22 | -from ._sandboxbwrap import SandboxBwrap
 | |
| 23 | 21 |  from ._sandboxremote import SandboxRemote
 | 
| 24 | 22 |  from ._sandboxdummy import SandboxDummy | 
| ... | ... | @@ -35,6 +35,7 @@ import tempfile | 
| 35 | 35 |  import itertools
 | 
| 36 | 36 |  import functools
 | 
| 37 | 37 |  from contextlib import contextmanager
 | 
| 38 | +from stat import S_ISDIR
 | |
| 38 | 39 |  | 
| 39 | 40 |  import psutil
 | 
| 40 | 41 |  | 
| ... | ... | @@ -328,27 +329,25 @@ def safe_remove(path): | 
| 328 | 329 |      Raises:
 | 
| 329 | 330 |         UtilError: In the case of unexpected system call failures
 | 
| 330 | 331 |      """
 | 
| 331 | -    if os.path.lexists(path):
 | |
| 332 | - | |
| 333 | -        # Try to remove anything that is in the way, but issue
 | |
| 334 | -        # a warning instead if it removes a non empty directory
 | |
| 335 | -        try:
 | |
| 332 | +    try:
 | |
| 333 | +        if S_ISDIR(os.lstat(path).st_mode):
 | |
| 334 | +            os.rmdir(path)
 | |
| 335 | +        else:
 | |
| 336 | 336 |              os.unlink(path)
 | 
| 337 | -        except OSError as e:
 | |
| 338 | -            if e.errno != errno.EISDIR:
 | |
| 339 | -                raise UtilError("Failed to remove '{}': {}"
 | |
| 340 | -                                .format(path, e))
 | |
| 341 | - | |
| 342 | -            try:
 | |
| 343 | -                os.rmdir(path)
 | |
| 344 | -            except OSError as e:
 | |
| 345 | -                if e.errno == errno.ENOTEMPTY:
 | |
| 346 | -                    return False
 | |
| 347 | -                else:
 | |
| 348 | -                    raise UtilError("Failed to remove '{}': {}"
 | |
| 349 | -                                    .format(path, e))
 | |
| 350 | 337 |  | 
| 351 | -    return True
 | |
| 338 | +        # File removed/unlinked successfully
 | |
| 339 | +        return True
 | |
| 340 | + | |
| 341 | +    except OSError as e:
 | |
| 342 | +        if e.errno == errno.ENOTEMPTY:
 | |
| 343 | +            # Path is non-empty directory
 | |
| 344 | +            return False
 | |
| 345 | +        elif e.errno == errno.ENOENT:
 | |
| 346 | +            # Path does not exist
 | |
| 347 | +            return True
 | |
| 348 | + | |
| 349 | +        raise UtilError("Failed to remove '{}': {}"
 | |
| 350 | +                        .format(path, e))
 | |
| 352 | 351 |  | 
| 353 | 352 |  | 
| 354 | 353 |  def copy_files(src, dest, *, files=None, ignore_missing=False, report_written=False):
 | 
