Commit Graph

212 Commits (master)

Author SHA1 Message Date
Alex Willmer 5ad3d14ceb mitogen: Support PEP 451 ModuleSpec API, required for Python 3.12
importlib.machinery.ModuleSpec and find_spec() were introduced in Python 3.4
under PEP 451. They replace the find_module() API of PEP 302, which was
deprecated from Python 3.4. They were removed in Python 3.12 along with the
imp module.

This change adds support for the PEP 451 APIs. Mitogen should no longer import
imp on Python versions that support ModuleSpec. Tests have been added to cover
the new APIs.

CI jobs have been added to cover Python 3.x on macOS.

Refs #1033
Co-authored-by: Witold Baryluk <witold.baryluk@gmail.com>
2 months ago
Alex Willmer fc3e788cb4 non functional: Add comments about imp module removal in Python 3.12 4 months ago
Alex Willmer ad4b686836 master.PkgutilMethod: Skip module loaders that raise ValueError
e.g. in Ansible 6, ansible-core 2.13

```
[mux  2717] 23:39:11.342416 D mitogen: PkgutilMethod(): _AnsibleCollectionLoader(path=None).get_file_name('ansible.plugins') failed: ValueError('_AnsibleCollectionLoader(path=None) cannot find files for ansible.plugins, only ansible_collections.ansible.builtin.plugins')
```
2 years ago
Alex Willmer 65809a6f0f mitogen: Handle Python 3.10 threading depreactions 2 years ago
Alex Willmer d2ca8a9423 master.ParentEnumerationMethod: Require matching pkg.__name__
Co-authored-by: Stefano Rivera <stefano@rivera.za.net>

When the requested module (e.g. ansible.module_utils.distro)
- is provided by another module *e.g. distro)
- that itself was a package (e.g. distro 1.7.0)

At runtime
- ansible/module_utils/distro/__init__.py executes
- if https://pypi.org/project/distro/ is present, it's loaded as
ansible.module_utils.distro
- otherwise ansible/module_utils/distro/_distro.py is loaded

ParentEnumerationMethod would wrongly use whatever was in
sys.modules['ansible.module_utils.distro]. Instead we should ascend to
the first parent that has fullname == sys.modules[fullname].__name__.
Then descend to the appropriate .py file on disk.

This bug didn't show up before because until distro 1.7.0 (Feb 2022) the
top-level distro module was a module (distro.py) not a package
(distro/__init__.py)

fixes #906
2 years ago
Alex Willmer 47699e15aa master.SysModulesMethod: log rejection reasons 2 years ago
Alex Willmer 0fa0a93f55 master.PkgutilMethod: log rejection reasons 2 years ago
Alex Willmer 60c4ae5599
Add notes on imports and importlib 2 years ago
Alex Willmer 9a19f0ed16 mitogen.master: Fix stdlib discovery when sys.prefix contains symlinks
This can be he case when Python has been installed with Homebrew.
2 years ago
Steven Robertson 4657979210 adding clarifying comments 3 years ago
Steven Robertson 33e176d62e add support for ansible_collections site-package (from pip ansible==2.10.0 install) + switch to ansible 2.10.0 rather than github tag 4 years ago
Steven Robertson 1ffb319300 remove debugging 4 years ago
Steven Robertson 207e36194d try and get some visibility into test failures 4 years ago
Steven Robertson 5b3d90dac4 see if sys.path is being loaded properly on azure 4 years ago
Steven Robertson ff8a276186 turn off failing Ansible-only tests for now, also raising errors to see what Azure is gonna do with collections 4 years ago
Steven Robertson 02aedcdacd fix issue with collections module_utils raised in alikins example 4 years ago
Steven Robertson 19f92a2435 fix inefficient basepath check 4 years ago
Steven Robertson 5215646c8a code cleanup 4 years ago
Steven Robertson 0b421e0d3c able to run docker_container installed via 'ansible-galaxy collection install community.general' 4 years ago
Steven Robertson 45797a0d34 able to send collections paths to master with very little change to core Mitogen, need to ensure imports work properly now though 4 years ago
Steven Robertson 583f540889 added comments 4 years ago
Steven Robertson 376d8d0fab remove old hacks; ansible_collections is available at time of invoker but not later 4 years ago
Steven Robertson 1ef96b5827 normal search-jump-around-code-easily word exists in the code already so switching to jjj 4 years ago
Steven Robertson afd8902d6b hackily sorta got imp.find_module to load module_utils but it breaks further package discovery, needs fix 4 years ago
Steven Robertson 955e77c5db WIP: able to load subdirs but now need to treat them as submodules properly 4 years ago
Steven Robertson 4b42dd1434 able to load things from sys.modules but not everything loads that way 4 years ago
Steven Robertson 3dbfe085ab close-ish, getting 'ansible_collections has no submodule alikins' though because of empty submodules 4 years ago
Steven Robertson 05f74e3b30 since correctly detecting python3 now, makes 'def disconnect_all()' python3-safe 4 years ago
David Wilson bdf6f1b9a9 issue #590: rework ParentEnumerationMethod to recursively handle bad modules
In the worst case it will start with sys.path and resolve everything
from scratch.
5 years ago
David Wilson 3b63da670f Fix up another handful of LGTM errors. 5 years ago
David Wilson 57012e0f72 Add mitogen.core.now() and use it everywhere; closes #614. 5 years ago
David Wilson 93e8d5dfcc docs: fix Sphinx warnings, add LogHandler, more docstrings 5 years ago
Marc Hartmayer 2ed8395d6c master: fix TypeError
Add a guard for the case `path == None`.

This commit fixes

`TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType`
5 years ago
David Wilson a79d2bd50b docs: another round of docstring cleanups. 5 years ago
David Wilson 20532ea591 master: allow filtering forwarded logs using logging package functions.
Given a message sent on "ssh.foo" to "mypkg.mymod", instead of logging
it to "mitogen.ctx.ssh.foo" in the master process, with the message
prefixed with the original logger name, instead log it to
"mypkg.mymod.[ssh.foo]", permitting normal logging package filtering
features to work as they usually do.

This also helps tidy up logging output a little bit.
5 years ago
David Wilson feb1654305 docs: many more internals.rst tidyups 5 years ago
David Wilson 5298e87548 Split out and make readable more log messages across both packages 5 years ago
David Wilson 93342ba60c Normalize docstring formatting 5 years ago
David Wilson 93abbcaf7a [stream-refactor] fix crash in runner/forking_active.yml 5 years ago
David Wilson 8d1b01d8ef Refactor Stream, introduce quasi-asynchronous connect, much more
Split Stream into many, many classes

  * mitogen.parent.Connection: Handles connection setup logic only.
    * Maintain references to stdout and stderr streams.
    * Manages TimerList timer to cancel connection attempt after
      deadline
    * Blocking setup code replaced by async equivalents running on the
      broker

  * mitogen.parent.Options: Tracks connection-specific options. This
    keeps the connection class small, but more importantly, it is
    generic to the future desire to build and execute command lines
    without starting a full connection.

  * mitogen.core.Protocol: Handles program behaviour relating to events
    on a stream. Protocol performs no IO of its own, instead deferring
    it to Stream and Side. This makes testing much easier, and means
    libssh can reimplement Stream and Side to reuse MitogenProtocol

  * mitogen.core.MitogenProtocol: Guts of the old Mitogen stream
    implementtion

  * mitogen.core.BufferedWriter: Guts of the old Mitogen buffered
    transmit implementation, made generic

  * mitogen.core.DelineatedProtocol: Guts of the old IoLogger, knows how
    to split up input and pass it on to a
    on_line_received()/on_partial_line_received() callback.

  * mitogen.parent.BootstrapProtocol: Asynchronous equivalent of the old
    blocking connect code. Waits for various prompts (MITO001 etc) and
    writes the bootstrap using a BufferedWriter. On success, switches
    the stream to MitogenProtocol.

  * mitogen.core.Message: move encoding parts of MitogenProtocol out to
    Message (where it belongs) and write a bunch of new tests for
    pickling.

  * The bizarre Stream.construct() is gone now, Option.__init__ is its
    own constructor. Should fix many LGTM errors.

* Update all connection methods:  Every connection method is updated to
  use async logic, defining protocols as required to handle interactive
  prompts like in SSH or su. Add new real integration tests for at least
  doas and su.

* Eliminate manual fd management: File descriptors are trapped in file
  objects at their point of origin, and Side is updated to use file
  objects rather than raw descriptors. This eliminates a whole class of
  bugs where unrelated FDs could be closed by the wrong component. Now
  an FD's open/closed status is fused to it everywhere in the library.

* Halve file descriptor usage: now FD open/close state is tracked by
  its file object, we don't need to duplicate FDs everywhere so that
  receive/transmit side can be closed independently. Instead both sides
  back on to the same file object. Closes #26, Closes #470.

* Remove most uses of dup/dup2: Closes #256. File descriptors are
  trapped in a common file object and shared among classes. The
  remaining few uses for dup/dup2 are as close to minimal as possible.

* Introduce mitogen.parent.Process: uniform interface for subprocesses
  created either via mitogen.fork or the subprocess module. Remove all
  the crap where we steal a pid from subprocess guts. Now we use
  subprocess to manage its processes as it should be. Closes #169 by
  using the new Timers facility to poll for a slow-to-exit subprocess.

* Fix su password race: Closes #363. DelineatedProtocol naturally
  retries partially received lines, preventing the cause of the original
  race.

* Delete old blocking IO utility functions
  iter_read()/write_all()/discard_until().

Closes #26
Closes #147
Closes #169
Closes #256
Closes #363
Closes #419
Closes #470
5 years ago
David Wilson 46ebd56c7a core/master: docstring, repr, and debug log message cleanups
Debug output is vastly more readable now.
5 years ago
David Wilson 3f1ef6e243 master: expect forwarded logs to be in UTF-8.
latin1 was causing corruption of internationalized messages.
5 years ago
David Wilson 2fbc77a155 issue #170: implement timers. 5 years ago
David Wilson ed8acb5153 master: sysconfig did not exist until 2.7. 5 years ago
David Wilson 5eb10aacef master: fix _is_stdlib_path() failure on Ubuntu. 5 years ago
David Wilson 72ab917c89 issue #590: add FinderMethod docstrings. 5 years ago
David Wilson 875ff5c060 issue #590: refactor ModuleFinder and teach it a new special case.
Now it's possible to find both packages and modules when the
sys.modules[...] state for the package/module is junk. Previously only
modules were possible.

This also refactors things to make writing better tests for all these
cases much simpler.
5 years ago
David Wilson 1f77d24bec Update copyright year everywhere. 5 years ago
David Wilson 7ff4e6694c issue #536: rework how 2.3-compatible simplejson is served
Regardless of the version of simplejson loaded in the master, load up
the ModuleResponder cache with our 2.4-compatible version.

To cope with simplejson being loaded due to modules like ec2_group that
try to import it before importing 'json', also update target.py to
remove it from the whitelist if a local 'json' module import succeeds.
5 years ago
David Wilson b263e01867 issue #481: avoid crash if disconnect occurs during forward_modules() 5 years ago