Introduce borg.platformflags.is_<os>

This commit is contained in:
Emmo Emminghaus 2018-11-10 23:34:43 +01:00
parent 973921df5a
commit 733a2bfa30
7 changed files with 46 additions and 30 deletions

View File

@ -13,6 +13,7 @@ from io import BytesIO
from itertools import groupby, zip_longest
from shutil import get_terminal_size
from .platformflags import is_win32, is_linux, is_freebsd, is_darwin
from .logger import create_logger
logger = create_logger()
@ -679,7 +680,7 @@ Utilization of max. archive size: {csize_max:.0%}
uid = item.uid if uid is None else uid
gid = item.gid if gid is None else gid
# This code is a bit of a mess due to os specific differences
try:
if not is_win32:
try:
if fd:
os.fchown(fd, uid, gid)
@ -719,20 +720,18 @@ Utilization of max. archive size: {csize_max:.0%}
except OSError:
# some systems don't support calling utime on a symlink
pass
except AttributeError:
pass
acl_set(path, item, self.numeric_owner, fd=fd)
# chown removes Linux capabilities, so set the extended attributes at the end, after chown, since they include
# the Linux capabilities in the "security.capability" attribute.
warning = xattr.set_all(fd or path, item.get('xattrs', {}), follow_symlinks=False)
if warning:
set_ec(EXIT_WARNING)
# bsdflags include the immutable flag and need to be set last:
if not self.nobsdflags and 'bsdflags' in item:
try:
set_flags(path, item.bsdflags, fd=fd)
except OSError:
pass
acl_set(path, item, self.numeric_owner, fd=fd)
# chown removes Linux capabilities, so set the extended attributes at the end, after chown, since they include
# the Linux capabilities in the "security.capability" attribute.
warning = xattr.set_all(fd or path, item.get('xattrs', {}), follow_symlinks=False)
if warning:
set_ec(EXIT_WARNING)
# bsdflags include the immutable flag and need to be set last:
if not self.nobsdflags and 'bsdflags' in item:
try:
set_flags(path, item.bsdflags, fd=fd)
except OSError:
pass
def set_meta(self, key, value):
metadata = self._load_meta(self.id)

View File

@ -2,6 +2,7 @@ import os
import sys
from .errors import Error
from ..platformflags import is_win32, is_linux, is_freebsd, is_darwin
class PythonLibcTooOld(Error):
@ -9,7 +10,7 @@ class PythonLibcTooOld(Error):
def check_python():
if sys.platform.startswith(('win32', )):
if is_win32:
required_funcs = {os.stat}
else:
required_funcs = {os.stat, os.utime, os.chown}

View File

@ -9,6 +9,7 @@ import sys
from .. import __version__
from ..platformflags import is_win32, is_linux, is_freebsd, is_darwin
from ..logger import create_logger
logger = create_logger()
@ -117,7 +118,7 @@ def popen_with_error_handling(cmd_line: str, log_prefix='', **kwargs):
def is_terminal(fd=sys.stdout):
return hasattr(fd, 'isatty') and fd.isatty() and (sys.platform != 'win32' or 'ANSICON' in os.environ)
return hasattr(fd, 'isatty') and fd.isatty() and (not is_win32 or 'ANSICON' in os.environ)
def prepare_subprocess_env(system, env=None):

View File

@ -1,11 +1,11 @@
import sys
"""
Platform-specific APIs.
Public APIs are documented in platform.base.
"""
from ..platformflags import is_win32, is_linux, is_freebsd, is_darwin
from .base import listxattr, getxattr, setxattr, ENOATTR
from .base import acl_get, acl_set
from .base import set_flags, get_flags
@ -15,24 +15,24 @@ from .base import process_alive, get_process_id, local_pid_alive, fqdn, hostname
OS_API_VERSION = API_VERSION
if not sys.platform.startswith(('win32', )):
if not is_win32:
from .posix import process_alive, local_pid_alive
# posix swidth implementation works for: linux, freebsd, darwin, openindiana, cygwin
from .posix import swidth
from .posix import get_errno
from .posix import uid2user, user2uid, gid2group, group2gid, getosusername
if sys.platform.startswith('linux'): # pragma: linux only
if is_linux: # pragma: linux only
from .linux import API_VERSION as OS_API_VERSION
from .linux import listxattr, getxattr, setxattr
from .linux import acl_get, acl_set
from .linux import set_flags, get_flags
from .linux import SyncFile
elif sys.platform.startswith('freebsd'): # pragma: freebsd only
elif is_freebsd: # pragma: freebsd only
from .freebsd import API_VERSION as OS_API_VERSION
from .freebsd import listxattr, getxattr, setxattr
from .freebsd import acl_get, acl_set
elif sys.platform == 'darwin': # pragma: darwin only
elif is_darwin: # pragma: darwin only
from .darwin import API_VERSION as OS_API_VERSION
from .darwin import listxattr, getxattr, setxattr
from .darwin import acl_get, acl_set

12
src/borg/platformflags.py Normal file
View File

@ -0,0 +1,12 @@
"""
Flags for Platform-specific APIs.
Use these Flags instead of sys.platform.startswith('<OS>') or try/except.
"""
import sys
is_win32 = sys.platform.startswith('win32')
is_linux = sys.platform.startswith('linux')
is_freebsd = sys.platform.startswith('freebsd')
is_darwin = sys.platform.startswith('darwin')

View File

@ -654,7 +654,7 @@ class Repository:
return
except AttributeError:
# TODO move the call to statvfs to platform
logger.warning('Failed to check free space before committing: no statvfs method available' )
logger.warning('Failed to check free space before committing: no statvfs method available')
return
# f_bavail: even as root - don't touch the Federal Block Reserve!
free_space = st_vfs.f_bavail * st_vfs.f_bsize

View File

@ -6,6 +6,7 @@ import sys
import tempfile
import unittest
from ..platformflags import is_win32, is_linux, is_freebsd, is_darwin
from ..platform import acl_get, acl_set, swidth
from ..platform import get_process_id, process_alive
from . import BaseTestCase, unopened_tempfile
@ -42,12 +43,14 @@ def fakeroot_detected():
def user_exists(username):
try:
import pwd # buildin but not on all OS
pwd.getpwnam(username)
return True
except (KeyError, ValueError):
return False
if not is_win32:
import pwd
try:
pwd.getpwnam(username)
return True
except (KeyError, ValueError):
pass
return False
@functools.lru_cache()