]> Git — Sourcephile - reloto-libreoffice.git/blob - pack_macro.py
Corrections d’orthographe et de cohérence.
[reloto-libreoffice.git] / pack_macro.py
1 from zipfile import ZipFile
2 import shutil
3 import sys
4 import os
5
6 if len(sys.argv) < 3:
7 print("Usage: {} scriptfile hostdocument".format(sys.argv[0]))
8 exit()
9
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;
14
15 hasMeta = False
16 with ZipFile(DOCUMENT_FILE) as bundle:
17
18 # grab the manifest
19 manifest = []
20 for rawLine in bundle.open('META-INF/manifest.xml'):
21 line = rawLine.decode('utf-8');
22 if MACRO_FILE in line:
23 hasMeta = True
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))
27 manifest.append(line)
28
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)
35
36
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)
40
41 with ZipFile(DOCUMENT_FILE, 'a') as bundle:
42 bundle.write(MACRO_FILE, EMBED_PATH)
43 bundle.writestr(MANIFEST_PATH, ''.join(manifest))
44
45 print("Added the script {} to {}".format(MACRO_FILE, DOCUMENT_FILE))