1################################################################ 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19 20"""Comparing two saves of the same document. Date and time fields keep the value they had when the document was saved, so two 21saves differ there by design; everything else in content.xml and styles.xml must be identical.""" 22import re 23import zipfile 24 25PARTS = ("content.xml", "styles.xml") 26_FIELD = re.compile(r"(<text:(?:time|date)\b)([^>]*)>[^<]*(</text:(?:time|date)>)") 27_VALUE = re.compile(r'\s+text:(?:time|date)-value="[^"]*"') 28 29 30def normalize(xml): 31 """`xml` with the cached text and the value attribute of text:date and text:time fields removed.""" 32 return _FIELD.sub(lambda m: m.group(1) + _VALUE.sub("", m.group(2)) + ">" + m.group(3), xml) 33 34 35def differing_parts(first, second): 36 """Names of the parts of two packages whose normalized content differs. A part that only one of them has differs; a part that 37 neither has (a Base document has no styles.xml) is left out.""" 38 with zipfile.ZipFile(first) as a, zipfile.ZipFile(second) as b: 39 have_a, have_b = set(a.namelist()), set(b.namelist()) 40 return [p for p in PARTS if (p in have_a or p in have_b) and 41 (p not in have_a or p not in have_b or normalize(a.read(p).decode("utf-8")) != normalize(b.read(p).decode("utf-8")))] 42