<commit_before># -*- coding: utf-8 -*-
'''
runit service module
(http://smarden.org/runit)

This module is compatible with the :mod:`service <salt.states.service>` states,
so it can be used to maintain services using the ``provider`` argument:

.. code-block:: yaml

    myservice:
      service:
        - running
        - provider: runit

Provides virtual `service` module on systems using runit as init.


Service management rules (`sv` command):

    service $n is ENABLED   if file SERVICE_DIR/$n/run exists
    service $n is AVAILABLE if ENABLED or if file AVAIL_SVR_DIR/$n/run exists
    service $n is DISABLED  if AVAILABLE but not ENABLED

    SERVICE_DIR/$n is normally a symlink to a AVAIL_SVR_DIR/$n folder


Service auto-start/stop mechanism:

    `sv` (auto)starts/stops service as soon as SERVICE_DIR/<service> is
    created/deleted, both on service creation or a boot time.

    autostart feature is disabled if file SERVICE_DIR/<n>/down exists. This
    does not affect the current's service status (if already running) nor
    manual service management.


Service's alias:

    Service `sva` is an alias of service `svc` when `AVAIL_SVR_DIR/sva` symlinks
    to folder `AVAIL_SVR_DIR/svc`. `svc` can't be enabled if it is already
    enabled through an alias already enabled, since `sv` files are stored in
    folder `SERVICE_DIR/svc/`.

    XBPS package management uses a service's alias to provides service
    alternative(s), such as chrony and openntpd both aliased to ntpd.
'''
from __future__ import absolute_import

# Import python libs
import os
import glob
import logging
import time

log = logging.getLogger(__name__)

# Import salt libs
from salt.exceptions import CommandExecutionError
import salt.utils

# Function alias to not shadow built-ins.
__func_alias__ = {
    'reload_': 'reload'
}

# which dir sv works with
VALID_SERVICE_DIRS = [
    '/service',
    '/var/service',
    '/etc/service',
]
SERVICE_DIR = None
for service_dir in VALID_SERVICE_DIRS:
    if os.path.exists(service_dir):
        SERVICE_DIR = service_dir
        break

# available service directory(ies)
AVAIL_SVR_DIRS = []

# Define the module's virtual name
__virtualname__ = 'service'


def __virtual__():
    '''
    Virtual service only on systems using runit as init process (PID 1).
    Otherwise, use this module with the provider mechanism.
    '''
    if __grains__['init'] == 'runit':
        if __grains__['os'] == 'Void':
            add_svc_avail_path('/etc/sv')
        return __virtualname__
    return False


def _service_path(name):
    '''
    Return SERVICE_DIR+name if possible

    name
        the service's name to work on
    '''
    if not SERVICE_DIR:
        raise CommandExecutionError('Could not find service directory.')
    return os.path.join(SERVICE_DIR, name)


#-- states.service  compatible args
def start(name):
    '''
    Start service<commit_msg>Fix KeyError when loading runit execution module (#36882)<commit_after># -*- coding: utf-8 -*-
'''
runit service module
(http://smarden.org/runit)

This module is compatible with the :mod:`service <salt.states.service>` states,
so it can be used to maintain services using the ``provider`` argument:

.. code-block:: yaml

    myservice:
      service:
        - running
        - provider: runit

Provides virtual `service` module on systems using runit as init.


Service management rules (`sv` command):

    service $n is ENABLED   if file SERVICE_DIR/$n/run exists
    service $n is AVAILABLE if ENABLED or if file AVAIL_SVR_DIR/$n/run exists
    service $n is DISABLED  if AVAILABLE but not ENABLED

    SERVICE_DIR/$n is normally a symlink to a AVAIL_SVR_DIR/$n folder


Service auto-start/stop mechanism:

    `sv` (auto)starts/stops service as soon as SERVICE_DIR/<service> is
    created/deleted, both on service creation or a boot time.

    autostart feature is disabled if file SERVICE_DIR/<n>/down exists. This
    does not affect the current's service status (if already running) nor
    manual service management.


Service's alias:

    Service `sva` is an alias of service `svc` when `AVAIL_SVR_DIR/sva` symlinks
    to folder `AVAIL_SVR_DIR/svc`. `svc` can't be enabled if it is already
    enabled through an alias already enabled, since `sv` files are stored in
    folder `SERVICE_DIR/svc/`.

    XBPS package management uses a service's alias to provides service
    alternative(s), such as chrony and openntpd both aliased to ntpd.
'''
from __future__ import absolute_import

# Import python libs
import os
import glob
import logging
import time

log = logging.getLogger(__name__)

# Import salt libs
from salt.exceptions import CommandExecutionError
import salt.utils

# Function alias to not shadow built-ins.
__func_alias__ = {
    'reload_': 'reload'
}

# which dir sv works with
VALID_SERVICE_DIRS = [
    '/service',
    '/var/service',
    '/etc/service',
]
SERVICE_DIR = None
for service_dir in VALID_SERVICE_DIRS:
    if os.path.exists(service_dir):
        SERVICE_DIR = service_dir
        break

# available service directory(ies)
AVAIL_SVR_DIRS = []

# Define the module's virtual name
__virtualname__ = 'service'


def __virtual__():
    '''
    Virtual service only on systems using runit as init process (PID 1).
    Otherwise, use this module with the provider mechanism.
    '''
    if __grains__.get('init') == 'runit':
        if __grains__['os'] == 'Void':
            add_svc_avail_path('/etc/sv')
        return __virtualname__
    return False


def _service_path(name):
    '''
    Return SERVICE_DIR+name if possible

    name
        the service's name to work on
    '''
    if not SERVICE_DIR:
        raise CommandExecutionError('Could not find service directory.')
    return os.path.join(SERVICE_DIR, name)


#-- states.service  compatible args
def start(name):
    '''
    Start service
{'fraction_non_alphanumeric': 0.08564576909900651, 'fraction_numerical': 0.001541623843782117, 'mean_word_length': 3.3348181143281366, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1165063', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1758, 'n_words': 644}