<commit_before>#!/usr/bin/env python2
#
# Copyright (C) Microsoft Corporation, All rights reserved.

"""Runtime module. Contains runtime base class and language specific runtime classes."""

import signal
import subprocess
import sys
import time
from distutils.spawn import find_executable

import serializerfactory
import subprocessfactory
import tracer
from runbook import *
from workerexception import *

json = serializerfactory.get_serializer(sys.version_info)


class Runtime:
    """Runtime base class."""

    def __init__(self, job_data, runbook):
        """
        :type job_data  : jrdsclient.JobData"
        :type runbook   : Runbook
        """
        # should be overwritten by language runtime
        self.execution_alias = None
        self.base_cmd = None

        # used for actual runtime
        self.runbook = runbook
        self.runbook_subprocess = None
        self.job_data = job_data

    def initialize(self):
        self.runbook.write_to_disk()

    def start_runbook_subprocess(self):
        """Creates the runbook subprocess based on the script language and using properties set by the derived class.

        Requires self.base_cmd & self.runbook_file_path to be set by derived class.
        """
        cmd = self.base_cmd + [self.runbook.runbook_file_path]
        job_parameters = self.job_data.parameters
        if job_parameters is not None and len(job_parameters) > 0:
            for parameter in job_parameters:
                tracer.log_debug_trace("Parameter is: \n" + str(parameter))
                if self.runbook.definition_kind_str == "PowerShell" and parameter["Name"]:
                    # Handle named parameters for PowerShell arriving out of order
                    cmd += ["-%s" % parameter["Name"]]
                cmd += [str(json.loads(parameter["Value"]))]

        # Do not copy current process env var to the sandbox process
        env = os.environ.copy()
        env.update({"AUTOMATION_JOB_ID": str(self.job_data.job_id),
                    "AUTOMATION_ACTIVITY_ID": str(tracer.u_activity_id),
                    "PYTHONPATH": str(
                        configuration.get_source_directory_path())})  # windows env have to be str (not unicode)
        self.runbook_subprocess = subprocessfactory.create_subprocess(cmd=cmd,
                                                                      env=env,
                                                                      stdout=subprocess.PIPE,
                                                                      stderr=subprocess.PIPE)

    def kill_runbook_subprocess(self):<commit_msg>fix HOME path for processes started as nxautomation user (#594)<commit_after>#!/usr/bin/env python2
#
# Copyright (C) Microsoft Corporation, All rights reserved.

"""Runtime module. Contains runtime base class and language specific runtime classes."""

import signal
import subprocess
import sys
import time
from distutils.spawn import find_executable

import serializerfactory
import subprocessfactory
import tracer
from runbook import *
from workerexception import *

json = serializerfactory.get_serializer(sys.version_info)


class Runtime:
    """Runtime base class."""

    def __init__(self, job_data, runbook):
        """
        :type job_data  : jrdsclient.JobData"
        :type runbook   : Runbook
        """
        # should be overwritten by language runtime
        self.execution_alias = None
        self.base_cmd = None

        # used for actual runtime
        self.runbook = runbook
        self.runbook_subprocess = None
        self.job_data = job_data

    def initialize(self):
        self.runbook.write_to_disk()

    def start_runbook_subprocess(self):
        """Creates the runbook subprocess based on the script language and using properties set by the derived class.

        Requires self.base_cmd & self.runbook_file_path to be set by derived class.
        """
        cmd = self.base_cmd + [self.runbook.runbook_file_path]
        job_parameters = self.job_data.parameters
        if job_parameters is not None and len(job_parameters) > 0:
            for parameter in job_parameters:
                tracer.log_debug_trace("Parameter is: \n" + str(parameter))
                if self.runbook.definition_kind_str == "PowerShell" and parameter["Name"]:
                    # Handle named parameters for PowerShell arriving out of order
                    cmd += ["-%s" % parameter["Name"]]
                cmd += [str(json.loads(parameter["Value"]))]

        # Do not copy current process env var to the sandbox process
        env = os.environ.copy()
        env.update({"AUTOMATION_JOB_ID": str(self.job_data.job_id),
                    "AUTOMATION_ACTIVITY_ID": str(tracer.u_activity_id),
                    "PYTHONPATH": str(configuration.get_source_directory_path()),
                    "HOME": str(os.getcwd())})  # windows env have to be str (not unicode)
        self.runbook_subprocess = subprocessfactory.create_subprocess(cmd=cmd,
                                                                      env=env,
                                                                      stdout=subprocess.PIPE,
                                                                      stderr=subprocess.PIPE)

    def kill_runbook_subprocess(self):
{'fraction_non_alphanumeric': 0.07948766245997363, 'fraction_numerical': 0.0013185157280090412, 'mean_word_length': 2.1125439624853457, 'pattern_counts': {'":': 7, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6889245', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1348, 'n_words': 383}