1from pyroute2 import NSPopen 2from distutils.spawn import find_executable 3 4def has_executable(name): 5 path = find_executable(name) 6 if path is None: 7 raise Exception(name + ": command not found") 8 return path 9 10class NSPopenWithCheck(NSPopen): 11 """ 12 A wrapper for NSPopen that additionally checks if the program 13 to be executed is available from the system path or not. 14 If found, it proceeds with the usual NSPopen() call. 15 Otherwise, it raises an exception. 16 """ 17 18 def __init__(self, nsname, *argv, **kwarg): 19 name = list(argv)[0][0] 20 has_executable(name) 21 super(NSPopenWithCheck, self).__init__(nsname, *argv, **kwarg) 22