Installation¶
(requires git cli to be installed) .. code-block:: bash
$ python -m pip install ptcx
Usage¶
usage: python -m ptcx [-h] [--srcroot [SRCROOT]] [--patchroot [PATCHROOT]]
[--reset]
[path]
A format for modularized AST-based patching of arbitary code
positional arguments:
path Relative path from patchroot to patch
options:
-h, --help show this help message and exit
--srcroot [SRCROOT], --src [SRCROOT]
Source code directory to patch
--patchroot [PATCHROOT], --patch [PATCHROOT]
directory where patches are placed.
--reset Resevert all uncommited changes within git repository
in src
Or over python:
- ptcx.patch.path(_path='', srcroot='./src', patchroot='./patch')View on GitHub ¶
Apply patch to src
- ptcx.patch.reset(srcroot='./src')View on GitHub ¶
Resevert all uncommited changes within git repository in src
- Parameters:
srcroot (
PathLike
)
Patch configuration¶
from ptcx import BasePTC
import re
class PTC(BasePTC):
def patch(self):
self.sub(r"return res \+ 1", "return res")
tree = self.tree
print(self.file)
Language support¶
Language |
python package |
---|---|
cpp |
|
python |
Reference¶
- class ptcx.BasePTC(file, srcroot, patchroot)View on GitHub ¶
-
- property bytes: bytes View on GitHub ¶
File to patch as bytes based on
ptcx.BasePTC.file()
. Setting it to str will automatically convert it to bytes (utf-8 encoded)
- property langView on GitHub ¶
(programming) Language of the file to patch based on
ptcx.BasePTC.file()
and based onptcx.BasePTC.bytes()
.
- property parser: Parser View on GitHub ¶
”
tree_sitter.Parser
based onptcx.BasePTC.lang()
- property tree: Tree View on GitHub ¶
”
tree_sitter.Tree
based onptcx.BasePTC.parser()
.This is automatically updated when
- abstractmethod patch()View on GitHub ¶
function to implement for patching
- insert(pattern, insert_func)View on GitHub ¶
Wraps
ptcx.utils.langs.search_and_insert()
forptcx.BasePTC.bytes()
.
- sub(pattern, repl, **kwargs)View on GitHub ¶
- rplace_fn(function)View on GitHub ¶
Replace function (based on signature) on
ptcx.BasePTC.tree()
Warning
This only supports C++ with tree-sitter-cpp yet