2 import os, subprocess, os.path
4 __all__ = ["python", "ssh_path"]
5 __all__ += ["rsh", "tcpdump_path", "sshd_path"]
6 __all__ += ["execute", "backticks"]
8 def find_bin(name, extra_path = None):
10 if "PATH" in os.environ:
11 search += os.environ["PATH"].split(":")
12 for pref in ("/", "/usr/", "/usr/local/"):
13 for d in ("bin", "sbin"):
14 search.append(pref + d)
20 os.stat(d + "/" + name)
23 if e.errno != os.errno.ENOENT:
27 def find_bin_or_die(name, extra_path = None):
30 raise RuntimeError(("Cannot find `%s' command, impossible to " +
34 ssh_path = find_bin_or_die("ssh")
35 python_path = find_bin_or_die("python")
38 rsh_path = find_bin("rsh")
39 tcpdump_path = find_bin("tcpdump")
40 sshd_path = find_bin("sshd")
43 # FIXME: create a global debug variable
44 #print "[pid %d]" % os.getpid(), " ".join(cmd)
45 null = open("/dev/null", "r+")
46 p = subprocess.Popen(cmd, stdout = null, stderr = subprocess.PIPE)
47 out, err = p.communicate()
49 raise RuntimeError("Error executing `%s': %s" % (" ".join(cmd), err))
52 p = subprocess.Popen(cmd, stdout = subprocess.PIPE,
53 stderr = subprocess.PIPE)
54 out, err = p.communicate()
56 raise RuntimeError("Error executing `%s': %s" % (" ".join(cmd), err))
59 def homepath(path, app='.nepi', mode = 0500, directory = False):
60 home = os.environ.get('HOME')
62 home = os.path.join(os.sep, 'home', os.getlogin())
64 path = os.path.join(home, app, path)
68 dirname = os.path.dirname(path)
69 if not os.path.exists(dirname):
74 def find_testbed(testbed_id):
77 # look for environment-specified testbeds
78 if 'NEPI_TESTBEDS' in os.environ:
81 # split space-separated items, filter empty items
82 testbed_map = filter(bool,os.environ['NEPI_TESTBEDS'].strip().split(' '))
83 # split items, keep pairs only, build map
84 testbed_map = dict([map(str.strip,i.split(':',1)) for i in testbed_map if ':' in i])
87 traceback.print_exc(file=sys.stderr)
89 # ignore malformed environment
92 mod_name = testbed_map.get(testbed_id)
95 # no explicit map, load built-in testbeds
96 mod_name = "nepi.testbeds.%s" % (testbed_id.lower())