<commit_before>import os
import time
import sh

REAL_SOCK = "/var/run/docker.real.sock"
POWERSTRIP_SOCK = "/var/run/docker.sock"

def create_restarter():
    """
    Detect what init system is being used and return the appropriate handler.
    :return: A "restarter" object.
    """
    if os.path.exists(UpstartRestarter.DOCKER_DEFAULT_FILENAME):
        return UpstartRestarter()
    elif os.path.exists(SystemdRestarter.DOCKER_SYSTEMD_SERVICE):
        return UpstartRestarter()
    else:
        return NullRestarter()

class NullRestarter():
    def is_using_alternative_socket(self):
        return False
    def restart_docker_with_alternative_unix_socket(self):
        print "Unsupported"
    def restart_docker_without_alternative_unix_socket(self):
        print "Unsupported"


def _clean_socks():
    if os.path.exists(REAL_SOCK):
        os.remove(REAL_SOCK)
    if os.path.exists(POWERSTRIP_SOCK):
        os.remove(POWERSTRIP_SOCK)


class SystemdRestarter():
    # ExecStart=/usr/bin/docker -d $OPTIONS \
    def __init__(self):
        pass

    DOCKER_SYSTEMD_SERVICE = "/usr/lib/systemd/system/docker.service"
    SYSTEMD_DEFAULT = "ExecStart=/usr/bin/docker -d $OPTIONS \\"
    SYSTEMD_MODIFIED = "ExecStart=/usr/bin/docker -d $OPTIONS " \
                       "-H unix://%s \\" % REAL_SOCK

    def _clean_restart_docker(self, sock_to_wait_on):
        _clean_socks()
        systemctl = sh.Command._create("systemctl")
        systemctl("restart", "docker.service")

        # Wait for docker to create the socket
        while not os.path.exists(sock_to_wait_on):
            time.sleep(0.1)


    def is_using_alternative_socket(self):
        if self.SYSTEMD_MODIFIED in open(self.DOCKER_SYSTEMD_SERVICE).read():
            return True

    def restart_docker_with_alternative_unix_socket(self):
        if not self.is_using_alternative_socket():
            #TODO - This needs to be a replace.
            with open(self.DOCKER_SYSTEMD_SERVICE, "a") as docker_config:
                docker_config.write(self.DOCKER_OPTIONS)
            self._clean_restart_docker(REAL_SOCK)

        # Always remove the socket that powerstrip will use, as it gets upset
        # otherwise.
        if os.path.exists(POWERSTRIP_SOCK):
            os.remove(POWERSTRIP_SOCK)


    def restart_docker_without_alternative_unix_socket(self):
        if self.is_using_alternative_socket():
            #TODO - Do the right thing here.
            good_lines = [line for line in open(
                self.DOCKER_SYSTEMD_SERVICE)
                          if self.DOCKER_OPTIONS not in line]
            open(self.DOCKER_SYSTEMD_SERVICE, 'w').writelines(good_lines)
            self._clean_restart_docker(POWERSTRIP_SOCK)

class UpstartRestarter():
    def __init__(self):
        pass

    DOCKER_DEFAULT_FILENAME = "/etc/default/docker"
    DOCKER_OPTIONS = 'DOCKER_OPTS="-H unix://%s"' % REAL_SOCK

    def _clean_restart_docker(self, sock_to_wait_on):
        _clean_socks()
        restart = sh.Command._create("restart")
        restart("docker")

        # Wait for docker to create the socket
        while not os.path.exists(sock_to_wait_on):
            time.sleep(0.1)

    def is_using_alternative_socket(self):
        if self.DOCKER_OPTIONS in open(self.DOCKER_DEFAULT_FILENAME).read():
            return True

    def restart_docker_with_alternative_unix_socket(self):
        if not self.is_using_alternative_socket():
            with open(self.DOCKER_DEFAULT_FILENAME, "a") as docker_config:
                docker_config.write(self.DOCKER_OPTIONS)
            self._clean_restart_docker(REAL_SOCK)

        # Always remove the socket that powerstrip will use, as it gets upset
        # otherwise.
        if os.path.exists(POWERSTRIP_SOCK):
            os.remove(POWERSTRIP_SOCK)


    def restart_docker_without_alternative_unix_socket(self):
        if self.is_using_alternative_socket():
            good_lines = [line for line in open(
                self.DOCKER_DEFAULT_FILENAME)<commit_msg>First version that could work<commit_after>import os
import time
import sh
import fileinput
import sys
REAL_SOCK = "/var/run/docker.real.sock"
POWERSTRIP_SOCK = "/var/run/docker.sock"


def _replace_all(filename, search, replace):
    for line in fileinput.input(filename, inplace=1):
        if search in line:
            line = line.replace(search, replace)
        sys.stdout.write(line)

def create_restarter():
    """
    Detect what init system is being used and return the appropriate handler.
    :return: A "restarter" object.
    """
    if os.path.exists(SystemdRestarter.DOCKER_SYSTEMD_SERVICE):
        return SystemdRestarter()
    elif os.path.exists(UpstartRestarter.DOCKER_DEFAULT_FILENAME):
        return UpstartRestarter()
    else:
        return NullRestarter()


class NullRestarter():
    def is_using_alternative_socket(self):
        return False
    def restart_docker_with_alternative_unix_socket(self):
        print "Unsupported"
    def restart_docker_without_alternative_unix_socket(self):
        print "Unsupported"


def _clean_socks():
    if os.path.exists(REAL_SOCK):
        os.remove(REAL_SOCK)
    if os.path.exists(POWERSTRIP_SOCK):
        os.remove(POWERSTRIP_SOCK)


class SystemdRestarter():
    def __init__(self):
        pass

    DOCKER_SYSTEMD_SERVICE = "/usr/lib/systemd/system/docker.service"
    SYSTEMD_DEFAULT = "ExecStart=/usr/bin/docker -d $OPTIONS \\"
    SYSTEMD_MODIFIED = "ExecStart=/usr/bin/docker -d $OPTIONS " \
                       "-H unix://%s \\" % REAL_SOCK

    def _clean_restart_docker(self, sock_to_wait_on):
        _clean_socks()
        systemctl = sh.Command._create("systemctl")
        systemctl("restart", "docker.service")

        # Wait for docker to create the socket
        while not os.path.exists(sock_to_wait_on):
            time.sleep(0.1)

    def is_using_alternative_socket(self):
        if self.SYSTEMD_MODIFIED in open(self.DOCKER_SYSTEMD_SERVICE).read():
            return True

    def restart_docker_with_alternative_unix_socket(self):
        if not self.is_using_alternative_socket():
            _replace_all(self.DOCKER_SYSTEMD_SERVICE,
                         self.SYSTEMD_DEFAULT,
                         self.SYSTEMD_MODIFIED)
            self._clean_restart_docker(REAL_SOCK)

        # Always remove the socket that powerstrip will use, as it gets upset
        # otherwise.
        if os.path.exists(POWERSTRIP_SOCK):
            os.remove(POWERSTRIP_SOCK)

    def restart_docker_without_alternative_unix_socket(self):
        if self.is_using_alternative_socket():
            _replace_all(self.DOCKER_SYSTEMD_SERVICE,
                         self.SYSTEMD_MODIFIED,
                         self.SYSTEMD_DEFAULT)
            self._clean_restart_docker(POWERSTRIP_SOCK)


class UpstartRestarter():
    def __init__(self):
        pass

    DOCKER_DEFAULT_FILENAME = "/etc/default/docker"
    DOCKER_OPTIONS = 'DOCKER_OPTS="-H unix://%s"' % REAL_SOCK

    def _clean_restart_docker(self, sock_to_wait_on):
        _clean_socks()
        restart = sh.Command._create("restart")
        restart("docker")

        # Wait for docker to create the socket
        while not os.path.exists(sock_to_wait_on):
            time.sleep(0.1)

    def is_using_alternative_socket(self):
        if self.DOCKER_OPTIONS in open(self.DOCKER_DEFAULT_FILENAME).read():
            return True

    def restart_docker_with_alternative_unix_socket(self):
        if not self.is_using_alternative_socket():
            with open(self.DOCKER_DEFAULT_FILENAME, "a") as docker_config:
                docker_config.write(self.DOCKER_OPTIONS)
            self._clean_restart_docker(REAL_SOCK)

        # Always remove the socket that powerstrip will use, as it gets upset
        # otherwise.
        if os.path.exists(POWERSTRIP_SOCK):
            os.remove(POWERSTRIP_SOCK)

    def restart_docker_without_alternative_unix_socket(self):
        if self.is_using_alternative_socket():
            good_lines = [line for line in open(
                self.DOCKER_DEFAULT_FILENAME)
{'fraction_non_alphanumeric': 0.08691358024691358, 'fraction_numerical': 0.0011111111111111111, 'mean_word_length': 2.729742173112339, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4901944', 'n_tokens_mistral': 2674, 'n_tokens_neox': 2514, 'n_words': 484}