Coverage for diffoscope/comparators/sphinx.py: 91%
23 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-04-07 13:38 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-04-07 13:38 +0000
1#
2# diffoscope: in-depth comparison of files, archives, and directories
3#
4# Copyright © 2021-2022 Chris Lamb <lamby@debian.org>
5#
6# diffoscope is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# diffoscope is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
19import zlib
21from diffoscope.difference import Difference
23from .utils.file import File
25MAGIC = b"# Sphinx inventory version"
28class SphinxInventoryFile(File):
29 DESCRIPTION = "Sphinx inventory files"
30 FILE_EXTENSION_SUFFIX = {".inv"}
32 def compare_details(self, other, source=None):
33 return [
34 Difference.from_text(
35 describe_inventory(self.path),
36 describe_inventory(other.path),
37 self.path,
38 other.path,
39 source="Sphinx inventory",
40 )
41 ]
44def describe_inventory(filename):
45 head = b""
46 tail = b""
48 with open(filename, "rb") as f:
49 for line in f:
50 if line.startswith(b"#"):
51 # Save commented lines at top of file
52 head += line
53 else:
54 # ... save the rest for decompression
55 tail += line
57 result = head + b"\n" if head else b""
59 try:
60 result += zlib.decompress(tail)
61 except zlib.error:
62 # Even if the file is labelled "The remainder of this file is
63 # compressed using zlib.", it might not actually be. In this case,
64 # simply return the original content.
65 result += tail
67 return result.decode("utf-8", "ignore")