1 # vim:ts=4:sw=4:et:ai:sts=4
3 import os, subprocess, os.path
5 __all__ = ["python", "ssh_path"]
6 __all__ += ["rsh", "tcpdump_path", "sshd_path"]
7 __all__ += ["execute", "backticks"]
9 def find_bin(name, extra_path = None):
11 if "PATH" in os.environ:
12 search += os.environ["PATH"].split(":")
13 for pref in ("/", "/usr/", "/usr/local/"):
14 for d in ("bin", "sbin"):
15 search.append(pref + d)
21 os.stat(d + "/" + name)
24 if e.errno != os.errno.ENOENT:
28 def find_bin_or_die(name, extra_path = None):
31 raise RuntimeError(("Cannot find `%s' command, impossible to " +
35 ssh_path = find_bin_or_die("ssh")
36 python_path = find_bin_or_die("python")
39 rsh_path = find_bin("rsh")
40 tcpdump_path = find_bin("tcpdump")
41 sshd_path = find_bin("sshd")
44 # FIXME: create a global debug variable
45 #print "[pid %d]" % os.getpid(), " ".join(cmd)
46 null = open("/dev/null", "r+")
47 p = subprocess.Popen(cmd, stdout = null, stderr = subprocess.PIPE)
48 out, err = p.communicate()
50 raise RuntimeError("Error executing `%s': %s" % (" ".join(cmd), err))
53 p = subprocess.Popen(cmd, stdout = subprocess.PIPE,
54 stderr = subprocess.PIPE)
55 out, err = p.communicate()
57 raise RuntimeError("Error executing `%s': %s" % (" ".join(cmd), err))
60 def homepath(path, app='.nepi', mode = 0500, directory = False):
61 home = os.environ.get('HOME')
63 home = os.path.join(os.sep, 'home', os.getlogin())
65 path = os.path.join(home, app, path)
69 dirname = os.path.dirname(path)
70 if not os.path.exists(dirname):
75 def find_testbed(testbed_id):
78 # look for environment-specified testbeds
79 if 'NEPI_TESTBEDS' in os.environ:
82 # split space-separated items, filter empty items
83 testbed_map = filter(bool,os.environ['NEPI_TESTBEDS'].strip().split(' '))
84 # split items, keep pairs only, build map
85 testbed_map = dict([map(str.strip,i.split(':',1)) for i in testbed_map if ':' in i])
88 traceback.print_exc(file=sys.stderr)
90 # ignore malformed environment
93 mod_name = testbed_map.get(testbed_id)
96 # no explicit map, load built-in testbeds
97 mod_name = "nepi.testbeds.%s" % (testbed_id.lower())