<commit_before># No shebang line, this module is meant to be imported
#
# Copyright 2014 <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.

import time
import os
import atexit
from datetime import timedelta, datetime
from errno import ENOENT, EEXIST
from os.path import isfile, dirname

try:
    from httplib import ACCEPTED, OK, BAD_REQUEST
except ImportError:  # pragma: no cover
    from http.client import ACCEPTED, OK, BAD_REQUEST
try:
    WindowsError
except NameError:  # pragma: no cover
    WindowsError = OSError

import psutil
from twisted.internet.defer import Deferred
from twisted.web.server import NOT_DONE_YET
from voluptuous import Schema, Optional

from pyfarm.agent.config import config
from pyfarm.agent.http.api.base import APIResource
from pyfarm.agent.logger import getLogger
from pyfarm.agent.sysinfo import memory
from pyfarm.agent.utility import dumps, total_seconds

logger = getLogger("agent.http.state")


class Stop(APIResource):
    isLeaf = False  # this is not really a collection of things
    SCHEMAS = {
        "POST": Schema({
            Optional("wait"): bool})}

    def post(self, **kwargs):
        request = kwargs["request"]
        data = kwargs["data"]
        agent = config["agent"]

        try:
            os.remove(config["run_control_file"])
        except (WindowsError, OSError, IOError) as e:
            if e.errno != ENOENT:
                logger.error("Could not delete run control file %s: %s: %s",
                             config["run_control_file"],
                             type(e).__name__, e)

                def remove_run_control_file():
                    try:
                        os.remove(config["run_control_file"])
                        logger.debug("Removed run control file %r",
                                     config["run_control_file"])
                    except (OSError, IOError, WindowsError) as e:
                        logger.error("Failed to remove run control file %s: "
                                     "%s: %s",
                                    config["run_control_file"],
                                    type(e).__name__, e)

                atexit.register(remove_run_control_file)

        stopping = agent.stop()

        # TODO: need to wire this up to the real deferred object in stop()
        if data.get("wait"):
            def finished(_, finish_request):
                finish_request.setResponseCode(OK)
                finish_request.finish()

            if isinstance(stopping, Deferred):
                stopping.addCallback(finished, request)
            else:  # pragma: no cover
                request.setResponseCode(OK)
                request.finish()

        else:
            request.setResponseCode(ACCEPTED)
            request.finish()

        return NOT_DONE_YET


class Restart(APIResource):<commit_msg>adding remove_file<commit_after># No shebang line, this module is meant to be imported
#
# Copyright 2014 <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.

import time
import os
from datetime import timedelta, datetime
from errno import EEXIST
from os.path import isfile, dirname

try:
    from httplib import ACCEPTED, OK, BAD_REQUEST
except ImportError:  # pragma: no cover
    from http.client import ACCEPTED, OK, BAD_REQUEST

import psutil
from twisted.internet.defer import Deferred
from twisted.web.server import NOT_DONE_YET
from voluptuous import Schema, Optional

from pyfarm.agent.config import config
from pyfarm.agent.http.api.base import APIResource
from pyfarm.agent.logger import getLogger
from pyfarm.agent.sysinfo import memory
from pyfarm.agent.utility import dumps, total_seconds, remove_file

logger = getLogger("agent.http.state")


class Stop(APIResource):
    isLeaf = False  # this is not really a collection of things
    SCHEMAS = {
        "POST": Schema({
            Optional("wait"): bool})}

    def post(self, **kwargs):
        request = kwargs["request"]
        data = kwargs["data"]
        agent = config["agent"]

        remove_file(
            config["run_control_file"],
            retry_on_exit=True, raise_=False)

        stopping = agent.stop()

        # TODO: need to wire this up to the real deferred object in stop()
        if data.get("wait"):
            def finished(_, finish_request):
                finish_request.setResponseCode(OK)
                finish_request.finish()

            if isinstance(stopping, Deferred):
                stopping.addCallback(finished, request)
            else:  # pragma: no cover
                request.setResponseCode(OK)
                request.finish()

        else:
            request.setResponseCode(ACCEPTED)
            request.finish()

        return NOT_DONE_YET


class Restart(APIResource):
{'fraction_non_alphanumeric': 0.07883313075186664, 'fraction_numerical': 0.0027782601146032295, 'mean_word_length': 2.484573502722323, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6320112', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1520, 'n_words': 533}