]>
Git — Sourcephile - reloto-libreoffice.git/blob - pack_macro.py
1 from zipfile
import ZipFile
7 print("Usage: {} scriptfile hostdocument".format(sys
.argv
[0]))
10 MACRO_FILE
= sys
.argv
[1]
11 DOCUMENT_FILE
= sys
.argv
[2]
12 MANIFEST_PATH
= 'META-INF/manifest.xml';
13 EMBED_PATH
= 'Scripts/python/' + MACRO_FILE
;
16 with ZipFile(DOCUMENT_FILE
) as bundle
:
20 for rawLine
in bundle
.open('META-INF/manifest.xml'):
21 line
= rawLine
.decode('utf-8');
22 if MACRO_FILE
in line
:
24 if ('</manifest:manifest>' in line
) and (hasMeta
== False):
25 for path
in ['Scripts/','Scripts/python/', EMBED_PATH
]:
26 manifest
.append('<manifest:file-entry manifest:media-type="application/binary" manifest:full-path="{}"/>'.format(path
))
29 # remove the manifest and script file
30 with ZipFile(DOCUMENT_FILE
+ '.tmp', 'w') as tmp
:
31 for item
in bundle
.infolist():
32 buffer = bundle
.read(item
.filename
)
33 if (item
.filename
not in [MANIFEST_PATH
, EMBED_PATH
]):
34 tmp
.writestr(item
, buffer)
37 os
.replace(DOCUMENT_FILE
+ '.tmp', DOCUMENT_FILE
); # require python 3.3+
38 #os.remove(DOCUMENT_FILE);
39 #shutil.move(DOCUMENT_FILE + '.tmp', DOCUMENT_FILE)
41 with ZipFile(DOCUMENT_FILE
, 'a') as bundle
:
42 bundle
.write(MACRO_FILE
, EMBED_PATH
)
43 bundle
.writestr(MANIFEST_PATH
, ''.join(manifest
))
45 print("Added the script {} to {}".format(MACRO_FILE
, DOCUMENT_FILE
))