<commit_before># Copyright 2015-2016 Yelp Inc.
#
# 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.
import datetime
import json
import logging
import os
import re
import socket
from collections import namedtuple
from urlparse import urlparse

import humanize
import requests
from kazoo.client import KazooClient
from mesos.cli import util
from mesos.cli.exceptions import SlaveDoesNotExist

from paasta_tools.utils import format_table
from paasta_tools.utils import get_user_agent
from paasta_tools.utils import PaastaColors
from paasta_tools.utils import timeout
from paasta_tools.utils import TimeoutError

SlaveTaskCount = namedtuple('SlaveTaskCount', ['count', 'chronos_count', 'slave'])


log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())


# mesos.cli.master reads its config file at *import* time, so we must have
# this environment variable set and ready to go at that time so we can
# read in the config for zookeeper, etc
if 'MESOS_CLI_CONFIG' not in os.environ:
    os.environ['MESOS_CLI_CONFIG'] = '/nail/etc/mesos-cli.json'


class MasterNotAvailableException(Exception):
    pass


class SlaveNotAvailableException(Exception):
    pass


class NoSlavesAvailable(Exception):
    pass


class TaskNotFoundException(Exception):
    pass


class FileNotFoundForTaskException(Exception):
    pass


def raise_cli_exception(msg):
    if msg.startswith("unable to connect to a master"):
        raise MasterNotAvailableException(msg)
    if msg.startswith("Slave no longer exists"):
        raise SlaveNotAvailableException(msg)
    if msg.startswith("Cannot find a task by that name"):
        raise TaskNotFoundException(msg)
    if msg.startswith("No such task has the requested file or directory"):
        raise FileNotFoundForTaskException(msg)
    else:
        raise Exception(msg)

# monkey patch the log.fatal method to raise an exception rather than a sys.exit
import mesos.cli.log  # noqa
mesos.cli.log.fatal = lambda msg, code = 1: raise_cli_exception(msg)


MY_HOSTNAME = socket.getfqdn()
MESOS_MASTER_PORT = 5050
MESOS_SLAVE_PORT = '5051'
from mesos.cli import master  # noqa
import mesos.cli.cluster  # noqa


# monkey patch MesosMaster.state to use a larger ttl
@util.CachedProperty(ttl=60)
def fetch_state(self):
    return master.CURRENT.fetch("/master/state.json").json()
master.MesosMaster.state = fetch_state

# Works around a mesos-cli bug ('MesosSlave' object has no attribute 'id' - PAASTA-4119).
# The method below gets a slave by its ID. Original code here:
# https://github.com/mesosphere/mesos-cli/blob/master/mesos/cli/master.py#L176
# uses "in" instead of "==", matching when the wanted ID is a substring of
# the candidate. Because of that, multiple slaves are returned and mesos-cli
# finds itself in a buggy "if" branch (hence the AttributeError).<commit_msg>remove mesos master state.json monkey patch<commit_after># Copyright 2015-2016 Yelp Inc.
#
# 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.
import datetime
import json
import logging
import os
import re
import socket
from collections import namedtuple
from urlparse import urlparse

import humanize
import requests
from kazoo.client import KazooClient
from mesos.cli.exceptions import SlaveDoesNotExist

from paasta_tools.utils import format_table
from paasta_tools.utils import get_user_agent
from paasta_tools.utils import PaastaColors
from paasta_tools.utils import timeout
from paasta_tools.utils import TimeoutError

SlaveTaskCount = namedtuple('SlaveTaskCount', ['count', 'chronos_count', 'slave'])


log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())


# mesos.cli.master reads its config file at *import* time, so we must have
# this environment variable set and ready to go at that time so we can
# read in the config for zookeeper, etc
if 'MESOS_CLI_CONFIG' not in os.environ:
    os.environ['MESOS_CLI_CONFIG'] = '/nail/etc/mesos-cli.json'


class MasterNotAvailableException(Exception):
    pass


class SlaveNotAvailableException(Exception):
    pass


class NoSlavesAvailable(Exception):
    pass


class TaskNotFoundException(Exception):
    pass


class FileNotFoundForTaskException(Exception):
    pass


def raise_cli_exception(msg):
    if msg.startswith("unable to connect to a master"):
        raise MasterNotAvailableException(msg)
    if msg.startswith("Slave no longer exists"):
        raise SlaveNotAvailableException(msg)
    if msg.startswith("Cannot find a task by that name"):
        raise TaskNotFoundException(msg)
    if msg.startswith("No such task has the requested file or directory"):
        raise FileNotFoundForTaskException(msg)
    else:
        raise Exception(msg)

# monkey patch the log.fatal method to raise an exception rather than a sys.exit
import mesos.cli.log  # noqa
mesos.cli.log.fatal = lambda msg, code = 1: raise_cli_exception(msg)


MY_HOSTNAME = socket.getfqdn()
MESOS_MASTER_PORT = 5050
MESOS_SLAVE_PORT = '5051'
from mesos.cli import master  # noqa
import mesos.cli.cluster  # noqa


# Works around a mesos-cli bug ('MesosSlave' object has no attribute 'id' - PAASTA-4119).
# The method below gets a slave by its ID. Original code here:
# https://github.com/mesosphere/mesos-cli/blob/master/mesos/cli/master.py#L176
# uses "in" instead of "==", matching when the wanted ID is a substring of
# the candidate. Because of that, multiple slaves are returned and mesos-cli
# finds itself in a buggy "if" branch (hence the AttributeError).
{'fraction_non_alphanumeric': 0.07577407810798195, 'fraction_numerical': 0.009024428193558426, 'mean_word_length': 4.579861111111111, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3683223', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1851, 'n_words': 695}