<commit_before># -*- coding: utf8 -*-
# Copyright © 2015 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import unicode_literals

from testinfra.modules.base import Module


class Service(Module):
    """Test services"""

    def __init__(self, _backend, name):
        self.name = name
        super(Service, self).__init__(_backend)

    def __call__(self, name):
        return self.__class__(self._backend, name)

    @property
    def is_running(self):
        """Check if service is running"""
        raise NotImplementedError

    @property
    def is_enabled(self):
        """Check if service is enabled"""
        raise NotImplementedError

    @classmethod
    def get_module(cls, _backend):
        SystemInfo = _backend.get_module("SystemInfo")
        Command = _backend.get_module("Command")
        File = _backend.get_module("File")
        if SystemInfo.type == "linux":
            if (
                Command.run_test("which systemctl").rc == 0
                and "systemd" in File("/sbin/init").linked_to
            ):
                return SystemdService(_backend, None)
            else:
                return LinuxService(_backend, None)
        elif SystemInfo.type == "freebsd":
            return FreeBSDService(_backend, None)
        elif SystemInfo.type == "openbsd":
            return OpenBSDService(_backend, None)
        elif SystemInfo.type == "netbsd":
            return NetBSDService(_backend, None)
        else:
            raise NotImplementedError

    def __repr__(self):
        return "<service %s>" % (self.name,)


class LinuxService(Service):

    @property
    def is_running(self):
        # based on /lib/lsb/init-functions
        # 0: program running
        # 1: program is dead and pid file exists
        # 3: not running and pid file does not exists
        # 4: Unable to determine status
        return self.run_expect(
            [0, 1, 3], "service %s status", self.name).rc == 0

    @property
    def is_enabled(self):
        return self.is_enabled_with_level(3)

    def is_enabled_with_level(self, level):
        # sysv
        if self.run_test(
            "ls /etc/rc%s.d | grep -q 'S..%s'",
            str(level), self.name
        ).rc == 0:
            return True
        # systemd
        elif self.run(
            "grep -q 'start on' /etc/init/%s.conf", self.name
        ).rc == 0:
            return True
        else:
            return False


class SystemdService(Service):

    @property
    def is_running(self):
        return self.run_expect(
            [0, 3], "systemctl is-active %s", self.name).rc == 0

    @property
    def is_enabled(self):
        return self.run_test("systemctl is-enabled %s", self.name).rc == 0


class FreeBSDService(Service):

    @property
    def is_running(self):
        return self.run_test("service %s onestatus", self.name).rc == 0

    @property
    def is_enabled(self):
        # Return list of enabled services like
        # /etc/rc.d/sshd
        # /etc/rc.d/sendmail
        for path in self.check_output("service -e").splitlines():
            if path and path.rsplit("/", 1)[1] == self.name:
                return True
        return False


class OpenBSDService(Service):

    @property
    def is_running(self):
        return self.run_test("/etc/rc.d/%s check", self.name).rc == 0


class NetBSDService(Service):

    @property
    def is_running(self):
        return self.run_test("/etc/rc.d/%s onestatus", self.name).rc == 0
<commit_msg>Service: Ignore sysv runlevels for "is_enabled"<commit_after># -*- coding: utf8 -*-
# Copyright © 2015 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import unicode_literals

from testinfra.modules.base import Module


class Service(Module):
    """Test services"""

    def __init__(self, _backend, name):
        self.name = name
        super(Service, self).__init__(_backend)

    def __call__(self, name):
        return self.__class__(self._backend, name)

    @property
    def is_running(self):
        """Check if service is running"""
        raise NotImplementedError

    @property
    def is_enabled(self):
        """Check if service is enabled"""
        raise NotImplementedError

    @classmethod
    def get_module(cls, _backend):
        SystemInfo = _backend.get_module("SystemInfo")
        Command = _backend.get_module("Command")
        File = _backend.get_module("File")
        if SystemInfo.type == "linux":
            if (
                Command.run_test("which systemctl").rc == 0
                and "systemd" in File("/sbin/init").linked_to
            ):
                return SystemdService(_backend, None)
            else:
                return LinuxService(_backend, None)
        elif SystemInfo.type == "freebsd":
            return FreeBSDService(_backend, None)
        elif SystemInfo.type == "openbsd":
            return OpenBSDService(_backend, None)
        elif SystemInfo.type == "netbsd":
            return NetBSDService(_backend, None)
        else:
            raise NotImplementedError

    def __repr__(self):
        return "<service %s>" % (self.name,)


class LinuxService(Service):

    @property
    def is_running(self):
        # based on /lib/lsb/init-functions
        # 0: program running
        # 1: program is dead and pid file exists
        # 3: not running and pid file does not exists
        # 4: Unable to determine status
        return self.run_expect(
            [0, 1, 3], "service %s status", self.name).rc == 0

    @property
    def is_enabled(self):
        # SysV
        if self.check_output("find /etc/rc?.d -name %s", "S??" + self.name):
            return True
        # Upstart
        elif self.run(
            "grep -q 'start on' /etc/init/%s.conf",
            self.name,
        ).rc == 0:
            return True
        else:
            return False


class SystemdService(Service):

    @property
    def is_running(self):
        return self.run_expect(
            [0, 3], "systemctl is-active %s", self.name).rc == 0

    @property
    def is_enabled(self):
        return self.run_test("systemctl is-enabled %s", self.name).rc == 0


class FreeBSDService(Service):

    @property
    def is_running(self):
        return self.run_test("service %s onestatus", self.name).rc == 0

    @property
    def is_enabled(self):
        # Return list of enabled services like
        # /etc/rc.d/sshd
        # /etc/rc.d/sendmail
        for path in self.check_output("service -e").splitlines():
            if path and path.rsplit("/", 1)[1] == self.name:
                return True
        return False


class OpenBSDService(Service):

    @property
    def is_running(self):
        return self.run_test("/etc/rc.d/%s check", self.name).rc == 0


class NetBSDService(Service):

    @property
    def is_running(self):
        return self.run_test("/etc/rc.d/%s onestatus", self.name).rc == 0

{'fraction_non_alphanumeric': 0.1084459032950385, 'fraction_numerical': 0.007322307789420527, 'mean_word_length': 2.2857735379510578, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5975030', 'n_tokens_mistral': 2347, 'n_tokens_neox': 2222, 'n_words': 690}