A tool for applying monkeypatches to python executables.
Project description
pymonkey
A tool for applying monkeypatches to python executables.
Installation
pip install pymonkey
Registering a patch
Make a module:
## mymod/pymonkey.py
# Be sure to not import anything at the module scope
# This ensures that import-hook patching will work better later
# This is your chance to do argument parsing before running the target
# executable.
# This will be called after all of the patch hooks have been registered.
def pymonkey_argparse(argv):
# You'll be passed the arguments as a tuple. Parse your specific arguments
# and return your parsed state and the remaining arguments.
# If you wish to forgo argparsing, simply `return None, argv`
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo')
return parser.parse_known_args(argv)
# This is your entry point. It will be passed module objects after they have
# been imported. Check the module name and then apply your patch (if
# applicable).
# This will be called as a post-import hook so you'll have a chance to modify
# the module before another module would import from it.
# The parsed state computed above will be passed as `args`
def pymonkey_patch(mod, args):
# This callback will be called with *every* module that gets imported
# Guard against the specific module you'd like to patch
if mod.__name__ != 'module_to_patch':
return
# Apply your patches here to module_to_patch
mod.foo = args.foo
And add the entrypoints to setup.py:
setup(
...,
entry_points={
'pymonkey': ['mymod = mymod.pymonkey:pymonkey_patch'],
'pymonkey.argparse': ['mymod = mymod.pymonkey:pymonkey_argparse'],
},
...
)
Commandline usage
Applying a single patch:
$ pymonkey mymod -- pip install simplejson
Applying all the patches available:
$ pymonkey --all -- pip install simplejson
Viewing the help
$ pymonkey --help
Making entry points with pymonkey
In a module:
## mymod_main.py
from pymonkey import make_entry_point
# The first argument is a list of pymonkey patches to apply
# The second argument is the entry point to run
main = make_entry_point(('mymod',), 'pip')
if __name__ == '__main__':
exit(main())
In setup.py
setup(
...,
entry_points={
'console_scripts': ['pip-patched = mymod_main:main'],
'pymonkey': ['mymod = mymod.pymonkey:pymonkey_patch'],
'pymonkey.argparse': ['mymod = mymod.pymonkey:pymonkey_argparse'],
},
...
)
Then instead of
$ pymonkey mymod -- pip ...
You can now do
$ pip-patched ...
Things using pymonkey
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pymonkey-0.3.0.tar.gz
(6.1 kB
view hashes)
Built Distribution
Close
Hashes for pymonkey-0.3.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c316df41ac2f936bb54fab18b3663985a212debba0dffa4041dd09c9d972e6f3 |
|
MD5 | 0eb5a61834995f604a7c15c0fb86b8b4 |
|
BLAKE2b-256 | daa91a74cf32dfb17eb7db7941174ab2c47d54b2d1f018dbd02ae7de9d2047b7 |