File: example.py

package info (click to toggle)
python-patch-ng 1.17.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 792 kB
  • sloc: python: 2,124; cpp: 1,598; xml: 38; sh: 11; makefile: 5
file content (42 lines) | stat: -rw-r--r-- 828 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import logging
from patch import fromfile, fromstring

class PatchLogHandler(logging.Handler):
    def __init__(self):
        logging.Handler.__init__(self, logging.DEBUG)

    def emit(self, record):
        logstr = self.format(record)
        print logstr

patchlog = logging.getLogger("patch")
patchlog.handlers = []
patchlog.addHandler(PatchLogHandler())

patch = fromstring("""--- /dev/null
+++ b/newfile
@@ -0,0 +0,3 @@
+New file1
+New file2
+New file3
""")

patch.apply(root=os.getcwd(), strip=0)


with open("newfile", "rb") as f:
    newfile = f.read()
    assert "New file1\nNew file2\nNew file3\n" == newfile

patch = fromstring("""--- a/newfile
+++ /dev/null
@@ -0,3 +0,0 @@
-New file1
-New file2
-New file3
""")

result = patch.apply(root=os.getcwd(), strip=0)

assert os.path.exists("newfile") is False