This module defines ModuleGraph, which is used to find the dependencies of scripts using bytecode analysis.
A number of APIs in this module refer to filesystem path. Those paths can refer to files inside zipfiles (for example when there are zipped egg files on sys.path). Filenames referring to entries in a zipfile are not marked any way, if "somepath.zip" refers to a zipfile, that is "somepath.zip/embedded/file" will be used to refer to embedded/file inside the zipfile.
Create a new ModuleGraph object. Use the run_script() method to add scripts, and their dependencies to the graph.
Parameters: |
|
---|
Create, and return, a node by path (not module name). The pathname should refer to a Python source file and will be scanned for dependencies.
The optional argument caller is the the node that calls this script, and is used to add a reference in the graph.
Import a module and analyse its dependencies
Parameters: |
|
---|
Explictly mark that node depends on other. Other is either a node or the name of a module that will be searched for as if it were an absolute import.
Create a reference from fromnode to tonode, with optional edge data.
The default for edge_data is "direct".
Find a node by identifier. If a node by that identifier exists, it will be returned.
If a lazy node exists by that identifier with no dependencies (excluded), it will be instantiated and returned.
If a lazy node exists by that identifier with dependencies, it and its dependencies will be instantiated and scanned for additional depende
Write an HTML file to the out stream (defaulting to sys.stdout).
The HTML file contains a textual description of the dependency graph.
Todo
To be documented
Print a report to stdout, listing the found modules with their paths, as well as modules that are missing, or seem to be missing.
The methods in this section should be considered as methods for subclassing at best, please let us know if you need these methods in your code as they are on track to be made private methods before the 1.0 release.
Warning
The methods in this section will be refactored in a future release, the current architecture makes it unnecessarily hard to write proper tests.
Returns the node of the package root voor caller. If caller is a package this is the node itself, if the node is a module in a package this is the node of for the package and otherwise the caller is not a package and the result is None.
Todo
To be documented
This method is called to load the rest of a dotted name after loading the root of a package. This will import all intermediate modules as well (using import_module()), and returns the module node for the requested node.
Note
When tail is empty this will just return mod.
Parameters: |
|
---|---|
Raises ImportError: | |
When the requested (or one of its parents) module cannot be found |
|
Returns: | the requested module |
Yield all submodules that would be imported when importing fromlist from m (using from m import fromlist...).
m must be a package and not a regular module.
Yield the filenames for submodules of in the same package as m.
Perform import of the module with basename partname (path) and full name fqname (os.path). Import is performed by parent.
This will create a reference from the parent node to the module node and will load the module node when it is not already loaded.
Load the module named fqname from the given pathame. The argument fp is either None, or a stream where the code for the Python module can be loaded (either byte-code or the source code). The (suffix, mode, type) tuple are the suffix of the source file, the open mode for the file and the type of module.
Creates a node of the right class and processes the dependencies of the node by scanning the byte-code for the node.
Returns the resulting node.
Scan the code object for module m and update the dependencies of m using the import statemets found in the code.
This will automaticly scan the code for nested functions, generator expressions and list comprehensions as well.
Load a package directory.
Locates a module named name that is not yet part of the graph. This method will raise ImportError when the module cannot be found or when it is already part of the graph. The name can not be a dotted name.
The path is the search path used, or None to use the default path.
When the parent is specified name refers to a subpackage of parent, and path should be the search path of the parent.
Returns the result of the global function find_module.
Todo
To be documented
Replace the filenames in code object co using the replace_paths value that was passed to the contructor. Returns the rewritten code object.
Returns a mapping from package name to a list of paths where that package can be found in --single-version-externally-managed form.
This method is used to be able to find those packages: these use a magic .pth file to ensure that the package is added to sys.path, as they do not contain an ___init__.py file.
Packages in this form are used by system packages and the “pip” installer.
The ModuleGraph contains nodes that represent the various types of modules.
This is a subclass of string that is used to mark module aliases.
Base class for nodes, which provides the common functionality.
Nodes can by used as mappings for storing arbitrary data in the node.
Nodes are compared by comparing their identifier.
Debug level (integer)
The node identifier, this is the value of the identifier argument to the constructor.
The node identifier, this is the value of the identifier argument to the constructor.
The filename associated with this node.
The value of __path__ for this node.
The code object associated with this node
The set of global names that are assigned to in this module. This includes those names imported through startimports of Python modules.
The set of startimports this module did that could not be resolved, ie. a startimport from a non-Python module.
Return if there is a value associated with name.
This method is usually accessed as name in aNode.
Set the value of name to value.
This method is usually accessed as aNode[name] = value.
Returns the value of name, raises KeyError when it cannot be found.
This method is usually accessed as value = aNode[name].
Returns the value of name, or the default value when it cannot be found. The default is None when not specified.
Returns a tuple with information used in the repr() output for the node. Subclasses can add additional informations to the result.
A node that represents an alias from a name to another node.
The value of attribute graphident for this node will be the value of name, the other Node attributed are references to those attributed in node.
Base class for nodes that should be ignored for some reason
A module that is explicitly excluded.
A module that is imported but cannot be located.
A python script.
The filename for the script
The base class for actual modules. The name is the possibly dotted module name, filename is the filesystem path to the module and path is the value of __path__ for the module.
The name of the module
The filesystem path to the module.
The value of __path__ for this module.
A built-in module (on in sys.builtin_module_names).
A module for which the python source code is available.
A module for which only byte-code is available.
Represents a python package
A native extension
Warning
A number of other node types are defined in the module. Those modules aren’t used by modulegraph and will be removed in a future version.
A version of imp.find_module() that works with zipped packages (and other PEP 302 importers).
Return the module name, readmode and type for the file at path, or None if it doesn’t seem to be a valid module (based on its name).
Add path to the value of __path__ for the package named packagename.
Rename oldname to newname when it is found by the module finder. This is used as a workaround for the hack that the _xmlplus package uses to inject itself in the xml namespace.