def test_get_put(): with warnings.catch_warnings(): fname = os.tmpnam() t1 = 'some data' t2 = 'other text' core.put_rf(fname, t1) try: ok(core.get_rf(fname)) == t1 sudo('chown root.root ' + fname) sudo('chmod o-w ' + fname) ok(lambda : core.put_rf(fname, t2)).raises(SystemExit) core.put_rf(fname, t2, use_sudo=True) ok(core.get_rf(fname)) == t2 with core.remote_fl(fname, use_sudo=True) as fc: ok(fc.getvalue()) == t2 fc.setvalue(t1) ok(core.get_rf(fname)) == t1 finally: sudo('rm ' + fname)
def update_hosts(hostname2ip, update_hostname=True): ip2hostname = dict((v,k) for k,v in hostname2ip.items()) all_host_names = set(hostname2ip.keys()) curr_hostname = ip2hostname.get(env.host_string.split('@')[1], None) update_hostname = update_hostname and curr_hostname is not None if update_hostname: set_host_name(curr_hostname) res = [] not_founded_hosts = set(all_host_names) with remote_fl('/etc/hosts', use_sudo=True) as hosts: for is_ip, obj in parse_hosts(hosts.getvalue()): if not is_ip: res.append(obj) else: ip, all_hosts = obj if ip in ip2hostname: all_hosts = [ip2hostname[ip]] if ip2hostname[ip] in not_founded_hosts: not_founded_hosts.remove(ip2hostname[ip]) elif ip == '127.0.1.1' and curr_hostname is not None: all_hosts = [curr_hostname] if curr_hostname in not_founded_hosts: not_founded_hosts.remove(curr_hostname) else: all_hosts = list(set(all_hosts).difference(all_host_names)) res.append(" ".join([ip] + all_hosts)) for host in not_founded_hosts: res.append("{0} {1}".format(hostname2ip[host], host)) hosts.setvalue("\n".join(res)) if update_hostname: sudo("/etc/init.d/hostname restart") sudo("/etc/init.d/networking restart")