def find_command(name, prefer_dir=None): """Finds a linaro-image-tools command. Prefers specified directory, otherwise searches only the current directory when running from a checkout, or only PATH when running from an installed version. """ assert name != "" assert os.path.dirname(name) == "" cmd_runner.sanitize_path(os.environ) # default to searching in current directory when running from a bzr # checkout dirs = [os.getcwd(), ] if os.path.isabs(__file__): dirs = os.environ["PATH"].split(os.pathsep) # empty dir in PATH means current directory dirs = map(lambda x: x == '' and '.' or x, dirs) if prefer_dir is not None: dirs.insert(0, prefer_dir) for dir in dirs: path = os.path.join(dir, name) if os.path.exists(path) and os.access(path, os.X_OK): return path return None
def find_command(name, prefer_dir=None): """Finds a linaro-image-tools command. Prefers specified directory, otherwise searches only the current directory when running from a checkout, or only PATH when running from an installed version. """ assert name != "" assert os.path.dirname(name) == "" cmd_runner.sanitize_path(os.environ) # default to searching in current directory when running from a bzr # checkout dirs = [ os.getcwd(), ] if os.path.isabs(__file__): dirs = os.environ["PATH"].split(os.pathsep) # empty dir in PATH means current directory dirs = map(lambda x: x == '' and '.' or x, dirs) if prefer_dir is not None: dirs.insert(0, prefer_dir) for dir in dirs: path = os.path.join(dir, name) if os.path.exists(path) and os.access(path, os.X_OK): return path return None
def test_idempotent(self): self.env['PATH'] = cmd_runner.DEFAULT_PATH cmd_runner.sanitize_path(self.env) self.assertEqual(cmd_runner.DEFAULT_PATH, self.env['PATH'])
def test_path_missing_dirs(self): path = '/bin:/sbin:/foo:/usr/local/sbin' self.env['PATH'] = path cmd_runner.sanitize_path(self.env) expected = '%s:/usr/local/bin:/usr/sbin:/usr/bin' % path self.assertEqual(expected, self.env['PATH'])
def test_path_unset(self): cmd_runner.sanitize_path(self.env) self.assertEqual(cmd_runner.DEFAULT_PATH, self.env['PATH'])