--- /dev/null
+#!/usr/bin/python2
+import sys, os, re
+
+# use space as delimiter
+glibdirs = "glib-2.0"
+
+# g_dbus api matcher
+re_func = re.compile("(?P<rtype>\w*[\t ]*[*]{0,3})[\t ]*(?P<api>[_]?g_(test_)?[d]?bus_[\w\d]+)[\t ]+(?P<param>\()")
+re_func_end = re.compile("[)]{1}.*[;]{1}")
+
+# special case matcher
+# G_DEFINE_AUTOPTR_CLEANUP_FUNC(GDBusNodeInfo, g_dbus_node_info_unref)
+re_def_auto_ptr = re.compile("G_DEFINE_AUTOPTR_CLEANUP_FUNC\(.*(?P<api>[_]?g_(test_)?[d]?bus_[\w\d]+)+[\w\s]*\)")
+
+def api_filter(relative_path):
+ # traverse header files
+ for glibdir in glibdirs.split():
+ glibpath = relative_path + "/usr/include/" + glibdir
+ if not os.path.isdir(glibpath):
+ print(glibpath, 'is not valid path')
+ continue
+ print(glibpath)
+ for subdir, dirs, files in os.walk(glibpath):
+ for file in files:
+ filepath = subdir + os.sep + file
+
+ buf = list()
+ fp = open(filepath, "rt")
+ if not fp:
+ print('file open error', filepath)
+ continue
+ lines = fp.readlines()
+ fp.close()
+
+ found_func = False
+ nline = False
+
+ for line in lines:
+ if nline:
+ if not line:
+ nline = False
+ continue
+ # find end of multi-line macro
+ if not line.strip() or line.strip()[-1] != '\\':
+ nline = False
+ buf.append(line)
+ continue
+
+ if not line:
+ continue
+
+ line_dup = line.strip()
+ if not line_dup:
+ buf.append(line)
+ continue
+
+ if found_func:
+ buf.append('//{0}'.format(line))
+ # find end of func
+ if re.search("[)].*[;]", line_dup):
+ found_func = False
+ continue
+ # ignore comment or macro
+ if line_dup[0] in ['#', '*', '/']:
+ buf.append(line)
+ # find macro : '#define + \'
+ if line_dup[-1] == '\\':
+ nline = True
+ continue
+
+ # find g_dbus_* function
+ result = re_func.search(line_dup)
+ if not result:
+ # G_DEFINE_AUTOPTR_CLEANUP_FUNC(GDBusNodeInfo, g_dbus_node_info_unref)
+ result = re_def_auto_ptr.search(line_dup)
+ if result:
+ buf.append('//{0}'.format(line))
+ print('{0: <30} {1: <45}\tX'.format(file, '(G_DEFINE*) ' + result.group('api')))
+ else:
+ buf.append(line)
+ continue
+
+ if not result.group('api') or not result.group('api').strip():
+ print('error:un reachable', result.groupdict())
+ continue
+
+ # ignore private api _g_dbus_xxx
+ if result.group('api').strip()[0] == '_':
+ buf.append(line)
+ continue
+
+ found_func = True
+
+ if result.start('param') == -1:
+ print('un reachable:can not find open parenthesis')
+ # find end of func
+ if re_func_end.search(line_dup[result.start('param'):]):
+ found_func = False
+
+ # check prefix macro
+ if len(buf) > 0 and buf[-1].find('GLIB_AVAILABLE_IN') == 0:
+ buf[-1] = '//' + buf[-1]
+ buf.append('//{0}'.format(line))
+
+ print('{0: <30} {1: <45}\tX'.format(file, result.group('api')))
+
+ # re-write header file
+ for i in range(3):
+ fp = open(filepath, "wt")
+ if not fp:
+ print('file open error. failed to re-write', filepath)
+ print('retry re-write', i)
+ continue
+ fp.writelines(buf)
+ fp.close()
+ break
+
+# main
+if __name__ == "__main__":
+ # check whether path is given
+ args = ''
+ if sys.argv[0].find('python') == 0:
+ args = sys.argv[1:]
+ else:
+ args = sys.argv
+ if len(args) < 2:
+ print("error: path is not specified.")
+ sys.exit()
+ api_filter(args[1])
+