<commit_before>import sys
import os.path
from .fileutils import writefile, find_executable, resolve_partition, makedirs
import logging
import stat

logger = logging.getLogger('sparts.runit')

def install(service_name):
    preferred = '/etc/service'
    dirs = get_runsvdir_dirs()
    assert len(dirs) > 0, "runsvdir is not running!"

    if preferred not in dirs:
        preferred = dirs[0]

    service_path = os.path.join(preferred, service_name)
    logger.info('Installing %s in %s', service_name, service_path)
    make_runit_dir(service_name, service_path)


def is_runit_installed():
    return bool(find_executable('runsv'))


def get_runsvdir_dirs():
    import psutil
    dirs = []
    for proc in psutil.process_iter():
        if proc.name == 'runsvdir':
            d = get_runsvdir_dir_from_cmdline(proc.cmdline)
            if d is not None:
                dirs.append(d)
    return dirs

def get_runsvdir_dir_from_cmdline(cmdline):
    # TODO - unittest this
    for i, arg in enumerate(cmdline):
        if arg == '-P' and len(cmdline) > i + 1:
            return cmdline[i + 1]
    return None

def on_same_filesystem(path1, path2):
    return resolve_partition(path1).mountpoint == \
            resolve_partition(path2).mountpoint

def get_default_args():
    args = sys.argv[:]
    assert len(args) > 0, "Something went horribly wrong"
    assert sys.executable is not None, "Something went horribly wrong"
    args.insert(0, sys.executable)

    # runsv will only redirect standard output to logs.
    # sparts logs stderr only by default.  Let's grab everything
    args.append('2>&1')
    return args

def make_runit_dir(service_name, path, args=None, make_logdir=True):
    # TODO - unittest this
    if args is None:
        args = get_default_args()

    makedirs(path)
<commit_msg>Update runit.py to handle flags ordering better.<commit_after>import sys
import os.path
from .fileutils import writefile, find_executable, resolve_partition, makedirs
import logging
import stat

logger = logging.getLogger('sparts.runit')

def install(service_name):
    preferred = '/etc/service'
    dirs = get_runsvdir_dirs()
    assert len(dirs) > 0, "runsvdir is not running!"

    if preferred not in dirs:
        preferred = dirs[0]

    service_path = os.path.join(preferred, service_name)
    logger.info('Installing %s in %s', service_name, service_path)
    make_runit_dir(service_name, service_path)


def is_runit_installed():
    return bool(find_executable('runsv'))


def get_runsvdir_dirs():
    import psutil
    dirs = []
    for proc in psutil.process_iter():
        if proc.name == 'runsvdir':
            d = get_runsvdir_dir_from_cmdline(proc.cmdline)
            if d is not None:
                dirs.append(d)
    return dirs

def get_runsvdir_dir_from_cmdline(cmdline):
    # TODO - unittest this
    for i, arg in enumerate(cmdline):
        # Skip the process name
        if i == 0:
            continue

        # Skip the -P, -H flags
        if arg in ['-P', '-H']:
            continue

        return arg
    return None

def on_same_filesystem(path1, path2):
    return resolve_partition(path1).mountpoint == \
            resolve_partition(path2).mountpoint

def get_default_args():
    args = sys.argv[:]
    assert len(args) > 0, "Something went horribly wrong"
    assert sys.executable is not None, "Something went horribly wrong"
    args.insert(0, sys.executable)

    # runsv will only redirect standard output to logs.
    # sparts logs stderr only by default.  Let's grab everything
    args.append('2>&1')
    return args

def make_runit_dir(service_name, path, args=None, make_logdir=True):
    # TODO - unittest this
    if args is None:
        args = get_default_args()

    makedirs(path)

{'fraction_non_alphanumeric': 0.09456579648375067, 'fraction_numerical': 0.006126798082045818, 'mean_word_length': 3.0550755939524836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1626232', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1210, 'n_words': 335}