whatthepatch


Namewhatthepatch JSON
Version 1.0.5 PyPI version JSON
download
home_page
SummaryA patch parsing and application library.
upload_time2023-05-06 15:00:05
maintainer
docs_urlNone
author
requires_python>=3.7
license
keywords patch diff parser
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            What The Patch!?
================

What The Patch!? is a library for both parsing and applying patch files.

Status
------

.. image:: https://github.com/cscorley/whatthepatch/workflows/Build/badge.svg

This has been released as 1.0, but has never had much active development. The
functions are stable and have been reliable for several years, even if they
are not ideally implemented. Pull requests will always be considered, merged,
and released; however, issues may not ever be fixed by the maintainer.

Contribute
^^^^^^^^^^

#. Fork this repository
#. Create a new branch to work on
#. Commit your tests and/or changes
#. Push and create a pull request here!

Features
--------

- Parsing of almost all ``diff`` formats (except forwarded ed):

  - normal (default, --normal)
  - copied context (-c, --context)
  - unified context (-u, --unified)
  - ed script (-e, --ed)
  - rcs ed script (-n, --rcs)

- Parsing of several SCM patches:

  - CVS
  - SVN
  - Git

Installation
------------

This library is available on `PyPI <https://pypi.org/project/whatthepatch/>`_
and can be installed via pip:

.. code-block:: bash

    $ pip install whatthepatch

Usage
=====

Let us say we have a patch file containing some changes, aptly named
'somechanges.patch':

.. code-block:: diff

    --- lao	2012-12-26 23:16:54.000000000 -0600
    +++ tzu	2012-12-26 23:16:50.000000000 -0600
    @@ -1,7 +1,6 @@
    -The Way that can be told of is not the eternal Way;
    -The name that can be named is not the eternal name.
     The Nameless is the origin of Heaven and Earth;
    -The Named is the mother of all things.
    +The named is the mother of all things.
    +
     Therefore let there always be non-being,
       so we may see their subtlety,
      And let there always be being,
    @@ -9,3 +8,6 @@
     The two are the same,
     But after they are produced,
       they have different names.
    +They both may be called deep and profound.
    +Deeper and more profound,
    +The door of all subtleties!


Parsing
-------

Here is how we would use What The Patch!? in Python to get the changeset for
each diff in the patch:

.. code-block:: python

    >>> import whatthepatch
    >>> import pprint
    >>> with open('tests/casefiles/diff-unified.diff') as f:
    ...     text = f.read()
    ...
    >>> for diff in whatthepatch.parse_patch(text):
    ...     print(diff) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
    ...
    diff(header=header(index_path=None,
                       old_path='lao',
                       old_version='2013-01-05 16:56:19.000000000 -0600',
                       new_path='tzu',
                       new_version='2013-01-05 16:56:35.000000000 -0600'),
         changes=[Change(old=1, new=None, line='The Way that can be told of is not the eternal Way;', hunk=1),
                  Change(old=2, new=None, line='The name that can be named is not the eternal name.', hunk=1),
                  Change(old=3, new=1, line='The Nameless is the origin of Heaven and Earth;', hunk=1),
                  Change(old=4, new=None, line='The Named is the mother of all things.', hunk=1),
                  Change(old=None, new=2, line='The named is the mother of all things.', hunk=1),
                  Change(old=None, new=3, line='', hunk=1),
                  Change(old=5, new=4, line='Therefore let there always be non-being,', hunk=1),
                  Change(old=6, new=5, line='  so we may see their subtlety,', hunk=1),
                  Change(old=7, new=6, line='And let there always be being,', hunk=1),
                  Change(old=9, new=8, line='The two are the same,', hunk=2),
                  Change(old=10, new=9, line='But after they are produced,', hunk=2),
                  Change(old=11, new=10, line='  they have different names.', hunk=2),
                  Change(old=None, new=11, line='They both may be called deep and profound.', hunk=2),
                  Change(old=None, new=12, line='Deeper and more profound,', hunk=2),
                  Change(old=None, new=13, line='The door of all subtleties!', hunk=2)],
         text='...')

The changes are listed as they are in the patch, but instead of the +/- syntax
of the patch, we get a tuple of two numbers and the text of the line.
What these numbers indicate are as follows:

#. ``( old=1, new=None, ... )`` indicates line 1 of the file lao was **removed**.
#. ``( old=None, new=2, ... )`` indicates line 2 of the file tzu was **inserted**.
#. ``( old=5, new=4, ... )`` indicates that line 5 of lao and line 4 of tzu are **equal**.

Please note that not all patch formats provide the actual lines modified, so some
results will have the text portion of the tuple set to ``None``.

Applying
--------

To apply a diff to some lines of text, first read the patch and parse it.

.. code-block:: python

    >>> import whatthepatch
    >>> with open('tests/casefiles/diff-default.diff') as f:
    ...     text = f.read()
    ...
    >>> with open('tests/casefiles/lao') as f:
    ...     lao = f.read()
    ...
    >>> diff = [x for x in whatthepatch.parse_patch(text)]
    >>> diff = diff[0]
    >>> tzu = whatthepatch.apply_diff(diff, lao)
    >>> tzu  # doctest: +NORMALIZE_WHITESPACE
    ['The Nameless is the origin of Heaven and Earth;',
     'The named is the mother of all things.',
     '',
     'Therefore let there always be non-being,',
     '  so we may see their subtlety,',
     'And let there always be being,',
     '  so we may see their outcome.',
     'The two are the same,',
     'But after they are produced,',
     '  they have different names.',
     'They both may be called deep and profound.',
     'Deeper and more profound,',
     'The door of all subtleties!']

If apply does not satisfy your needs and you are on a system that has
``patch`` in ``PATH``, you can also call ``apply_diff(diff, lao,
use_patch=True)``. The default is false, and patch is not necessary to apply
diffs to text.


            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "whatthepatch",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "\"Christopher S. Corley\" <cscorley@gmail.com>",
    "keywords": "patch,diff,parser",
    "author": "",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/22/5a/794d55721aa8414e617c431ec469dc0df3de74c7e28d44dd31ff7aa56f7d/whatthepatch-1.0.5.tar.gz",
    "platform": null,
    "description": "What The Patch!?\n================\n\nWhat The Patch!? is a library for both parsing and applying patch files.\n\nStatus\n------\n\n.. image:: https://github.com/cscorley/whatthepatch/workflows/Build/badge.svg\n\nThis has been released as 1.0, but has never had much active development. The\nfunctions are stable and have been reliable for several years, even if they\nare not ideally implemented. Pull requests will always be considered, merged,\nand released; however, issues may not ever be fixed by the maintainer.\n\nContribute\n^^^^^^^^^^\n\n#. Fork this repository\n#. Create a new branch to work on\n#. Commit your tests and/or changes\n#. Push and create a pull request here!\n\nFeatures\n--------\n\n- Parsing of almost all ``diff`` formats (except forwarded ed):\n\n  - normal (default, --normal)\n  - copied context (-c, --context)\n  - unified context (-u, --unified)\n  - ed script (-e, --ed)\n  - rcs ed script (-n, --rcs)\n\n- Parsing of several SCM patches:\n\n  - CVS\n  - SVN\n  - Git\n\nInstallation\n------------\n\nThis library is available on `PyPI <https://pypi.org/project/whatthepatch/>`_\nand can be installed via pip:\n\n.. code-block:: bash\n\n    $ pip install whatthepatch\n\nUsage\n=====\n\nLet us say we have a patch file containing some changes, aptly named\n'somechanges.patch':\n\n.. code-block:: diff\n\n    --- lao\t2012-12-26 23:16:54.000000000 -0600\n    +++ tzu\t2012-12-26 23:16:50.000000000 -0600\n    @@ -1,7 +1,6 @@\n    -The Way that can be told of is not the eternal Way;\n    -The name that can be named is not the eternal name.\n     The Nameless is the origin of Heaven and Earth;\n    -The Named is the mother of all things.\n    +The named is the mother of all things.\n    +\n     Therefore let there always be non-being,\n       so we may see their subtlety,\n      And let there always be being,\n    @@ -9,3 +8,6 @@\n     The two are the same,\n     But after they are produced,\n       they have different names.\n    +They both may be called deep and profound.\n    +Deeper and more profound,\n    +The door of all subtleties!\n\n\nParsing\n-------\n\nHere is how we would use What The Patch!? in Python to get the changeset for\neach diff in the patch:\n\n.. code-block:: python\n\n    >>> import whatthepatch\n    >>> import pprint\n    >>> with open('tests/casefiles/diff-unified.diff') as f:\n    ...     text = f.read()\n    ...\n    >>> for diff in whatthepatch.parse_patch(text):\n    ...     print(diff) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE\n    ...\n    diff(header=header(index_path=None,\n                       old_path='lao',\n                       old_version='2013-01-05 16:56:19.000000000 -0600',\n                       new_path='tzu',\n                       new_version='2013-01-05 16:56:35.000000000 -0600'),\n         changes=[Change(old=1, new=None, line='The Way that can be told of is not the eternal Way;', hunk=1),\n                  Change(old=2, new=None, line='The name that can be named is not the eternal name.', hunk=1),\n                  Change(old=3, new=1, line='The Nameless is the origin of Heaven and Earth;', hunk=1),\n                  Change(old=4, new=None, line='The Named is the mother of all things.', hunk=1),\n                  Change(old=None, new=2, line='The named is the mother of all things.', hunk=1),\n                  Change(old=None, new=3, line='', hunk=1),\n                  Change(old=5, new=4, line='Therefore let there always be non-being,', hunk=1),\n                  Change(old=6, new=5, line='  so we may see their subtlety,', hunk=1),\n                  Change(old=7, new=6, line='And let there always be being,', hunk=1),\n                  Change(old=9, new=8, line='The two are the same,', hunk=2),\n                  Change(old=10, new=9, line='But after they are produced,', hunk=2),\n                  Change(old=11, new=10, line='  they have different names.', hunk=2),\n                  Change(old=None, new=11, line='They both may be called deep and profound.', hunk=2),\n                  Change(old=None, new=12, line='Deeper and more profound,', hunk=2),\n                  Change(old=None, new=13, line='The door of all subtleties!', hunk=2)],\n         text='...')\n\nThe changes are listed as they are in the patch, but instead of the +/- syntax\nof the patch, we get a tuple of two numbers and the text of the line.\nWhat these numbers indicate are as follows:\n\n#. ``( old=1, new=None, ... )`` indicates line 1 of the file lao was **removed**.\n#. ``( old=None, new=2, ... )`` indicates line 2 of the file tzu was **inserted**.\n#. ``( old=5, new=4, ... )`` indicates that line 5 of lao and line 4 of tzu are **equal**.\n\nPlease note that not all patch formats provide the actual lines modified, so some\nresults will have the text portion of the tuple set to ``None``.\n\nApplying\n--------\n\nTo apply a diff to some lines of text, first read the patch and parse it.\n\n.. code-block:: python\n\n    >>> import whatthepatch\n    >>> with open('tests/casefiles/diff-default.diff') as f:\n    ...     text = f.read()\n    ...\n    >>> with open('tests/casefiles/lao') as f:\n    ...     lao = f.read()\n    ...\n    >>> diff = [x for x in whatthepatch.parse_patch(text)]\n    >>> diff = diff[0]\n    >>> tzu = whatthepatch.apply_diff(diff, lao)\n    >>> tzu  # doctest: +NORMALIZE_WHITESPACE\n    ['The Nameless is the origin of Heaven and Earth;',\n     'The named is the mother of all things.',\n     '',\n     'Therefore let there always be non-being,',\n     '  so we may see their subtlety,',\n     'And let there always be being,',\n     '  so we may see their outcome.',\n     'The two are the same,',\n     'But after they are produced,',\n     '  they have different names.',\n     'They both may be called deep and profound.',\n     'Deeper and more profound,',\n     'The door of all subtleties!']\n\nIf apply does not satisfy your needs and you are on a system that has\n``patch`` in ``PATH``, you can also call ``apply_diff(diff, lao,\nuse_patch=True)``. The default is false, and patch is not necessary to apply\ndiffs to text.\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A patch parsing and application library.",
    "version": "1.0.5",
    "project_urls": {
        "Bug Tracker": "https://github.com/cscorley/whatthepatch/issues",
        "Homepage": "https://github.com/cscorley/whatthepatch"
    },
    "split_keywords": [
        "patch",
        "diff",
        "parser"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3271fb2edaee7c28ec6e22b7775a2606517f70e1f0af4d8a9fc333b026725d99",
                "md5": "b17765d00a188b5d812df5841ef06251",
                "sha256": "6bc41f9f48a63384be4478d8b2d5b22185aac75be853cdcb150a2dc174ede7e1"
            },
            "downloads": -1,
            "filename": "whatthepatch-1.0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b17765d00a188b5d812df5841ef06251",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 11963,
            "upload_time": "2023-05-06T15:00:03",
            "upload_time_iso_8601": "2023-05-06T15:00:03.067463Z",
            "url": "https://files.pythonhosted.org/packages/32/71/fb2edaee7c28ec6e22b7775a2606517f70e1f0af4d8a9fc333b026725d99/whatthepatch-1.0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "225a794d55721aa8414e617c431ec469dc0df3de74c7e28d44dd31ff7aa56f7d",
                "md5": "194d8e127240c26db79386ebdc99e8a5",
                "sha256": "7f374c172812581bc3763587525d14a143aac7fe4220bc4676ecce0d86cb8f08"
            },
            "downloads": -1,
            "filename": "whatthepatch-1.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "194d8e127240c26db79386ebdc99e8a5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 31850,
            "upload_time": "2023-05-06T15:00:05",
            "upload_time_iso_8601": "2023-05-06T15:00:05.171471Z",
            "url": "https://files.pythonhosted.org/packages/22/5a/794d55721aa8414e617c431ec469dc0df3de74c7e28d44dd31ff7aa56f7d/whatthepatch-1.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-06 15:00:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "cscorley",
    "github_project": "whatthepatch",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "whatthepatch"
}
        
Elapsed time: 0.06218s