<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4

#    Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
#
#    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 json
import re
import time

from devstack import date
from devstack import exceptions as excp
from devstack import log as logging
from devstack import settings
from devstack import shell as sh
from devstack import trace as tr
from devstack import utils

LOG = logging.getLogger("devstack.runners.screen")

#my type
RUN_TYPE = settings.RUN_TYPE_SCREEN
TYPE = settings.RUN_TYPE_TYPE

#trace constants
SCREEN_TEMPL = "%s.screen"
ARGS = "ARGS"
NAME = "NAME"
SESSION_ID = 'SESSION_ID'

#screen session name
SESSION_NAME = 'stack'
SESSION_DEF_TITLE = SESSION_NAME
SESSION_NAME_MTCHER = re.compile(r"^\s*([\d]+\.%s)\s*(.*)$" % (SESSION_NAME), re.I)

#how we setup screens status bar
STATUS_BAR_CMD = r'hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%< %= %H"'

#cmds
SESSION_INIT = ['screen', '-d', '-m', '-S', SESSION_NAME, '-t', SESSION_DEF_TITLE, '-s', "/bin/bash"]
BAR_INIT = ['screen', '-r', SESSION_NAME, '-X', STATUS_BAR_CMD]
CMD_INIT = ['screen', '-S', '%SESSION_NAME%', '-X', 'screen', '-t', "%NAME%"]
CMD_KILL = ['screen', '-S', '%SESSION_NAME%', '-p', "%NAME%", '-X', 'kill']
CMD_WIPE = ['screen', '-S', '%SESSION_NAME%', '-wipe']
CMD_START = ['screen', '-S', '%SESSION_NAME%', '-p', "%NAME%", '-X', 'stuff', "\"%CMD%\r\""]
LIST_CMD = ['screen', '-ls']
SCREEN_KILLER = ['screen', '-X', '-S', '%SCREEN_ID%', 'quit']

#where our screen sockets will go
SCREEN_SOCKET_DIR = "/tmp/devstack-sockets"
SCREEN_SOCKET_PERM = 0700

#used to wait until started before we can run the actual start cmd
WAIT_ONLINE_TO = settings.WAIT_ALIVE_SECS

#run screen as root?
ROOT_GO = True

#screen rc file
SCREEN_RC = 'stack-screenrc'


class ScreenRunner(object):
    def __init__(self, cfg):
        self.cfg = cfg

    def stop(self, name, tracedir):
        fn_name = SCREEN_TEMPL % (name)
        trace_fn = tr.trace_fn(tracedir, fn_name)
        session_id = self._find_session(name, trace_fn)
        self._do_stop(name, session_id)
        sh.unlink(trace_fn)

    def _gen_rc(self, session_name):
        lines = list()
        lines.append("# RC file generated on %s" % (date.rcf8222date()))
        lines.append("")
        lines.append("# Environment settings (these will need to be exported)")
        lines.append("# export SCREENDIR=%s" % (SCREEN_SOCKET_DIR))
        lines.append("")
        lines.append("# Session settings")
        lines.append("sessionname %s" % (session_name))
        lines.append(STATUS_BAR_CMD)
        lines.append("screen -t %s bash" % (SESSION_DEF_TITLE))
        return lines

    def _write_rc(self, session_name, out_fn):
        lines = self._gen_rc(session_name)
        contents = utils.joinlinesep(*lines)
        LOG.info("Writing your created screen rc file to [%s]" % (out_fn))
        sh.write_file(out_fn, contents)

    def _find_session(self, name, trace_fn):
        session_id = None
        for (key, value) in tr.parse_fn(trace_fn):
            if key == SESSION_ID and value:
                session_id = value
        if not session_id:
            msg = "Could not find a screen session id for %s" % (name)
            raise excp.StopException(msg)
        return session_id

    def _do_stop(self, name, session_id):
        mp = dict()
        mp['SESSION_NAME'] = session_id
        mp['NAME'] = name
        LOG.info("Stopping program running in session %s in window named %s." % (session_id, name))
        kill_cmd = self._gen_cmd(CMD_KILL, mp)
        sh.execute(*kill_cmd,<commit_msg>Updated screen rc creation for env variables.<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4

#    Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
#
#    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 json
import re
import time

from devstack import date
from devstack import exceptions as excp
from devstack import log as logging
from devstack import settings
from devstack import shell as sh
from devstack import trace as tr
from devstack import utils

LOG = logging.getLogger("devstack.runners.screen")

#my type
RUN_TYPE = settings.RUN_TYPE_SCREEN
TYPE = settings.RUN_TYPE_TYPE

#trace constants
SCREEN_TEMPL = "%s.screen"
ARGS = "ARGS"
NAME = "NAME"
SESSION_ID = 'SESSION_ID'

#screen session name
SESSION_NAME = 'stack'
SESSION_DEF_TITLE = SESSION_NAME
SESSION_NAME_MTCHER = re.compile(r"^\s*([\d]+\.%s)\s*(.*)$" % (SESSION_NAME), re.I)

#how we setup screens status bar
STATUS_BAR_CMD = r'hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%< %= %H"'

#cmds
SESSION_INIT = ['screen', '-d', '-m', '-S', SESSION_NAME, '-t', SESSION_DEF_TITLE, '-s', "/bin/bash"]
BAR_INIT = ['screen', '-r', SESSION_NAME, '-X', STATUS_BAR_CMD]
CMD_INIT = ['screen', '-S', '%SESSION_NAME%', '-X', 'screen', '-t', "%NAME%"]
CMD_KILL = ['screen', '-S', '%SESSION_NAME%', '-p', "%NAME%", '-X', 'kill']
CMD_WIPE = ['screen', '-S', '%SESSION_NAME%', '-wipe']
CMD_START = ['screen', '-S', '%SESSION_NAME%', '-p', "%NAME%", '-X', 'stuff', "\"%CMD%\r\""]
LIST_CMD = ['screen', '-ls']
SCREEN_KILLER = ['screen', '-X', '-S', '%SCREEN_ID%', 'quit']

#where our screen sockets will go
SCREEN_SOCKET_DIR = "/tmp/devstack-sockets"
SCREEN_SOCKET_PERM = 0700

#used to wait until started before we can run the actual start cmd
WAIT_ONLINE_TO = settings.WAIT_ALIVE_SECS

#run screen as root?
ROOT_GO = True

#screen rc file
SCREEN_RC = 'stack-screenrc'


class ScreenRunner(object):
    def __init__(self, cfg):
        self.cfg = cfg

    def stop(self, name, tracedir):
        fn_name = SCREEN_TEMPL % (name)
        trace_fn = tr.trace_fn(tracedir, fn_name)
        session_id = self._find_session(name, trace_fn)
        self._do_stop(name, session_id)
        sh.unlink(trace_fn)

    def _gen_rc(self, session_name):
        lines = list()
        lines.append("# RC file generated on %s" % (date.rcf8222date()))
        lines.append("")
        env_exports = self._get_env()
        if env_exports:
            lines.append("# Environment settings (these will need to be exported)")
            for (k, v) in env_exports.items():
                lines.append("# export %s=%s" % (k, sh.shellquote(v)))
            lines.append("")
        lines.append("# Session settings")
        lines.append("sessionname %s" % (session_name))
        lines.append(STATUS_BAR_CMD)
        lines.append("screen -t %s bash" % (SESSION_DEF_TITLE))
        lines.append("")
        return lines

    def _write_rc(self, session_name, out_fn):
        lines = self._gen_rc(session_name)
        contents = utils.joinlinesep(*lines)
        LOG.info("Writing your created screen rc file to [%s]" % (out_fn))
        sh.write_file(out_fn, contents)

    def _find_session(self, name, trace_fn):
        session_id = None
        for (key, value) in tr.parse_fn(trace_fn):
            if key == SESSION_ID and value:
                session_id = value
        if not session_id:
            msg = "Could not find a screen session id for %s" % (name)
            raise excp.StopException(msg)
        return session_id

    def _do_stop(self, name, session_id):
        mp = dict()
        mp['SESSION_NAME'] = session_id
        mp['NAME'] = name
        LOG.info("Stopping program running in session %s in window named %s." % (session_id, name))
        kill_cmd = self._gen_cmd(CMD_KILL, mp)
        sh.execute(*kill_cmd,
{'fraction_non_alphanumeric': 0.136047197640118, 'fraction_numerical': 0.00495575221238938, 'mean_word_length': 3.337768679631525, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5687783', 'n_tokens_mistral': 2824, 'n_tokens_neox': 2763, 'n_words': 840}