<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import time

from functools import partial
from collections import namedtuple

from libshadow import *
from exception import *
from taskstats.taskstats import Taskstats
from priorities import nice, setnice, ioprio


class Profile(object):
    '''
    Profile object: builds a <Profile> object from the "pid" provided on
    initialization.
    '''

    STAT_FIELDS = ['pid', 'tcomm', 'state', 'ppid', 'pgrp', 'sid', 'tty_nr', 
                   'tty_pgrp', 'flags', 'min_flt', 'cmin_flt', 'maj_flt',
                   'cmaj_flt', 'utime', 'stime', 'cutime', 'cstime',
                   'priority', 'nice', 'num_threads', 'it_real_value', 
                   'start_time', 'vsize', 'rss', 'rsslim', 'start_code', 
                   'end_code', 'start_stack', 'esp', 'eip', 'pending', 
                   'blocked', 'sigign', 'sigcatch', 'wchan', 'NONE', 'NONE', 
                   'exit_signal', 'task_cpu', 'rt_priority', 'policy', 
                   'blkio_ticks', 'gtime', 'cgtime', 'start_data', 'end_data', 
                   'arg_start', 'arg_end', 'env_start', 'env_end', 'exit_code'
                  ]

    PROC_STATES = {'R':'running', 'S':'interruptible_sleep', 
                   'D':'uninterruptible_disk_sleep', 'Z':'zombie', 
                   'T':'traced', 'W':'paging'
                  }

    def __init__(self, pid):
        self.pid = pid
        self.__taskstats = Taskstats(self.pid)
        self.__rst_state = self.rBytes()
        self.__wst_state = self.wBytes()
        self._last_r = self.__rst_state
        self._last_w = self.__wst_state
        self.__st_switches = self.__pid_status_attrs.get(
                                 'voluntary_ctxt_switches')
        if not self.__st_switches:
            self.__st_switches = 0
        
    @property
    def is_alive(self):
        '''
        Class property: returns <type 'bool'> response of processes state.
        '''
        all_processes = filter(lambda i: i.isdigit(), os.listdir('/proc'))
        if self.pid in map(int, all_processes):
            return True
        return False

    @property
    def ppid(self):
        '''
        Class property: returns <type 'int'> of the profiled pid's parent.
        '''
        pid_stats = self.file_reader('/proc/%s/stat' % self.pid)
        return int(pid_stats.split()[3])

    @property
    def name(self):
        '''
        Class property: returns <type 'str'> of profiled pid's name.
        '''<commit_msg>Create setter method for property is_alive, allows for killing of process<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import time

from functools import partial
from collections import namedtuple

from libshadow import *
from exception import *
from taskstats.taskstats import Taskstats
from priorities import nice, setnice, ioprio


class Profile(object):
    '''
    Profile object: builds a <Profile> object from the "pid" provided on
    initialization.
    '''

    STAT_FIELDS = ['pid', 'tcomm', 'state', 'ppid', 'pgrp', 'sid', 'tty_nr', 
                   'tty_pgrp', 'flags', 'min_flt', 'cmin_flt', 'maj_flt',
                   'cmaj_flt', 'utime', 'stime', 'cutime', 'cstime',
                   'priority', 'nice', 'num_threads', 'it_real_value', 
                   'start_time', 'vsize', 'rss', 'rsslim', 'start_code', 
                   'end_code', 'start_stack', 'esp', 'eip', 'pending', 
                   'blocked', 'sigign', 'sigcatch', 'wchan', 'NONE', 'NONE', 
                   'exit_signal', 'task_cpu', 'rt_priority', 'policy', 
                   'blkio_ticks', 'gtime', 'cgtime', 'start_data', 'end_data', 
                   'arg_start', 'arg_end', 'env_start', 'env_end', 'exit_code'
                  ]

    PROC_STATES = {'R':'running', 'S':'interruptible_sleep', 
                   'D':'uninterruptible_disk_sleep', 'Z':'zombie', 
                   'T':'traced', 'W':'paging'
                  }

    def __init__(self, pid):
        self.pid = pid
        self.__taskstats = Taskstats(self.pid)
        self.__rst_state = self.rBytes()
        self.__wst_state = self.wBytes()
        self._last_r = self.__rst_state
        self._last_w = self.__wst_state
        self.__st_switches = self.__pid_status_attrs.get(
                                 'voluntary_ctxt_switches')
        if self.__st_switches is None:
            self.__st_switches = 0
        
    @property
    def is_alive(self):
        '''
        Class property.getter: returns <type 'bool'> response of processes 
        state.
        '''
        all_processes = filter(lambda i: i.isdigit(), os.listdir('/proc'))
        if self.pid in map(int, all_processes):
            return True
        return False

    @is_alive.setter
    def is_alive(self, state):
        '''
        Class property.setter: sets the is_alive property to False if ending
        the process is needed.
        '''
        if not isinstance(state, bool):
            raise TypeError
        tgid = self.tgid
        if not tgid:
            raise BadProcess(self.pid)
        tkill(tgid, self.pid, SIGKILL)
        
    @property
    def ppid(self):
        '''
        Class property: returns <type 'int'> of the profiled pid's parent.
        '''
        pid_stats = self.file_reader('/proc/%s/stat' % self.pid)
        return int(pid_stats.split()[3])

    @property
    def name(self):
        '''
        Class property: returns <type 'str'> of profiled pid's name.
        '''
{'fraction_non_alphanumeric': 0.13507151910193735, 'fraction_numerical': 0.0010863661053775121, 'mean_word_length': 2.0929451287793954, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4578449', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1668, 'n_words': 413}