def PushMetadata(dut, otafile, metadata_path): payload = update_payload.Payload(otafile) payload.Init() with tempfile.TemporaryDirectory() as tmpdir: with zipfile.ZipFile(otafile, "r") as zfp: extracted_path = os.path.join(tmpdir, "payload.bin") with zfp.open("payload.bin") as payload_fp, \ open(extracted_path, "wb") as output_fp: # Only extract the first |data_offset| bytes from the payload. # This is because allocateSpaceForPayload only needs to see # the manifest, not the entire payload. # Extracting the entire payload works, but is slow for full # OTA. output_fp.write(payload_fp.read(payload.data_offset)) return dut.adb(["push", extracted_path, metadata_path]) == 0
def do_POST(self): # pylint: disable=invalid-name """Reply with the omaha response xml.""" if self.path != '/update': self.send_error(404, 'Unknown request') return if not self.serving_payload: self.send_error(500, 'No serving payload set') return try: f = open(self.serving_payload, 'rb') except IOError: self.send_error(404, 'File not found') return content_length = int(self.headers.getheader('Content-Length')) request_xml = self.rfile.read(content_length) xml_root = xml.etree.ElementTree.fromstring(request_xml) appid = None for app in xml_root.iter('app'): if 'appid' in app.attrib: appid = app.attrib['appid'] break if not appid: self.send_error(400, 'No appid in Omaha request') return self.send_response(200) self.send_header("Content-type", "text/xml") self.end_headers() serving_start, serving_size = self.serving_range sha256 = hashlib.sha256() f.seek(serving_start) bytes_to_hash = serving_size while bytes_to_hash: buf = f.read(min(bytes_to_hash, 1024 * 1024)) if not buf: self.send_error(500, 'Payload too small') return sha256.update(buf) bytes_to_hash -= len(buf) payload = update_payload.Payload(f, payload_file_offset=serving_start) payload.Init() response_xml = ''' <?xml version="1.0" encoding="UTF-8"?> <response protocol="3.0"> <app appid="{appid}"> <updatecheck status="ok"> <urls> <url codebase="http://127.0.0.1:{port}/"/> </urls> <manifest version="0.0.0.1"> <actions> <action event="install" run="payload"/> <action event="postinstall" MetadataSize="{metadata_size}"/> </actions> <packages> <package hash_sha256="{payload_hash}" name="payload" size="{payload_size}"/> </packages> </manifest> </updatecheck> </app> </response> '''.format(appid=appid, port=DEVICE_PORT, metadata_size=payload.metadata_size, payload_hash=sha256.hexdigest(), payload_size=serving_size) self.wfile.write(response_xml.strip()) return