补丁解析库。

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第三方库


热门话题
java Eclipse无法识别jsonsimple   linux Tomcat是否在符号链接/usr/bin/java下使用java?   使用Swing工具包构建Java GUI   java Subversive SVN只检出丢失的文件   java语言在没有显式类型转换的情况下能否“完整”?   Selenium>Webdriver>Java>单击模型窗口中的按钮时,另一个后台按钮正在触发   Java私有视频YouTube   JavaScriptJava。lang.NullPointerException servlet   java More Mockito返回\u DEEP\u STUBS悲哀   迭代器Java,谷歌收藏库;AbstractIterator有问题吗?   使用java在flash网页中获取验证码会话的http图像   sql从java向ms access插入数据   java Android WebView不加载URL?   制作java自定义注释以标记用于html清理的bean属性   java如何在Jframe中输出结果