<commit_before>#!/usr/bin/env python
#
# exec_command
#
# Implements exec_command function that is (almost) equivalent to
# commands.getstatusoutput function but on NT, DOS systems the
# returned status is actually correct (though, the returned status
# values may be different by a factor). In addition, exec_command
# takes keyword arguments for (re-)defining environment variables.
#
# Provides functions:
#   exec_command  --- execute command in a specified directory and
#                     in the modified environment.
#   splitcmdline  --- inverse of ' '.join(argv)
#   find_executable --- locate a command using info from environment
#                     variable PATH. Equivalent to posix `which`
#                     command.
#
# Author: <NAME> <<EMAIL>>
# Created: 11 January 2003
#
# Requires: Python 2.x
#
# Succesfully tested on:
#   os.name | sys.platform | comments
#   --------+--------------+----------
#   posix   | linux2       | Debian (sid) Linux, Python 2.1.3+, 2.2.3+, 2.3.3
#                            PyCrust 0.9.3, Idle 1.0.2
#   posix   | linux2       | Red Hat 9 Linux, Python 2.1.3, 2.2.2, 2.3.2
#   posix   | sunos5       | SunOS 5.9, Python 2.2, 2.3.2
#   posix   | darwin       | Darwin 7.2.0, Python 2.3
#   nt      | win32        | Windows Me
#                            Python 2.3(EE), Idle 1.0, PyCrust 0.7.2
#                            Python 2.1.1 Idle 0.8
#   nt      | win32        | Windows 98, Python 2.1.1. Idle 0.8
#   nt      | win32        | Cygwin 98-4.10, Python 2.1.1(MSC) - echo tests
#                            fail i.e. redefining environment variables may
#                            not work. FIXED: don't use cygwin echo!
#                            Comment: also `cmd /c echo` will not work
#                            but redefining environment variables do work.
#   posix   | cygwin       | Cygwin 98-4.10, Python 2.3.3(cygming special)
#   nt      | win32        | Windows XP, Python 2.3.3
#
# Known bugs:
# - Tests, that send messages to stderr, fail when executed from MSYS prompt
#   because the messages are lost at some point.

__all__ = ['exec_command','find_executable']

import os
import re
import sys
import tempfile

############################################################

from log import _global_log as log

############################################################

def get_pythonexe():
    pythonexe = sys.executable
    if os.name in ['nt','dos']:
        fdir,fn = os.path.split(pythonexe)
        fn = fn.upper().replace('PYTHONW','PYTHON')
        pythonexe = os.path.join(fdir,fn)
        assert os.path.isfile(pythonexe),`pythonexe`+' is not a file'
    return pythonexe

############################################################

def splitcmdline(line):
    """ Inverse of ' '.join(sys.argv).
    """
    log.debug('splitcmdline(%r)' % (line))
    lst = []
    flag = 0
    s,pc,cc = '','',''
    for nc in line+' ':
        if flag==0:
            flag = (pc != '\\' and \
                     ((cc=='"' and 1) or (cc=="'" and 2) or \
                       (cc==' ' and pc!=' ' and -2))) or flag
        elif flag==1:
            flag = (cc=='"' and pc!='\\' and nc==' ' and -1) or flag
        elif flag==2:
            flag = (cc=="'" and pc!='\\' and nc==' ' and -1) or flag
        if flag!=-2:
            s += cc
        if flag<0:
            flag = 0
            s = s.strip()
            if s:
                lst.append(s)
                s = ''
        pc,cc = cc,nc
    else:
        s = s.strip()
        if s:
            lst.append(s)
    log.debug('splitcmdline -> %r' % (lst))
    return lst

def test_splitcmdline():
    l = splitcmdline('a   b  cc')
    assert l==['a','b','cc'],`l`
    l = splitcmdline('a')
    assert l==['a'],`l`
    l = splitcmdline('a "  b  cc"')
    assert l==['a','"  b  cc"'],`l`
    l = splitcmdline('"a bcc"  -h')
    assert l==['"a bcc"','-h'],`l`
    l = splitcmdline(r'"\"a \" bcc" -h')
    assert l==[r'"\"a \" bcc"','-h'],`l`
    l = splitcmdline(" 'a bcc'  -h")
    assert l==["'a bcc'",'-h'],`l`
    l = splitcmdline(r"'\'a \' bcc' -h")
    assert l==[r"'\'a \' bcc'",'-h'],`l`

############################################################

def find_executable(exe, path=None):
    """ Return full path of a executable.
    """
    log.debug('find_executable(%r)' % exe)
    if path is None:
        path = os.environ.get('PATH',os.defpath)
    if os.name=='posix':
        realpath = os.path.realpath
    else:
        realpath = lambda a:a
    if exe[0]=='"':
        exe = exe[1:-1]
    suffices = ['']
    if os.name in ['nt','dos','os2']:
        fn,ext = os.path.splitext(exe)
        extra_suffices = ['.exe','.com','.bat']
        if ext.lower() not in extra_suffices:
            suffices = extra_suffices
    if os.path.isabs(exe):
        paths = ['']
    else:
        paths = map(os.path.abspath, path.split(os.pathsep))
        if 0 and os.name == 'nt':
            new_paths = []
            cygwin_paths = []
            for path in paths:
                d,p = os.path.splitdrive(path)
                if p.lower().find('cygwin') >= 0:
                    cygwin_paths.append(path)
                else:
                    new_paths.append(path)
            paths = new_paths + cygwin_paths
    for path in paths:
        fn = os.path.join(path,exe)
        for s in suffices:
            f_ext = realpath(fn+s)
            if os.path.isfile(f_ext) and os.access(f_ext,os.X_OK):
                log.debug('Found executable %s' % f_ext)
                return f_ext
    exe = realpath(exe)
    if not os.path.isfile(exe) or os.access(exe,os.X_OK):
        log.warn('Could not locate executable %s' % exe)
    return exe

############################################################

def _preserve_environment( names ):
    log.debug('_preserve_environment(%r)' % (names))
    env = {}
    for name in names:
        env[name] = os.environ.get(name)
    return env

def _update_environment( **env ):
    log.debug('_update_environment(...)')
    for name,value in env.items():
        os.environ[name] = value or ''

def exec_command( command,
                  execute_in='', use_shell=None, use_tee = None,
                  _with_python = 1,<commit_msg>Return original exe name when not found.<commit_after>#!/usr/bin/env python
#
# exec_command
#
# Implements exec_command function that is (almost) equivalent to
# commands.getstatusoutput function but on NT, DOS systems the
# returned status is actually correct (though, the returned status
# values may be different by a factor). In addition, exec_command
# takes keyword arguments for (re-)defining environment variables.
#
# Provides functions:
#   exec_command  --- execute command in a specified directory and
#                     in the modified environment.
#   splitcmdline  --- inverse of ' '.join(argv)
#   find_executable --- locate a command using info from environment
#                     variable PATH. Equivalent to posix `which`
#                     command.
#
# Author: <NAME> <<EMAIL>>
# Created: 11 January 2003
#
# Requires: Python 2.x
#
# Succesfully tested on:
#   os.name | sys.platform | comments
#   --------+--------------+----------
#   posix   | linux2       | Debian (sid) Linux, Python 2.1.3+, 2.2.3+, 2.3.3
#                            PyCrust 0.9.3, Idle 1.0.2
#   posix   | linux2       | Red Hat 9 Linux, Python 2.1.3, 2.2.2, 2.3.2
#   posix   | sunos5       | SunOS 5.9, Python 2.2, 2.3.2
#   posix   | darwin       | Darwin 7.2.0, Python 2.3
#   nt      | win32        | Windows Me
#                            Python 2.3(EE), Idle 1.0, PyCrust 0.7.2
#                            Python 2.1.1 Idle 0.8
#   nt      | win32        | Windows 98, Python 2.1.1. Idle 0.8
#   nt      | win32        | Cygwin 98-4.10, Python 2.1.1(MSC) - echo tests
#                            fail i.e. redefining environment variables may
#                            not work. FIXED: don't use cygwin echo!
#                            Comment: also `cmd /c echo` will not work
#                            but redefining environment variables do work.
#   posix   | cygwin       | Cygwin 98-4.10, Python 2.3.3(cygming special)
#   nt      | win32        | Windows XP, Python 2.3.3
#
# Known bugs:
# - Tests, that send messages to stderr, fail when executed from MSYS prompt
#   because the messages are lost at some point.

__all__ = ['exec_command','find_executable']

import os
import re
import sys
import tempfile

############################################################

from log import _global_log as log

############################################################

def get_pythonexe():
    pythonexe = sys.executable
    if os.name in ['nt','dos']:
        fdir,fn = os.path.split(pythonexe)
        fn = fn.upper().replace('PYTHONW','PYTHON')
        pythonexe = os.path.join(fdir,fn)
        assert os.path.isfile(pythonexe),`pythonexe`+' is not a file'
    return pythonexe

############################################################

def splitcmdline(line):
    """ Inverse of ' '.join(sys.argv).
    """
    log.debug('splitcmdline(%r)' % (line))
    lst = []
    flag = 0
    s,pc,cc = '','',''
    for nc in line+' ':
        if flag==0:
            flag = (pc != '\\' and \
                     ((cc=='"' and 1) or (cc=="'" and 2) or \
                       (cc==' ' and pc!=' ' and -2))) or flag
        elif flag==1:
            flag = (cc=='"' and pc!='\\' and nc==' ' and -1) or flag
        elif flag==2:
            flag = (cc=="'" and pc!='\\' and nc==' ' and -1) or flag
        if flag!=-2:
            s += cc
        if flag<0:
            flag = 0
            s = s.strip()
            if s:
                lst.append(s)
                s = ''
        pc,cc = cc,nc
    else:
        s = s.strip()
        if s:
            lst.append(s)
    log.debug('splitcmdline -> %r' % (lst))
    return lst

def test_splitcmdline():
    l = splitcmdline('a   b  cc')
    assert l==['a','b','cc'],`l`
    l = splitcmdline('a')
    assert l==['a'],`l`
    l = splitcmdline('a "  b  cc"')
    assert l==['a','"  b  cc"'],`l`
    l = splitcmdline('"a bcc"  -h')
    assert l==['"a bcc"','-h'],`l`
    l = splitcmdline(r'"\"a \" bcc" -h')
    assert l==[r'"\"a \" bcc"','-h'],`l`
    l = splitcmdline(" 'a bcc'  -h")
    assert l==["'a bcc'",'-h'],`l`
    l = splitcmdline(r"'\'a \' bcc' -h")
    assert l==[r"'\'a \' bcc'",'-h'],`l`

############################################################

def find_executable(exe, path=None):
    """ Return full path of a executable.
    """
    log.debug('find_executable(%r)' % exe)
    orig_exe = exe
    if path is None:
        path = os.environ.get('PATH',os.defpath)
    if os.name=='posix':
        realpath = os.path.realpath
    else:
        realpath = lambda a:a
    if exe[0]=='"':
        exe = exe[1:-1]
    suffices = ['']
    if os.name in ['nt','dos','os2']:
        fn,ext = os.path.splitext(exe)
        extra_suffices = ['.exe','.com','.bat']
        if ext.lower() not in extra_suffices:
            suffices = extra_suffices
    if os.path.isabs(exe):
        paths = ['']
    else:
        paths = map(os.path.abspath, path.split(os.pathsep))
        if 0 and os.name == 'nt':
            new_paths = []
            cygwin_paths = []
            for path in paths:
                d,p = os.path.splitdrive(path)
                if p.lower().find('cygwin') >= 0:
                    cygwin_paths.append(path)
                else:
                    new_paths.append(path)
            paths = new_paths + cygwin_paths
    for path in paths:
        fn = os.path.join(path,exe)
        for s in suffices:
            f_ext = realpath(fn+s)
            if os.path.isfile(f_ext) and os.access(f_ext,os.X_OK):
                log.debug('Found executable %s' % f_ext)
                return f_ext
    exe = realpath(exe)
    if not os.path.isfile(exe) or os.access(exe,os.X_OK):
        log.warn('Could not locate executable %s' % orig_exe)
        return orig_exe
    return exe

############################################################

def _preserve_environment( names ):
    log.debug('_preserve_environment(%r)' % (names))
    env = {}
    for name in names:
        env[name] = os.environ.get(name)
    return env

def _update_environment( **env ):
    log.debug('_update_environment(...)')
    for name,value in env.items():
        os.environ[name] = value or ''

def exec_command( command,
                  execute_in='', use_shell=None, use_tee = None,
                  _with_python = 1,
{'fraction_non_alphanumeric': 0.19251933966026, 'fraction_numerical': 0.017864263497886595, 'mean_word_length': 1.94021101992966, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '5606174', 'n_tokens_mistral': 4312, 'n_tokens_neox': 4084, 'n_words': 1089}