summaryrefslogtreecommitdiffstats
path: root/SCons/Environment.py
diff options
context:
space:
mode:
authorAndrii Doroshenko (Xrayez) <xrayez@gmail.com>2020-05-27 16:43:13 (GMT)
committerAndrii Doroshenko (Xrayez) <xrayez@gmail.com>2020-05-28 20:29:41 (GMT)
commit8662a4efdf65dd5be31315c23338dc13d898498b (patch)
treee75a7c1b85cc62f83af3e3d58ab4ba55b98e93b1 /SCons/Environment.py
parent47b761fa3f9e24419db48393ca9d6064f7b5d21c (diff)
downloadSCons-8662a4efdf65dd5be31315c23338dc13d898498b.zip
SCons-8662a4efdf65dd5be31315c23338dc13d898498b.tar.gz
SCons-8662a4efdf65dd5be31315c23338dc13d898498b.tar.bz2
Extend `Environment.Dump()` to select serialization format
Environment.Dump() produces pretty-printable results only, so the usefulness of this method is limited to debugging purposes. The existing method is extended to allow selecting a serialization format to use via a `format` parameter. Namely, it's now possible to serialize variables as a JSON-formatted string, which makes it possible for automated external tools to inspect the environment more easily.
Diffstat (limited to 'SCons/Environment.py')
-rw-r--r--SCons/Environment.py36
1 files changed, 26 insertions, 10 deletions
diff --git a/SCons/Environment.py b/SCons/Environment.py
index 0b4be1b..cd52ee5 100644
--- a/SCons/Environment.py
+++ b/SCons/Environment.py
@@ -1517,26 +1517,42 @@ class Base(SubstitutionEnvironment):
return dlist
- def Dump(self, key=None):
- """ Return pretty-printed string of construction variables.
+ def Dump(self, key=None, format='pretty'):
+ """ Serialize the construction variables to a string.
:param key: if None, format the whole dict of variables.
Else look up and format just the value for key.
+
+ :param format: specify the format of the variables to be serialized:
+ - pretty: pretty-printed string.
+ - json: JSON-formatted string.
"""
- import pprint
- pp = pprint.PrettyPrinter(indent=2)
if key:
cvars = self.Dictionary(key)
else:
cvars = self.Dictionary()
- # TODO: pprint doesn't do a nice job on path-style values
- # if the paths contain spaces (i.e. Windows), because the
- # algorithm tries to break lines on spaces, while breaking
- # on the path-separator would be more "natural". Is there
- # a better way to format those?
- return pp.pformat(cvars)
+ fmt = format.lower()
+
+ if fmt == 'pretty':
+ import pprint
+ pp = pprint.PrettyPrinter(indent=2)
+
+ # TODO: pprint doesn't do a nice job on path-style values
+ # if the paths contain spaces (i.e. Windows), because the
+ # algorithm tries to break lines on spaces, while breaking
+ # on the path-separator would be more "natural". Is there
+ # a better way to format those?
+ return pp.pformat(cvars)
+
+ elif fmt == 'json':
+ import json
+ def non_serializable(obj):
+ return str(type(obj).__qualname__)
+ return json.dumps(cvars, indent=4, default=non_serializable)
+ else:
+ raise ValueError("Unsupported serialization format: %s." % fmt)
def FindIxes(self, paths, prefix, suffix):