rmsfab / installers /

Full commit

from fabric.api import env, sudo, run
from fabric.operations import put, get
from fabric.context_managers import settings, hide
from fabric.utils import warn, abort
from os import system, remove
from tempfile import NamedTemporaryFile

def lsb_release():
    """Return the /etc/lsb-release file as a dict"""
    f = NamedTemporaryFile(delete=True)
    return dict([ kv.strip().split('=') for kv in file( ])

def append_file(file_name, lines, comment="", dup_check=True, lf="\n"):
    Append lines to a file. If dup_check, make sure those 
    lines aren't already in the file

    assert isinstance(lines, str) and isinstance(comment, str) and isinstance(lf, str)

    if dup_check and find_remote(file_name, lines) > -1:
        warn("'''%s''' already in %s, skipping" % (lines, file_name))
        f = NamedTemporaryFile(delete=False)
        f.write(lf + comment + lf + lines + lf)
        remote = + '-remote'
        p = put(, remote)
        if p.succeeded:
            sudo("cat %s" % remote)
            sudo("cat %s >>%s && echo rm %s" % (remote, file_name, remote))
            abort("%s: unable to append text '''%s'''" % (file_name, lines))

def find_remote(file_name, text):
    Find text in a remote file
    index = run('''python -c 'print open("%s").read().find("""%s""")' ''' % (file_name, text))
    if index.failed:
        return -1
    return int(index)

def file_exists(file_name):
    Check if a remote file exists
    with settings(
        hide('warnings', 'running', 'stdout', 'stderr'),
        return run('ls %s' % file_name).succeeded