Esempio n. 1
0
def is_group(value):
    """
    Check whether groupname or gid as argument exists.
    if this function recieved groupname, convert gid and exec validation.
    """

    if type(value) == str:
        try:
            entry = grp.getgrnam(value)
            value = entry.gr_gid
        except KeyError:
            err_message = ('{0}: No such group.'.format(value))
            raise validate.VdtValueError(err_message)

        return value

    elif type(value) == int:
        try:
            grp.getgrgid(value)
        except KeyError:
            err_message = ('{0}: No such group.'.format(value))
            raise validate.VdtValueError(err_message)

        return value

    else:
        err_message = ('Please, use str or int to "user" parameter.')
        raise validate.VdtTypeError(err_message)
Esempio n. 2
0
def is_user(value, min=None, max=None):
    """
    Check whether username or uid as argument exists.
    if this function recieved username, convert uid and exec validation.
    """

    if type(value) == str:
        try:
            entry = pwd.getpwnam(value)
            value = entry.pw_uid
        except KeyError:
            err_message = ('{0}: No such user.'.format(value))
            raise validate.VdtValueError(err_message)

        return value

    elif type(value) == int:
        try:
            pwd.getpwuid(value)
        except KeyError:
            err_message = ('{0}: No such user.'.format(value))
            raise validate.VdtValueError(err_message)

        return value

    else:
        err_message = ('Please, use str or int to "user" parameter.')
        raise validate.VdtTypeError(err_message)
Esempio n. 3
0
def is_dir(value):
    """
    This function checks whether given path as argument exists.
    :param str value: Assumed directory path
    :rtype: str
    :return: If given value is valid, retuning given value.
    """
    value = os.path.expanduser(value)
    value = os.path.expandvars(value)
    value = os.path.abspath(value)

    if os.path.exists(value):
        if os.path.isdir(value):
            if os.access(value, os.R_OK):
                return value

            else:
                err_message = ('{path}: Permission denied.'
                               ''.format(path=value))
                raise validate.VdtValueError(err_message)
        else:
            err_message = ('{value} is file.' ''.format(value=value))
            raise validate.VdtTypeError(err_message)
    else:
        err_message = ('{path}: No such file or directory.'
                       ''.format(path=value))
        raise validate.VdtValueError(err_message)
 def validate_convert(value, func, name=None):
     """Helper function for validate parameter conversion"""
     if value is None:
         return None
     try:
         return func(value)
     except (TypeError, ValueError):
         if name is None:
             raise validate.VdtValueError(value)
         else:
             raise validate.VdtParamError(name, value)
Esempio n. 5
0
def is_log_format(value):
    u"""
    Check whether the value as argument be included the following list.
    ['ltsv', 'combined']
    """

    log_levels = ['ltsv', 'combined']

    if value in log_levels:
        return value

    else:
        err_message = ('"log_format" supported following value: '
                       '{0}'.format(log_levels))
        raise validate.VdtValueError(err_message)
Esempio n. 6
0
def is_log_level(value):
    u"""
    Check whether the value as argument be included the following list.
    ['debug', 'info', 'warn', 'error', 'crit']
    """

    log_levels = ['debug', 'info', 'warn', 'error', 'crit']

    if value in log_levels:
        return value

    else:
        err_message = ('"log_level" supported following value: '
                       '{0}'.format(log_levels))
        raise validate.VdtValueError(err_message)
Esempio n. 7
0
def is_pid(value):
    """
    This function checks whether file path
    that is specified at "pid_file" option eixsts,
    whether write permission to the file path.

    Return the following value:
    case1: exists path and write permission
        is_pid('/tmp')
            '/tmp/hogehoge.pid'

    case2: non-exists path and write permission
        is_pid('/tmp/hogehoge')
            '/tmp/hogehoge'
        In this case, hogehoge doesn't exist.
        but hogehoge is considered as a file.
        Thus, create pid file named 'hogehoge'.

    case3: complete non-exists path
        is_pid('/tmp/hogehoge/fugafuga')
            IOError: [Error 2] No such file or directory.
        The last part of given path is only considered pid_file's name.
        In this case, "fugafuga" is considered pid_file's name.

    In any case, check whether given path exists before checking permission.

    Notes: Even if given relative path, works fine.
           But, if don't use as much as possible if good.

           Recommended giving the absolute path including the pid file name.
    """

    value = os.path.expanduser(value)
    value = os.path.expandvars(value)
    value = os.path.abspath(value)

    pid_file = 'blackbird.pid'

    if os.path.exists(value):

        if os.path.isdir(value):

            if os.access(value, os.W_OK):
                return os.path.join(value, pid_file)

            else:
                err_message = ('{path}: Permission denied.'
                               ''.format(path=value)
                               )
                raise validate.VdtValueError(err_message)

        else:
            err_message = 'pid file already exists.'
            raise AlreadyLocked(err_message)

    else:
        directory = os.path.split(value)[0]

        if os.path.isdir(directory):

            if os.access(directory, os.W_OK):
                return value

            else:
                err_message = ('{directory}: Permission denied.'
                               ''.format(directory=directory)
                               )
                raise validate.VdtValueError(err_message)

        else:

            if os.path.exists(directory):
                err_message = ('{directory} is file.'
                               ''.format(directory=directory)
                               )
                raise validate.VdtTypeError(err_message)

            else:
                err_message = ('{directory}: No such file or directory.'
                               ''.format(directory=directory)
                               )
                raise validate.VdtValueError(err_message)