textfile.py (scons-4.2.0) | : | textfile.py (SCons-4.3.0) | ||
---|---|---|---|---|
# -*- python -*- | # MIT License | |||
# | # | |||
# __COPYRIGHT__ | # Copyright The SCons Foundation | |||
# | # | |||
# Permission is hereby granted, free of charge, to any person obtaining | # Permission is hereby granted, free of charge, to any person obtaining | |||
# a copy of this software and associated documentation files (the | # a copy of this software and associated documentation files (the | |||
# "Software"), to deal in the Software without restriction, including | # "Software"), to deal in the Software without restriction, including | |||
# without limitation the rights to use, copy, modify, merge, publish, | # without limitation the rights to use, copy, modify, merge, publish, | |||
# distribute, sublicense, and/or sell copies of the Software, and to | # distribute, sublicense, and/or sell copies of the Software, and to | |||
# permit persons to whom the Software is furnished to do so, subject to | # permit persons to whom the Software is furnished to do so, subject to | |||
# the following conditions: | # the following conditions: | |||
# | # | |||
# The above copyright notice and this permission notice shall be included | # The above copyright notice and this permission notice shall be included | |||
# in all copies or substantial portions of the Software. | # in all copies or substantial portions of the Software. | |||
# | # | |||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY | |||
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |||
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |||
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |||
# | ||||
__doc__ = """ | """ | |||
Textfile/Substfile builder for SCons. | Textfile/Substfile builder for SCons. | |||
Create file 'target' which typically is a textfile. The 'source' | Create file 'target' which typically is a textfile. The 'source' | |||
may be any combination of strings, Nodes, or lists of same. A | may be any combination of strings, Nodes, or lists of same. A | |||
'linesep' will be put between any part written and defaults to | 'linesep' will be put between any part written and defaults to | |||
os.linesep. | os.linesep. | |||
The only difference between the Textfile builder and the Substfile | The only difference between the Textfile builder and the Substfile | |||
builder is that strings are converted to Value() nodes for the | builder is that strings are converted to Value() nodes for the | |||
former and File() nodes for the latter. To insert files in the | former and File() nodes for the latter. To insert files in the | |||
skipping to change at line 47 | skipping to change at line 46 | |||
respectively. | respectively. | |||
The values of SUBST_DICT first have any construction variables | The values of SUBST_DICT first have any construction variables | |||
expanded (its keys are not expanded). If a value of SUBST_DICT is | expanded (its keys are not expanded). If a value of SUBST_DICT is | |||
a python callable function, it is called and the result is expanded | a python callable function, it is called and the result is expanded | |||
as the value. Values are substituted in a "random" order; if any | as the value. Values are substituted in a "random" order; if any | |||
substitution could be further expanded by another substitution, it | substitution could be further expanded by another substitution, it | |||
is unpredictable whether the expansion will occur. | is unpredictable whether the expansion will occur. | |||
""" | """ | |||
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" | ||||
import SCons | import SCons | |||
from SCons.Node import Node | from SCons.Node import Node | |||
from SCons.Node.Python import Value | from SCons.Node.Python import Value | |||
from SCons.Util import is_String, is_Sequence, is_Dict, to_bytes | from SCons.Util import is_String, is_Sequence, is_Dict, to_bytes | |||
TEXTFILE_FILE_WRITE_MODE = 'w' | TEXTFILE_FILE_WRITE_MODE = 'w' | |||
LINESEP = '\n' | LINESEP = '\n' | |||
skipping to change at line 86 | skipping to change at line 83 | |||
# TODO: this should not happen, get_text_contents returns text | # TODO: this should not happen, get_text_contents returns text | |||
contents = bytearray(contents) | contents = bytearray(contents) | |||
return contents | return contents | |||
def _action(target, source, env): | def _action(target, source, env): | |||
# prepare the line separator | # prepare the line separator | |||
linesep = env['LINESEPARATOR'] | linesep = env['LINESEPARATOR'] | |||
if linesep is None: | if linesep is None: | |||
linesep = LINESEP # os.linesep | linesep = LINESEP # os.linesep | |||
elif is_String(linesep): | elif is_String(linesep): | |||
pass | pass | |||
elif isinstance(linesep, Value): | elif isinstance(linesep, Value): | |||
linesep = linesep.get_text_contents() | linesep = linesep.get_text_contents() | |||
else: | else: | |||
raise SCons.Errors.UserError('unexpected type/class for LINESEPARATOR: % s' | raise SCons.Errors.UserError('unexpected type/class for LINESEPARATOR: % s' | |||
% repr(linesep), None) | % repr(linesep), None) | |||
if 'b' in TEXTFILE_FILE_WRITE_MODE: | if 'b' in TEXTFILE_FILE_WRITE_MODE: | |||
linesep = to_bytes(linesep) | linesep = to_bytes(linesep) | |||
skipping to change at line 114 | skipping to change at line 111 | |||
subst_dict = list(subst_dict.items()) | subst_dict = list(subst_dict.items()) | |||
elif is_Sequence(subst_dict): | elif is_Sequence(subst_dict): | |||
pass | pass | |||
else: | else: | |||
raise SCons.Errors.UserError('SUBST_DICT must be dict or sequence') | raise SCons.Errors.UserError('SUBST_DICT must be dict or sequence') | |||
subs = [] | subs = [] | |||
for (k, value) in subst_dict: | for (k, value) in subst_dict: | |||
if callable(value): | if callable(value): | |||
value = value() | value = value() | |||
if is_String(value): | if is_String(value): | |||
value = env.subst(value) | value = env.subst(value, raw=1) | |||
else: | else: | |||
value = str(value) | value = str(value) | |||
subs.append((k, value)) | subs.append((k, value)) | |||
# write the file | # write the file | |||
try: | try: | |||
target_file = open(target[0].get_path(), TEXTFILE_FILE_WRITE_MODE, newli ne='') | target_file = open(target[0].get_path(), TEXTFILE_FILE_WRITE_MODE, newli ne='') | |||
except (OSError, IOError) as e: | except (OSError, IOError) as e: | |||
raise SCons.Errors.UserError("Can't write target file %s [%s]" % (target [0],e)) | raise SCons.Errors.UserError("Can't write target file %s [%s]" % (target [0],e)) | |||
End of changes. 7 change blocks. | ||||
8 lines changed or deleted | 5 lines changed or added |