Installation

Source code

(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

Parameters:
ptcx.patch.reset(srcroot='./src')View on GitHub

Resevert all uncommited changes within git repository in src

Parameters:

srcroot (PathLike )

Patch configuration

Example ptcx file: patch/main.py.ptcx
Extra file to add: patch/extra.py
Source file to patch: src/main.py
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

tree-sitter-python

python

tree-sitter-cpp

Reference

class ptcx.BasePTC(file, srcroot, patchroot)View on GitHub
Parameters:
srcroot: Path

source root directory to patch

patchroot: Path

patch root directory, used for specifying patches

file: Path

absolute path of the file to patch

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 on ptcx.BasePTC.bytes().

property parser: Parser View on GitHub

tree_sitter.Parser based on ptcx.BasePTC.lang()

property tree: Tree View on GitHub

tree_sitter.Tree based on ptcx.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() for ptcx.BasePTC.bytes().

Parameters:
sub(pattern, repl, **kwargs)View on GitHub

re.sub() on ptcx.BasePTC.bytes()

Parameters:
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

Parameters:

function (bytes)

Return type:

bytes