# Path: subuserlib/classes/installationTask.py
# class InstallationTask(UserOwnedObject):
#   def __init__(self,op):
#     UserOwnedObject.__init__(self,op.user)
#     self.op = op
#     self.__upToDateImageSources = set()
#     self.__outOfDateImageSources = set()
#     self.__outOfDateSubusers = None
#     self.__subusersWhosImagesFailedToBuild = set()
# 
#   def getOutOfDateSubusers(self):
#     """
#     Returns a list of subusers which are out of date or have no InstalledImage associated with them.
#     """
#     if self.__outOfDateSubusers is None:
#       self.user.registry.log("Checking if images need to be updated or installed...")
#       self.__outOfDateSubusers = set()
#       for subuser in self.op.subusers:
#         try:
#           if (not subuser.locked) and (not (subuser.imageSource.getLatestInstalledImage() is None)):
#             self.user.registry.log("Checking if subuser "+subuser.name+" is up to date.")
#             for imageSource in getTargetLineage(subuser.imageSource):
#               if imageSource in self.__upToDateImageSources:
#                 continue
#               if imageSource in self.__outOfDateImageSources:
#                 upToDate = False
#               else:
#                 upToDate = self.isUpToDate(imageSource)
#               if upToDate:
#                 self.__upToDateImageSources.add(imageSource)
#               else:
#                 self.__outOfDateImageSources.add(imageSource)
#                 self.__outOfDateSubusers.add(subuser)
#                 break
#           if subuser.imageSource.getLatestInstalledImage() is None or subuser.imageId is None or not subuser.isImageInstalled():
#             if subuser.locked:
#               self.user.registry.log("Subuser "+subuser.name+" has no image. But is locked. Marking for installation anyways.")
#             self.__outOfDateSubusers.add(subuser)
#         except (exceptions.ImageBuildException, subuserlib.classes.subuser.NoImageSourceException) as e :
#           self.user.registry.log(str(e))
#           self.__subusersWhosImagesFailedToBuild.add(subuser)
#     outOfDateSubusers = list(self.__outOfDateSubusers)
#     outOfDateSubusers.sort(key=lambda s:s.name)
#     return outOfDateSubusers
# 
#   def isUpToDate(self,imageSource):
#     installedImage = imageSource.getLatestInstalledImage()
#     if installedImage is None:
#       return False
#     if not installedImage.isDockerImageThere():
#       return False
#     targetLineage = getTargetLineage(imageSource)
#     installedLineage = installedImage.getImageLineage()
#     if not (len(targetLineage) == len(installedLineage)):
#       return False
#     sideBySideLineages = zip(installedLineage,targetLineage)
#     for installed,target in sideBySideLineages:
#       if target in self.__outOfDateImageSources:
#         return False
#       if not installed.imageId == target.getLatestInstalledImage().imageId:
#         return False
#     if not installedImage.imageSourceHash == imageSource.getHash():
#       return False
#     if self.op.checkForUpdatesExternally and installedImage.checkForUpdates():
#       return False
#     return True
# 
#   def updateOutOfDateSubusers(self):
#     """
#     Install new images for those subusers which are out of date.
#     """
#     parent = None
#     for subuser in self.getOutOfDateSubusers():
#       try:
#         for imageSource in getTargetLineage(subuser.imageSource):
#           if imageSource in self.__upToDateImageSources:
#             parent = imageSource.getLatestInstalledImage().imageId
#           elif imageSource in self.__outOfDateImageSources:
#             parent = installImage(imageSource,parent=parent)
#             self.__outOfDateImageSources.remove(imageSource)
#             self.__upToDateImageSources.add(imageSource)
#           else:
#             if not self.isUpToDate(imageSource):
#               parent = installImage(imageSource,parent=parent)
#             else:
#               parent = imageSource.getLatestInstalledImage().imageId
#             self.__upToDateImageSources.add(imageSource)
#         if not subuser.imageId == parent:
#           subuser.imageId = parent
#           subuser.user.registry.logChange("Installed new image <"+subuser.imageId+"> for subuser "+subuser.name)
#       except exceptions.ImageBuildException as e:
#         self.user.registry.log(str(e))
#         self.__subusersWhosImagesFailedToBuild.add(subuser)
# 
#   def getSubusersWhosImagesFailedToBuild(self):
#     return self.__subusersWhosImagesFailedToBuild


import shutil
import os
import subuserlib.classes.exceptions as exceptions
import subuserlib.classes.subuser
from subuserlib.classes.installationTask import InstallationTask
    subuserNamesWithNoImageSource = list(subusersWithNoImageSource.keys())
    subuserNamesWithNoImageSource.sort()
    registry.log(" ".join(subuserNamesWithNoImageSource))

  registry.log("Unregistering any non-existant installed images.",2)
  user.installedImages.unregisterNonExistantImages()

  registry.cleanOutOldPermissions()

  if op.subusers or subusersWithNoImageSource:
    registry.setChanged(True)
    registry.log("Loading and approving permissions...",verbosityLevel=2)
    (failedSubusers,permissionParsingExceptions) = approvePermissions(op)
    for exception in permissionParsingExceptions:
      registry.log(str(exception))
    registry.log("Permissions set...",verbosityLevel=2)
    for failedSubuser in failedSubusers:
      registry.log("New permissions for subuser "+failedSubuser.name+" were not accepted.",5)
      try:
        failedSubuser.permissions
      except subuserlib.classes.subuser.SubuserHasNoPermissionsException:
        registry.log("Deleting subuser "+failedSubuser.name+" as it has no permissions.",verbosityLevel=3)
        del registry.subusers[failedSubuser.name]
        op.subusers.remove(failedSubuser)
    registry.log("Setting up service subusers...",verbosityLevel=3)
    op.subusers += ensureServiceSubusersAreSetup(op.user,op.subusers)
    op.subusers += ensureServiceSubusersAreSetup(op.user,subusersWithNoImageSource.values())
    registry.log("Service subusers set up...",verbosityLevel=3)
    registry.log("Building images...",verbosityLevel=3)
    if op.build:

# Path: subuserlib/classes/installationTask.py
# class InstallationTask(UserOwnedObject):
#   def __init__(self,op):
#     UserOwnedObject.__init__(self,op.user)
#     self.op = op
#     self.__upToDateImageSources = set()
#     self.__outOfDateImageSources = set()
#     self.__outOfDateSubusers = None
#     self.__subusersWhosImagesFailedToBuild = set()
# 
#   def getOutOfDateSubusers(self):
#     """
#     Returns a list of subusers which are out of date or have no InstalledImage associated with them.
#     """
#     if self.__outOfDateSubusers is None:
#       self.user.registry.log("Checking if images need to be updated or installed...")
#       self.__outOfDateSubusers = set()
#       for subuser in self.op.subusers:
#         try:
#           if (not subuser.locked) and (not (subuser.imageSource.getLatestInstalledImage() is None)):
#             self.user.registry.log("Checking if subuser "+subuser.name+" is up to date.")
#             for imageSource in getTargetLineage(subuser.imageSource):
#               if imageSource in self.__upToDateImageSources:
#                 continue
#               if imageSource in self.__outOfDateImageSources:
#                 upToDate = False
#               else:
#                 upToDate = self.isUpToDate(imageSource)
#               if upToDate:
#                 self.__upToDateImageSources.add(imageSource)
#               else:
#                 self.__outOfDateImageSources.add(imageSource)
#                 self.__outOfDateSubusers.add(subuser)
#                 break
#           if subuser.imageSource.getLatestInstalledImage() is None or subuser.imageId is None or not subuser.isImageInstalled():
#             if subuser.locked:
#               self.user.registry.log("Subuser "+subuser.name+" has no image. But is locked. Marking for installation anyways.")
#             self.__outOfDateSubusers.add(subuser)
#         except (exceptions.ImageBuildException, subuserlib.classes.subuser.NoImageSourceException) as e :
#           self.user.registry.log(str(e))
#           self.__subusersWhosImagesFailedToBuild.add(subuser)
#     outOfDateSubusers = list(self.__outOfDateSubusers)
#     outOfDateSubusers.sort(key=lambda s:s.name)
#     return outOfDateSubusers
# 
#   def isUpToDate(self,imageSource):
#     installedImage = imageSource.getLatestInstalledImage()
#     if installedImage is None:
#       return False
#     if not installedImage.isDockerImageThere():
#       return False
#     targetLineage = getTargetLineage(imageSource)
#     installedLineage = installedImage.getImageLineage()
#     if not (len(targetLineage) == len(installedLineage)):
#       return False
#     sideBySideLineages = zip(installedLineage,targetLineage)
#     for installed,target in sideBySideLineages:
#       if target in self.__outOfDateImageSources:
#         return False
#       if not installed.imageId == target.getLatestInstalledImage().imageId:
#         return False
#     if not installedImage.imageSourceHash == imageSource.getHash():
#       return False
#     if self.op.checkForUpdatesExternally and installedImage.checkForUpdates():
#       return False
#     return True
# 
#   def updateOutOfDateSubusers(self):
#     """
#     Install new images for those subusers which are out of date.
#     """
#     parent = None
#     for subuser in self.getOutOfDateSubusers():
#       try:
#         for imageSource in getTargetLineage(subuser.imageSource):
#           if imageSource in self.__upToDateImageSources:
#             parent = imageSource.getLatestInstalledImage().imageId
#           elif imageSource in self.__outOfDateImageSources:
#             parent = installImage(imageSource,parent=parent)
#             self.__outOfDateImageSources.remove(imageSource)
#             self.__upToDateImageSources.add(imageSource)
#           else:
#             if not self.isUpToDate(imageSource):
#               parent = installImage(imageSource,parent=parent)
#             else:
#               parent = imageSource.getLatestInstalledImage().imageId
#             self.__upToDateImageSources.add(imageSource)
#         if not subuser.imageId == parent:
#           subuser.imageId = parent
#           subuser.user.registry.logChange("Installed new image <"+subuser.imageId+"> for subuser "+subuser.name)
#       except exceptions.ImageBuildException as e:
#         self.user.registry.log(str(e))
#         self.__subusersWhosImagesFailedToBuild.add(subuser)
# 
#   def getSubusersWhosImagesFailedToBuild(self):
#     return self.__subusersWhosImagesFailedToBuild


# Path: subuserlib/verify.py
import shutil
import os
import subuserlib.classes.exceptions as exceptions
import subuserlib.classes.subuser
from subuserlib.classes.installationTask import InstallationTask

    subuserNamesWithNoImageSource = list(subusersWithNoImageSource.keys())
    subuserNamesWithNoImageSource.sort()
    registry.log(" ".join(subuserNamesWithNoImageSource))

  registry.log("Unregistering any non-existant installed images.",2)
  user.installedImages.unregisterNonExistantImages()

  registry.cleanOutOldPermissions()

  if op.subusers or subusersWithNoImageSource:
    registry.setChanged(True)
    registry.log("Loading and approving permissions...",verbosityLevel=2)
    (failedSubusers,permissionParsingExceptions) = approvePermissions(op)
    for exception in permissionParsingExceptions:
      registry.log(str(exception))
    registry.log("Permissions set...",verbosityLevel=2)
    for failedSubuser in failedSubusers:
      registry.log("New permissions for subuser "+failedSubuser.name+" were not accepted.",5)
      try:
        failedSubuser.permissions
      except subuserlib.classes.subuser.SubuserHasNoPermissionsException:
        registry.log("Deleting subuser "+failedSubuser.name+" as it has no permissions.",verbosityLevel=3)
        del registry.subusers[failedSubuser.name]
        op.subusers.remove(failedSubuser)
    registry.log("Setting up service subusers...",verbosityLevel=3)
    op.subusers += ensureServiceSubusersAreSetup(op.user,op.subusers)
    op.subusers += ensureServiceSubusersAreSetup(op.user,subusersWithNoImageSource.values())
    registry.log("Service subusers set up...",verbosityLevel=3)
    registry.log("Building images...",verbosityLevel=3)
    if op.build:

      installationTask = InstallationTask(op)