Exemplo n.º 1
0
def FindPythonDependencies(module_path):
    logging.info('Finding Python dependencies of %s', module_path)
    if modulegraph is None:
        raise import_error

    prefixes = [sys.prefix]
    if hasattr(sys, 'real_prefix'):
        prefixes.append(sys.real_prefix)
    logging.info('Excluding Prefixes: %r', prefixes)

    sys_path = sys.path
    sys.path = list(sys_path)
    try:
        # Load the module to inherit its sys.path modifications.
        sys.path.insert(0, os.path.abspath(os.path.dirname(module_path)))
        imp.load_source(
            os.path.splitext(os.path.basename(module_path))[0], module_path)

        # Analyze the module for its imports.
        graph = modulegraph.ModuleGraph()
        graph.run_script(module_path)

        # Filter for only imports in Chromium.
        for node in graph.nodes():
            if not node.filename:
                continue
            module_path = os.path.realpath(node.filename)

            _, incoming_edges = graph.get_edges(node)
            message = 'Discovered %s (Imported by: %s)' % (
                node.filename, ', '.join(
                    d.filename for d in incoming_edges
                    if d is not None and d.filename is not None))
            logging.info(message)

            # This check is done after the logging/printing above to make sure that
            # we also print out the dependency edges that include python packages
            # that are not in chromium.
            if not path.IsSubpath(module_path, path_util.GetChromiumSrcDir()):
                continue

            # Exclude any dependencies which exist in the python installation.
            if any(path.IsSubpath(module_path, pfx) for pfx in prefixes):
                continue

            yield module_path
            if node.packagepath is not None:
                for p in node.packagepath:
                    yield p

    finally:
        sys.path = sys_path
 def testNoNewTelemetryDependencies(self):
   telemetry_deps = _GetRestrictedTelemetryDeps()
   current_dependencies = _GetCurrentTelemetryDependencies()
   extra_dep_paths = []
   for dep_path in current_dependencies:
     if not (dep_path in telemetry_deps['file_deps'] or
             any(path.IsSubpath(dep_path, d)
                 for d in telemetry_deps['directory_deps'])):
       extra_dep_paths.append(dep_path)
   if extra_dep_paths:
     self.fail(
         'Your patch adds new dependencies to telemetry. Please contact '
         'aiolos@,dtu@, or nednguyen@ on how to proceed with this change. '
         'Extra dependencies:\n%s' % '\n'.join(extra_dep_paths))
 def testNoNewTelemetryDependencies(self):
     telemetry_deps = _GetRestrictedTelemetryDeps()
     current_dependencies = _GetCurrentTelemetryDependencies()
     extra_dep_paths = []
     for dep_path in current_dependencies:
         if not (dep_path in telemetry_deps['file_deps'] or any(
                 path.IsSubpath(dep_path, d)
                 for d in telemetry_deps['directory_deps'])):
             extra_dep_paths.append(dep_path)
     # Temporarily ignore failure on Mac because test is failing on Mac 10.8 bot.
     # crbug.com/522335
     if extra_dep_paths:
         if platform.system() != 'Darwin':
             self.fail(
                 'Your patch adds new dependencies to telemetry. Please contact '
                 'aiolos@,dtu@, or nednguyen@ on how to proceed with this change. '
                 'Extra dependencies:\n%s' % '\n'.join(extra_dep_paths))
         else:
             print(
                 'Dependencies check failed on mac platform. Extra deps: %s\n'
                 ' sys.path: %s' % (extra_dep_paths, sys.path))
Exemplo n.º 4
0
def FindPythonDependencies(module_path):
    logging.info('Finding Python dependencies of %s' % module_path)

    # Load the module to inherit its sys.path modifications.
    imp.load_source(
        os.path.splitext(os.path.basename(module_path))[0], module_path)

    # Analyze the module for its imports.
    finder = modulefinder.ModuleFinder()
    finder.run_script(module_path)

    # Filter for only imports in Chromium.
    for module in finder.modules.itervalues():
        # If it's an __init__.py, module.__path__ gives the package's folder.
        module_path = module.__path__[0] if module.__path__ else module.__file__
        if not module_path:
            continue

        module_path = os.path.realpath(module_path)
        if not path.IsSubpath(module_path, path.GetChromiumSrcDir()):
            continue

        yield module_path
Exemplo n.º 5
0
def FindPythonDependencies(module_path):
    logging.info('Finding Python dependencies of %s', module_path)
    if modulegraph is None:
        raise import_error

    prefixes = [sys.prefix]
    if hasattr(sys, 'real_prefix'):
        prefixes.append(sys.real_prefix)
    logging.info('Excluding Prefixes: %r', prefixes)

    sys_path = sys.path
    sys.path = list(sys_path)
    try:
        # Load the module to inherit its sys.path modifications.
        sys.path.insert(0, os.path.abspath(os.path.dirname(module_path)))
        imp.load_source(
            os.path.splitext(os.path.basename(module_path))[0], module_path)

        # Analyze the module for its imports.
        graph = modulegraph.ModuleGraph()
        graph.run_script(module_path)

        # We do a BFS instead of checking all nodes because for some reason it is
        # possible to have bogus dependencies from the Python installation to other
        # files (which may not even exist) due to the packagepath, such as to `//-`.
        # This only appears to occur when run under Python 3. By performing BFS and
        # simply ignoring anything from the Python installation, we can avoid this
        # issue.
        nodes_to_visit = _GetSourceNodes(graph)
        visited = set()

        # Filter for only imports in Chromium.
        while nodes_to_visit:
            node = nodes_to_visit.popleft()
            if node in visited:
                continue
            visited.add(node)
            if not node.filename:
                continue
            module_path = os.path.realpath(node.filename)

            incoming_edges = graph.getReferers(node)
            message = 'Discovered %s (Imported by: %s)' % (
                node.filename, ', '.join(
                    d.filename for d in incoming_edges
                    if d is not None and d.filename is not None))
            logging.info(message)

            # This check is done after the logging/printing above to make sure that
            # we also print out the dependency edges that include python packages
            # that are not in chromium.
            if not path.IsSubpath(module_path, path_util.GetChromiumSrcDir()):
                continue

            # Exclude any dependencies which exist in the python installation.
            if any(path.IsSubpath(module_path, pfx) for pfx in prefixes):
                continue

            for outgoing_edge in graph.getReferences(node):
                nodes_to_visit.append(outgoing_edge)

            yield module_path
            if node.packagepath is not None:
                for p in node.packagepath:
                    yield p

    finally:
        sys.path = sys_path