|
| 1 | +"""Regenerate the owned PDF/DOCX fixtures using only Python's standard library.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import hashlib |
| 5 | +import io |
| 6 | +from pathlib import Path |
| 7 | +import zipfile |
| 8 | + |
| 9 | + |
| 10 | +def generate_pdf() -> bytes: |
| 11 | + stream = ( |
| 12 | + b"BT\n/F1 12 Tf\n72 720 Td\n" |
| 13 | + b"(Approved PDF first line) Tj\n0 -18 Td\n" |
| 14 | + b"(Approved PDF second line) Tj\nET\n" |
| 15 | + ) |
| 16 | + objects = [ |
| 17 | + b"<< /Type /Catalog /Pages 2 0 R >>", |
| 18 | + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", |
| 19 | + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] " |
| 20 | + b"/Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>", |
| 21 | + b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", |
| 22 | + b"<< /Length %d >>\nstream\n" % len(stream) + stream + b"endstream", |
| 23 | + ] |
| 24 | + document = bytearray(b"%PDF-1.4\n%\xe2\xe3\xcf\xd3\n") |
| 25 | + offsets = [] |
| 26 | + for number, content in enumerate(objects, 1): |
| 27 | + offsets.append(len(document)) |
| 28 | + document += b"%d 0 obj\n" % number + content + b"\nendobj\n" |
| 29 | + |
| 30 | + xref_offset = len(document) |
| 31 | + document += b"xref\n0 6\n0000000000 65535 f \n" |
| 32 | + for offset in offsets: |
| 33 | + document += b"%010d 00000 n \n" % offset |
| 34 | + document += ( |
| 35 | + b"trailer\n<< /Size 6 /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n" |
| 36 | + % xref_offset |
| 37 | + ) |
| 38 | + return bytes(document) |
| 39 | + |
| 40 | + |
| 41 | +def generate_docx() -> bytes: |
| 42 | + header = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' |
| 43 | + parts = [ |
| 44 | + ( |
| 45 | + "[Content_Types].xml", |
| 46 | + header |
| 47 | + + '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">\n' |
| 48 | + '<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>\n' |
| 49 | + '<Default Extension="xml" ContentType="application/xml"/>\n' |
| 50 | + '<Override PartName="/word/document.xml" ' |
| 51 | + 'ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>\n' |
| 52 | + '</Types>', |
| 53 | + ), |
| 54 | + ( |
| 55 | + "_rels/.rels", |
| 56 | + header |
| 57 | + + '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">\n' |
| 58 | + '<Relationship Id="rId1" ' |
| 59 | + 'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" ' |
| 60 | + 'Target="word/document.xml"/>\n' |
| 61 | + '</Relationships>', |
| 62 | + ), |
| 63 | + ( |
| 64 | + "word/document.xml", |
| 65 | + header |
| 66 | + + '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">\n' |
| 67 | + '<w:body>\n' |
| 68 | + '<w:p><w:r><w:t>Approved DOCX first paragraph</w:t></w:r></w:p>\n' |
| 69 | + '<w:p><w:r><w:t xml:space="preserve">Approved DOCX second & </w:t></w:r>' |
| 70 | + '<w:r><w:t>final paragraph</w:t></w:r></w:p>\n' |
| 71 | + '<w:sectPr><w:pgSz w:w="12240" w:h="15840"/></w:sectPr>\n' |
| 72 | + '</w:body>\n</w:document>', |
| 73 | + ), |
| 74 | + ] |
| 75 | + |
| 76 | + output = io.BytesIO() |
| 77 | + with zipfile.ZipFile(output, "w") as archive: |
| 78 | + for name, content in parts: |
| 79 | + entry = zipfile.ZipInfo(name, (2020, 1, 1, 0, 0, 0)) |
| 80 | + entry.compress_type = zipfile.ZIP_DEFLATED |
| 81 | + entry.create_system = 3 |
| 82 | + entry.external_attr = 0o100644 << 16 |
| 83 | + archive.writestr(entry, content.encode("utf-8"), compresslevel=6) |
| 84 | + return output.getvalue() |
| 85 | + |
| 86 | + |
| 87 | +def main() -> None: |
| 88 | + parser = argparse.ArgumentParser(description=__doc__) |
| 89 | + parser.add_argument( |
| 90 | + "output_directory", |
| 91 | + nargs="?", |
| 92 | + type=Path, |
| 93 | + default=Path(__file__).resolve().parent, |
| 94 | + help="destination directory (defaults to this script's directory)", |
| 95 | + ) |
| 96 | + output_directory = parser.parse_args().output_directory |
| 97 | + output_directory.mkdir(parents=True, exist_ok=True) |
| 98 | + |
| 99 | + for name, content in ( |
| 100 | + ("approved.pdf", generate_pdf()), |
| 101 | + ("approved.docx", generate_docx()), |
| 102 | + ): |
| 103 | + (output_directory / name).write_bytes(content) |
| 104 | + print(f"{hashlib.sha256(content).hexdigest()} {name}") |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments