补丁解析库。

whatthepatch-graingert的Python项目详细描述


什么补丁!?

https://travis-ci.org/cscorley/whatthepatch.svg?style=flat

什么补丁!?是一个用于分析和应用修补程序文件的库。

功能

  • 几乎所有diff格式的解析(转发的ed除外):
    • 正常(默认,–正常)
    • 复制的上下文(-c,–context)
    • 统一上下文(-u,–统一)
    • 编辑脚本(-e,–ed)
    • rcs ed脚本(-n,–rcs)
  • 多个单片机补丁解析:
    • 简历
    • SVN
    • 吉特

安装

安装什么补丁!?,只需:

$ pip install whatthepatch

用法

假设我们有一个补丁文件,其中包含一些更改,恰当地命名为 'somechanges.patch':

--- 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!

解析

下面是我们将如何使用什么补丁!?在python中获取 修补程序中的每个差异:

>>>importwhatthepatch>>>importpprint>>>withopen('tests/casefiles/diff-unified.diff')asf:...text=f.read()...>>>fordiffinwhatthepatch.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,hunk=1,line='The Way that can be told of is not the eternal Way;'),Change(old=2,new=None,hunk=1,line='The name that can be named is not the eternal name.'),Change(old=3,new=1,hunk=1,line='The Nameless is the origin of Heaven and Earth;'),Change(old=4,new=None,hunk=1,line='The Named is the mother of all things.'),Change(old=None,new=2,hunk=1,line='The named is the mother of all things.'),Change(old=None,new=3,hunk=1,line=''),Change(old=5,new=4,hunk=1,line='Therefore let there always be non-being,'),Change(old=6,new=5,hunk=1,line='  so we may see their subtlety,'),Change(old=7,new=6,hunk=1,line='And let there always be being,'),Change(old=9,new=8,hunk=2,line='The two are the same,'),Change(old=10,new=9,hunk=2,line='But after they are produced,'),Change(old=11,new=10,hunk=2,line='  they have different names.'),Change(old=None,new=11,hunk=2,line='They both may be called deep and profound.'),Change(old=None,new=12,hunk=2,line='Deeper and more profound,'),Change(old=None,new=13,hunk=2,line='The door of all subtleties!')],text='...')

这些更改按补丁中的方式列出,但不使用+/-语法 在补丁中,我们得到一个由两个数字和行的文本组成的元组。 这些数字表示如下:

  1. ( old=1, new=None, ... )表示lao文件的第1行已被删除
  2. ( old=None, new=2, ... )表示文件子的第2行已插入
  3. ( old=5, new=4, ... )表示lao的第5行和zi的第4行是相等的

请注意,并非所有的修补程序格式都提供了修改过的实际行,因此有些 结果将元组的文本部分设置为None

应用

若要对某些文本行应用diff,请首先读取修补程序并对其进行解析。

>>>importwhatthepatch>>>withopen('tests/casefiles/diff-default.diff')asf:...text=f.read()...>>>withopen('tests/casefiles/lao')asf:...lao=f.read()...>>>diff=[xforxinwhatthepatch.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!']

贡献

  1. 分叉此存储库
  2. 创建要处理的新分支
  3. 提交测试和/或更改
  4. 在此处按下并创建拉取请求!

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
HTTP标头的java InputStream未终止   java测试SpringWebListener   具有定义的替换编号Java的regex ReplaceAll   在java中使用contains()方法   java在选择查询中使用JoinColumn字段   具有用户的java Start-stop demon不是以给定用户启动   java glBufferData生成GL\u无效\u操作   java中循环代码的循环   Java位无符号移位(>>>>)会产生奇怪的结果   java HQL使用点分隔符从select获取结果   条纹、弹簧、玩耍(或?):使用哪种高性能Java框架?   广播接收机中的java停止服务   java回收器视图占据整个屏幕。不在上面显示我的UI元素   java使MySQL查询更快   java MappedByteBuffer查询   java递归算法问题