Go to the documentation of this file.00001 import os
00002 import collections
00003 from source_code_file import is_python_hashbang_line
00004
00005 KEY = ['package.xml', 'CMakeLists.txt', 'setup.py']
00006 SRC_EXTS = ['.py', '.cpp', '.h', '.hpp', '.c']
00007 GENERATORS = ['.msg', '.srv', '.action']
00008
00009
00010 def get_filetype_by_contents(filename, ext):
00011 with open(filename) as f:
00012 first_line = f.readline()
00013 if is_python_hashbang_line(first_line):
00014 return 'source'
00015 elif '<launch' in first_line:
00016 return 'launch'
00017 elif ext == '.xml' and ('<library' in first_line or '<class_libraries' in first_line):
00018 return 'plugin_config'
00019
00020
00021 def get_package_structure(pkg_root):
00022 structure = collections.defaultdict(dict)
00023
00024 for root, dirs, files in os.walk(pkg_root):
00025 if '.git' in root or '.svn' in root:
00026 continue
00027 for fn in files:
00028 ext = os.path.splitext(fn)[-1]
00029 full = '%s/%s' % (root, fn)
00030 rel_fn = full.replace(pkg_root + '/', '')
00031
00032 if fn[-1] == '~' or fn[-4:] == '.pyc':
00033 continue
00034 if fn in KEY:
00035 structure['key'][rel_fn] = full
00036 elif ext in SRC_EXTS:
00037 structure['source'][rel_fn] = full
00038 elif ext == '.launch':
00039 structure['launch'][rel_fn] = full
00040 elif ext in GENERATORS:
00041 structure['generators'][rel_fn] = full
00042 elif ext == '.cfg' and 'cfg/' in full:
00043 structure['cfg'][rel_fn] = full
00044 else:
00045 structure[get_filetype_by_contents(full, ext)][rel_fn] = full
00046 return structure