forked from google/subpar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.bzl
120 lines (107 loc) · 3.73 KB
/
debug.bzl
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
def dump(obj, obj_name):
"""Debugging method that recursively prints object fields to stderr
Args:
obj: Object to dump
obj_name: Name to print for that object
Example Usage:
```
load("debug", "dump")
...
dump(ctx, "ctx")
```
Example Output:
```
WARNING: /code/rrrrr/subpar/debug.bzl:11:5:
ctx[ctx]:
action[string]: <getattr(action) failed>
attr[struct]:
_action_listener[list]: []
_compiler[RuleConfiguredTarget]:
data_runfiles[runfiles]:
```
"""
s = '\n' + _dumpstr(obj, obj_name)
print(s)
def _dumpstr(root_obj, root_obj_name):
"""Helper method for dump() to just generate the string
Some fields always raise errors if we getattr() on them. We
manually blacklist them here. Other fields raise errors only if
we getattr() without a default. Those are handled below.
A bug was filed against Bazel, but it got fixed in a way that
didn't actually fix this.
"""
BLACKLIST = [
"InputFileConfiguredTarget.output_group",
"Label.Label",
"Label.relative",
"License.to_json",
"RuleConfiguredTarget.output_group",
"ctx.action",
"ctx.check_placeholders",
"ctx.empty_action",
"ctx.expand",
"ctx.expand_location",
"ctx.expand_make_variables",
"ctx.file_action",
"ctx.middle_man",
"ctx.new_file",
"ctx.resolve_command",
"ctx.rule",
"ctx.runfiles",
"ctx.template_action",
"ctx.tokenize",
"fragments.apple",
"fragments.cpp",
"fragments.java",
"fragments.jvm",
"fragments.objc",
"runfiles.symlinks",
"struct.output_licenses",
"struct.to_json",
"struct.to_proto",
]
MAXLINES = 4000
ROOT_MAXDEPTH = 5
# List of printable lines
lines = []
# Bazel doesn't allow a function to recursively call itself, so
# use an explicit stack
stack = [(root_obj, root_obj_name, 0, ROOT_MAXDEPTH)]
# No while() in Bazel, so use for loop over large range
for _ in range(MAXLINES):
if len(stack) == 0:
break
obj, obj_name, indent, maxdepth = stack.pop()
obj_type = type(obj)
indent_str = ' '*indent
line = '{indent_str}{obj_name}[{obj_type}]:'.format(
indent_str=indent_str, obj_name=obj_name, obj_type=obj_type)
if maxdepth == 0 or obj_type in ['dict', 'list', 'set', 'string']:
# Dump value as string, inline
line += ' ' + str(obj)
else:
# Dump all of value's fields on separate lines
attrs = dir(obj)
# Push each field to stack in reverse order, so they pop
# in sorted order
for attr in reversed(attrs):
if "%s.%s" % (obj_type, attr) in BLACKLIST:
value = '<blacklisted attr (%s)>' % attr
else:
value = getattr(obj, attr, '<getattr(%s) failed>' % attr)
stack.append((value, attr, indent+4, maxdepth-1))
lines.append(line)
return '\n'.join(lines)