| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
import concurrent.futures |
| 6 |
import lzma |
| 7 |
import plistlib |
| 8 |
import struct |
| 9 |
import subprocess |
| 10 |
from pathlib import Path |
| 11 |
from string import Template |
| 12 |
from typing import List |
| 13 |
from urllib.parse import quote |
| 14 |
|
| 15 |
import mozfile |
| 16 |
|
| 17 |
from mozbuild.util import cpu_count |
| 18 |
|
| 19 |
TEMPLATE_DIRECTORY = Path(__file__).parent / "apple_pkg" |
| 20 |
PBZX_CHUNK_SIZE = 16 * 1024 * 1024 # 16MB chunks |
| 21 |
|
| 22 |
|
| 23 |
def get_apple_template(name: str) -> Template: |
| 24 |
""" |
| 25 |
Given <name>, open file at <TEMPLATE_DIRECTORY>/<name>, read contents and |
| 26 |
return as a Template |
| 27 |
|
| 28 |
Args: |
| 29 |
name: str, Filename for the template |
| 30 |
|
| 31 |
Returns: |
| 32 |
Template, loaded from file |
| 33 |
""" |
| 34 |
tmpl_path = TEMPLATE_DIRECTORY / name |
| 35 |
if not tmpl_path.is_file(): |
| 36 |
raise Exception(f"Could not find template: {tmpl_path}") |
| 37 |
with tmpl_path.open("r") as tmpl: |
| 38 |
contents = tmpl.read() |
| 39 |
return Template(contents) |
| 40 |
|
| 41 |
|
| 42 |
def save_text_file(content: str, destination: Path): |
| 43 |
""" |
| 44 |
Saves a text file to <destination> with provided <content> |
| 45 |
Note: Overwrites contents |
| 46 |
|
| 47 |
Args: |
| 48 |
content: str, The desired contents of the file |
| 49 |
destination: Path, The file path |
| 50 |
""" |
| 51 |
with destination.open("w") as out_fd: |
| 52 |
out_fd.write(content) |
| 53 |
print(f"Created text file at {destination}") |
| 54 |
print(f"Created text file size: {destination.stat().st_size} bytes") |
| 55 |
|
| 56 |
|
| 57 |
def get_app_info_plist(app_path: Path) -> dict: |
| 58 |
""" |
| 59 |
Retrieve most information from Info.plist file of an app. |
| 60 |
The Info.plist file should be located in ?.app/Contents/Info.plist |
| 61 |
|
| 62 |
Note: Ignores properties that are not <string> type |
| 63 |
|
| 64 |
Args: |
| 65 |
app_path: Path, the .app file/directory path |
| 66 |
|
| 67 |
Returns: |
| 68 |
dict, the dictionary of properties found in Info.plist |
| 69 |
""" |
| 70 |
info_plist = app_path / "Contents/Info.plist" |
| 71 |
if not info_plist.is_file(): |
| 72 |
raise Exception(f"Could not find Info.plist in {info_plist}") |
| 73 |
|
| 74 |
print(f"Reading app Info.plist from: {info_plist}") |
| 75 |
|
| 76 |
with info_plist.open("rb") as plist_fd: |
| 77 |
data = plistlib.load(plist_fd) |
| 78 |
|
| 79 |
return data |
| 80 |
|
| 81 |
|
| 82 |
def create_payload(destination: Path, root_path: Path, cpio_tool: str): |
| 83 |
""" |
| 84 |
Creates a payload at <destination> based on <root_path> |
| 85 |
|
| 86 |
Args: |
| 87 |
destination: Path, the destination Path |
| 88 |
root_path: Path, the root directory Path |
| 89 |
cpio_tool: str, |
| 90 |
""" |
| 91 |
# Files to be cpio'd are root folder + contents |
| 92 |
file_list = ["./"] + get_relative_glob_list(root_path, "**/*") |
| 93 |
|
| 94 |
with mozfile.TemporaryDirectory() as tmp_dir: |
| 95 |
tmp_payload_path = Path(tmp_dir) / "Payload" |
| 96 |
print(f"Creating Payload with cpio from {root_path} to {tmp_payload_path}") |
| 97 |
print(f"Found {len(file_list)} files") |
| 98 |
with tmp_payload_path.open("wb") as tmp_payload: |
| 99 |
process = subprocess.run( |
| 100 |
[ |
| 101 |
cpio_tool, |
| 102 |
"-o", # copy-out mode |
| 103 |
"--format", |
| 104 |
"odc", # old POSIX .1 portable format |
| 105 |
"--owner", |
| 106 |
"0:80", # clean ownership |
| 107 |
], |
| 108 |
stdout=tmp_payload, |
| 109 |
stderr=subprocess.PIPE, |
| 110 |
input="\n".join(file_list) + "\n", |
| 111 |
encoding="ascii", |
| 112 |
cwd=root_path, |
| 113 |
) |
| 114 |
# cpio outputs number of blocks to stderr |
| 115 |
print(f"[CPIO]: {process.stderr}") |
| 116 |
if process.returncode: |
| 117 |
raise Exception(f"CPIO error {process.returncode}") |
| 118 |
|
| 119 |
tmp_payload_size = tmp_payload_path.stat().st_size |
| 120 |
print(f"Uncompressed Payload size: {tmp_payload_size // 1024}kb") |
| 121 |
|
| 122 |
def compress_chunk(chunk): |
| 123 |
compressed_chunk = lzma.compress(chunk) |
| 124 |
return len(chunk), compressed_chunk |
| 125 |
|
| 126 |
def chunker(fileobj, chunk_size): |
| 127 |
while True: |
| 128 |
chunk = fileobj.read(chunk_size) |
| 129 |
if not chunk: |
| 130 |
break |
| 131 |
yield chunk |
| 132 |
|
| 133 |
with tmp_payload_path.open("rb") as f_in, destination.open( |
| 134 |
"wb" |
| 135 |
) as f_out, concurrent.futures.ThreadPoolExecutor( |
| 136 |
max_workers=cpu_count() |
| 137 |
) as executor: |
| 138 |
f_out.write(b"pbzx") |
| 139 |
f_out.write(struct.pack(">Q", PBZX_CHUNK_SIZE)) |
| 140 |
chunks = chunker(f_in, PBZX_CHUNK_SIZE) |
| 141 |
for uncompressed_size, compressed_chunk in executor.map( |
| 142 |
compress_chunk, chunks |
| 143 |
): |
| 144 |
f_out.write(struct.pack(">Q", uncompressed_size)) |
| 145 |
if len(compressed_chunk) < uncompressed_size: |
| 146 |
f_out.write(struct.pack(">Q", len(compressed_chunk))) |
| 147 |
f_out.write(compressed_chunk) |
| 148 |
else: |
| 149 |
# Considering how unlikely this is, we prefer to just decompress |
| 150 |
# here than to keep the original uncompressed chunk around |
| 151 |
f_out.write(struct.pack(">Q", uncompressed_size)) |
| 152 |
f_out.write(lzma.decompress(compressed_chunk)) |
| 153 |
|
| 154 |
print(f"Compressed Payload file to {destination}") |
| 155 |
print(f"Compressed Payload size: {destination.stat().st_size // 1024}kb") |
| 156 |
|
| 157 |
|
| 158 |
def create_bom(bom_path: Path, root_path: Path, mkbom_tool: Path): |
| 159 |
""" |
| 160 |
Creates a Bill Of Materials file at <bom_path> based on <root_path> |
| 161 |
|
| 162 |
Args: |
| 163 |
bom_path: Path, destination Path for the BOM file |
| 164 |
root_path: Path, root directory Path |
| 165 |
mkbom_tool: Path, mkbom tool Path |
| 166 |
""" |
| 167 |
print(f"Creating BOM file from {root_path} to {bom_path}") |
| 168 |
subprocess.check_call( |
| 169 |
[ |
| 170 |
mkbom_tool, |
| 171 |
"-u", |
| 172 |
"0", |
| 173 |
"-g", |
| 174 |
"80", |
| 175 |
str(root_path), |
| 176 |
str(bom_path), |
| 177 |
] |
| 178 |
) |
| 179 |
print(f"Created BOM File size: {bom_path.stat().st_size // 1024}kb") |
| 180 |
|
| 181 |
|
| 182 |
def get_relative_glob_list(source: Path, glob: str) -> List[str]: |
| 183 |
""" |
| 184 |
Given a source path, return a list of relative path based on glob |
| 185 |
|
| 186 |
Args: |
| 187 |
source: Path, source directory Path |
| 188 |
glob: str, unix style glob |
| 189 |
|
| 190 |
Returns: |
| 191 |
list[str], paths found in source directory |
| 192 |
""" |
| 193 |
return [f"./{c.relative_to(source)}" for c in source.glob(glob)] |
| 194 |
|
| 195 |
|
| 196 |
def xar_package_folder(source_path: Path, destination: Path, xar_tool: Path): |
| 197 |
""" |
| 198 |
Create a pkg from <source_path> to <destination> |
| 199 |
The command is issued with <source_path> as cwd |
| 200 |
|
| 201 |
Args: |
| 202 |
source_path: Path, source absolute Path |
| 203 |
destination: Path, destination absolute Path |
| 204 |
xar_tool: Path, xar tool Path |
| 205 |
""" |
| 206 |
if not source_path.is_absolute() or not destination.is_absolute(): |
| 207 |
raise Exception("Source and destination should be absolute.") |
| 208 |
|
| 209 |
print(f"Creating pkg from {source_path} to {destination}") |
| 210 |
# Create a list of ./<file> - noting xar takes care of <file>/** |
| 211 |
file_list = get_relative_glob_list(source_path, "*") |
| 212 |
|
| 213 |
subprocess.check_call( |
| 214 |
[ |
| 215 |
xar_tool, |
| 216 |
"--compression", |
| 217 |
"none", |
| 218 |
"-vcf", |
| 219 |
destination, |
| 220 |
*file_list, |
| 221 |
], |
| 222 |
cwd=source_path, |
| 223 |
) |
| 224 |
print(f"Created PKG file to {destination}") |
| 225 |
print(f"Created PKG size: {destination.stat().st_size // 1024}kb") |
| 226 |
|
| 227 |
|
| 228 |
def create_pkg( |
| 229 |
source_app: Path, |
| 230 |
output_pkg: Path, |
| 231 |
mkbom_tool: Path, |
| 232 |
xar_tool: Path, |
| 233 |
cpio_tool: Path, |
| 234 |
): |
| 235 |
""" |
| 236 |
Create a mac PKG installer from <source_app> to <output_pkg> |
| 237 |
|
| 238 |
Args: |
| 239 |
source_app: Path, source .app file/directory Path |
| 240 |
output_pkg: Path, destination .pkg file |
| 241 |
mkbom_tool: Path, mkbom tool Path |
| 242 |
xar_tool: Path, xar tool Path |
| 243 |
cpio: Path, cpio tool Path |
| 244 |
""" |
| 245 |
|
| 246 |
app_name = source_app.name.rsplit(".", maxsplit=1)[0] |
| 247 |
|
| 248 |
with mozfile.TemporaryDirectory() as tmpdir: |
| 249 |
root_path = Path(tmpdir) / "darwin/root" |
| 250 |
flat_path = Path(tmpdir) / "darwin/flat" |
| 251 |
|
| 252 |
# Create required directories |
| 253 |
# TODO: Investigate Resources folder contents for other lproj? |
| 254 |
(flat_path / "Resources/en.lproj").mkdir(parents=True, exist_ok=True) |
| 255 |
(flat_path / f"{app_name}.pkg").mkdir(parents=True, exist_ok=True) |
| 256 |
root_path.mkdir(parents=True, exist_ok=True) |
| 257 |
|
| 258 |
# Copy files over |
| 259 |
subprocess.check_call( |
| 260 |
[ |
| 261 |
"cp", |
| 262 |
"-R", |
| 263 |
str(source_app), |
| 264 |
str(root_path), |
| 265 |
] |
| 266 |
) |
| 267 |
|
| 268 |
# Count all files (innards + itself) |
| 269 |
file_count = len(list(source_app.glob("**/*"))) + 1 |
| 270 |
print(f"Calculated source files count: {file_count}") |
| 271 |
# Get package contents size |
| 272 |
package_size = sum(f.stat().st_size for f in source_app.glob("**/*")) // 1024 |
| 273 |
print(f"Calculated source package size: {package_size}kb") |
| 274 |
|
| 275 |
app_info = get_app_info_plist(source_app) |
| 276 |
app_info["numberOfFiles"] = file_count |
| 277 |
app_info["installKBytes"] = package_size |
| 278 |
app_info["app_name"] = app_name |
| 279 |
app_info["app_name_url_encoded"] = quote(app_name) |
| 280 |
|
| 281 |
# This seems arbitrary, there might be another way of doing it, |
| 282 |
# but Info.plist doesn't provide the simple version we need |
| 283 |
major_version = app_info["CFBundleShortVersionString"].split(".")[0] |
| 284 |
app_info["simple_version"] = f"{major_version}.0.0" |
| 285 |
|
| 286 |
pkg_info_tmpl = get_apple_template("PackageInfo.template") |
| 287 |
pkg_info = pkg_info_tmpl.substitute(app_info) |
| 288 |
save_text_file(pkg_info, flat_path / f"{app_name}.pkg/PackageInfo") |
| 289 |
|
| 290 |
distribution_tmp = get_apple_template("Distribution.template") |
| 291 |
distribution = distribution_tmp.substitute(app_info) |
| 292 |
save_text_file(distribution, flat_path / "Distribution") |
| 293 |
|
| 294 |
payload_path = flat_path / f"{app_name}.pkg/Payload" |
| 295 |
create_payload(payload_path, root_path, cpio_tool) |
| 296 |
|
| 297 |
bom_path = flat_path / f"{app_name}.pkg/Bom" |
| 298 |
create_bom(bom_path, root_path, mkbom_tool) |
| 299 |
|
| 300 |
xar_package_folder(flat_path, output_pkg, xar_tool) |