2 Defines utilities useful for performing standard "configuration" style tasks.
8 def configure_file(input_path, output_path, substitutions):
9 """configure_file(input_path, output_path, substitutions) -> bool
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.
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.
19 The output path's parent directory need not exist (it will be created).
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.
26 # Read in the input data.
27 f = open(input_path, "rb")
33 # Perform the substitutions.
34 for regex_string,replacement in substitutions:
35 regex = re.compile(regex_string)
36 data = regex.sub(replacement, data)
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)
43 # If the output path exists, load it and compare to the configured contents.
44 if os.path.exists(output_path):
47 f = open(output_path, "rb")
49 current_data = f.read()
56 if current_data is not None and current_data == data:
59 # Write the output contents.
60 f = open(output_path, "wb")