blob: ecdc16731fb606ebc5f9f1ca641ed20d65d19ccc [file] [log] [blame]
Doug Zongkerc494d7c2009-06-18 08:43:44 -07001# Copyright (C) 2009 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Doug Zongkerc494d7c2009-06-18 08:43:44 -070015import re
16
17import common
18
19class EdifyGenerator(object):
20 """Class to generate scripts in the 'edify' recovery script language
21 used from donut onwards."""
22
Tao Bao34b47bf2015-06-22 19:17:41 -070023 def __init__(self, version, info, fstab=None):
Doug Zongkerc494d7c2009-06-18 08:43:44 -070024 self.script = []
25 self.mounts = set()
Tao Baod8d14be2016-02-04 14:26:02 -080026 self._required_cache = 0
Doug Zongkerc494d7c2009-06-18 08:43:44 -070027 self.version = version
Doug Zongkerb4c7d322010-07-01 15:30:11 -070028 self.info = info
Tao Bao34b47bf2015-06-22 19:17:41 -070029 if fstab is None:
30 self.fstab = self.info.get("fstab", None)
31 else:
32 self.fstab = fstab
Doug Zongkerc494d7c2009-06-18 08:43:44 -070033
34 def MakeTemporary(self):
35 """Make a temporary script object whose commands can latter be
36 appended to the parent script with AppendScript(). Used when the
37 caller wants to generate script commands out-of-order."""
Doug Zongker67369982010-07-07 13:53:32 -070038 x = EdifyGenerator(self.version, self.info)
Doug Zongkerc494d7c2009-06-18 08:43:44 -070039 x.mounts = self.mounts
40 return x
41
Tao Baod8d14be2016-02-04 14:26:02 -080042 @property
43 def required_cache(self):
44 """Return the minimum cache size to apply the update."""
45 return self._required_cache
46
Doug Zongkerc494d7c2009-06-18 08:43:44 -070047 @staticmethod
Dan Albert8b72aef2015-03-23 19:13:21 -070048 def WordWrap(cmd, linelen=80):
Doug Zongkerc494d7c2009-06-18 08:43:44 -070049 """'cmd' should be a function call with null characters after each
50 parameter (eg, "somefun(foo,\0bar,\0baz)"). This function wraps cmd
51 to a given line length, replacing nulls with spaces and/or newlines
52 to format it nicely."""
53 indent = cmd.index("(")+1
54 out = []
55 first = True
56 x = re.compile("^(.{,%d})\0" % (linelen-indent,))
57 while True:
58 if not first:
59 out.append(" " * indent)
60 first = False
61 m = x.search(cmd)
62 if not m:
63 parts = cmd.split("\0", 1)
64 out.append(parts[0]+"\n")
65 if len(parts) == 1:
66 break
67 else:
68 cmd = parts[1]
69 continue
70 out.append(m.group(1)+"\n")
71 cmd = cmd[m.end():]
72
73 return "".join(out).replace("\0", " ").rstrip("\n")
74
75 def AppendScript(self, other):
76 """Append the contents of another script (which should be created
77 with temporary=True) to this one."""
78 self.script.extend(other.script)
79
Michael Runge6e836112014-04-15 17:40:21 -070080 def AssertOemProperty(self, name, value):
81 """Assert that a property on the OEM paritition matches a value."""
82 if not name:
83 raise ValueError("must specify an OEM property")
84 if not value:
85 raise ValueError("must specify the OEM value")
Tao Bao8608cde2016-02-25 19:49:55 -080086 if common.OPTIONS.oem_no_mount:
87 cmd = ('getprop("{name}") == "{value}" || '
88 'abort("This package expects the value \\"{value}\\" for '
89 '\\"{name}\\"; this has value \\"" + '
90 'getprop("{name}") + "\\".");').format(name=name, value=value)
91 else:
92 cmd = ('file_getprop("/oem/oem.prop", "{name}") == "{value}" || '
93 'abort("This package expects the value \\"{value}\\" for '
94 '\\"{name}\\" on the OEM partition; this has value \\"" + '
95 'file_getprop("/oem/oem.prop", "{name}") + "\\".");').format(
96 name=name, value=value)
Michael Runge6e836112014-04-15 17:40:21 -070097 self.script.append(cmd)
98
Doug Zongkerc494d7c2009-06-18 08:43:44 -070099 def AssertSomeFingerprint(self, *fp):
Doug Zongkeraf845252014-05-09 08:29:05 -0700100 """Assert that the current recovery build fingerprint is one of *fp."""
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700101 if not fp:
102 raise ValueError("must specify some fingerprints")
Dan Albert8b72aef2015-03-23 19:13:21 -0700103 cmd = (' ||\n '.join([('getprop("ro.build.fingerprint") == "%s"') % i
104 for i in fp]) +
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700105 ' ||\n abort("Package expects build fingerprint of %s; this '
Dan Albert8b72aef2015-03-23 19:13:21 -0700106 'device has " + getprop("ro.build.fingerprint") + ".");') % (
107 " or ".join(fp))
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700108 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700109
Michael Runge6e836112014-04-15 17:40:21 -0700110 def AssertSomeThumbprint(self, *fp):
Doug Zongkeraf845252014-05-09 08:29:05 -0700111 """Assert that the current recovery build thumbprint is one of *fp."""
Geremy Condra36bd3652014-02-06 19:45:10 -0800112 if not fp:
Michael Runge6e836112014-04-15 17:40:21 -0700113 raise ValueError("must specify some thumbprints")
Dan Albert8b72aef2015-03-23 19:13:21 -0700114 cmd = (' ||\n '.join([('getprop("ro.build.thumbprint") == "%s"') % i
115 for i in fp]) +
Michael Runge6e836112014-04-15 17:40:21 -0700116 ' ||\n abort("Package expects build thumbprint of %s; this '
Dan Albert8b72aef2015-03-23 19:13:21 -0700117 'device has " + getprop("ro.build.thumbprint") + ".");') % (
118 " or ".join(fp))
Geremy Condra36bd3652014-02-06 19:45:10 -0800119 self.script.append(cmd)
120
Tao Bao3e30d972016-03-15 13:20:19 -0700121 def AssertFingerprintOrThumbprint(self, fp, tp):
122 """Assert that the current recovery build fingerprint is fp, or thumbprint
123 is tp."""
124 cmd = ('getprop("ro.build.fingerprint") == "{fp}" ||\n'
125 ' getprop("ro.build.thumbprint") == "{tp}" ||\n'
126 ' abort("Package expects build fingerprint of {fp} or '
127 'thumbprint of {tp}; this device has a fingerprint of " '
128 '+ getprop("ro.build.fingerprint") and a thumbprint of " '
129 '+ getprop("ro.build.thumbprint") + ".");').format(fp=fp, tp=tp)
130 self.script.append(cmd)
131
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700132 def AssertOlderBuild(self, timestamp, timestamp_text):
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700133 """Assert that the build on the device is older (or the same as)
134 the given timestamp."""
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700135 self.script.append(
136 ('(!less_than_int(%s, getprop("ro.build.date.utc"))) || '
137 'abort("Can\'t install this package (%s) over newer '
Dan Albert8b72aef2015-03-23 19:13:21 -0700138 'build (" + getprop("ro.build.date") + ").");') % (timestamp,
139 timestamp_text))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700140
141 def AssertDevice(self, device):
142 """Assert that the device identifier is the given string."""
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700143 cmd = ('getprop("ro.product.device") == "%s" || '
144 'abort("This package is for \\"%s\\" devices; '
Dan Albert8b72aef2015-03-23 19:13:21 -0700145 'this is a \\"" + getprop("ro.product.device") + "\\".");') % (
146 device, device)
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700147 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700148
149 def AssertSomeBootloader(self, *bootloaders):
150 """Asert that the bootloader version is one of *bootloaders."""
151 cmd = ("assert(" +
152 " ||\0".join(['getprop("ro.bootloader") == "%s"' % (b,)
153 for b in bootloaders]) +
154 ");")
Dan Albert8b72aef2015-03-23 19:13:21 -0700155 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700156
157 def ShowProgress(self, frac, dur):
158 """Update the progress bar, advancing it over 'frac' over the next
Doug Zongker881dd402009-09-20 14:03:55 -0700159 'dur' seconds. 'dur' may be zero to advance it via SetProgress
160 commands instead of by time."""
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700161 self.script.append("show_progress(%f, %d);" % (frac, int(dur)))
162
Doug Zongker881dd402009-09-20 14:03:55 -0700163 def SetProgress(self, frac):
164 """Set the position of the progress bar within the chunk defined
165 by the most recent ShowProgress call. 'frac' should be in
166 [0,1]."""
167 self.script.append("set_progress(%f);" % (frac,))
168
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700169 def PatchCheck(self, filename, *sha1):
170 """Check that the given file (or MTD reference) has one of the
Doug Zongkerc8d446b2010-02-22 15:41:53 -0800171 given *sha1 hashes, checking the version saved in cache if the
172 file does not match."""
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700173 self.script.append(
174 'apply_patch_check("%s"' % (filename,) +
175 "".join([', "%s"' % (i,) for i in sha1]) +
176 ') || abort("\\"%s\\" has unexpected contents.");' % (filename,))
Doug Zongkerc8d446b2010-02-22 15:41:53 -0800177
Tao Bao9bc6bb22015-11-09 16:58:28 -0800178 def Verify(self, filename):
179 """Check that the given file (or MTD reference) has one of the
180 given hashes (encoded in the filename)."""
181 self.script.append(
182 'apply_patch_check("{filename}") && '
183 'ui_print(" Verified.") || '
184 'ui_print("\\"{filename}\\" has unexpected contents.");'.format(
185 filename=filename))
186
Doug Zongkerc8d446b2010-02-22 15:41:53 -0800187 def FileCheck(self, filename, *sha1):
188 """Check that the given file (or MTD reference) has one of the
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700189 given *sha1 hashes."""
Doug Zongker5a482092010-02-17 16:09:18 -0800190 self.script.append('assert(sha1_check(read_file("%s")' % (filename,) +
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700191 "".join([', "%s"' % (i,) for i in sha1]) +
192 '));')
193
194 def CacheFreeSpaceCheck(self, amount):
195 """Check that there's at least 'amount' space that can be made
196 available on /cache."""
Tao Baod8d14be2016-02-04 14:26:02 -0800197 self._required_cache = max(self._required_cache, amount)
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700198 self.script.append(('apply_patch_space(%d) || abort("Not enough free space '
Tao Baoe7b10372015-06-03 09:24:08 -0700199 'on /cache to apply patches.");') % (amount,))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700200
Michael Runge7cd99ba2014-10-22 17:21:48 -0700201 def Mount(self, mount_point, mount_options_by_format=""):
202 """Mount the partition with the given mount_point.
203 mount_options_by_format:
204 [fs_type=option[,option]...[|fs_type=option[,option]...]...]
205 where option is optname[=optvalue]
206 E.g. ext4=barrier=1,nodelalloc,errors=panic|f2fs=errors=recover
207 """
Tao Bao34b47bf2015-06-22 19:17:41 -0700208 fstab = self.fstab
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700209 if fstab:
210 p = fstab[mount_point]
Michael Runge7cd99ba2014-10-22 17:21:48 -0700211 mount_dict = {}
212 if mount_options_by_format is not None:
213 for option in mount_options_by_format.split("|"):
214 if "=" in option:
215 key, value = option.split("=", 1)
216 mount_dict[key] = value
Tao Baodf06e962015-06-10 12:32:41 -0700217 mount_flags = mount_dict.get(p.fs_type, "")
218 if p.context is not None:
219 mount_flags = p.context + ("," + mount_flags if mount_flags else "")
Dan Albert8b72aef2015-03-23 19:13:21 -0700220 self.script.append('mount("%s", "%s", "%s", "%s", "%s");' % (
221 p.fs_type, common.PARTITION_TYPES[p.fs_type], p.device,
Tao Baodf06e962015-06-10 12:32:41 -0700222 p.mount_point, mount_flags))
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700223 self.mounts.add(p.mount_point)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700224
225 def UnpackPackageDir(self, src, dst):
226 """Unpack a given directory from the OTA package into the given
227 destination directory."""
228 self.script.append('package_extract_dir("%s", "%s");' % (src, dst))
229
230 def Comment(self, comment):
231 """Write a comment into the update script."""
232 self.script.append("")
233 for i in comment.split("\n"):
234 self.script.append("# " + i)
235 self.script.append("")
236
237 def Print(self, message):
238 """Log a message to the screen (if the logs are visible)."""
239 self.script.append('ui_print("%s");' % (message,))
240
Michael Runge3e286642014-11-21 00:46:03 -0800241 def TunePartition(self, partition, *options):
Tao Bao34b47bf2015-06-22 19:17:41 -0700242 fstab = self.fstab
Michael Runge3e286642014-11-21 00:46:03 -0800243 if fstab:
244 p = fstab[partition]
Dan Albert8b72aef2015-03-23 19:13:21 -0700245 if p.fs_type not in ("ext2", "ext3", "ext4"):
Michael Runge3e286642014-11-21 00:46:03 -0800246 raise ValueError("Partition %s cannot be tuned\n" % (partition,))
Dan Albert8b72aef2015-03-23 19:13:21 -0700247 self.script.append(
248 'tune2fs(' + "".join(['"%s", ' % (i,) for i in options]) +
249 '"%s") || abort("Failed to tune partition %s");' % (
250 p.device, partition))
Michael Runge3e286642014-11-21 00:46:03 -0800251
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700252 def FormatPartition(self, partition):
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700253 """Format the given partition, specified by its mount point (eg,
254 "/system")."""
255
Tao Bao34b47bf2015-06-22 19:17:41 -0700256 fstab = self.fstab
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700257 if fstab:
258 p = fstab[partition]
Doug Zongkerdf2056e2012-04-09 12:27:43 -0700259 self.script.append('format("%s", "%s", "%s", "%s", "%s");' %
Doug Zongker086cbb02011-02-17 15:54:20 -0800260 (p.fs_type, common.PARTITION_TYPES[p.fs_type],
Doug Zongkerdf2056e2012-04-09 12:27:43 -0700261 p.device, p.length, p.mount_point))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700262
Doug Zongker5fad2032014-02-24 08:13:45 -0800263 def WipeBlockDevice(self, partition):
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700264 if partition not in ("/system", "/vendor"):
265 raise ValueError(("WipeBlockDevice doesn't work on %s\n") % (partition,))
Tao Bao34b47bf2015-06-22 19:17:41 -0700266 fstab = self.fstab
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700267 size = self.info.get(partition.lstrip("/") + "_size", None)
Doug Zongker5fad2032014-02-24 08:13:45 -0800268 device = fstab[partition].device
269
270 self.script.append('wipe_block_device("%s", %s);' % (device, size))
271
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700272 def DeleteFiles(self, file_list):
273 """Delete all files in file_list."""
Dan Albert8b72aef2015-03-23 19:13:21 -0700274 if not file_list:
275 return
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700276 cmd = "delete(" + ",\0".join(['"%s"' % (i,) for i in file_list]) + ");"
Dan Albert8b72aef2015-03-23 19:13:21 -0700277 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700278
Tao Bao84006ea2015-09-02 10:28:08 -0700279 def DeleteFilesIfNotMatching(self, file_list):
280 """Delete the file in file_list if not matching the checksum."""
281 if not file_list:
282 return
283 for name, sha1 in file_list:
284 cmd = ('sha1_check(read_file("{name}"), "{sha1}") || '
285 'delete("{name}");'.format(name=name, sha1=sha1))
286 self.script.append(self.WordWrap(cmd))
287
Michael Runge4038aa82013-12-13 18:06:28 -0800288 def RenameFile(self, srcfile, tgtfile):
289 """Moves a file from one location to another."""
290 if self.info.get("update_rename_support", False):
291 self.script.append('rename("%s", "%s");' % (srcfile, tgtfile))
292 else:
293 raise ValueError("Rename not supported by update binary")
294
295 def SkipNextActionIfTargetExists(self, tgtfile, tgtsha1):
296 """Prepend an action with an apply_patch_check in order to
297 skip the action if the file exists. Used when a patch
298 is later renamed."""
Tao Bao84006ea2015-09-02 10:28:08 -0700299 cmd = ('sha1_check(read_file("%s"), %s) ||' % (tgtfile, tgtsha1))
Dan Albert8b72aef2015-03-23 19:13:21 -0700300 self.script.append(self.WordWrap(cmd))
Michael Runge4038aa82013-12-13 18:06:28 -0800301
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700302 def ApplyPatch(self, srcfile, tgtfile, tgtsize, tgtsha1, *patchpairs):
303 """Apply binary patches (in *patchpairs) to the given srcfile to
304 produce tgtfile (which may be "-" to indicate overwriting the
305 source file."""
306 if len(patchpairs) % 2 != 0 or len(patchpairs) == 0:
307 raise ValueError("bad patches given to ApplyPatch")
308 cmd = ['apply_patch("%s",\0"%s",\0%s,\0%d'
309 % (srcfile, tgtfile, tgtsha1, tgtsize)]
310 for i in range(0, len(patchpairs), 2):
Tao Baoc3868902015-12-01 17:46:46 -0800311 cmd.append(',\0%s,\0package_extract_file("%s")' % patchpairs[i:i+2])
312 cmd.append(') ||\n abort("Failed to apply patch to %s");' % (srcfile,))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700313 cmd = "".join(cmd)
Dan Albert8b72aef2015-03-23 19:13:21 -0700314 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700315
Doug Zongker5fad2032014-02-24 08:13:45 -0800316 def WriteRawImage(self, mount_point, fn, mapfn=None):
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700317 """Write the given package file into the partition for the given
318 mount point."""
Doug Zongkerb4c7d322010-07-01 15:30:11 -0700319
Tao Bao34b47bf2015-06-22 19:17:41 -0700320 fstab = self.fstab
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700321 if fstab:
322 p = fstab[mount_point]
Doug Zongker96a57e72010-09-26 14:57:41 -0700323 partition_type = common.PARTITION_TYPES[p.fs_type]
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700324 args = {'device': p.device, 'fn': fn}
325 if partition_type == "MTD":
326 self.script.append(
Doug Zongker02da2102011-04-12 15:50:17 -0700327 'write_raw_image(package_extract_file("%(fn)s"), "%(device)s");'
328 % args)
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700329 elif partition_type == "EMMC":
Doug Zongker5fad2032014-02-24 08:13:45 -0800330 if mapfn:
331 args["map"] = mapfn
332 self.script.append(
333 'package_extract_file("%(fn)s", "%(device)s", "%(map)s");' % args)
334 else:
335 self.script.append(
336 'package_extract_file("%(fn)s", "%(device)s");' % args)
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700337 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700338 raise ValueError(
339 "don't know how to write \"%s\" partitions" % p.fs_type)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700340
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700341 def SetPermissions(self, fn, uid, gid, mode, selabel, capabilities):
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700342 """Set file ownership and permissions."""
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700343 if not self.info.get("use_set_metadata", False):
344 self.script.append('set_perm(%d, %d, 0%o, "%s");' % (uid, gid, mode, fn))
345 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700346 if capabilities is None:
347 capabilities = "0x0"
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700348 cmd = 'set_metadata("%s", "uid", %d, "gid", %d, "mode", 0%o, ' \
349 '"capabilities", %s' % (fn, uid, gid, mode, capabilities)
350 if selabel is not None:
Dan Albert8b72aef2015-03-23 19:13:21 -0700351 cmd += ', "selabel", "%s"' % selabel
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700352 cmd += ');'
353 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700354
Dan Albert8b72aef2015-03-23 19:13:21 -0700355 def SetPermissionsRecursive(self, fn, uid, gid, dmode, fmode, selabel,
356 capabilities):
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700357 """Recursively set path ownership and permissions."""
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700358 if not self.info.get("use_set_metadata", False):
359 self.script.append('set_perm_recursive(%d, %d, 0%o, 0%o, "%s");'
360 % (uid, gid, dmode, fmode, fn))
361 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700362 if capabilities is None:
363 capabilities = "0x0"
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700364 cmd = 'set_metadata_recursive("%s", "uid", %d, "gid", %d, ' \
365 '"dmode", 0%o, "fmode", 0%o, "capabilities", %s' \
366 % (fn, uid, gid, dmode, fmode, capabilities)
367 if selabel is not None:
Dan Albert8b72aef2015-03-23 19:13:21 -0700368 cmd += ', "selabel", "%s"' % selabel
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700369 cmd += ');'
370 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700371
372 def MakeSymlinks(self, symlink_list):
373 """Create symlinks, given a list of (dest, link) pairs."""
374 by_dest = {}
375 for d, l in symlink_list:
376 by_dest.setdefault(d, []).append(l)
377
378 for dest, links in sorted(by_dest.iteritems()):
379 cmd = ('symlink("%s", ' % (dest,) +
380 ",\0".join(['"' + i + '"' for i in sorted(links)]) + ");")
Dan Albert8b72aef2015-03-23 19:13:21 -0700381 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700382
383 def AppendExtra(self, extra):
384 """Append text verbatim to the output script."""
385 self.script.append(extra)
386
Michael Runge63f01de2014-10-28 19:24:19 -0700387 def Unmount(self, mount_point):
Dan Albert8b72aef2015-03-23 19:13:21 -0700388 self.script.append('unmount("%s");' % mount_point)
389 self.mounts.remove(mount_point)
Michael Runge63f01de2014-10-28 19:24:19 -0700390
Doug Zongker14833602010-02-02 13:12:04 -0800391 def UnmountAll(self):
392 for p in sorted(self.mounts):
393 self.script.append('unmount("%s");' % (p,))
394 self.mounts = set()
395
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700396 def AddToZip(self, input_zip, output_zip, input_path=None):
397 """Write the accumulated script to the output_zip file. input_zip
398 is used as the source for the 'updater' binary needed to run
399 script. If input_path is not None, it will be used as a local
400 path for the binary instead of input_zip."""
401
Doug Zongker14833602010-02-02 13:12:04 -0800402 self.UnmountAll()
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700403
404 common.ZipWriteStr(output_zip, "META-INF/com/google/android/updater-script",
405 "\n".join(self.script) + "\n")
406
407 if input_path is None:
408 data = input_zip.read("OTA/bin/updater")
409 else:
Doug Zongker25568482014-03-03 10:21:27 -0800410 data = open(input_path, "rb").read()
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700411 common.ZipWriteStr(output_zip, "META-INF/com/google/android/update-binary",
Dan Albert8b72aef2015-03-23 19:13:21 -0700412 data, perms=0o755)