Skip to content

Instantly share code, notes, and snippets.

@chrisjurich
Created May 9, 2020 02:42
Show Gist options
  • Save chrisjurich/59211627e5966f01768e7c8fb483fb82 to your computer and use it in GitHub Desktop.
Save chrisjurich/59211627e5966f01768e7c8fb483fb82 to your computer and use it in GitHub Desktop.
def is_header_file(file_name):
"""Method that checks if the file is a C++ header file"""
file_ending = file_name.split('.')[-1].lower()
for ending in "h|hh|hpp|hxx|h++".split('|'):
if ending == file_ending:
return True
return False
def is_source_file(file_name):
"""Method that checks if the file is a C++ source file"""
file_ending = file_name.split('.')[-1].lower()
for ending in "c|cc|cpp|cxx|c++".split('|'):
if ending == file_ending:
return True
return False
def is_cpp_file(file_name):
"""Method that checks if the file is a C++ file"""
return is_header_file(file_name) or is_source_file(file_name)
def get_file_name(raw_name):
"""Method that gets the file_name from the input raw_name"""
return raw_name.split('.')[-2]
def make_file_list(raw_file_names):
"""Method that takes a list of files and returns the singular list of files for the CMakeLists.txt file"""
source_files = dict()
header_files = dict()
# looping through the file names and adding them to the set
for file_name in raw_file_names:
# need to check if the file is a C++ file
if is_cpp_file(file_name):
# sort into the appropriate dict
name = get_file_name(file_name)
if is_source_file(file_name):
source_files[name] = file_name
else:
header_files[name] = file_name
# all source files go into the output file set
output_file_set = set(source_files.values())
# and the header files that are not already included as source
for name, file_path in header_files.items():
if name not in source_files:
output_file_set.add(file_path)
return list(output_file_set)
if __name__ == "__main__":
pass
# usage: to get all the file names for a CMake set() declaration for files, feed the raw files into
# make_file_list()
# Example:
# src/
# ---text.txt
# ---Class1.h
# ---Class1.cpp
# ---Class2.h
# ---Class2.cc
# ---Class3.h
# ---pyfile.py
# make_file_list([str(fp) for fp in glob.glob('src/*')])
# returns => "src/Class1.cpp", "src/Class2.cc","src/Class3.h"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment