support/package-builder/clean-up-chroot.py
87815216
 #!/usr/bin/python3
a8025533
 import subprocess
 import sys
 
 def cleanUpChroot(chrootPath):
87815216
     returnVal, listmountpoints = findmountpoints(chrootPath)
09a03988
 
a8025533
     if not returnVal:
         return False
09a03988
 
87815216
     sortmountpoints(listmountpoints)
09a03988
 
87815216
     print(listmountpoints)
09a03988
 
a8025533
     if not unmountmountpoints(listmountpoints):
         return False
09a03988
 
a8025533
     if not removeAllFilesFromChroot(chrootPath):
         return False
09a03988
 
a8025533
     return True
 
 def removeAllFilesFromChroot(chrootPath):
8f56b626
     cmd = "rm -rf " + chrootPath
87815216
     process = subprocess.Popen("%s" %cmd, shell=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
a8025533
     retval = process.wait()
87815216
     if retval != 0:
         print("Unable to remove files from chroot " + chrootPath)
a8025533
         return False
     return True
09a03988
 
a8025533
 def unmountmountpoints(listmountpoints):
     if listmountpoints is None:
         return True
87815216
     result = True
a8025533
     for mountpoint in listmountpoints:
87815216
         cmd = "umount " + mountpoint
         process = subprocess.Popen("%s" %cmd, shell=True, stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
a8025533
         retval = process.wait()
87815216
         if retval != 0:
a8025533
             result = False
87815216
             print("Unable to unmount " + mountpoint)
a8025533
             break
     if not result:
87815216
         print("Unable to unmount all mounts. Unable to clean up the chroot")
a8025533
         return False
     return True
09a03988
 
a8025533
 def findmountpoints(chrootPath):
4a6785ba
     if not chrootPath.endswith("/"):
         chrootPath = chrootPath + "/"
87815216
     cmd = "mount | grep " + chrootPath + " | cut -d' ' -s -f3"
     process = subprocess.Popen("%s" %cmd, shell=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
a8025533
     retval = process.wait()
87815216
     if retval != 0:
         print("Unable to find mountpoints in chroot")
         return False, None
     mountpoints = process.communicate()[0].decode()
     mountpoints = mountpoints.replace("\n", " ").strip()
a8025533
     if mountpoints == "":
87815216
         print("No mount points found")
         return True, None
     listmountpoints = mountpoints.split(" ")
     return True, listmountpoints
a8025533
 
87815216
 def sortmountpoints(listmountpoints):
a8025533
     if listmountpoints is None:
         return True
87815216
     sortedmountpoints = listmountpoints
a8025533
     sorted(sortedmountpoints)
     sortedmountpoints.reverse()
 
 def main():
     if len(sys.argv) < 2:
87815216
         print("Usage: ./clean-up-chroot.py <chrootpath>")
09a03988
         sys.exit(1)
     if not cleanUpChroot(sys.argv[1]):
         sys.exit(1)
     sys.exit(0)
a8025533
 
87815216
 if __name__ == "__main__":
a8025533
     main()