Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/extractcode/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
LOG = logging.getLogger(__name__)


def _as_text(line):
if not isinstance(line, bytes):
return line
try:
return line.decode("utf-8")
except UnicodeDecodeError:
return line.decode("latin-1")


def extract(location, target_dir):
"""
Extract each patch of a patch file at `location` as files in a target_dir
Expand Down Expand Up @@ -74,7 +83,7 @@ def extract(location, target_dir):
with open(subfile_path, "w") as subfile:
subfile.write("\n".join(text))

return []
return []


def is_patch(location, include_extracted=False):
Expand Down Expand Up @@ -140,5 +149,5 @@ def patch_info(location):
for ptch in patchset.items:
src = fileutils.as_posixpath(ptch.source.strip())
tgt = fileutils.as_posixpath(ptch.target.strip())
text = [l.strip() for l in patch_text(ptch) if l]
yield src, tgt, text
lines = [_as_text(line).strip() for line in patch_text(ptch) if line]
yield src, tgt, lines
20 changes: 20 additions & 0 deletions tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1615,3 +1615,23 @@ def test_patch_info_patch_patches_xvidcap_xorg_patch(self):
test_file = self.get_test_loc("patch/patches/xvidcap-xorg.patch")
expected_file = self.get_test_loc("patch/patches/xvidcap-xorg.patch.expected")
check_patch(test_file, expected_file)


def test_extract_writes_a_subfile_for_each_patch_segment(tmp_path):
test_file = os.path.join(
os.path.dirname(__file__), "data", "patch", "patches", "dnsmasq_2.63-1.diff"
)
warnings = patch.extract(test_file, str(tmp_path))
extracted = [p for p in tmp_path.rglob("*") if p.is_file()]
assert warnings == []
assert len(extracted) == 19


def test_extract_handles_bytes_patch_lines(tmp_path):
test_file = os.path.join(
os.path.dirname(__file__), "data", "patch", "patches", "libmediainfo-0.7.43.diff"
)
warnings = patch.extract(test_file, str(tmp_path))
extracted = [p for p in tmp_path.rglob("*") if p.is_file()]
assert warnings == []
assert len(extracted) == 1
Loading