<commit_before>import os
import sys


def egg_preinstall(package_path, scripts):
    """Updates platform specific startup scripts.
    
    Run as part of the easy_install egg creation procedure. This is the only way 
    to get the scripts updated when the easy_install is used. Updates the 
    scripts before the egg is created and therefore the created egg cannot be
    used at any other machine unless the Python and Jython installations are 
    exactly equal.
    """
    robot_dir = _get_robot_target_dir(os.path.basename(package_path))
    if robot_dir is None: 
        print """Could not find site-packages from PYTHONPATH! 
Robot Framework start-up scripts needs to be updated manually."""
    else:
        _update_scripts(scripts, package_path, robot_dir)


def generic_install(script_names, script_dir, robot_dir):
    """Updates given startup scripts.
    
    Run as part of the generic installation procedure from 'setup.py' after 
    running 'python setyp.py install'.
    """
    _update_scripts(script_names, script_dir, robot_dir)


def windows_binary_install():
    """Updates start-up scripts.
    
    Executed as the last part of Windows binary installation started by 
    running 'robot-<version>.win32.exe'.
    """
    scripts = ['pybot.bat','jybot.bat', 'rebot.bat']
    script_dir = os.path.join(sys.prefix, 'Scripts')
    python_exe = os.path.join(sys.prefix, 'python.exe')  # sys.executable doesn't work here
    try:
        _update_scripts(scripts, script_dir, _get_robot_location(), python_exe)
        print '\nInstallation was successful. Happy Roboting!'
    except Exception, err:
        print '\nRunning post-install script failed: %s' % err
        print 'Robot Framework start-up scripts may not work correctly.'


def windows_binary_uninstall():
    """Deletes Jython compiled files ('*$py.class')
    
    This function is executed as part of the Windows binary uninstallation
    started from 'Add/Remove Programs'.
    
    Uninstaller deletes files only if installer has created them and also
    deletes directories only if they are empty. Thus compiled files created
    by Jython must be deleted separately.
    """
    for base, dirs, files in os.walk(_get_robot_location()):
        for name in files:
            if name.endswith('$py.class'):
                path = os.path.join(base, name)
                try:
                    os.remove(path)
                except Exception, err:
                    print "Failed to remove Jython compiled file '%s': %s" \
                            % (path, str(err))


def _get_robot_target_dir(version):
    site_packages = _get_site_packages()
    if site_packages is None:
        return None
    major, minor = sys.version_info[0:2]
    version = version.replace('-', '_').replace('_', '-', 1)
    egg_name = '%s-py%s.%s.egg' % (version, major, minor)
    return os.path.join(site_packages, egg_name, 'robot')
    
def _get_site_packages():
    # There might be multiple 'site-packages' in sys.path. In that case, 
    # the 'site-packages' under python installation directory is returned.
    potential_paths = [ path for path in sys.path if
                        os.path.basename(path) == 'site-packages']
    if len(potential_paths) == 1:
        return potential_paths[0]
    for path in potential_paths:
        if 'os.py' in os.listdir(os.path.dirname(path)):
            return path
    return None


def _get_robot_location():
    try:
        import robot
        return os.path.dirname(os.path.abspath(robot.__file__))
    except:
        return os.path.join(sys.prefix, 'Lib', 'site-packages', 'robot')

def _update_scripts(scripts, script_dir, robot_dir, python_exe=sys.executable):
    jython_exe, how_found = _find_jython()
    print 'Creating Robot Framework start-up scripts...'
    print 'Installation directory:', robot_dir
    print 'Python executable:', python_exe
    print 'Jython executable: %s (%s)' % (jython_exe, how_found)
    for script in scripts:
        path = os.path.join(script_dir, script)
        content = _read(path)
        for pattern, replace in [ ('[ROBOT_DIR]', robot_dir),
                                  ('[PYTHON_EXECUTABLE]', python_exe),
                                  ('[JYTHON_EXECUTABLE]', jython_exe) ]:
            content = content.replace(pattern, replace)
        _write(path, content)
        name = os.path.splitext(os.path.basename(script))[0].capitalize()
        print '%s script: %s' % (name, path)

    <commit_msg>Changed to find installation directory with get_python_lib found from distutils.<commit_after>import os
import sys
from distutils.sysconfig import get_python_lib


def egg_preinstall(package_path, scripts):
    """Updates platform specific startup scripts.
    
    Run as part of the easy_install egg creation procedure. This is the only way 
    to get the scripts updated when the easy_install is used. Updates the 
    scripts before the egg is created and therefore the created egg cannot be
    used at any other machine unless the Python and Jython installations are 
    exactly equal.
    """
    robot_dir = _get_robot_target_dir(os.path.basename(package_path))
    _update_scripts(scripts, package_path, robot_dir)


def generic_install(script_names, script_dir, robot_dir):
    """Updates given startup scripts.
    
    Run as part of the generic installation procedure from 'setup.py' after 
    running 'python setyp.py install'.
    """
    _update_scripts(script_names, script_dir, robot_dir)


def windows_binary_install():
    """Updates start-up scripts.
    
    Executed as the last part of Windows binary installation started by 
    running 'robotframework-<version>.win32.exe'.
    """
    scripts = ['pybot.bat','jybot.bat', 'rebot.bat']
    script_dir = os.path.join(sys.prefix, 'Scripts')
    python_exe = os.path.join(sys.prefix, 'python.exe')  # sys.executable doesn't work here
    try:
        _update_scripts(scripts, script_dir, _get_robot_location(), python_exe)
        print '\nInstallation was successful. Happy Roboting!'
    except Exception, err:
        print '\nRunning post-install script failed: %s' % err
        print 'Robot Framework start-up scripts may not work correctly.'


def windows_binary_uninstall():
    """Deletes Jython compiled files ('*$py.class')
    
    This function is executed as part of the Windows binary uninstallation
    started from 'Add/Remove Programs'.
    
    Uninstaller deletes files only if installer has created them and also
    deletes directories only if they are empty. Thus compiled files created
    by Jython must be deleted separately.
    """
    for base, dirs, files in os.walk(_get_robot_location()):
        for name in files:
            if name.endswith('$py.class'):
                path = os.path.join(base, name)
                try:
                    os.remove(path)
                except Exception, err:
                    print "Failed to remove Jython compiled file '%s': %s" \
                            % (path, str(err))


def _get_robot_target_dir(version):
    major, minor = sys.version_info[0:2]
    version = version.replace('-', '_').replace('_', '-', 1)
    egg_name = '%s-py%s.%s.egg' % (version, major, minor)
    return os.path.join(get_python_lib(), egg_name, 'robot')

def _get_robot_location():
    try:
        import robot
        return os.path.dirname(os.path.abspath(robot.__file__))
    except:
        return os.path.join(get_python_lib(), 'robot')

def _update_scripts(scripts, script_dir, robot_dir, python_exe=sys.executable):
    jython_exe, how_found = _find_jython()
    print 'Creating Robot Framework start-up scripts...'
    print 'Installation directory:', robot_dir
    print 'Python executable:', python_exe
    print 'Jython executable: %s (%s)' % (jython_exe, how_found)
    for script in scripts:
        path = os.path.join(script_dir, script)
        content = _read(path)
        for pattern, replace in [ ('[ROBOT_DIR]', robot_dir),
                                  ('[PYTHON_EXECUTABLE]', python_exe),
                                  ('[JYTHON_EXECUTABLE]', jython_exe) ]:
            content = content.replace(pattern, replace)
        _write(path, content)
        name = os.path.splitext(os.path.basename(script))[0].capitalize()
        print '%s script: %s' % (name, path)

    
{'fraction_non_alphanumeric': 0.09902772776377386, 'fraction_numerical': 0.0016804705317488896, 'mean_word_length': 2.8237723726480035, 'pattern_counts': {'":': 0, '<': 5, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6919176', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2417, 'n_words': 748}