How ihongren/hpack works
Project Type and Purpose
• Type: Open-source command-line tool (Python)
• Purpose: Automates building (via hvigorw), packaging, signing (Java jars), manifest generation, HTML index page creation (templating), QR code generation, and optional upload (OSS or custom) for HarmonyOS internal testing.
File Structure
• src/harmony_hpack/: core Python modules
• main.py – CLI entry point and command dispatcher
• PackFile.py – post-pack callback hooks (extensibility)
• config.py – static Config class (server, signing, template settings)
• packSign.py, signInfo.py, template.py, toolConfig.py – core logic modules
• hdc.py – device listing via hdc
• hdc & hvigorw tool adapters
• hap-manifest-sign-tool folder (Java JARs, libs)
• templates/ – built-in HTML templates
• setup.py – packaging for PyPI
• demo/ – example HarmonyOS project, showing how hpack is used
• oh_modules/ – package dependencies for demo
README for Additional Insights
• Commands: hpack init, hpack pack, hpack install, hpack targets, hpack template, hpack -u
• Workflow:
a. Read config.py
b. Invoke hvigorw to assemble HAP/HSP
c. Sign packages via Java tool (manifest-sign-tool.jar)
d. Generate signed manifest.json5
e. Generate index HTML (choose & render template)
f. Generate QR code
g. Optional: upload artifacts via OSS SDK or custom PackFile.py hook
h. Provide CLI feedback
Main Components and Their Relationships
a. CLI Frontend (main.py)
• Parses user commands & flags
• Routes to individual handlers (init, pack, install, etc.)
b. Config Module (config.py)
• Holds server URLs, keys, passwords, template choice, debug flags
• Read-only at runtime
c. Build Adapter (toolConfig.py + hvigorw binary)
• Constructs and executes hvigorw commands
• Produces .hap and .hsp artifacts
d. Signing Engine
• Python wrapper (packSign.py, signInfo.py)
• Java-based manifest-sign-tool.jar + BouncyCastle libs
• Consumes raw artifacts + .cer/.p7b/.p12 to produce signed artifacts & manifest
e. Template Renderer (template.py + templates/)
• Loads HTML template based on Config.IndexTemplate
• Substitutes variables (version, size, URLs, QR code image)
• Emits index.html in hpack/build/{product}
f. QR Code Generator (utility within template or signInfo)
• Renders QR images pointing to manifest or install URL
g. Uploader / Callback Hook (PackFile.py)
• Default: no-op or prints results
• Customizable: user can implement didPack(packInfo) to push to OSS or another server
• Uses oss2 library if OSS is chosen
h. Device Inspector (hdc.py)
• Uses Huawei Device Connector (hdc) to list targets, retrieve UDID
• Implements hpack targets and hpack -u
Architectural Patterns & Design Principles
• Modular CLI: One command per handler, clear separation
• Adapter Pattern: Tool adapters for hvigorw & hdc to isolate external dependencies
• Strategy / Plugin Hook: PackFile.py as user-extensible callback for uploading
• Template Strategy: Multiple HTML templates loaded at runtime
• Configuration-Driven: Config centralizes all environment-specific settings