| 1 |
#!/usr/bin/env python |
| 2 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
# License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 |
# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 |
"""desktop_l10n.py |
| 6 |
|
| 7 |
This script manages Desktop repacks for nightly builds. |
| 8 |
""" |
| 9 |
|
| 10 |
import glob |
| 11 |
import os |
| 12 |
import shlex |
| 13 |
import sys |
| 14 |
|
| 15 |
# load modules from parent dir |
| 16 |
sys.path.insert(1, os.path.dirname(sys.path[0])) # noqa |
| 17 |
|
| 18 |
from mozharness.base.errors import MakefileErrorList |
| 19 |
from mozharness.base.script import BaseScript |
| 20 |
from mozharness.base.vcs.vcsbase import VCSMixin |
| 21 |
from mozharness.mozilla.automation import AutomationMixin |
| 22 |
from mozharness.mozilla.building.buildbase import ( |
| 23 |
MakeUploadOutputParser, |
| 24 |
get_mozconfig_path, |
| 25 |
) |
| 26 |
from mozharness.mozilla.l10n.locales import LocalesMixin |
| 27 |
|
| 28 |
try: |
| 29 |
import simplejson as json |
| 30 |
|
| 31 |
assert json |
| 32 |
except ImportError: |
| 33 |
import json |
| 34 |
|
| 35 |
|
| 36 |
# needed by _map |
| 37 |
SUCCESS = 0 |
| 38 |
FAILURE = 1 |
| 39 |
|
| 40 |
SUCCESS_STR = "Success" |
| 41 |
FAILURE_STR = "Failed" |
| 42 |
|
| 43 |
|
| 44 |
# DesktopSingleLocale {{{1 |
| 45 |
class DesktopSingleLocale(LocalesMixin, AutomationMixin, VCSMixin, BaseScript): |
| 46 |
"""Manages desktop repacks""" |
| 47 |
|
| 48 |
config_options = [ |
| 49 |
[ |
| 50 |
[ |
| 51 |
"--locale", |
| 52 |
], |
| 53 |
{ |
| 54 |
"action": "extend", |
| 55 |
"dest": "locales", |
| 56 |
"type": "string", |
| 57 |
"help": "Specify the locale(s) to sign and update. Optionally pass" |
| 58 |
" revision separated by colon, en-GB:default.", |
| 59 |
}, |
| 60 |
], |
| 61 |
] |
| 62 |
|
| 63 |
def __init__(self, require_config_file=True): |
| 64 |
# fxbuild style: |
| 65 |
buildscript_kwargs = { |
| 66 |
"all_actions": [ |
| 67 |
"clone-locales", |
| 68 |
"list-locales", |
| 69 |
"setup", |
| 70 |
"repack", |
| 71 |
"summary", |
| 72 |
], |
| 73 |
"config": { |
| 74 |
"ignore_locales": ["en-US"], |
| 75 |
"locales_dir": "browser/locales", |
| 76 |
"log_name": "single_locale", |
| 77 |
"git_repository": "https://github.com/mozilla-l10n/firefox-l10n", |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
LocalesMixin.__init__(self) |
| 82 |
BaseScript.__init__( |
| 83 |
self, |
| 84 |
config_options=self.config_options, |
| 85 |
require_config_file=require_config_file, |
| 86 |
**buildscript_kwargs, |
| 87 |
) |
| 88 |
|
| 89 |
self.bootstrap_env = None |
| 90 |
self.upload_env = None |
| 91 |
self.upload_urls = {} |
| 92 |
self.pushdate = None |
| 93 |
# upload_files is a dictionary of files to upload, keyed by locale. |
| 94 |
self.upload_files = {} |
| 95 |
|
| 96 |
# Helper methods {{{2 |
| 97 |
def query_bootstrap_env(self): |
| 98 |
"""returns the env for repacks""" |
| 99 |
if self.bootstrap_env: |
| 100 |
return self.bootstrap_env |
| 101 |
config = self.config |
| 102 |
abs_dirs = self.query_abs_dirs() |
| 103 |
|
| 104 |
bootstrap_env = self.query_env( |
| 105 |
partial_env=config.get("bootstrap_env"), replace_dict=abs_dirs |
| 106 |
) |
| 107 |
|
| 108 |
bootstrap_env["L10NBASEDIR"] = abs_dirs["abs_l10n_dir"] |
| 109 |
if self.query_is_nightly(): |
| 110 |
# we might set update_channel explicitly |
| 111 |
if config.get("update_channel"): |
| 112 |
update_channel = config["update_channel"] |
| 113 |
else: # Let's just give the generic channel based on branch. |
| 114 |
update_channel = "nightly-%s" % (config["branch"],) |
| 115 |
if not isinstance(update_channel, bytes): |
| 116 |
update_channel = update_channel.encode("utf-8") |
| 117 |
bootstrap_env["MOZ_UPDATE_CHANNEL"] = update_channel |
| 118 |
self.info( |
| 119 |
"Update channel set to: {}".format(bootstrap_env["MOZ_UPDATE_CHANNEL"]) |
| 120 |
) |
| 121 |
self.bootstrap_env = bootstrap_env |
| 122 |
return self.bootstrap_env |
| 123 |
|
| 124 |
def _query_upload_env(self): |
| 125 |
"""returns the environment used for the upload step""" |
| 126 |
if self.upload_env: |
| 127 |
return self.upload_env |
| 128 |
config = self.config |
| 129 |
|
| 130 |
upload_env = self.query_env(partial_env=config.get("upload_env")) |
| 131 |
# check if there are any extra option from the platform configuration |
| 132 |
# and append them to the env |
| 133 |
|
| 134 |
if "upload_env_extra" in config: |
| 135 |
for extra in config["upload_env_extra"]: |
| 136 |
upload_env[extra] = config["upload_env_extra"][extra] |
| 137 |
|
| 138 |
self.upload_env = upload_env |
| 139 |
return self.upload_env |
| 140 |
|
| 141 |
def query_l10n_env(self): |
| 142 |
l10n_env = self._query_upload_env().copy() |
| 143 |
l10n_env.update(self.query_bootstrap_env()) |
| 144 |
return l10n_env |
| 145 |
|
| 146 |
def _query_make_variable(self, variable, make_args=None): |
| 147 |
"""returns the value of make echo-variable-<variable> |
| 148 |
it accepts extra make arguements (make_args) |
| 149 |
""" |
| 150 |
dirs = self.query_abs_dirs() |
| 151 |
make_args = make_args or [] |
| 152 |
target = ["echo-variable-%s" % variable] + make_args |
| 153 |
cwd = dirs["abs_locales_dir"] |
| 154 |
raw_output = self._get_output_from_make( |
| 155 |
target, cwd=cwd, env=self.query_bootstrap_env() |
| 156 |
) |
| 157 |
# we want to log all the messages from make |
| 158 |
output = [] |
| 159 |
for line in raw_output.split("\n"): |
| 160 |
output.append(line.strip()) |
| 161 |
output = " ".join(output).strip() |
| 162 |
self.info("echo-variable-%s: %s" % (variable, output)) |
| 163 |
return output |
| 164 |
|
| 165 |
def _map(self, func, items): |
| 166 |
"""runs func for any item in items, calls the add_failure() for each |
| 167 |
error. It assumes that function returns 0 when successful. |
| 168 |
returns a two element tuple with (success_count, total_count)""" |
| 169 |
success_count = 0 |
| 170 |
total_count = len(items) |
| 171 |
name = func.__name__ |
| 172 |
for item in items: |
| 173 |
result = func(item) |
| 174 |
if result == SUCCESS: |
| 175 |
# success! |
| 176 |
success_count += 1 |
| 177 |
else: |
| 178 |
# func failed... |
| 179 |
message = "failure: %s(%s)" % (name, item) |
| 180 |
self.add_failure(item, message) |
| 181 |
return (success_count, total_count) |
| 182 |
|
| 183 |
# Actions {{{2 |
| 184 |
def clone_locales(self): |
| 185 |
self.pull_locale_source() |
| 186 |
|
| 187 |
def setup(self): |
| 188 |
"""setup step""" |
| 189 |
self._run_tooltool() |
| 190 |
self._copy_mozconfig() |
| 191 |
self._mach_configure() |
| 192 |
self._make_export() |
| 193 |
|
| 194 |
def _make_export(self): |
| 195 |
"""this step creates nsinstall, needed on Windows hosts |
| 196 |
and creates buildid.h, used by NSIS installer for Windows UBR telemetry |
| 197 |
""" |
| 198 |
dirs = self.query_abs_dirs() |
| 199 |
config_dir = os.path.join(dirs["abs_obj_dir"], "config") |
| 200 |
env = self.query_bootstrap_env() |
| 201 |
self._make(target=["export"], cwd=config_dir, env=env) |
| 202 |
return self._make(target=["buildid.h"], cwd=dirs["abs_obj_dir"], env=env) |
| 203 |
|
| 204 |
def _copy_mozconfig(self): |
| 205 |
"""copies the mozconfig file into abs_src_dir/.mozconfig |
| 206 |
and logs the content |
| 207 |
""" |
| 208 |
config = self.config |
| 209 |
dirs = self.query_abs_dirs() |
| 210 |
src = get_mozconfig_path(self, config, dirs) |
| 211 |
dst = os.path.join(dirs["abs_src_dir"], ".mozconfig") |
| 212 |
self.copyfile(src, dst) |
| 213 |
self.read_from_file(dst, verbose=True) |
| 214 |
|
| 215 |
def _mach(self, target, env, halt_on_failure=True, output_parser=None): |
| 216 |
dirs = self.query_abs_dirs() |
| 217 |
mach = self._get_mach_executable() |
| 218 |
return self.run_command( |
| 219 |
mach + target, |
| 220 |
halt_on_failure=True, |
| 221 |
env=env, |
| 222 |
cwd=dirs["abs_src_dir"], |
| 223 |
output_parser=None, |
| 224 |
) |
| 225 |
|
| 226 |
def _mach_configure(self): |
| 227 |
"""calls mach configure""" |
| 228 |
env = self.query_bootstrap_env() |
| 229 |
target = ["configure"] |
| 230 |
return self._mach(target=target, env=env) |
| 231 |
|
| 232 |
def _get_mach_executable(self): |
| 233 |
return [sys.executable, "mach"] |
| 234 |
|
| 235 |
def _make( |
| 236 |
self, |
| 237 |
target, |
| 238 |
cwd, |
| 239 |
env, |
| 240 |
error_list=MakefileErrorList, |
| 241 |
halt_on_failure=True, |
| 242 |
output_parser=None, |
| 243 |
): |
| 244 |
"""Runs make. Returns the exit code""" |
| 245 |
make = ["make"] |
| 246 |
if target: |
| 247 |
make = make + target |
| 248 |
return self.run_command( |
| 249 |
make, |
| 250 |
cwd=cwd, |
| 251 |
env=env, |
| 252 |
error_list=error_list, |
| 253 |
halt_on_failure=halt_on_failure, |
| 254 |
output_parser=output_parser, |
| 255 |
) |
| 256 |
|
| 257 |
def _get_output_from_make( |
| 258 |
self, target, cwd, env, halt_on_failure=True, ignore_errors=False |
| 259 |
): |
| 260 |
"""runs make and returns the output of the command""" |
| 261 |
return self.get_output_from_command( |
| 262 |
["make"] + target, |
| 263 |
cwd=cwd, |
| 264 |
env=env, |
| 265 |
silent=True, |
| 266 |
halt_on_failure=halt_on_failure, |
| 267 |
ignore_errors=ignore_errors, |
| 268 |
) |
| 269 |
|
| 270 |
def make_upload(self, locale): |
| 271 |
"""wrapper for make upload command""" |
| 272 |
env = self.query_l10n_env() |
| 273 |
dirs = self.query_abs_dirs() |
| 274 |
target = ["upload", "AB_CD=%s" % (locale)] |
| 275 |
cwd = dirs["abs_locales_dir"] |
| 276 |
parser = MakeUploadOutputParser(config=self.config, log_obj=self.log_obj) |
| 277 |
retval = self._make( |
| 278 |
target=target, cwd=cwd, env=env, halt_on_failure=False, output_parser=parser |
| 279 |
) |
| 280 |
if retval == SUCCESS: |
| 281 |
self.info("Upload successful (%s)" % locale) |
| 282 |
ret = SUCCESS |
| 283 |
else: |
| 284 |
self.error("failed to upload %s" % locale) |
| 285 |
ret = FAILURE |
| 286 |
|
| 287 |
if ret == FAILURE: |
| 288 |
# If we failed above, we shouldn't even attempt a SIMPLE_NAME move |
| 289 |
# even if we are configured to do so |
| 290 |
return ret |
| 291 |
|
| 292 |
# XXX Move the files to a SIMPLE_NAME format until we can enable |
| 293 |
# Simple names in the build system |
| 294 |
if self.config.get("simple_name_move"): |
| 295 |
# Assume an UPLOAD PATH |
| 296 |
upload_target = self.config["upload_env"]["UPLOAD_PATH"] |
| 297 |
target_path = os.path.join(upload_target, locale) |
| 298 |
self.mkdir_p(target_path) |
| 299 |
glob_name = "*.%s.*" % locale |
| 300 |
matches = ( |
| 301 |
glob.glob(os.path.join(upload_target, glob_name)) |
| 302 |
+ glob.glob(os.path.join(upload_target, "update", glob_name)) |
| 303 |
+ glob.glob(os.path.join(upload_target, "*", "xpi", glob_name)) |
| 304 |
+ glob.glob(os.path.join(upload_target, "install", "sea", glob_name)) |
| 305 |
+ glob.glob(os.path.join(upload_target, "setup.exe")) |
| 306 |
+ glob.glob(os.path.join(upload_target, "setup-stub.exe")) |
| 307 |
) |
| 308 |
targets_exts = [ |
| 309 |
"tar.bz2", |
| 310 |
"tar.xz", |
| 311 |
"dmg", |
| 312 |
"langpack.xpi", |
| 313 |
"checksums", |
| 314 |
"zip", |
| 315 |
"installer.exe", |
| 316 |
"installer-stub.exe", |
| 317 |
] |
| 318 |
targets = [(".%s" % (ext,), "target.%s" % (ext,)) for ext in targets_exts] |
| 319 |
targets.extend([(f, f) for f in ("setup.exe", "setup-stub.exe")]) |
| 320 |
for f in matches: |
| 321 |
possible_targets = [ |
| 322 |
(tail, target_file) |
| 323 |
for (tail, target_file) in targets |
| 324 |
if f.endswith(tail) |
| 325 |
] |
| 326 |
if len(possible_targets) == 1: |
| 327 |
_, target_file = possible_targets[0] |
| 328 |
# Remove from list of available options for this locale |
| 329 |
targets.remove(possible_targets[0]) |
| 330 |
else: |
| 331 |
# wasn't valid (or already matched) |
| 332 |
raise RuntimeError( |
| 333 |
"Unexpected matching file name encountered: %s" % f |
| 334 |
) |
| 335 |
self.move(os.path.join(f), os.path.join(target_path, target_file)) |
| 336 |
self.log("Converted uploads for %s to simple names" % locale) |
| 337 |
return ret |
| 338 |
|
| 339 |
def set_upload_files(self, locale): |
| 340 |
# The tree doesn't have a good way of exporting the list of files |
| 341 |
# created during locale generation, but we can grab them by echoing the |
| 342 |
# UPLOAD_FILES variable for each locale. |
| 343 |
env = self.query_l10n_env() |
| 344 |
target = [ |
| 345 |
"echo-variable-UPLOAD_FILES", |
| 346 |
"echo-variable-CHECKSUM_FILES", |
| 347 |
"AB_CD=%s" % locale, |
| 348 |
] |
| 349 |
dirs = self.query_abs_dirs() |
| 350 |
cwd = dirs["abs_locales_dir"] |
| 351 |
# Bug 1242771 - echo-variable-UPLOAD_FILES via mozharness fails when stderr is found |
| 352 |
# we should ignore stderr as unfortunately it's expected when parsing for values |
| 353 |
output = self._get_output_from_make( |
| 354 |
target=target, cwd=cwd, env=env, ignore_errors=True |
| 355 |
) |
| 356 |
self.info('UPLOAD_FILES is "%s"' % output) |
| 357 |
files = shlex.split(output) |
| 358 |
if not files: |
| 359 |
self.error("failed to get upload file list for locale %s" % locale) |
| 360 |
return FAILURE |
| 361 |
|
| 362 |
self.upload_files[locale] = [ |
| 363 |
os.path.abspath(os.path.join(cwd, f)) for f in files |
| 364 |
] |
| 365 |
return SUCCESS |
| 366 |
|
| 367 |
def make_installers(self, locale): |
| 368 |
"""wrapper for make installers-(locale)""" |
| 369 |
env = self.query_l10n_env() |
| 370 |
env["PYTHONIOENCODING"] = "utf-8" |
| 371 |
self._copy_mozconfig() |
| 372 |
dirs = self.query_abs_dirs() |
| 373 |
cwd = os.path.join(dirs["abs_locales_dir"]) |
| 374 |
target = [ |
| 375 |
"installers-%s" % locale, |
| 376 |
] |
| 377 |
return self._make(target=target, cwd=cwd, env=env, halt_on_failure=False) |
| 378 |
|
| 379 |
def repack_locale(self, locale): |
| 380 |
"""wraps the logic for make installers and generating |
| 381 |
complete updates.""" |
| 382 |
|
| 383 |
# run make installers |
| 384 |
if self.make_installers(locale) != SUCCESS: |
| 385 |
self.error("make installers-%s failed" % (locale)) |
| 386 |
return FAILURE |
| 387 |
|
| 388 |
# now try to upload the artifacts |
| 389 |
if self.make_upload(locale): |
| 390 |
self.error("make upload for locale %s failed!" % (locale)) |
| 391 |
return FAILURE |
| 392 |
|
| 393 |
# set_upload_files() should be called after make upload, to make sure |
| 394 |
# we have all files in place (checksums, etc) |
| 395 |
if self.set_upload_files(locale): |
| 396 |
self.error("failed to get list of files to upload for locale %s" % locale) |
| 397 |
return FAILURE |
| 398 |
|
| 399 |
return SUCCESS |
| 400 |
|
| 401 |
def repack(self): |
| 402 |
"""creates the repacks and udpates""" |
| 403 |
self._map(self.repack_locale, self.query_locales()) |
| 404 |
|
| 405 |
def _run_tooltool(self): |
| 406 |
env = self.query_bootstrap_env() |
| 407 |
config = self.config |
| 408 |
dirs = self.query_abs_dirs() |
| 409 |
manifest_src = os.environ.get("TOOLTOOL_MANIFEST") |
| 410 |
if not manifest_src: |
| 411 |
manifest_src = config.get("tooltool_manifest_src") |
| 412 |
if not manifest_src: |
| 413 |
return |
| 414 |
python = sys.executable |
| 415 |
|
| 416 |
cmd = [ |
| 417 |
python, |
| 418 |
"-u", |
| 419 |
os.path.join(dirs["abs_src_dir"], "mach"), |
| 420 |
"artifact", |
| 421 |
"toolchain", |
| 422 |
"-v", |
| 423 |
"--retry", |
| 424 |
"4", |
| 425 |
"--artifact-manifest", |
| 426 |
os.path.join(dirs["abs_src_dir"], "toolchains.json"), |
| 427 |
] |
| 428 |
if manifest_src: |
| 429 |
cmd.extend( |
| 430 |
[ |
| 431 |
"--tooltool-manifest", |
| 432 |
os.path.join(dirs["abs_src_dir"], manifest_src), |
| 433 |
] |
| 434 |
) |
| 435 |
cache = config["bootstrap_env"].get("TOOLTOOL_CACHE") |
| 436 |
if cache: |
| 437 |
cmd.extend(["--cache-dir", cache]) |
| 438 |
self.info(str(cmd)) |
| 439 |
self.run_command(cmd, cwd=dirs["abs_src_dir"], halt_on_failure=True, env=env) |
| 440 |
|
| 441 |
|
| 442 |
# main {{{ |
| 443 |
if __name__ == "__main__": |
| 444 |
single_locale = DesktopSingleLocale() |
| 445 |
single_locale.run_and_exit() |