releasetools: Track edify function API changes.
In particular, it replaces the generated calls to edify function of
`apply_patch()` and `apply_patch_check()` with `patch_partition()` and
`patch_partition_check()` instead.
It adds two functions to EdifyGenerator: PatchPartition() and
PatchPartitionCheck() for Python releasetools callers. It also tries to
handle the callers of obsolete EdifyGenerator.PatchCheck() and
EdifyGenerator.ApplyPatch(), if their inputs are in known format.
Otherwise it raises an exception to avoid generating an OTA that updater
doesn't understand.
It requires the matching updater changes in the same topic.
Bug: 110106408
Test: Generate an incremental package with the new script and updater.
Apply the package on device.
Test: Revert the change in ota_from_target_files.py, so that it calls
the obsolete EdifyGenerator functions. Check that it generates the
same incremental package.
Change-Id: Ifc55cb40d3a45116fd4d408536d037eca249effa
diff --git a/tools/releasetools/edify_generator.py b/tools/releasetools/edify_generator.py
index 3595a9e..7ed85fe 100644
--- a/tools/releasetools/edify_generator.py
+++ b/tools/releasetools/edify_generator.py
@@ -163,15 +163,32 @@
[0,1]."""
self.script.append("set_progress(%f);" % (frac,))
- def PatchCheck(self, filename, *sha1):
- """Check that the given file has one of the
- given *sha1 hashes, checking the version saved in cache if the
- file does not match."""
- self.script.append(
- 'apply_patch_check("%s"' % (filename,) +
- "".join([', "%s"' % (i,) for i in sha1]) +
- ') || abort("E%d: \\"%s\\" has unexpected contents.");' % (
- common.ErrorCode.BAD_PATCH_FILE, filename))
+ def PatchCheck(self, filename, *sha1): # pylint: disable=unused-argument
+ """Checks that the given partition has the desired checksum.
+
+ The call to this function is being deprecated in favor of
+ PatchPartitionCheck(). It will try to parse and handle the old format,
+ unless the format is unknown.
+ """
+ tokens = filename.split(':')
+ assert len(tokens) == 6 and tokens[0] == 'EMMC', \
+ "Failed to handle unknown format. Use PatchPartitionCheck() instead."
+ source = '{}:{}:{}:{}'.format(tokens[0], tokens[1], tokens[2], tokens[3])
+ target = '{}:{}:{}:{}'.format(tokens[0], tokens[1], tokens[4], tokens[5])
+ self.PatchPartitionCheck(target, source)
+
+ def PatchPartitionCheck(self, target, source):
+ """Checks whether updater can patch the given partitions.
+
+ It checks the checksums of the given partitions. If none of them matches the
+ expected checksum, updater will additionally look for a backup on /cache.
+ """
+ self.script.append(self.WordWrap((
+ 'patch_partition_check("{target}",\0"{source}") ||\n abort('
+ '"E{code}: \\"{target}\\" or \\"{source}\\" has unexpected '
+ 'contents.");').format(
+ target=target, source=source,
+ code=common.ErrorCode.BAD_PATCH_FILE)))
def CacheFreeSpaceCheck(self, amount):
"""Check that there's at least 'amount' space that can be made
@@ -251,17 +268,41 @@
def ApplyPatch(self, srcfile, tgtfile, tgtsize, tgtsha1, *patchpairs):
"""Apply binary patches (in *patchpairs) to the given srcfile to
produce tgtfile (which may be "-" to indicate overwriting the
- source file."""
- if len(patchpairs) % 2 != 0 or len(patchpairs) == 0:
- raise ValueError("bad patches given to ApplyPatch")
- cmd = ['apply_patch("%s",\0"%s",\0%s,\0%d'
- % (srcfile, tgtfile, tgtsha1, tgtsize)]
- for i in range(0, len(patchpairs), 2):
- cmd.append(',\0%s,\0package_extract_file("%s")' % patchpairs[i:i+2])
- cmd.append(') ||\n abort("E%d: Failed to apply patch to %s");' % (
- common.ErrorCode.APPLY_PATCH_FAILURE, srcfile))
- cmd_str = "".join(cmd)
- self.script.append(self.WordWrap(cmd_str))
+ source file.
+
+ This edify function is being deprecated in favor of PatchPartition(). It
+ will try to redirect calls to PatchPartition() if possible. On unknown /
+ invalid inputs, raises an exception.
+ """
+ tokens = srcfile.split(':')
+ assert (len(tokens) == 6 and tokens[0] == 'EMMC' and tgtfile == '-' and
+ len(patchpairs) == 2), \
+ "Failed to handle unknown format. Use PatchPartition() instead."
+
+ # Also sanity check the args.
+ assert tokens[3] == patchpairs[0], \
+ "Found mismatching values for source SHA-1: {} vs {}".format(
+ tokens[3], patchpairs[0])
+ assert int(tokens[4]) == tgtsize, \
+ "Found mismatching values for target size: {} vs {}".format(
+ tokens[4], tgtsize)
+ assert tokens[5] == tgtsha1, \
+ "Found mismatching values for target SHA-1: {} vs {}".format(
+ tokens[5], tgtsha1)
+
+ source = '{}:{}:{}:{}'.format(tokens[0], tokens[1], tokens[2], tokens[3])
+ target = '{}:{}:{}:{}'.format(tokens[0], tokens[1], tokens[4], tokens[5])
+ patch = patchpairs[1]
+ self.PatchPartition(target, source, patch)
+
+ def PatchPartition(self, target, source, patch):
+ """Applies the patch to the source partition and writes it to target."""
+ self.script.append(self.WordWrap((
+ 'patch_partition("{target}",\0"{source}",\0'
+ 'package_extract_file("{patch}")) ||\n'
+ ' abort("E{code}: Failed to apply patch to {source}");').format(
+ target=target, source=source, patch=patch,
+ code=common.ErrorCode.APPLY_PATCH_FAILURE)))
def WriteRawImage(self, mount_point, fn, mapfn=None):
"""Write the given package file into the partition for the given