michael@0: #!/usr/bin/env python michael@0: # michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import argparse michael@0: import json michael@0: import os michael@0: import shutil michael@0: import stat michael@0: import subprocess michael@0: import sys michael@0: michael@0: from ConfigParser import ConfigParser michael@0: from smoketest import * michael@0: michael@0: this_dir = os.path.abspath(os.path.dirname(__file__)) michael@0: michael@0: def stage_update(device, stage_dir): michael@0: config = SmokeTestConfig(stage_dir) michael@0: if device not in config.devices: michael@0: raise SmokeTestConfigError('Device "%s" not found' % device) michael@0: michael@0: device_data = config.devices[device] michael@0: michael@0: b2g_config = b2g.import_update_tools().B2GConfig() michael@0: target_out_dir = os.path.join(b2g.homedir, 'out', 'target', 'product', device) michael@0: app_ini = os.path.join(b2g_config.gecko_objdir, 'dist', 'bin', michael@0: 'application.ini') michael@0: gecko_mar = os.path.join(b2g_config.gecko_objdir, 'dist', 'b2g-update', michael@0: 'b2g-gecko-update.mar') michael@0: michael@0: build_data = ConfigParser() michael@0: build_data.read(app_ini) michael@0: build_id = build_data.get('App', 'buildid') michael@0: app_version = build_data.get('App', 'version') michael@0: michael@0: build_dir = os.path.join(config.top_dir, device, build_id) michael@0: if not os.path.exists(build_dir): michael@0: os.makedirs(build_dir) michael@0: michael@0: if not os.path.exists(build_dir): michael@0: raise SmokeTestError('Couldn\'t create directory: %s' % build_dir) michael@0: michael@0: build_flash_fota = os.path.join(b2g.update_tools, 'build-flash-fota.py') michael@0: michael@0: flash_zip = os.path.join(build_dir, 'flash.zip') michael@0: michael@0: print 'Building flash zip for device %s, version %s, build %s...' % \ michael@0: (device, app_version, build_id) michael@0: michael@0: subprocess.check_call([sys.executable, build_flash_fota, michael@0: '--system-dir', os.path.join(target_out_dir, 'system'), michael@0: '--system-fs-type', device_data.system_fs_type, michael@0: '--system-location', device_data.system_location, michael@0: '--data-fs-type', device_data.data_fs_type, michael@0: '--data-location', device_data.data_location, michael@0: '--output', flash_zip]) michael@0: michael@0: complete_mar = os.path.join(build_dir, 'complete.mar') michael@0: shutil.copy(gecko_mar, complete_mar) michael@0: michael@0: flash_template = open(os.path.join(this_dir, 'flash-template.sh')).read() michael@0: flash_script = os.path.join(build_dir, 'flash.sh') michael@0: open(os.path.join(build_dir, 'flash.sh'), 'w').write(flash_template % { michael@0: 'flash_zip': flash_zip, michael@0: 'sdcard': device_data.sdcard, michael@0: 'sdcard_recovery': device_data.sdcard_recovery michael@0: }) michael@0: os.chmod(flash_script, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | michael@0: stat.S_IRGRP | stat.S_IXGRP | michael@0: stat.S_IROTH | stat.S_IXOTH) michael@0: michael@0: def main(): michael@0: parser = argparse.ArgumentParser(prog='stage-update.py') michael@0: parser.add_argument('device', help='device name for staging') michael@0: parser.add_argument('stage_dir', michael@0: help='directory to stage update and flash script for smoketests') michael@0: args = parser.parse_args() michael@0: michael@0: try: michael@0: global b2g michael@0: b2g = find_b2g() michael@0: except EnvironmentError, e: michael@0: parser.exit(1, 'This tool must be run while inside the B2G directory, ' michael@0: 'or B2G_HOME must be set in the environment.') michael@0: michael@0: try: michael@0: stage_update(args.device, args.stage_dir) michael@0: except SmokeTestError, e: michael@0: parser.exit(1, str(e)) michael@0: michael@0: if __name__ == '__main__': michael@0: main()