<commit_before>#!/usr/bin/python
# Author: <NAME>, MIPT 2014

import sys, os, subprocess

ROS_INSTALL_DIR = "/opt/ros/indigo"
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
LOCAL_BASH_FILE = ROOT_DIR + "/devel/setup.bash"
LAUNCHER_DIR = ROOT_DIR + "/contrib/launchers_gen/"
HOME_DIR = os.path.expanduser("~")

print "\nINSTALL SCRIPT FOR MIPT-AIRDRONE PROJECT"
print "author: <NAME>, 2014"
print "Warning! This script assumes you had installed ROS Indigo to the", ROS_INSTALL_DIR
print ""
print "Workspace root directory:", ROOT_DIR
print "Launcher bash scripts directory:", LAUNCHER_DIR
print ""

'''
Deps:
#sudo adduser second_user dialout
'''

def ensure_dir(f):
    if not os.path.exists(f):
        print "Created directory", f
        os.makedirs(f)

def add_to_file(f, contents, string):
    if string not in contents:
        f.write(string + ' # Generated by AIRDRONE-INSTALL-PY\n')
        print "ADDED: " + string
    else:
        print "OK: " + string


def exec_command(cmd_):
    command = ['bash', '-c', cmd_]
    proc = subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    for line in proc.stdout:
      (key, _, value) = line.partition("=")
      os.environ[key] = value
    (stdout, stderr) = proc.communicate()
    if len(stderr) == 0:
        return 0
    print stderr
    return 1

def cleanup():
    output = []
    cnt = 0
    with open(HOME_DIR + "/.bashrc", 'r') as fbashrc:
        for line in fbashrc:
            cnt = cnt + 1
            if not 'Generated by AIRDRONE-INSTALL-PY' in line:
                cnt = cnt - 1
                output.append(line)

    with open(HOME_DIR + "/.bashrc", 'w') as fbashrc:
        fbashrc.writelines(output)
    print "Removed", cnt, "lines"

    for name in os.listdir(HOME_DIR + '/.local/share/applications/'):
        if '_ad_gen.desktop' in name:
            print "removing " + name
            os.remove(HOME_DIR + '/.local/share/applications/' + name)

def init_workspace():
    try:
        os.remove(ROOT_DIR + "/src/CMakeLists.txt")
    except:
        pass


    print "\nInitializing ROS workspace at " + ROOT_DIR + "/src";
    if not exec_command("source " + ROS_INSTALL_DIR + "/setup.bash\n"\
                           "cd " + ROOT_DIR + "/src\n"\
                           "catkin_init_workspace") == 0:
        print "Unable to execute catkin_init_workspace. Have you installed ROS?!"
        exit(1)



def add_shortcut(name, icon, command):
    # The generation is done analogicaly to alacarte utilite
    if not os.path.isfile(icon):
        print "No such icon: " + icon
        exit(1)

    file_path = HOME_DIR + '/.local/share/applications/' + name + '_ad_gen.desktop'
    format_str = '#!/usr/bin/env xdg-open\n\n'\
                 '[Desktop Entry]\n'\
                 'Version=1.0\n'\
                 'Type=Application\n'\
                 'Terminal=true\n'\
                 'Name=' + name + '\n'\
                 'Icon=' + icon + '\n'\
                 'Exec=' + command
    try:
        desktop_file = open(file_path, 'w+')
        desktop_file.write(format_str)
        os.chmod(file_path, 0775)
        print "Written to", file_path
    except:
        print "Error creating ", file_path
        print sys.exc_info()
        exit(1)



def gen_launcher(bash_name, launcher_name, icon_name, command):
    format_str = '#!/bin/bash\n'\
    'source ' + ROS_INSTALL_DIR + '/setup.bash\n'\
    'source ' + LOCAL_BASH_FILE + '\n'\
    'export CMAKE_PREFIX_PATH=/usr/local:$CMAKE_PREFIX_PATH\n\n'\
    '' + command + '\n\n'\
    'sleep 0.2'

    file_path = LAUNCHER_DIR + bash_name
    try:
        bash_scipt_file = open(file_path, 'w+')
        bash_scipt_file.write(format_str)
        os.chmod(file_path, 0744)
        print "Written to", file_path
    except:
        print "Error creating ", file_path
        print "Check the permissions"
        exit(1)

    add_shortcut(launcher_name, ROOT_DIR + '/contrib/icons/' + icon_name, file_path)


cleanup() #Remove obsolete config paths
init_workspace() #Initialize catkin workspace
with open(HOME_DIR + "/.bashrc", 'r+') as fbashrc:
    contents = fbashrc.read()
    add_to_file(fbashrc, contents, "source " + ROS_INSTALL_DIR + "/setup.bash")
    add_to_file(fbashrc, contents, "source " + LOCAL_BASH_FILE)
    add_to_file(fbashrc, contents, "export PATH=" + LAUNCHER_DIR + ":$PATH")
    add_to_file(fbashrc, contents, "export CMAKE_PREFIX_PATH=/usr/local:$CMAKE_PREFIX_PATH")



print ""
ensure_dir(LAUNCHER_DIR)

gen_launcher('ad_rebuild',                   'AdRebuild',         'AdRebuild.png', 'cd ' + ROOT_DIR + '\n\n'\
                                                                                   'catkin_make\n\n'\
                                                                                   'source ' + LOCAL_BASH_FILE + '\n'\
                                                                                   'echo "Press any key to continue..."\nread')
gen_launcher('ad_rebuild_eclipse',           'AdRebuild4Eclipse', 'AdRebuild4Eclipse.png', 'cd ' + ROOT_DIR + '\n'\
                                                                                           'catkin_make --force-cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug'\
                                                                                           ' -DCMAKE_ECLIPSE_MAKE_ARGUMENTS=-j8\n'\
                                                                                           'source ' + LOCAL_BASH_FILE + '\n'\
                                                                                           'echo "Press any key to continue..."\nread')
gen_launcher('ad_simulator',                 'AdSimulator',       'AdAirdroneTest.png',          'roslaunch airdrone_launch simulator.launch')
gen_launcher('ad_airdrone_simulator_launch', 'AdRunInSimulator',  'AdAirdroneLaunch.png',        'roslaunch airdrone_launch airdrone_simulator.launch')
gen_launcher('ad_airdrone_real_launch',      'AdRun4Real',        'AdAirdroneRealLaunch.png',    'roslaunch airdrone_launch airdrone.launch')
gen_launcher('ad_workstation',               'AdWorkstation',     'AdWorkstation.png',           'roslaunch airdrone_launch workstation.launch')
gen_launcher('ad_optical_flow',              'AdOpticalFlow',     'AdSpare.png',                 'roslaunch optical_flow test_cpu_farn.launch')
gen_launcher('ad_keyboard_control',          'AdKeyboardControl', 'AdKeyboardControlLaunch.png', 'roslaunch keyboard_control keyboard_control.launch')


<commit_msg>fixed INSTALL.py to support ros networking<commit_after>#!/usr/bin/python
# Author: <NAME>, MIPT 2014

import sys, os, subprocess

ROS_INSTALL_DIR = "/opt/ros/indigo"
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
LOCAL_BASH_FILE = ROOT_DIR + "/devel/setup.bash"
LAUNCHER_DIR = ROOT_DIR + "/contrib/launchers_gen/"
HOME_DIR = os.path.expanduser("~")
ROS_HOSTNAME=os.environ["ROS_HOSTNAME"]
ROS_MASTER_URI=os.environ["ROS_MASTER_URI"]


print "\nINSTALL SCRIPT FOR MIPT-AIRDRONE PROJECT"
print "author: <NAME>, 2014"
print "Warning! This script assumes you had installed ROS Indigo to the", ROS_INSTALL_DIR
print ""
print "Workspace root directory:", ROOT_DIR
print "Launcher bash scripts directory:", LAUNCHER_DIR
print ""
print "Network setup:"
print "ROS_HOSTNAME=", ROS_HOSTNAME
print "ROS_MASTER_URI=", ROS_MASTER_URI
print ""


'''
Deps:
#sudo adduser second_user dialout
'''

def ensure_dir(f):
    if not os.path.exists(f):
        print "Created directory", f
        os.makedirs(f)

def add_to_file(f, contents, string):
    if string not in contents:
        f.write(string + ' # Generated by AIRDRONE-INSTALL-PY\n')
        print "ADDED: " + string
    else:
        print "OK: " + string


def exec_command(cmd_):
    command = ['bash', '-c', cmd_]
    proc = subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    for line in proc.stdout:
      (key, _, value) = line.partition("=")
      os.environ[key] = value
    (stdout, stderr) = proc.communicate()
    if len(stderr) == 0:
        return 0
    print stderr
    return 1

def cleanup():
    output = []
    cnt = 0
    with open(HOME_DIR + "/.bashrc", 'r') as fbashrc:
        for line in fbashrc:
            cnt = cnt + 1
            if not 'Generated by AIRDRONE-INSTALL-PY' in line:
                cnt = cnt - 1
                output.append(line)

    with open(HOME_DIR + "/.bashrc", 'w') as fbashrc:
        fbashrc.writelines(output)
    print "Removed", cnt, "lines"

    for name in os.listdir(HOME_DIR + '/.local/share/applications/'):
        if '_ad_gen.desktop' in name:
            print "removing " + name
            os.remove(HOME_DIR + '/.local/share/applications/' + name)

def init_workspace():
    try:
        os.remove(ROOT_DIR + "/src/CMakeLists.txt")
    except:
        pass


    print "\nInitializing ROS workspace at " + ROOT_DIR + "/src";
    if not exec_command("source " + ROS_INSTALL_DIR + "/setup.bash\n"\
                           "cd " + ROOT_DIR + "/src\n"\
                           "catkin_init_workspace") == 0:
        print "Unable to execute catkin_init_workspace. Have you installed ROS?!"
        exit(1)



def add_shortcut(name, icon, command):
    # The generation is done analogicaly to alacarte utilite
    if not os.path.isfile(icon):
        print "No such icon: " + icon
        exit(1)

    file_path = HOME_DIR + '/.local/share/applications/' + name + '_ad_gen.desktop'
    format_str = '#!/usr/bin/env xdg-open\n\n'\
                 '[Desktop Entry]\n'\
                 'Version=1.0\n'\
                 'Type=Application\n'\
                 'Terminal=true\n'\
                 'Name=' + name + '\n'\
                 'Icon=' + icon + '\n'\
                 'Exec=' + command
    try:
        desktop_file = open(file_path, 'w+')
        desktop_file.write(format_str)
        os.chmod(file_path, 0775)
        print "Written to", file_path
    except:
        print "Error creating ", file_path
        print sys.exc_info()
        exit(1)



def gen_launcher(bash_name, launcher_name, icon_name, command):
    format_str = '#!/bin/bash\n'\
    'source ' + ROS_INSTALL_DIR + '/setup.bash\n'\
    'source ' + LOCAL_BASH_FILE + '\n'\
    'export CMAKE_PREFIX_PATH=/usr/local:$CMAKE_PREFIX_PATH\n'\
    'export ROS_HOSTNAME='   + str(ROS_HOSTNAME) + '\n'\
    'export ROS_MASTER_URI=' + str(ROS_MASTER_URI) + '\n\n'\
    '' + command + '\n\n'\
    'sleep 0.2'

    file_path = LAUNCHER_DIR + bash_name
    try:
        bash_scipt_file = open(file_path, 'w+')
        bash_scipt_file.write(format_str)
        os.chmod(file_path, 0744)
        print "Written to", file_path
    except:
        print "Error creating ", file_path
        print "Check the permissions"
        exit(1)

    add_shortcut(launcher_name, ROOT_DIR + '/contrib/icons/' + icon_name, file_path)


cleanup() #Remove obsolete config paths
init_workspace() #Initialize catkin workspace
with open(HOME_DIR + "/.bashrc", 'r+') as fbashrc:
    contents = fbashrc.read()
    add_to_file(fbashrc, contents, "source " + ROS_INSTALL_DIR + "/setup.bash")
    add_to_file(fbashrc, contents, "source " + LOCAL_BASH_FILE)
    add_to_file(fbashrc, contents, "export PATH=" + LAUNCHER_DIR + ":$PATH")
    add_to_file(fbashrc, contents, "export CMAKE_PREFIX_PATH=/usr/local:$CMAKE_PREFIX_PATH")



print ""
ensure_dir(LAUNCHER_DIR)

gen_launcher('ad_rebuild',                   'AdRebuild',         'AdRebuild.png', 'cd ' + ROOT_DIR + '\n\n'\
                                                                                   'catkin_make\n\n'\
                                                                                   'source ' + LOCAL_BASH_FILE + '\n'\
                                                                                   'echo "Press any key to continue..."\nread')
gen_launcher('ad_rebuild_eclipse',           'AdRebuild4Eclipse', 'AdRebuild4Eclipse.png', 'cd ' + ROOT_DIR + '\n'\
                                                                                           'catkin_make --force-cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug'\
                                                                                           ' -DCMAKE_ECLIPSE_MAKE_ARGUMENTS=-j8\n'\
                                                                                           'source ' + LOCAL_BASH_FILE + '\n'\
                                                                                           'echo "Press any key to continue..."\nread')
gen_launcher('ad_simulator',                 'AdSimulator',       'AdAirdroneTest.png',          'roslaunch airdrone_launch simulator.launch')
gen_launcher('ad_airdrone_simulator_launch', 'AdRunInSimulator',  'AdAirdroneLaunch.png',        'roslaunch airdrone_launch airdrone_simulator.launch')
gen_launcher('ad_airdrone_real_launch',      'AdRun4Real',        'AdAirdroneRealLaunch.png',    'roslaunch airdrone_launch airdrone.launch')
gen_launcher('ad_optical_flow',              'AdOpticalFlow',     'AdSpare.png',                 'roslaunch optical_flow test_cpu_farn.launch')
gen_launcher('ad_keyboard_control',          'AdKeyboardControl', 'AdKeyboardControlLaunch.png', 'roslaunch keyboard_control keyboard_control.launch')
gen_launcher('ad_workstation',               'AdWorkstation',     'AdWorkstation.png',           'roslaunch airdrone_launch workstation.launch')

{'fraction_non_alphanumeric': 0.11993757895519061, 'fraction_numerical': 0.0053503752693765325, 'mean_word_length': 2.0406687754179846, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5678321', 'n_tokens_mistral': 4427, 'n_tokens_neox': 4121, 'n_words': 897}