Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def exclude_noise(self):
for src in list(self.sources.values()):
if src.excluded:
continue
if src.is_noise():
cli.verbose(2, "excluding", src, "because it is noisy:", src.degree)
src.excluded = True
# print "Exluding noise:", src.name
self._add_skip(src.name)
self.target = target
self.fname = '_dummy_' + target.modpath.replace('.', '_') + '.py'
self.absname = os.path.join(target.workdir, self.fname)
log.debug("dummy-filename: %r (%s)", self.fname, self.absname)
if target.is_module:
cli.verbose(1, "target is a PACKAGE")
with open(self.fname, 'w') as fp:
for fname in python_sources_below(target.package_root):
modname = fname2modname(fname, target.syspath_dir)
self.print_import(fp, modname)
elif target.is_dir:
# FIXME?: not sure what the intended semantics was here, as it is
# this will almost certainly not do the right thing...
cli.verbose(1, "target is a DIRECTORY")
with open(self.fname, 'w') as fp:
for fname in os.listdir(target.dirname):
if is_pysource(fname):
self.print_import(fp, fname2modname(fname, ''))
else:
assert target.is_pysource
cli.verbose(1, "target is a FILE")
with open(self.fname, 'w') as fp:
self.print_import(fp, target.modpath)
colors.START_COLOR = kw.get('start_color')
show_cycles = kw.get('show_cycles')
nodot = kw.get('nodot')
no_output = kw.get('no_output')
output = kw.get('output')
fmt = kw['format']
show_svg = kw.get('show')
reverse = kw.get('reverse')
if os.getcwd() != trgt.workdir:
# the tests are calling _pydeps directoy
os.chdir(trgt.workdir)
dep_graph = py2depgraph.py2dep(trgt, **kw)
if kw.get('show_deps'):
cli.verbose("DEPS:")
pprint.pprint(dep_graph)
dotsrc = depgraph_to_dotsrc(trgt, dep_graph, **kw)
if not nodot:
if kw.get('show_dot'):
cli.verbose("DOTSRC:")
print(dotsrc)
if not no_output:
svg = dot.call_graphviz_dot(dotsrc, fmt)
if fmt == 'svg':
svg = svg.replace(b'', b'<style>.edge>path:hover{stroke-width:8}</style>')
with open(output, 'wb') as fp:
cli.verbose("Writing output to:", output)
self.connect_generations()
if self.args['show_cycles']:
self.find_import_cycles()
self.calculate_bacon()
if self.args['show_raw_deps']:
print(self)
self.exclude_noise()
self.exclude_bacon(self.args['max_bacon'])
self.only_filter(self.args.get('only'))
excluded = [v for v in list(self.sources.values()) if v.excluded]
# print "EXCLUDED:", excluded
self.skip_count = len(excluded)
cli.verbose(1, "skipping", self.skip_count, "modules")
for module in excluded:
# print 'exclude:', module.name
cli.verbose(2, " ", module.name)
self.remove_excluded()
if not self.args['show_deps']:
cli.verbose(3, self)
def call_graphviz_dot(src, fmt):
"""Call dot command, and provide helpful error message if we
cannot find it.
"""
try:
svg = dot(src, T=fmt)
except OSError as e: # pragma: nocover
if e.errno == 2:
cli.error("""
cannot find 'dot'
pydeps calls dot (from graphviz) to create svg diagrams,
please make sure that the dot executable is available
on your path.
""")
raise
return svg
def display_svg(kw, fname): # pragma: nocover
"""Try to display the svg file on this platform.
"""
if kw['display'] is None:
cli.verbose("Displaying:", fname)
if sys.platform == 'win32':
os.startfile(fname)
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, fname])
else:
cli.verbose(kw['display'] + " " + fname)
os.system(kw['display'] + " " + fname)
def pydeps(**args):
"""Entry point for the ``pydeps`` command.
This function should do all the initial parameter and environment
munging before calling ``_pydeps`` (so that function has a clean
execution path).
"""
_args = args if args else cli.parse_args(sys.argv[1:])
inp = target.Target(_args['fname'])
log.debug("Target: %r", inp)
if _args.get('output'):
_args['output'] = os.path.abspath(_args['output'])
else:
_args['output'] = os.path.join(
inp.calling_dir,
inp.modpath.replace('.', '_') + '.' + _args.get('format', 'svg')
)
with inp.chdir_work():
_args['fname'] = inp.fname
_args['isdir'] = inp.is_dir
if _args.get('externals'):
pprint.pprint(dep_graph)
dotsrc = depgraph_to_dotsrc(trgt, dep_graph, **kw)
if not nodot:
if kw.get('show_dot'):
cli.verbose("DOTSRC:")
print(dotsrc)
if not no_output:
svg = dot.call_graphviz_dot(dotsrc, fmt)
if fmt == 'svg':
svg = svg.replace(b'', b'<style>.edge>path:hover{stroke-width:8}</style>')
with open(output, 'wb') as fp:
cli.verbose("Writing output to:", output)
fp.write(svg)
if show_svg:
dot.display_svg(kw, output)