9 from common
import FormatReturn
15 def find_current_library(path):
17 match = re.search(
'(?:(?:include\/)|(?:src\/)).*', path)
19 raise Exception(
'lib path not found')
25 split_by_inc = string.split(path,
'include/')
28 if len(split_by_inc) > 1:
29 lib_path = split_by_inc
31 lib_path = string.split(path,
'src/')
36 lib_name = string.split(lib,
'/')[-2]
40 lib_name = string.split(lib,
'/')[-4]
47 def find_libraries_and_bundles(fw4spl_projects):
54 for project_dir
in fw4spl_projects:
55 if not os.path.isdir(project_dir):
56 common.warn(
"%s isn't a valid directory." % project_dir)
58 for root, dirs, files
in os.walk(project_dir):
59 rootdir = os.path.split(root)[1]
61 if not rootdir.startswith(
"."):
63 if file ==
"CMakeLists.txt":
64 if re.match(
'.*Bundles', root):
65 g_bundles += [rootdir]
66 elif re.match(
'.*SrcLib', root):
75 def clean_list(includes):
78 if len(includes) == 0:
79 return new_include_list
81 includes.sort(key=
lambda s: s[1].lower())
83 prev_module = includes[0][0]
84 for module, include
in includes:
85 if prev_module != module:
86 new_include_list += [
'\n']
87 new_include_list += [include]
90 new_include_list += [
'\n']
91 return new_include_list
94 def sort_includes(path, enable_reformat):
96 cur_lib = find_current_library(path)
98 common.warn(
'Failed to find current library for file ' + path +
', includes order might be wrong.\n')
99 cur_lib =
'!!NOTFOUND!!' 101 pathname = os.path.dirname(__file__) +
"/" 103 file = open(pathname +
"std_headers.txt",
'r') 105 lib_std = file.read() 108 file = open(path, 'rb')
109 content = file.readlines()
116 out_of_include =
False 118 for i, line
in enumerate(content):
119 if re.match(
"#include", line):
122 'Failed to parse includes in file ' + path +
', includes sort is skipped. Maybe there is a #ifdef ? This may be handled in a future version.\n')
130 elif first_line > -1
and line !=
"\n":
131 out_of_include =
True 133 if first_line == -1
and last_line == -1:
140 for include
in includes:
141 include_path_match = re.match(
'.*<(.*/.*)>', include)
143 if include_path_match:
144 module = include_path_match.group(1).split(
'/')[0]
146 include_path_match = re.match(
'.*"(.*/.*)"', include)
147 if include_path_match:
148 module = include_path_match.group(1).split(
'/')[0]
152 include_modules += [module]
154 own_header_include = []
155 current_module_includes = []
157 bundles_includes = []
161 orig_path = re.sub(
".hg-new",
"", path)
162 extension = os.path.splitext(orig_path)[1]
165 if extension ==
".cpp":
166 filename = os.path.basename(orig_path)
167 matched_header = re.sub(extension,
".hpp", filename)
170 for include, module
in zip(includes, include_modules):
172 if cpp
and re.search(
'".*' + matched_header +
'.*"', include):
173 own_header_include += [(module, include)]
174 elif module == cur_lib
or re.search(
'".*"', include):
175 current_module_includes += [(module, include)]
176 elif module
in g_libs:
177 lib_includes += [(module, include)]
178 elif module
in g_bundles:
179 bundles_includes += [(module, include)]
181 include_path_match = re.match(
'.*<(.*)>', include)
182 if include_path_match
and include_path_match.group(1)
in lib_std:
183 std_includes += [(module, include)]
185 other_includes += [(module, include)]
187 new_includes = clean_list(own_header_include) + clean_list(current_module_includes) + clean_list(
188 lib_includes) + clean_list(bundles_includes) + clean_list(other_includes) + clean_list(std_includes)
191 for i, line
in enumerate(content):
193 new_content += new_includes[:-1]
194 elif i < first_line
or i > last_line:
195 new_content += [line]
197 if content != new_content:
199 open(path,
'wb').writelines(new_content)
200 return FormatReturn.Modified
202 common.error(
'Include headers are not correctly sorted in file : ' + path +
'.')
203 return FormatReturn.Error
205 return FormatReturn.NotModified