Teach llvm-build to avoid touching LibraryDependencies.inc unless the contents
[oota-llvm.git] / utils / llvm-build / llvmbuild / configutil.py
1 """
2 Defines utilities useful for performing standard "configuration" style tasks.
3 """
4
5 import re
6 import os
7
8 def configure_file(input_path, output_path, substitutions):
9     """configure_file(input_path, output_path, substitutions) -> bool
10
11     Given an input and output path, "configure" the file at the given input path
12     by replacing variables in the file with those given in the substitutions
13     list. Returns true if the output file was written.
14
15     The substitutions list should be given as a list of tuples (regex string,
16     replacement), where the regex and replacement will be used as in 're.sub' to
17     execute the variable replacement.
18
19     The output path's parent directory need not exist (it will be created).
20
21     If the output path does exist and the configured data is not different than
22     it's current contents, the output file will not be modified. This is
23     designed to limit the impact of configured files on build dependencies.
24     """
25
26     # Read in the input data.
27     f = open(input_path, "rb")
28     try:
29         data = f.read()
30     finally:
31         f.close()
32
33     # Perform the substitutions.
34     for regex_string,replacement in substitutions:
35         regex = re.compile(regex_string)
36         data = regex.sub(replacement, data)
37
38     # Ensure the output parent directory exists.
39     output_parent_path = os.path.dirname(os.path.abspath(output_path))
40     if not os.path.exists(output_parent_path):
41         os.makedirs(output_parent_path)
42
43     # If the output path exists, load it and compare to the configured contents.
44     if os.path.exists(output_path):
45         current_data = None
46         try:
47             f = open(output_path, "rb")
48             try:
49                 current_data = f.read()
50             except:
51                 current_data = None
52             f.close()
53         except:
54             current_data = None
55
56         if current_data is not None and current_data == data:
57             return False
58
59     # Write the output contents.
60     f = open(output_path, "wb")
61     try:
62         f.write(data)
63     finally:
64         f.close()
65
66     return True