Source

BlingNode / customized_linux_tools.py

Full commit
try:
	import common
except:	
	import initial_preperation
	initial_preperation.install_requirements()
	import common

from optparse import OptionParser, make_option
from datetime import datetime
import os

sudo = None

def sudo_run(command):
    if sudo:
        sudo.run(command)
    else:
        common.command_line_call(command)

def customize_vim():
    #install/update vim
    sudo_run('apt-get install vim')
    
    #backup system vimrc and vimfiles
    backup_dir = "'/var/lib/misc/blingnode/lib/customizations/setting-backup/vim %s'" % datetime.today()
    vimrc = '/etc/vim/vimrc'
    vimfiles = '/etc/vim/vimfiles'
    
    sudo_run("mkdir -pv %s" % backup_dir)
    
    if os.path.exists(vimrc):
        common.command_line_call('mv -v %s %s' % (vimrc, backup_dir, ))
    
    if os.path.exists(vimfiles):
        common.command_line_call('mv -v %s %s' % (vimfiles, backup_dir, ))
    
    #copy over customized vimrc and vimfiles
    sudo_run('cp /var/lib/misc/blingnode/lib/customizations/vim/vimrc /etc/vim/')
    sudo_run('cp -r /var/lib/misc/blingnode/lib/customizations/vim/vimfiles /etc/vim/')
    print 'Copied vim customizations over to /etc/vim'
    
    print '\nSuccessfully customized vim\n'

def customize_bash_env():
    backup_dir = "'/var/lib/misc/blingnode/lib/customizations/setting-backup/bash_env %s'" % datetime.today()
    home_folder = os.getenv('HOME')
    user = os.getlogin()
    
    sudo_run('mkdir -p %s' % backup_dir)
    
    print 'backup directory is %s\n' % backup_dir
    
    for filename in ('.bashrc', '.bash_logout', '.profile'):
        filepath = os.path.join(home_folder, filename)
        
        if os.path.exists(filepath):
            common.command_line_call('cp %s %s' % (filepath, backup_dir))
            print 'backed up %s' % filename
        
        common.command_line_call('cp /var/lib/misc/blingnode/lib/customizations/bash_env/%s %s' % (filename, filepath))
        sudo_run('chown %s:%s %s' % (user, user, filepath))
        common.command_line_call('chmod 644 %s' % filepath)
        
        print '\ncustomized %s\n' % filename
    
    print '\nSuccessfully customized bash env'

def main(options, args):
    if options.password:
        sudo = common.Sudo(password = options.password)
    else:
        sudo = None
    
    if not sudo and not common.is_user_root():
        common.error_exit(msg="\nERROR: Run this script as root\n")
    
    sudo_run('apt-get update')
    
    if common.ask_yes_or_no("\n\nWould you like to customize vim? (y/n) ") == "y":
        print '\nYou decided to CUSTOMIZE vim\n'
        customize_vim()
    else:
        print '\nYou decided NOT to customize vim'
        
    if common.ask_yes_or_no("\n\nWould you like to customize the bash env for the current user? (y/n) ") == "y":
        print 'You decided to CUSTOMIZE the bash env'
        customize_bash_env()
    else:
        print 'You decided NOT to customize the bash env'

if __name__ == "__main__":
    options = ( 
        make_option('-p', '--password', dest='password', action='store',
            help="User password to send to sudo. Note that this will"
            "appear in the operating system's process list."),
    )

    parser = OptionParser(option_list=options)
    (options, args) = parser.parse_args()
    main(options, args)