3 from __future__
import print_function
10 addr_re =
r"(?P<address>[0-9a-f]{1,16})?"
11 code_re =
r"(?P<code>[a-zA-Z])"
12 symbol_re =
r"(?P<symbol>[a-zA-Z0-9_.$@]+)"
13 symbol_demunged_re =
r"(?P<symbol>[a-zA-Z0-9_.$@:&()<>{}\[\]|^!%,~*+-=# ]+)"
14 symbols_re_skip = re.compile(
"(@@)")
15 nm_line_re = re.compile(
r"\s+".
join([addr_re, code_re, symbol_re]) +
r"\s*$",
17 ldd_line_re = re.compile(
r"\s+(.*) => (.*) \(0x")
19 requires = collections.defaultdict(set)
20 provides = collections.defaultdict(set)
21 dependencies = collections.defaultdict(set)
22 libraries = collections.defaultdict(set)
26 lines = subprocess.check_output([
"nm",
"-g", fname])
27 for line
in lines.splitlines():
28 m = nm_line_re.match(line)
31 symbol = m.group(
'symbol')
32 if m.group(
'code') ==
'U':
33 requires[os.path.basename(fname)].add(symbol)
35 provides[symbol].
add(os.path.basename(fname))
39 lines = subprocess.check_output([
"ldd", fname])
40 for line
in lines.splitlines():
41 m = ldd_line_re.match(line)
45 libraries[os.path.basename(fname)].
add(
46 os.path.basename(library.rstrip(
'\r\n')))
49 paths = os.environ[
'LD_LIBRARY_PATH'].
split(
':')
52 for dirpath, dirnames, filenames
in os.walk(p):
54 fpth = os.path.realpath(os.path.join(dirpath, f))
55 filetype = subprocess.check_output([
"file", fpth])
56 if filetype.find(
"dynamically linked") >= 0:
60 for fname, symbols
in requires.items():
62 for library
in libraries[fname]:
64 if library
in provides[s]:
66 dependencies[fname] = deps
67 print(fname +
' : primary dependencies : ' +
68 ', '.
join(sorted(dependencies[fname]))+
'\n')
72 if s
not in provides
and not symbols_re_skip.search(s):
74 for u
in sorted(unmet):
75 dm = subprocess.check_output([
"c++filt", u])
76 demangled.add(dm.rstrip(
'\r\n'))
78 print(fname +
': undefined : ' +
', '.
join(sorted(demangled)))
81 for key, values
in dependencies.items():
86 for node
in nx.nodes_iter(G):
87 s = nx.dfs_successors(G, node)
90 for key, vals
in s.items():
95 print(node +
': primary and secondary dependencies :' +
', '.
join(sorted(deps)))
99 for key, values
in dependencies.items():
100 H.add_node(os.path.basename(key))
102 H.add_edge(os.path.basename(key), os.path.basename(val))
103 for node
in nx.nodes_iter(H):
104 T = nx.dfs_tree(H, node)
106 nx.write_dot(T, name)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
static std::string join(char **cmd)