Merge pull request #4584 from siegria/fix_piped_spawn_encoding
[scons.git] / site_scons / SConsRevision.py
blob5d764491a2fcd5c498db9bc72d2bb0b9703fffeb
1 # SPDX-License-Identifier: MIT
3 # Copyright The SCons Foundation
5 import os
7 def SCons_revision(target, source, env):
8 """Interpolate specific values from the environment into a file.
10 This is used to copy files into a tree that gets packaged up
11 into the source file package.
12 """
13 t = str(target[0])
14 s = source[0].rstr()
16 try:
17 with open(s) as fp:
18 contents = fp.read()
21 # Note: We construct the __*__ substitution strings here
22 # so that they don't get replaced when this file gets
23 # copied into the tree for packaging.
24 contents = contents.replace('__BUILD' + '__', env['BUILD'])
25 contents = contents.replace('__BUILDSYS' + '__', env['BUILDSYS'])
26 contents = contents.replace('__COPYRIGHT' + '__', env['COPYRIGHT'])
27 contents = contents.replace('__DATE' + '__', env['DATE'])
28 contents = contents.replace('__DEB_DATE' + '__', env['DEB_DATE'])
30 contents = contents.replace('__DEVELOPER' + '__', env['DEVELOPER'])
31 contents = contents.replace('__FILE' + '__', str(source[0]).replace('\\', '/'))
32 contents = contents.replace('__MONTH_YEAR'+ '__', env['MONTH_YEAR'])
33 contents = contents.replace('__REVISION' + '__', env['REVISION'])
34 contents = contents.replace('__VERSION' + '__', env['VERSION'])
35 contents = contents.replace('__NULL' + '__', '')
37 with open(t,'w') as of:
38 of.write(contents)
39 except UnicodeDecodeError as e:
40 print("Error decoding file:%s just copying no revision edit")
41 with open(s, 'rb') as fp, open(t, 'wb') as of:
42 contents = fp.read()
43 of.write(contents)
46 os.chmod(t, os.stat(s)[0])