Skip to content

Commit

Permalink
Merge pull request #7396 from goanpeca/enh/organize-common-os-utils
Browse files Browse the repository at this point in the history
Rename common.platform to common.os and split among windows, linux and unix utils
  • Loading branch information
kalefranz committed Jun 15, 2018
2 parents 15c423c + 7543746 commit 616cf99
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 54 deletions.
2 changes: 1 addition & 1 deletion conda/base/context.py
Expand Up @@ -26,7 +26,7 @@
SequenceParameter, ValidationError)
from ..common.disk import conda_bld_ensure_dir
from ..common.path import expand
from ..common.platform import linux_get_libc_version
from ..common.os.linux import linux_get_libc_version
from ..common.url import has_scheme, path_to_url, split_scheme_auth_token

try:
Expand Down
2 changes: 1 addition & 1 deletion conda/cli/common.py
Expand Up @@ -206,7 +206,7 @@ def disp_env(prefix):


def check_non_admin():
from ..common.platform import is_admin
from ..common.os import is_admin
if not context.non_admin_enabled and not is_admin():
from ..exceptions import OperationNotAllowed
raise OperationNotAllowed(dals("""
Expand Down
2 changes: 1 addition & 1 deletion conda/cli/main_info.py
Expand Up @@ -175,7 +175,7 @@ def get_info_dict(system=False):
netrc_file=netrc_file,
)
if on_win:
from ..common.platform import is_admin_on_windows
from ..common.os.windows import is_admin_on_windows
info_dict['is_windows_admin'] = is_admin_on_windows()
else:
info_dict['UID'] = os.geteuid()
Expand Down
19 changes: 19 additions & 0 deletions conda/common/os/__init__.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import absolute_import, division, print_function, unicode_literals

from logging import getLogger

from ..compat import on_win


if on_win:
from .windows import get_free_space_on_windows as get_free_space
from .windows import is_admin_on_windows as is_admin
else:
from .unix import get_free_space_on_unix as get_free_space # noqa
from .unix import is_admin_on_unix as is_admin # noqa


log = getLogger(__name__)
50 changes: 4 additions & 46 deletions conda/common/platform.py → conda/common/os/linux.py
Expand Up @@ -4,45 +4,22 @@
from __future__ import absolute_import, division, print_function, unicode_literals

from collections import OrderedDict
import ctypes
from genericpath import exists
from glob import glob
from logging import getLogger
import os
import sys

from .compat import iteritems, on_win
from .._vendor.auxlib.decorators import memoize
from ..._vendor.auxlib.decorators import memoize
from ..compat import iteritems

log = getLogger(__name__)


def is_admin_on_windows(): # pragma: unix no cover
# http://stackoverflow.com/a/1026626/2127762
if not on_win: # pragma: no cover
return False
try:
from ctypes import windll
return windll.shell32.IsUserAnAdmin() != 0
except ImportError as e: # pragma: no cover
log.debug('%r', e)
return 'unknown'
except Exception as e: # pragma: no cover
log.info('%r', e)
return 'unknown'


def is_admin():
if on_win:
return is_admin_on_windows()
else:
return os.geteuid() == 0 or os.getegid() == 0
log = getLogger(__name__)


@memoize
def linux_get_libc_version():
"""
If on linux, returns (libc_family, version), otherwise (None, None)
If on linux, returns (libc_family, version), otherwise (None, None).
"""

if not sys.platform.startswith('linux'):
Expand Down Expand Up @@ -96,22 +73,3 @@ def linux_get_libc_version():
log.warning("Failed to detect non-glibc family, assuming %s (%s)", family, version)
return family, version
return family, version


def get_free_space(dir_name):
"""Return folder/drive free space (in bytes).
:param dir_name: the dir name need to check
:return: amount of free space
Examples:
>>> get_free_space(os.getcwd()) > 0
True
"""
if on_win:
free_bytes = ctypes.c_ulonglong(0)
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(dir_name), None, None,
ctypes.pointer(free_bytes))
return free_bytes.value
else:
st = os.statvfs(dir_name)
return st.f_bavail * st.f_frsize
21 changes: 21 additions & 0 deletions conda/common/os/unix.py
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import absolute_import, division, print_function, unicode_literals

import os

from logging import getLogger


log = getLogger(__name__)


def get_free_space_on_unix(dir_name):
st = os.statvfs(dir_name)
return st.f_bavail * st.f_frsize


def is_admin_on_unix():
# http://stackoverflow.com/a/1026626/2127762
return os.geteuid() == 0 or os.getegid() == 0
40 changes: 40 additions & 0 deletions conda/common/os/windows.py
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import absolute_import, division, print_function, unicode_literals

from logging import getLogger

from ..compat import on_win


_ctypes = None
if on_win:
import ctypes as _ctypes


log = getLogger(__name__)


def get_free_space_on_windows(dir_name):
result = None
if _ctypes:
free_bytes = _ctypes.c_ulonglong(0)
_ctypes.windll.kernel32.GetDiskFreeSpaceExW(_ctypes.c_wchar_p(dir_name), None,
None, _ctypes.pointer(free_bytes))
result = free_bytes.value

return result


def is_admin_on_windows(): # pragma: unix no cover
# http://stackoverflow.com/a/1026626/2127762
result = False
if _ctypes:
try:
result = _ctypes.windll.shell32.IsUserAnAdmin() != 0
except Exception as e: # pragma: no cover
log.info('%r', e)
result = 'unknown'

return result
2 changes: 1 addition & 1 deletion tests/cli/test_common.py
Expand Up @@ -27,7 +27,7 @@
def test_check_non_admin_enabled_false():
with env_var('CONDA_NON_ADMIN_ENABLED', 'false', reset_context):
if on_win:
from conda.common.platform import is_admin_on_windows
from conda.common.os.windows import is_admin_on_windows
if is_admin_on_windows():
check_non_admin()
else:
Expand Down
Expand Up @@ -2,12 +2,12 @@
from __future__ import absolute_import, division, print_function, unicode_literals

from conda.common.compat import on_win
from conda.common.platform import is_admin_on_windows
from conda.common.os.windows import is_admin_on_windows


def test_is_admin_on_windows():
result = is_admin_on_windows()
if not on_win:
assert result is False
else:
if on_win:
assert result is False or result is True
else:
assert result is False

0 comments on commit 616cf99

Please sign in to comment.