-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.py
More file actions
213 lines (179 loc) · 7.03 KB
/
build.py
File metadata and controls
213 lines (179 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
"""
Build script for TejOCR LibreOffice Extension
Creates the .oxt file for distribution
"""
import os
import zipfile
import shutil
import tempfile
import re
from pathlib import Path
import sys
# Import version from constants
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'python'))
from tejocr.constants import EXTENSION_VERSION
VERSION = EXTENSION_VERSION # Use version from constants
EXTENSION_NAME = "TejOCR"
OXT_FILENAME = f"{EXTENSION_NAME}-{VERSION}.oxt"
def clean_xml_files(temp_dir):
"""
Ensure XML files have proper encoding (UTF-8 without BOM).
This is critical for LibreOffice to properly parse the files.
"""
xml_files = [
os.path.join(temp_dir, "description.xml"),
os.path.join(temp_dir, "Addons.xcu"),
os.path.join(temp_dir, "META-INF", "manifest.xml"),
*[os.path.join(temp_dir, "dialogs", f) for f in os.listdir(os.path.join(temp_dir, "dialogs"))
if f.endswith(".xdl")]
]
for xml_file in xml_files:
if os.path.exists(xml_file):
# Read the file content
with open(xml_file, 'rb') as f:
content = f.read()
# Remove BOM if present
if content.startswith(b'\xef\xbb\xbf'): # UTF-8 BOM
print(f"Removing BOM from {os.path.basename(xml_file)}")
content = content[3:]
# Ensure XML declaration is correct and at the very beginning
content_str = content.decode('utf-8')
if not content_str.startswith('<?xml version="1.0" encoding="UTF-8"?>'):
content_str = re.sub(r'<\?xml.*?\?>', '', content_str)
content_str = '<?xml version="1.0" encoding="UTF-8"?>\n' + content_str.lstrip()
# Write back cleaned content
with open(xml_file, 'wb') as f:
f.write(content_str.encode('utf-8'))
def verify_required_files(temp_dir):
"""Check if all required files exist"""
required_files = [
"description.xml",
"Addons.xcu",
os.path.join("META-INF", "manifest.xml"),
"LICENSE",
"description_en.txt",
"description_hi.txt",
# Add other essential files
]
missing = []
for file_path in required_files:
full_path = os.path.join(temp_dir, file_path)
if not os.path.exists(full_path):
missing.append(file_path)
if missing:
print("ERROR: Missing required files:", ", ".join(missing))
return False
return True
def verify_icons(temp_dir):
"""Check if icon files referenced in XML exist"""
icon_dir = os.path.join(temp_dir, "icons")
needed_icons = [
"tejocr_16.png",
"tejocr_26.png",
"tejocr_26_hc.png",
"tejocr_48.png",
"tejocr_48_hc.png",
"tejocr_64.png",
"tejocr_64_hc.png",
]
missing = []
for icon in needed_icons:
if not os.path.exists(os.path.join(icon_dir, icon)):
missing.append(icon)
if missing:
print("WARNING: Missing icon files:", ", ".join(missing))
print("Some extension functionality may be limited without these icons.")
return False
return True
def create_extension(build_dir=None, output_name=None):
"""
Package the TejOCR extension as an .oxt file.
Args:
build_dir: Directory to build in (temporary if None)
output_name: Name of output file (default: TejOCR-{VERSION}.oxt)
Returns:
Path to the created .oxt file
"""
# Get the project root directory (where this script is located)
project_dir = os.path.dirname(os.path.abspath(__file__))
# Create a clean temporary directory for building
if build_dir is None:
temp_dir = tempfile.mkdtemp(prefix="tejocr_build_")
should_cleanup = True
else:
temp_dir = build_dir
os.makedirs(temp_dir, exist_ok=True)
should_cleanup = False
try:
print(f"Building TejOCR v{VERSION} extension in {temp_dir}")
# Define exactly which top-level items belong in the extension package
include_items = {
"META-INF",
"python",
"dialogs",
"icons",
"l10n",
"description.xml",
"Addons.xcu",
"ProtocolHandler.xcu",
"LICENSE",
"README.md",
"CHANGELOG.md",
"description_en.txt",
"description_hi.txt",
"CODEMAP.md",
"DEVELOPER_GUIDE.md",
"FUNCTIONALITY.md",
"TECHNICAL.md",
}
# Copy only the necessary project files to the temporary directory
for item in os.listdir(project_dir):
if item not in include_items:
continue
src = os.path.join(project_dir, item)
dst = os.path.join(temp_dir, item)
if os.path.isdir(src):
shutil.copytree(src, dst, dirs_exist_ok=True)
else:
shutil.copy2(src, dst)
# Clean XML files to ensure proper encoding (UTF-8 without BOM)
clean_xml_files(temp_dir)
# Verify required files exist
if not verify_required_files(temp_dir):
print("ERROR: Extension cannot be built due to missing required files.")
return None
# Verify icon files
verify_icons(temp_dir)
# Create the .oxt file (which is just a ZIP file with .oxt extension)
if output_name is None:
output_name = OXT_FILENAME
output_path = os.path.join(project_dir, output_name)
# Remove existing file if it exists
if os.path.exists(output_path):
os.remove(output_path)
# Create the ZIP file with all contents
with zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(temp_dir):
# Skip __pycache__ directories
dirs[:] = [d for d in dirs if d != '__pycache__']
for file in files:
# Skip non-extension files that may have been copied
if file.endswith(('.pyc', '.oxt')) or file == '.DS_Store':
continue
file_path = os.path.join(root, file)
# Get the path relative to temp_dir for proper structure in ZIP
rel_path = os.path.relpath(file_path, temp_dir)
zipf.write(file_path, rel_path)
print(f"Extension successfully built: {output_path}")
return output_path
finally:
# Clean up temporary directory unless specified otherwise
if should_cleanup and os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
if __name__ == "__main__":
# Allow specifying output filename from command line
output_name = None
if len(sys.argv) > 1:
output_name = sys.argv[1]
create_extension(output_name=output_name)