from pathlib import Path
from zipfile import ZipFile,ZipInfo,ZIP_DEFLATED
from lxml import etree as E
from docx import Document
from docx.shared import Inches
import docx,json,hashlib,datetime,sys,shutil
root=Path(sys.argv[1]);root.mkdir(exist_ok=False);assert docx.__version__=='1.2.0';document=Document();document.core_properties.title='Independent writer — café Ω';document.core_properties.author='Original Tabmorph CC0 fixture author';document.core_properties.language='en-US';document.core_properties.created=datetime.datetime(2020,1,1);document.core_properties.modified=datetime.datetime(2020,1,1)
document.add_heading('Independent writer holdout',0);document.add_paragraph('Original CC0 paragraph: café Ω Привет. Literal spaces stay in this document.');p=document.add_paragraph();p.add_run('Bold control. ').bold=True;p.add_run('Italic control.').italic=True
cells=document.add_table(rows=2,cols=2).rows
for row,values in zip(cells,[['Heading one','Heading two'],['Value one','Value two']]):
 for cell,value in zip(row.cells,values):cell.text=value
document.sections[0].header.paragraphs[0].text='Independent header';document.sections[0].footer.paragraphs[0].text='Independent footer';document.sections[0].left_margin=Inches(.8)
source=root/'independent.docx';document.save(source);files={}
with ZipFile(source)as z:
 for n in z.namelist():files[n]=z.read(n)
records=[]
for fmt in ['docm','dotx']:
 typed=E.fromstring(files['[Content_Types].xml']);main=next(e for e in typed if e.get('PartName')=='/word/document.xml');main.set('ContentType','application/vnd.ms-word.document.macroEnabled.main+xml'if fmt=='docm'else'application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml');members={**files,'[Content_Types].xml':E.tostring(typed,xml_declaration=True,encoding='UTF-8')};out=root/('independent.'+fmt)
 with ZipFile(out,'w',ZIP_DEFLATED,compresslevel=6)as z:
  for name,data in members.items():i=ZipInfo(name,(2020,1,1,0,0,0));i.compress_type=ZIP_DEFLATED;z.writestr(i,data)
 records.append(dict(file=out.name,bytes=out.stat().st_size,sha256=hashlib.sha256(out.read_bytes()).hexdigest()))
report=dict(createdAt=datetime.datetime.now(datetime.timezone.utc).isoformat(),originalBodyCorpora=1,derivedPackages=2,writer='python-docx '+docx.__version__,developerOnly=True,noUserFiles=True,license='Original document text CC0; python-docx MIT default template. No added application dependency; no provider upload.',defaultTemplateParts=sorted(files),files=records,scope='Original same-input ordinary Word fixture from the accepted94 failed holdout. Regenerated packages must match retained hashes. One body/two wrappers; zero new pairs. App/browser/readers/production acceptance required.')
(root/'manifest.json').write_text(json.dumps(report,indent=2)+'\n');(root/'LICENSE.txt').write_text('Original holdout prose is CC0-1.0. Generated using installed MIT python-docx and its default template; original tool license is retained alongside this file. No user files.\n');shutil.copyfile(Path(docx.__file__).parent.parent/'python_docx-1.2.0.dist-info/licenses/LICENSE',root/'python-docx-MIT-LICENSE.txt');print(json.dumps({'writer':report['writer'],'parts':len(files),'customXml':any(n.startswith('customXml/')for n in files)}))
