summaryrefslogtreecommitdiff
path: root/utils/llvm-build/llvmbuild/configutil.py
blob: b5582c34de46c77452b16db59a297cefa929ba71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Defines utilities useful for performing standard "configuration" style tasks.
"""

import re
import os

def configure_file(input_path, output_path, substitutions):
    """configure_file(input_path, output_path, substitutions) -> bool

    Given an input and output path, "configure" the file at the given input path
    by replacing variables in the file with those given in the substitutions
    list. Returns true if the output file was written.

    The substitutions list should be given as a list of tuples (regex string,
    replacement), where the regex and replacement will be used as in 're.sub' to
    execute the variable replacement.

    The output path's parent directory need not exist (it will be created).

    If the output path does exist and the configured data is not different than
    it's current contents, the output file will not be modified. This is
    designed to limit the impact of configured files on build dependencies.
    """

    # Read in the input data.
    f = open(input_path, "rb")
    try:
        data = f.read()
    finally:
        f.close()

    # Perform the substitutions.
    for regex_string,replacement in substitutions:
        regex = re.compile(regex_string)
        data = regex.sub(replacement, data)

    # Ensure the output parent directory exists.
    output_parent_path = os.path.dirname(os.path.abspath(output_path))
    if not os.path.exists(output_parent_path):
        os.makedirs(output_parent_path)

    # If the output path exists, load it and compare to the configured contents.
    if os.path.exists(output_path):
        current_data = None
        try:
            f = open(output_path, "rb")
            try:
                current_data = f.read()
            except:
                current_data = None
            f.close()
        except:
            current_data = None

        if current_data is not None and current_data == data:
            return False

    # Write the output contents.
    f = open(output_path, "wb")
    try:
        f.write(data)
    finally:
        f.close()

    return True