Prettify the output of the override-feature-defines script
[WebKit-https.git] / Tools / gtk / override-feature-defines
1 #!/usr/bin/env python
2
3 import os
4 import re
5 import shutil
6 import sys
7
8 def copy_feature_defines_makefile():
9     src_path = os.path.join('Source', 'WebCore', 'GNUmakefile.features.am.in')
10     dest_path = os.path.join('Source', 'WebCore', 'GNUmakefile.features.am')
11     shutil.copy(src_path, dest_path)
12
13 def adjust_feature_defines_makefile(build_dir, feature_defines_overriding):
14     feature_defines_makefile_file = os.path.join('Source', 'WebCore', 'GNUmakefile.features.am')
15     with open(feature_defines_makefile_file) as f:
16         feature_defines_makefile = f.read()
17
18     overridden_feature_defines = {}
19     def override_feature_define(match):
20         feature_define = match.group('feature')
21         if feature_define not in feature_defines_overriding:
22             return match.string[match.start():match.end()]
23
24         default_value = int(match.group('default_value'))
25         overriding_value = feature_defines_overriding[feature_define]
26         if default_value is not overriding_value:
27             overridden_feature_defines[feature_define] = default_value, overriding_value
28         return "%s=%d" % (feature_define, overriding_value)
29
30     feature_defines_makefile = re.sub(r"(?P<feature>(?:ENABLE_)\w+)=(?P<default_value>0|1)", override_feature_define, feature_defines_makefile)
31     with open(feature_defines_makefile_file, 'w') as f:
32         f.write(feature_defines_makefile)
33
34     if overridden_feature_defines:
35         print("The following feature defines were overriden:")
36         for feature, values in iter(overridden_feature_defines.items()):
37             print("\t{0}: {1} => {2}".format(feature, *values))
38
39 def override_feature_defines(build_dir):
40     copy_feature_defines_makefile()
41
42     feature_defines_overriding_file = os.path.join(build_dir, 'feature-defines-overriding.txt')
43     if not os.path.exists(feature_defines_overriding_file):
44         return
45
46     with open(feature_defines_overriding_file) as f:
47         match_iter = re.findall(r"((?:ENABLE_)\w+)=(0|1)", f.read())
48
49     feature_defines_overriding = {}
50     for match in match_iter:
51         feature_defines_overriding[match[0]] = int(match[1])
52
53     adjust_feature_defines_makefile(build_dir, feature_defines_overriding)
54
55 if __name__=='__main__':
56     override_feature_defines(sys.argv[1])