-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_tejocr.py
More file actions
532 lines (447 loc) · 19.5 KB
/
build_tejocr.py
File metadata and controls
532 lines (447 loc) · 19.5 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#!/usr/bin/env python3
"""
TejOCR LibreOffice Extension Build Script
=========================================
This script provides comprehensive build automation for the TejOCR extension.
It handles:
- Phase 1: Critical error fixes verification
- Phase 2: UI enhancements
- Phase 3: Build packaging
- Testing and validation
- LibreOffice installation management
Author: Assistant working with Devansh
Date: May 2025
"""
import os
import sys
import shutil
import subprocess
import zipfile
import tempfile
import xml.etree.ElementTree as ET
from pathlib import Path
import time
import json
import glob
import re
class TejOCRBuilder:
"""Comprehensive builder for TejOCR extension."""
def __init__(self, workspace_dir=None):
"""Initialize the builder with workspace directory."""
self.workspace_dir = Path(workspace_dir or os.getcwd())
self.build_dir = self.workspace_dir / "build"
self.dist_dir = self.workspace_dir / "dist"
self.version = "0.2.0" # Will be read from constants.py
# Create build directories
self.build_dir.mkdir(exist_ok=True)
self.dist_dir.mkdir(exist_ok=True)
print(f"TejOCR Builder initialized")
print(f"Workspace: {self.workspace_dir}")
print(f"Build dir: {self.build_dir}")
print(f"Dist dir: {self.dist_dir}")
def _detect_running_soffice_path(self):
"""Best-effort detection of the LibreOffice executable that is currently running."""
try:
result = subprocess.run(
["ps", "axww", "-o", "command="],
capture_output=True,
text=True,
timeout=5,
check=False,
)
except Exception:
return None
pattern = re.compile(r"(/[^\s]*?(?:LibreOffice[^/\s]*\.app/Contents/MacOS/soffice(?:\.bin)?|program/soffice(?:\.bin|\.exe)?|/soffice(?:\.bin)?))")
for line in result.stdout.splitlines():
match = pattern.search(line)
if not match:
continue
candidate = match.group(1)
if os.path.exists(candidate):
return candidate
return None
def _candidate_soffice_paths(self):
"""Return LibreOffice executable candidates, preferring the running app and PATH."""
candidates = []
def add(path):
if not path:
return
normalized = os.path.abspath(os.path.expanduser(str(path)))
if normalized not in candidates and os.path.exists(normalized):
candidates.append(normalized)
add(os.environ.get("LIBREOFFICE_SOFFICE_PATH"))
add(self._detect_running_soffice_path())
add(shutil.which("soffice"))
add(shutil.which("libreoffice"))
if sys.platform == "darwin":
for pattern in (
"/Applications/LibreOffice*.app/Contents/MacOS/soffice",
os.path.expanduser("~/Applications/LibreOffice*.app/Contents/MacOS/soffice"),
):
for match in sorted(glob.glob(pattern)):
add(match)
elif sys.platform == "win32":
for path in (
r"C:\Program Files\LibreOffice\program\soffice.exe",
r"C:\Program Files (x86)\LibreOffice\program\soffice.exe",
):
add(path)
else:
for path in ("/usr/bin/libreoffice", "/usr/bin/soffice", "/snap/bin/libreoffice"):
add(path)
return candidates
def _find_soffice_path(self):
"""Resolve the best LibreOffice executable path for install/test flows."""
candidates = self._candidate_soffice_paths()
return candidates[0] if candidates else None
def get_version_from_constants(self):
"""Extract version from constants.py file."""
try:
constants_file = self.workspace_dir / "python" / "tejocr" / "constants.py"
if constants_file.exists():
with open(constants_file, 'r', encoding='utf-8') as f:
for line in f:
if line.strip().startswith('EXTENSION_VERSION'):
# Extract version from line like: EXTENSION_VERSION = "0.2.0"
version = line.split('=')[1].strip().strip('"\'')
self.version = version
print(f"Found version in constants.py: {self.version}")
return
except Exception as e:
print(f"Warning: Could not extract version from constants.py: {e}")
print(f"Using default version: {self.version}")
def run_phase1_tests(self):
"""Run Phase 1 critical error fixes tests."""
print("\n" + "="*60)
print("PHASE 1: Running Critical Error Fixes Tests")
print("="*60)
test_file = self.workspace_dir / "test_phase1_fixes.py"
if not test_file.exists():
print("❌ test_phase1_fixes.py not found!")
return False
try:
# Test constants only (since uno modules won't be available)
result = subprocess.run([
sys.executable, str(test_file)
], capture_output=True, text=True, cwd=self.workspace_dir)
print("Test output:")
print(result.stdout)
if result.stderr:
print("Test errors:")
print(result.stderr)
# Check if at least constants tests passed
if "All missing constants are now defined correctly" in result.stdout:
print("✅ Phase 1 basic constants validation passed")
return True
else:
print("❌ Phase 1 tests failed")
return False
except Exception as e:
print(f"❌ Error running Phase 1 tests: {e}")
return False
def validate_file_structure(self):
"""Validate that all required files exist."""
print("\n" + "="*60)
print("PHASE 2: Validating File Structure")
print("="*60)
required_files = [
"python/tejocr/__init__.py",
"python/tejocr/constants.py",
"python/tejocr/uno_utils.py",
"python/tejocr/tejocr_service.py",
"python/tejocr/tejocr_engine.py",
"python/tejocr/tejocr_output.py",
"python/tejocr/tejocr_interactive_dialogs.py",
"python/tejocr/locale_setup.py",
"dialogs/tejocr_settings_dialog_full.xdl",
"dialogs/tejocr_settings_dialog.xdl",
"dialogs/tejocr_options_dialog_full.xdl",
"dialogs/tejocr_options_dialog.xdl",
"dialogs/tejocr_setup_dialog.xdl",
"dialogs/tejocr_ocr_complete_dialog_v2.xdl",
"dialogs/tejocr_message_dialog.xdl",
"META-INF/manifest.xml",
"description.xml",
"CHANGELOG.md",
"description_en.txt",
"description_hi.txt",
"README.md",
"LICENSE",
"Addons.xcu",
"ProtocolHandler.xcu"
]
missing_files = []
for file_path in required_files:
full_path = self.workspace_dir / file_path
if not full_path.exists():
missing_files.append(file_path)
print(f"❌ Missing: {file_path}")
else:
print(f"✅ Found: {file_path}")
if missing_files:
print(f"\n❌ {len(missing_files)} required files are missing!")
return False
else:
print(f"\n✅ All {len(required_files)} required files found!")
return True
def validate_python_syntax(self):
"""Validate Python syntax for all Python modules."""
print("\n" + "="*60)
print("PHASE 2: Validating Python Syntax")
print("="*60)
python_files = list((self.workspace_dir / "python").rglob("*.py"))
syntax_errors = []
for py_file in python_files:
try:
with open(py_file, 'r', encoding='utf-8') as f:
source = f.read()
compile(source, str(py_file), 'exec')
print(f"✅ Syntax OK: {py_file.relative_to(self.workspace_dir)}")
except SyntaxError as e:
syntax_errors.append((py_file, e))
print(f"❌ Syntax Error: {py_file.relative_to(self.workspace_dir)} - {e}")
except Exception as e:
print(f"⚠️ Warning: {py_file.relative_to(self.workspace_dir)} - {e}")
if syntax_errors:
print(f"\n❌ {len(syntax_errors)} files have syntax errors!")
return False
else:
print(f"\n✅ All {len(python_files)} Python files have valid syntax!")
return True
def update_version_info(self):
"""Update version information in various files."""
print("\n" + "="*60)
print("PHASE 3: Updating Version Information")
print("="*60)
self.get_version_from_constants()
# Update description.xml
desc_file = self.workspace_dir / "description.xml"
if desc_file.exists():
try:
tree = ET.parse(desc_file)
root = tree.getroot()
# Update version in namespaced description.xml
namespace = {"desc": "http://openoffice.org/extensions/description/2006"}
version_elem = root.find("desc:version", namespace)
if version_elem is None:
version_elem = root.find(".//version")
if version_elem is not None:
version_elem.set("value", self.version)
tree.write(desc_file, encoding='utf-8', xml_declaration=True)
print(f"✅ Updated description.xml version to {self.version}")
else:
print("⚠️ Could not find version element in description.xml")
except Exception as e:
print(f"❌ Error updating description.xml: {e}")
print(f"📦 Extension version: {self.version}")
def create_extension_package(self):
"""Create the .oxt extension package."""
print("\n" + "="*60)
print("PHASE 3: Creating Extension Package")
print("="*60)
# Clean build directory
if self.build_dir.exists():
shutil.rmtree(self.build_dir)
self.build_dir.mkdir()
# Package name
package_name = f"TejOCR-{self.version}.oxt"
package_path = self.dist_dir / package_name
# Remove old package if exists
if package_path.exists():
package_path.unlink()
# Files and directories to include in the package
include_items = [
"python/",
"dialogs/",
"icons/",
"l10n/",
"META-INF/",
"description.xml",
"Addons.xcu",
"ProtocolHandler.xcu",
"CHANGELOG.md",
"description_en.txt",
"description_hi.txt",
"README.md",
"LICENSE",
"CODEMAP.md",
"DEVELOPER_GUIDE.md",
"FUNCTIONALITY.md",
"TECHNICAL.md",
]
print("Creating extension package...")
with zipfile.ZipFile(package_path, 'w', zipfile.ZIP_DEFLATED) as zf:
for item in include_items:
item_path = self.workspace_dir / item
if item_path.is_file():
zf.write(item_path, item)
print(f" 📄 Added file: {item}")
elif item_path.is_dir():
for file_path in item_path.rglob('*'):
if file_path.is_file() and not self._should_exclude_file(file_path):
arcname = file_path.relative_to(self.workspace_dir)
zf.write(file_path, arcname)
print(f" 📄 Added: {arcname}")
else:
print(f" ⚠️ Skipped missing: {item}")
# Verify package was created
if package_path.exists():
size = package_path.stat().st_size
print(f"\n✅ Extension package created successfully!")
print(f"📦 Package: {package_path}")
print(f"📏 Size: {size:,} bytes ({size/1024:.1f} KB)")
# List package contents
with zipfile.ZipFile(package_path, 'r') as zf:
file_count = len(zf.namelist())
print(f"📁 Files in package: {file_count}")
return package_path
else:
print("❌ Failed to create extension package!")
return None
def _should_exclude_file(self, file_path):
"""Check if a file should be excluded from the package."""
exclude_patterns = [
'__pycache__',
'.pyc',
'.pyo',
'.DS_Store',
'.git',
'test_',
'.pytest_cache'
]
file_str = str(file_path)
return any(pattern in file_str for pattern in exclude_patterns)
def install_extension(self, package_path):
"""Install the extension into LibreOffice."""
print("\n" + "="*60)
print("PHASE 4: Installing Extension")
print("="*60)
if not package_path or not package_path.exists():
print("❌ No package to install!")
return False
soffice_path = self._find_soffice_path()
if not soffice_path:
print("❌ LibreOffice installation not found!")
print("Please install the extension manually:")
print(f" 1. Open LibreOffice")
print(f" 2. Go to Tools > Extension Manager")
print(f" 3. Click 'Add' and select: {package_path}")
return False
print(f"Found LibreOffice: {soffice_path}")
try:
# Install extension using unopkg
unopkg_dir = os.path.dirname(soffice_path)
unopkg_path = os.path.join(unopkg_dir, "unopkg")
if sys.platform == "win32":
unopkg_path += ".exe"
if not os.path.exists(unopkg_path):
print(f"⚠️ unopkg not found at {unopkg_path}")
print("Trying alternative installation method...")
# Try using soffice directly
cmd = [soffice_path, "--headless", "--install-extension", str(package_path)]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if result.returncode == 0:
print("✅ Extension installed successfully using soffice!")
return True
else:
print(f"❌ Installation failed: {result.stderr}")
return False
else:
# Use unopkg for installation
cmd = [unopkg_path, "add", "--force", str(package_path)]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if result.returncode == 0:
print("✅ Extension installed successfully using unopkg!")
return True
else:
print(f"❌ Installation failed: {result.stderr}")
return False
except subprocess.TimeoutExpired:
print("❌ Installation timed out!")
return False
except Exception as e:
print(f"❌ Installation error: {e}")
return False
def run_libreoffice_test(self):
"""Run LibreOffice with the extension to test it."""
print("\n" + "="*60)
print("PHASE 4: Testing Extension in LibreOffice")
print("="*60)
soffice_path = self._find_soffice_path()
if not soffice_path:
print("❌ LibreOffice not found for testing!")
return False
print(f"Starting LibreOffice Writer for testing...")
print(f"Look for TejOCR menu items in the Tools menu.")
print(f"Test the extension functionality manually.")
try:
# Start LibreOffice Writer
cmd = [soffice_path, "--writer"]
process = subprocess.Popen(cmd)
print(f"✅ LibreOffice started (PID: {process.pid})")
print(f"Manual testing instructions:")
print(f" 1. Look for 'TejOCR' in Tools menu")
print(f" 2. Try 'TejOCR Settings' to configure")
print(f" 3. Try 'OCR from File' to test functionality")
print(f" 4. Check the log file for any errors")
return True
except Exception as e:
print(f"❌ Error starting LibreOffice: {e}")
return False
def generate_build_report(self, results):
"""Generate a comprehensive build report."""
print("\n" + "="*60)
print("BUILD REPORT")
print("="*60)
report = {
"build_time": time.strftime("%Y-%m-%d %H:%M:%S"),
"version": self.version,
"workspace": str(self.workspace_dir),
"results": results
}
# Save report to file
report_file = self.dist_dir / f"build_report_{self.version}.json"
with open(report_file, 'w', encoding='utf-8') as f:
json.dump(report, f, indent=2, ensure_ascii=False)
# Print summary
passed = sum(1 for r in results.values() if r)
total = len(results)
print(f"Build Summary:")
print(f" Version: {self.version}")
print(f" Passed: {passed}/{total} phases")
for phase, result in results.items():
status = "✅" if result else "❌"
print(f" {status} {phase}")
if passed == total:
print(f"\n🎉 BUILD SUCCESSFUL! Extension ready for use.")
else:
print(f"\n⚠️ BUILD COMPLETED WITH ISSUES. Check logs above.")
print(f"\nReport saved: {report_file}")
return passed == total
def main():
"""Main build script entry point."""
print("TejOCR Extension Build Script")
print("============================")
builder = TejOCRBuilder()
results = {}
# Phase 1: Critical Tests
results["Phase 1: Critical Tests"] = builder.run_phase1_tests()
# Phase 2: Validation
results["Phase 2: File Structure"] = builder.validate_file_structure()
results["Phase 2: Python Syntax"] = builder.validate_python_syntax()
# Phase 3: Build
builder.update_version_info()
package_path = builder.create_extension_package()
results["Phase 3: Package Creation"] = package_path is not None
# Phase 4: Installation & Testing (optional)
if package_path:
results["Phase 4: Extension Installation"] = builder.install_extension(package_path)
results["Phase 4: LibreOffice Testing"] = builder.run_libreoffice_test()
else:
results["Phase 4: Extension Installation"] = False
results["Phase 4: LibreOffice Testing"] = False
# Generate final report
success = builder.generate_build_report(results)
return 0 if success else 1
if __name__ == "__main__":
sys.exit(main())