<commit_before>#!/usr/bin/env python

'''
Copyright 2011 <NAME> <<EMAIL>> [http://www.sideralis.org]

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.
'''

__all__ = ['get_system_start_time','process_exists','process_kill','already_running']

import os,sys,atexit
from fcntl import lockf,LOCK_EX,LOCK_NB,LOCK_UN
from tempfile import gettempdir

from time import mktime,strptime
from datetime import date,datetime,timedelta

def get_system_start_time():
    """
    Retrieve system start time
    
    :return: unixtime
    :rtype: int
    """
    
    today = date.today()
    askTime,uptime = (x.strip() for x in os.popen('uptime').read().split(',')[0].split('up'))
    tsUptime = strptime(uptime,'%H:%M')
    tsAskTime = strptime(askTime,'%H:%M:%S')
    now = datetime(today.year,today.month,today.day) + timedelta(hours=tsAskTime.tm_hour,minutes=tsAskTime.tm_min)
    startTime = now - timedelta(hours=tsUptime.tm_hour,minutes=tsUptime.tm_min,seconds=tsUptime.tm_sec)
    return int(mktime(startTime.utctimetuple()))

def process_exists(pid,partial_name=''):
    """
    Check if a process is running
    
    :param pid: process id
    :type pid:  int
    :param partial_name: process' name (or part of it)
    :type partial_name:  string
    
    :return: whether the process is running
    :rtype: bool
    """
    
    pid = str(pid).strip()
    if not pid: return false
    pid = int(pid)
    
    # it would be faster to check for /proc/{pid} but osx doesn't support it
    process_string = os.popen('ps -p %d -o cmd=' % pid).read().strip()
    return process_string and partial_name in process_string

def process_kill(pid):
    """
    End the process identified by pid.
    
    :param pid: process id
    :type pid:  int
    """
    try:
      os.kill(int(pid),9) # send SIGKILL to pid
    except OSError as e:
      if e.errno == 3: # No such process
        pass
      else: raise e

def already_running(codeName):
    """
    Discover if the program is already running as another process
    <commit_msg>(posix) process_exists() returned an empty string instead of a boolean false<commit_after>#!/usr/bin/env python

'''
Copyright 2011 <NAME> <<EMAIL>> [http://www.sideralis.org]

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.
'''

__all__ = ['get_system_start_time','process_exists','process_kill','already_running']

import os,sys,atexit
from fcntl import lockf,LOCK_EX,LOCK_NB,LOCK_UN
from tempfile import gettempdir

from time import mktime,strptime
from datetime import date,datetime,timedelta

def get_system_start_time():
    """
    Retrieve system start time
    
    :return: unixtime
    :rtype: int
    """
    
    today = date.today()
    askTime,uptime = (x.strip() for x in os.popen('uptime').read().split(',')[0].split('up'))
    tsUptime = strptime(uptime,'%H:%M')
    tsAskTime = strptime(askTime,'%H:%M:%S')
    now = datetime(today.year,today.month,today.day) + timedelta(hours=tsAskTime.tm_hour,minutes=tsAskTime.tm_min)
    startTime = now - timedelta(hours=tsUptime.tm_hour,minutes=tsUptime.tm_min,seconds=tsUptime.tm_sec)
    return int(mktime(startTime.utctimetuple()))

def process_exists(pid,partial_name=''):
    """
    Check if a process is running
    
    :param pid: process id
    :type pid:  int
    :param partial_name: process' name (or part of it)
    :type partial_name:  string
    
    :return: whether the process is running
    :rtype: bool
    """
    
    pid = str(pid).strip()
    if not pid: return false
    pid = int(pid)
    
    # it would be faster to check for /proc/{pid} but osx doesn't support it
    process_string = os.popen('ps -p %d -o cmd=' % pid).read().strip()
    return bool(process_string and partial_name in process_string)

def process_kill(pid):
    """
    End the process identified by pid.
    
    :param pid: process id
    :type pid:  int
    """
    try:
      os.kill(int(pid),9) # send SIGKILL to pid
    except OSError as e:
      if e.errno == 3: # No such process
        pass
      else: raise e

def already_running(codeName):
    """
    Discover if the program is already running as another process
    
{'fraction_non_alphanumeric': 0.10770144525836468, 'fraction_numerical': 0.004355573153830924, 'mean_word_length': 3.5802357207615594, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '306549', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1590, 'n_words': 512}