<commit_before>from deployer.service import Service, required_property, isolate_host, isolate_one_only
from deployer.utils import esc1
from deployer.contrib.services.apt_get import AptGet
from deployer.exceptions import ExecCommandFailed


class MySQL(Service):
    port = 3306


@isolate_host
class MySQLClient(Service):
    """
    For simple Mysql operations, on a remote host.
    """
    hostname = required_property()
    username = required_property()
    password = required_property()
    database = required_property()

    def ensure_database_exists(self):
        """
        Create this database in MySQL if it wasn't created yet.
        """
        name = self.database
        try:
            # Try connect.
            self.hosts.run("echo 'CONNECT %s;' | /usr/bin/mysql --user '%s' --password='%s' --host '%s' " %
                    (name, esc1(self.username), esc1(self.password), esc1(self.hostname)))
        except ExecCommandFailed, e:
            # Connect raised error, so create it.
            self.hosts.run("echo 'CREATE DATABASE %s;' | /usr/bin/mysql --user '%s' --password='%s' --host '%s' " %
                    (name, esc1(self.username), esc1(self.password), esc1(self.hostname)))

    @isolate_one_only
    def restore_backup_from_url(self):
        backup_url = self.console.input('Enter the URL of the backup location (an .sql.gz file)')
        self.hosts.run("curl '%s' | gunzip | /usr/bin/mysql --user '%s' --password='%s' --host '%s' '%s' " %
                    (esc1(backup_url), esc1(self.username), esc1(self.password), esc1(self.hostname), esc1(self.database)))

    @isolate_one_only
    def shell(self):
        self.hosts.run("/usr/bin/mysql --user '%s' --password='%s' --host '%s' '%s' " %
                    (esc1(self.username), esc1(self.password), esc1(self.hostname), esc1(self.database)))

<commit_msg>mysql restore from backup, optionally pass URL.<commit_after>from deployer.service import Service, required_property, isolate_host, isolate_one_only
from deployer.utils import esc1
from deployer.contrib.services.apt_get import AptGet
from deployer.exceptions import ExecCommandFailed


class MySQL(Service):
    port = 3306


@isolate_host
class MySQLClient(Service):
    """
    For simple Mysql operations, on a remote host.
    """
    hostname = required_property()
    username = required_property()
    password = required_property()
    database = required_property()

    def ensure_database_exists(self):
        """
        Create this database in MySQL if it wasn't created yet.
        """
        name = self.database
        try:
            # Try connect.
            self.hosts.run("echo 'CONNECT %s;' | /usr/bin/mysql --user '%s' --password='%s' --host '%s' " %
                    (name, esc1(self.username), esc1(self.password), esc1(self.hostname)))
        except ExecCommandFailed, e:
            # Connect raised error, so create it.
            self.hosts.run("echo 'CREATE DATABASE %s;' | /usr/bin/mysql --user '%s' --password='%s' --host '%s' " %
                    (name, esc1(self.username), esc1(self.password), esc1(self.hostname)))

    @isolate_one_only
    def restore_backup_from_url(self, url=None):
        if not url:
            url = self.console.input('Enter the URL of the backup location (an .sql.gz file)')
        self.hosts.run("curl '%s' | gunzip | /usr/bin/mysql --user '%s' --password='%s' --host '%s' '%s' " %
                    (esc1(url), esc1(self.username), esc1(self.password), esc1(self.hostname), esc1(self.database)))

    @isolate_one_only
    def shell(self):
        self.hosts.run("/usr/bin/mysql --user '%s' --password='%s' --host '%s' '%s' " %
                    (esc1(self.username), esc1(self.password), esc1(self.hostname), esc1(self.database)))


{'fraction_non_alphanumeric': 0.14425622022233986, 'fraction_numerical': 0.010587612493382742, 'mean_word_length': 2.719488188976378, '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': '2744245', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1144, 'n_words': 286}