ATLAS Offline Software
Loading...
Searching...
No Matches
apydep Namespace Reference

Classes

class  DependencyFinder

Functions

 get_dependencies (filename, print_error=False)
 walk_tree (path='./', print_error=False, filterFnc=None)
 make_graph (deps, filterFnc=None)
 main ()

Detailed Description

Extract Python dependencies between packages and create DOT graph.
Both `import` and `include` dependencies are considered.

Function Documentation

◆ get_dependencies()

apydep.get_dependencies ( filename,
print_error = False )
Get all the imports/includes in a file.

Definition at line 41 of file apydep.py.

41def get_dependencies(filename, print_error=False):
42 """Get all the imports/includes in a file."""
43
44 try:
45 tree = ast.parse(open(filename,'rb').read(), filename=filename)
46 except Exception as e:
47 if print_error:
48 print(e, file=sys.stderr)
49 return DependencyFinder()
50
51 finder = DependencyFinder()
52 try:
53 finder.visit(tree)
54 except Exception as e:
55 if print_error:
56 print(e, f'({os.path.basename(filename)})', file=sys.stderr)
57
58 return finder
59
60
void print(char *figname, TCanvas *c1)
IovVectorMap_t read(const Folder &theFolder, const SelectionCriterion &choice, const unsigned int limit=10)

◆ main()

apydep.main ( )

Definition at line 93 of file apydep.py.

93def main():
94 parser = argparse.ArgumentParser(description=__doc__)
95
96 parser.add_argument('path', metavar='DIRECTORY', nargs='?', default='./',
97 help='root of source tree [%(default)s]')
98
99 parser.add_argument('-o', '--output', metavar='FILE', type=str,
100 help='output file for DOT graph')
101
102 parser.add_argument('-p', '--packages', metavar='FILE', type=str,
103 help='path to packages.txt file [from release]')
104
105 parser.add_argument('-a', '--all', action='store_true',
106 help='include non-athena dependencies')
107
108 parser.add_argument('-v', '--verbose', action='store_true',
109 help='print parse errors')
110
111 args = parser.parse_args()
112
113 packages = None
114 if not args.all:
115 package_file = args.packages or os.path.join(os.environ['AtlasArea'],'InstallArea',
116 os.environ['BINARY_TAG'],'packages.txt')
117
118 try:
119 with open(package_file) as f:
120 packages = set(line.rstrip().split('/')[-1] for line in f if not line.startswith('#'))
121 except FileNotFoundError:
122 parser.error(f"Cannot read '{package_file}'. Specify via '-p/--packages' or run with '-a/--all'")
123
124 # By default only show athena packages:
125 filterFnc = None if args.all else lambda p : p in packages
126
127 # Walk source tree and create DOT graph:
128 g = make_graph(walk_tree(args.path, args.verbose, filterFnc), filterFnc)
129
130 if args.output:
131 g.write(args.output)
132 else:
133 print(g)
134
STL class.
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
int main()
Definition hello.cxx:18

◆ make_graph()

apydep.make_graph ( deps,
filterFnc = None )
Save the dependencies as dot graph, nodes filtered by filterFnc

Definition at line 81 of file apydep.py.

81def make_graph(deps, filterFnc=None):
82 """Save the dependencies as dot graph, nodes filtered by filterFnc"""
83
84 graph = pygraphviz.AGraph(name='AthPyGraph', directed=True)
85 for a in deps:
86 for t in ['import','include']:
87 graph.add_edges_from(((a,b) for b in deps[a][t]
88 if a!=b and (filterFnc is None or (filterFnc(a) and filterFnc(b)))),
89 label = t)
90 return graph
91
92

◆ walk_tree()

apydep.walk_tree ( path = './',
print_error = False,
filterFnc = None )
Walk the source tree and extract python dependencies, filtered by FilterFnc

Definition at line 61 of file apydep.py.

61def walk_tree(path='./', print_error=False, filterFnc=None):
62 """Walk the source tree and extract python dependencies, filtered by FilterFnc"""
63
64 pkg = 'UNKNOWN'
65 deps = defaultdict(lambda : defaultdict(set))
66 for root, dirs, files in os.walk(path):
67 if 'CMakeLists.txt' in files:
68 pkg = os.path.basename(root)
69
70 if (filterFnc and not filterFnc(pkg)):
71 continue
72
73 for f in filter(lambda p : os.path.splitext(p)[1]=='.py', files):
74 d = get_dependencies(os.path.join(root,f), print_error)
75 deps[pkg]['import'].update(d.imports)
76 deps[pkg]['include'].update(d.includes)
77
78 return deps
79
80