Skip to content

Instantly share code, notes, and snippets.

@adamheins
Created February 24, 2017 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamheins/ea98f0f0e058af8a93134979ac216a3e to your computer and use it in GitHub Desktop.
Save adamheins/ea98f0f0e058af8a93134979ac216a3e to your computer and use it in GitHub Desktop.
Prints the root of the current catkin workspace, or prints nothing and exits with non-zero exit code if not in a catkin workspace.
#!/usr/bin/env python
from __future__ import print_function
import os
CMAKELISTS_FILE_NAME = 'CMakeLists.txt'
def dir_is_ws_src(d):
''' Returns true if the directory d is the src directory of a catkin
workspace. False otherwise. '''
dirs = os.listdir(d)
if CMAKELISTS_FILE_NAME in dirs:
cmakelists_path = os.path.join(d, CMAKELISTS_FILE_NAME)
return (os.path.islink(cmakelists_path)
and 'toplevel' in os.readlink(cmakelists_path))
return False
def dir_is_ws_root(d):
''' Return True is the directory d is the root of a catkin workspace. False
otherwise. '''
dirs = os.listdir(d)
if 'src' in dirs:
src_dir = os.path.join(d, 'src')
return dir_is_ws_src(src_dir)
return False
def main():
home_dir = os.path.expanduser('~')
while True:
cur_dir = os.path.abspath(os.curdir)
# We don't want to progress any further if we're not under a user's
# home directory. If you're not making your workspaces under a home
# directory... start.
if cur_dir == home_dir or home_dir not in cur_dir:
return 1
if dir_is_ws_root(cur_dir):
print(cur_dir)
return 0
# Go up a directory.
os.chdir('..')
if __name__ == '__main__':
status = main()
exit(status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment