1516N/A#!/usr/bin/python
290N/A#
290N/A# CDDL HEADER START
290N/A#
290N/A# The contents of this file are subject to the terms of the
290N/A# Common Development and Distribution License (the "License").
290N/A# You may not use this file except in compliance with the License.
290N/A#
290N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
290N/A# or http://www.opensolaris.org/os/licensing.
290N/A# See the License for the specific language governing permissions
290N/A# and limitations under the License.
290N/A#
290N/A# When distributing Covered Code, include this CDDL HEADER in each
290N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
290N/A# If applicable, add the following below this CDDL HEADER, with the
290N/A# fields enclosed by brackets "[]" replaced with your own identifying
290N/A# information: Portions Copyright [yyyy] [name of copyright owner]
290N/A#
290N/A# CDDL HEADER END
290N/A#
3339N/A# Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
290N/A#
290N/A
290N/A# The portable module provide access to methods that require operating system-
290N/A# specific implementations. The module initialization logic selects the right
290N/A# implementation the module is loaded. The module methods then
290N/A# delegate to the implementation class object.
290N/A#
290N/A# The documentation for the methods is provided in this module. To support
290N/A# another operating system, each of these methods must be implemented by the
290N/A# class for that operating system even if it is effectively a no-op.
290N/A#
290N/A# The module and class must be named using os_[impl], where
290N/A# [impl] corresponds to the OS distro, name, or type of OS
290N/A# the class implements. For example, to add specific support
290N/A# for mandrake linux (above and beyond existing support for
290N/A# generic unix), one would create os_mandrake.py.
290N/A#
290N/A# The following high-level groups of methods are defined in this module:
290N/A#
290N/A# - Platform Attribute Methods: These methods give access to
290N/A# attributes of the underlying platform not available through
290N/A# existing python libraries. For example, the list of implemented
290N/A# ISAs of a given platform.
290N/A#
290N/A# - Account access: Retrieval of account information (users and
290N/A# groups), in some cases for dormant, relocated OS images.
290N/A#
290N/A# - Miscellaneous filesystem operations: common operations that
290N/A# differ in implementation or are only available on a subset
290N/A# of OS or filesystem implementations, such as chown() or rename().
290N/A
383N/A# This module exports the methods defined below. They are defined here as
383N/A# not implemented to avoid pylint errors. The included OS-specific module
383N/A# redefines the methods with an OS-specific implementation.
290N/A
290N/A# Platform Methods
290N/A# ----------------
383N/Adef get_isainfo():
383N/A """ Return the information for the OS's supported ISAs.
383N/A This can be a list or a single string."""
383N/A raise NotImplementedError
383N/A
383N/Adef get_release():
383N/A """ Return the information for the OS's release version. This
383N/A must be a dot-separated set of integers (i.e. no alphabetic
383N/A or punctuation)."""
383N/A raise NotImplementedError
383N/A
383N/Adef get_platform():
383N/A """ Return a string representing the current hardware model
383N/A information, e.g. "i86pc"."""
383N/A raise NotImplementedError
383N/A
1908N/Adef get_file_type(actions):
1231N/A """ Return a list containing the file type for each file in paths."""
1231N/A raise NotImplementedError
1231N/A
290N/A# Account access
290N/A# --------------
383N/Adef get_group_by_name(name, dirpath, use_file):
383N/A """ Return the group ID for a group name.
383N/A If use_file is true, an OS-specific file from within the file tree
383N/A rooted by dirpath will be consulted, if it exists. Otherwise, the
383N/A group ID is retrieved from the operating system.
383N/A Exceptions:
383N/A KeyError if the specified group does not exist"""
383N/A raise NotImplementedError
383N/A
383N/Adef get_user_by_name(name, dirpath, use_file):
383N/A """ Return the user ID for a user name.
383N/A If use_file is true, an OS-specific file from within the file tree
383N/A rooted by dirpath will be consulted, if it exists. Otherwise, the
383N/A user ID is retrieved from the operating system.
383N/A Exceptions:
383N/A KeyError if the specified group does not exist"""
383N/A raise NotImplementedError
383N/A
383N/Adef get_name_by_gid(gid, dirpath, use_file):
383N/A """ Return the group name for a group ID.
383N/A If use_file is true, an OS-specific file from within the file tree
383N/A rooted by dirpath will be consulted, if it exists. Otherwise, the
383N/A group name is retrieved from the operating system.
383N/A Exceptions:
383N/A KeyError if the specified group does not exist"""
383N/A raise NotImplementedError
383N/A
383N/Adef get_name_by_uid(uid, dirpath, use_file):
383N/A """ Return the user name for a user ID.
383N/A If use_file is true, an OS-specific file from within the file tree
383N/A rooted by dirpath will be consulted, if it exists. Otherwise, the
383N/A user name is retrieved from the operating system.
383N/A Exceptions:
383N/A KeyError if the specified group does not exist"""
383N/A raise NotImplementedError
383N/A
3275N/Adef get_usernames_by_gid(gid, dirpath):
3275N/A """ Return all user names associated with a group ID.
3275N/A The user name is first retrieved from an OS-specific file rooted
3275N/A by dirpath. If failed, try to retrieve it from the operating system."""
3275N/A raise NotImplementedError
3275N/A
383N/Adef is_admin():
383N/A """ Return true if the invoking user has administrative
383N/A privileges on the current runtime OS (e.g. are they the
383N/A root user?)."""
383N/A raise NotImplementedError
383N/A
539N/Adef get_userid():
1815N/A """ Return a string representing the invoking user's id. To be used
1815N/A for display purposes only!"""
539N/A raise NotImplementedError
539N/A
383N/Adef get_username():
1815N/A """ Return a string representing the invoking user's username. To be
1815N/A used for display purposes only!"""
383N/A raise NotImplementedError
383N/A
383N/A
290N/A# Miscellaneous filesystem operations
290N/A# -----------------------------------
383N/Adef chown(path, owner, group):
383N/A """ Change ownership of a file in an OS-specific way.
383N/A The owner and group ownership information should be applied to
383N/A the given file, if applicable on the current runtime OS.
383N/A Exceptions:
383N/A EnvironmentError (or subclass) if the path does not exist
383N/A or ownership cannot be changed"""
383N/A raise NotImplementedError
383N/A
383N/Adef rename(src, dst):
383N/A """ Change the name of the given file, using the most
383N/A appropriate method for the OS.
383N/A Exceptions:
383N/A OSError (or subclass) if the source path does not exist
383N/A EnvironmentError if the rename fails."""
383N/A raise NotImplementedError
383N/A
383N/Adef link(src, dst):
383N/A """ Link the src to the dst if supported, otherwise copy
383N/A Exceptions:
383N/A OSError (or subclass) if the source path does not exist or the link
383N/A or copy files"""
383N/A raise NotImplementedError
383N/A
481N/Adef remove(path):
481N/A """ Remove the given file in an OS-specific way
481N/A Exceptions:
481N/A OSError (or subclass) if the source path does not exist or
481N/A the file cannot be removed"""
481N/A raise NotImplementedError
481N/A
383N/Adef copyfile(src, dst):
383N/A """ Copy the contents of the file named src to a file named dst.
383N/A If dst already exists, it will be replaced. src and dst are
383N/A path names given as strings.
383N/A This is similar to python's shutil.copyfile() except that
383N/A the intention is to deal with platform specifics, such as
383N/A copying metadata associated with the file (e.g. Resource
383N/A forks on Mac OS X).
383N/A Exceptions: IOError if the destination location is not writable"""
383N/A raise NotImplementedError
383N/A
383N/Adef split_path(path):
383N/A """ Splits a path and gives back the components of the path.
383N/A This is intended to hide platform-specific details about splitting
383N/A a path into its components. This interface is similar to
383N/A os.path.split() except that the entire path is split, not just
383N/A the head/tail.
383N/A
383N/A For platforms where there are additional components (like
383N/A a windows drive letter), these should be discarded before
383N/A performing the split."""
383N/A raise NotImplementedError
383N/A
383N/Adef get_root(path):
383N/A """ Returns the 'root' of the given path.
383N/A This should include any and all components of a path up to the first
383N/A non-platform-specific component. For example, on Windows,
383N/A it should include the drive letter prefix.
383N/A
383N/A This is intended to be used when constructing or deconstructing
383N/A paths, where the root of the filesystem is significant (and
383N/A often leads to ambiguity in cross-platform code)."""
383N/A raise NotImplementedError
290N/A
1407N/Adef assert_mode(path, mode):
1407N/A """ Checks that the file identified by path has the given mode to
1407N/A the extent possible by the host operating system. Otherwise raises
1407N/A an AssertionError where the mode attribute of the assertion is the
1407N/A mode of the file."""
1407N/A raise NotImplementedError
1407N/A
3026N/Adef fsetattr(path, attrs):
3026N/A """ Set system attributes for file specified by 'path'. 'attrs' can be
3026N/A a list of verbose system attributes or a string containing a sequence of
3026N/A short options."""
3026N/A raise NotImplementedError
3026N/A
3026N/Adef fgetattr(path, compact=False):
3026N/A """ Get system attributes for file specified by 'path'. If 'compact' is
3026N/A True, it returns a string of short attribute options, otherwise a list
3026N/A of verbose attributes."""
3026N/A raise NotImplementedError
3026N/A
3026N/Adef get_sysattr_dict():
3026N/A """ Returns a dictionary containing all supported system attributes. The
3026N/A keys of the dict are verbose attributes, the values short options."""
3026N/A raise NotImplementedError
1407N/A
1231N/A# File type constants
1231N/A# -------------------
1933N/AELF, EXEC, UNFOUND, SMF_MANIFEST = range(0, 4)
1231N/A
1500N/A# String to be used for an action attribute created for the internal use of
1500N/A# dependency analysis.
1500N/APD_LOCAL_PATH = "pkg.internal.depend.localpath"
1908N/APD_PROTO_DIR = "pkg.internal.depend.protodir"
1933N/APD_PROTO_DIR_LIST = "pkg.internal.depend.protodirlist"
1231N/A
2236N/A# A String to be used for an action attribute created for pkgdepend, indicating
2236N/A# module or run paths that can be used to specify the paths that it should use
2236N/A# when searching for dependencies on given files. For example setting the
2236N/A# elf runpath for elf binaries, or $PYTHONPATH (or sys.path) for python modules.
2236N/APD_RUN_PATH = "pkg.depend.runpath"
2236N/A
2236N/A# A string used as a component of the pkg.depend.runpath value as a special
2236N/A# token to determine where to insert the runpath that pkgdepend automatically
2236N/A# generates.
2236N/APD_DEFAULT_RUNPATH = "$PKGDEPEND_RUNPATH"
2236N/A
2236N/A# A String used for an action attribute to allow pkgdepend to bypass generation
2236N/A# of dependencies against a given filename, eg. don't try to generate a
2236N/A# dependency on dtracestubs from platform/i86pc/kernel/amd64/unix
2236N/APD_BYPASS_GENERATE = "pkg.depend.bypass-generate"
2236N/A
290N/Aimport platform
3339N/Afrom . import util as os_util
290N/A
290N/Aosname = os_util.get_canonical_os_name()
290N/Aostype = os_util.get_canonical_os_type()
290N/Adistro = platform.dist()[0].lower()
290N/A
290N/Afragments = [distro, osname, ostype]
290N/Afor fragment in fragments:
290N/A modname = 'os_' + fragment
290N/A
290N/A # try the most-specific module name first (e.g. os_suse),
290N/A # then try the more generic OS Name module (e.g. os_linux),
290N/A # then the OS type module (e.g. os_unix)
290N/A try:
3339N/A exec('from .{0} import *'.format(modname))
290N/A break
290N/A except ImportError:
290N/A pass
290N/Aelse:
290N/A raise ImportError(
290N/A "cannot find portable implementation class for os " + str(fragments))