summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/engine.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/wic/engine.py')
-rw-r--r--scripts/lib/wic/engine.py62
1 files changed, 45 insertions, 17 deletions
diff --git a/scripts/lib/wic/engine.py b/scripts/lib/wic/engine.py
index 18776fa8a0..7e6620747d 100644
--- a/scripts/lib/wic/engine.py
+++ b/scripts/lib/wic/engine.py
@@ -19,6 +19,7 @@ import os
19import tempfile 19import tempfile
20import json 20import json
21import subprocess 21import subprocess
22import re
22 23
23from collections import namedtuple, OrderedDict 24from collections import namedtuple, OrderedDict
24from distutils.spawn import find_executable 25from distutils.spawn import find_executable
@@ -335,25 +336,52 @@ class Disk:
335 exec_cmd(cmd, as_shell=True) 336 exec_cmd(cmd, as_shell=True)
336 self._put_part_image(pnum) 337 self._put_part_image(pnum)
337 338
338 def remove(self, pnum, path): 339 def remove_ext(self, pnum, path, recursive):
340 """
341 Remove files/dirs and their contents from the partition.
342 This only applies to ext* partition.
343 """
344 abs_path = re.sub('\/\/+', '/', path)
345 cmd = "{} {} -wR 'rm \"{}\"'".format(self.debugfs,
346 self._get_part_image(pnum),
347 abs_path)
348 out = exec_cmd(cmd , as_shell=True)
349 for line in out.splitlines():
350 if line.startswith("rm:"):
351 if "file is a directory" in line:
352 if recursive:
353 # loop through content and delete them one by one if
354 # flaged with -r
355 subdirs = iter(self.dir(pnum, abs_path).splitlines())
356 next(subdirs)
357 for subdir in subdirs:
358 dir = subdir.split(':')[1].split(" ", 1)[1]
359 if not dir == "." and not dir == "..":
360 self.remove_ext(pnum, "%s/%s" % (abs_path, dir), recursive)
361
362 rmdir_out = exec_cmd("{} {} -wR 'rmdir \"{}\"'".format(self.debugfs,
363 self._get_part_image(pnum),
364 abs_path.rstrip('/'))
365 , as_shell=True)
366
367 for rmdir_line in rmdir_out.splitlines():
368 if "directory not empty" in rmdir_line:
369 raise WicError("Could not complete operation: \n%s \n"
370 "use -r to remove non-empty directory" % rmdir_line)
371 if rmdir_line.startswith("rmdir:"):
372 raise WicError("Could not complete operation: \n%s "
373 "\n%s" % (str(line), rmdir_line))
374
375 else:
376 raise WicError("Could not complete operation: \n%s "
377 "\nUnable to remove %s" % (str(line), abs_path))
378
379 def remove(self, pnum, path, recursive):
339 """Remove files/dirs from the partition.""" 380 """Remove files/dirs from the partition."""
340 partimg = self._get_part_image(pnum) 381 partimg = self._get_part_image(pnum)
341 if self.partitions[pnum].fstype.startswith('ext'): 382 if self.partitions[pnum].fstype.startswith('ext'):
342 cmd = "{} {} -wR 'rm {}'".format(self.debugfs, 383 self.remove_ext(pnum, path, recursive)
343 self._get_part_image(pnum), 384
344 path)
345 out = exec_cmd(cmd , as_shell=True)
346 for line in out.splitlines():
347 if line.startswith("rm:"):
348 if "file is a directory" in line:
349 # Try rmdir to see if this is an empty directory. This won't delete
350 # any non empty directory so let user know about any error that this might
351 # generate.
352 print(exec_cmd("{} {} -wR 'rmdir {}'".format(self.debugfs,
353 self._get_part_image(pnum),
354 path), as_shell=True))
355 else:
356 raise WicError("Could not complete operation: wic %s" % str(line))
357 else: # fat 385 else: # fat
358 cmd = "{} -i {} ::{}".format(self.mdel, partimg, path) 386 cmd = "{} -i {} ::{}".format(self.mdel, partimg, path)
359 try: 387 try:
@@ -535,7 +563,7 @@ def wic_rm(args, native_sysroot):
535 partitioned image. 563 partitioned image.
536 """ 564 """
537 disk = Disk(args.path.image, native_sysroot) 565 disk = Disk(args.path.image, native_sysroot)
538 disk.remove(args.path.part, args.path.path) 566 disk.remove(args.path.part, args.path.path, args.recursive_delete)
539 567
540def wic_write(args, native_sysroot): 568def wic_write(args, native_sysroot):
541 """ 569 """