# Path: subuserlib/classes/user.py
# class LockedUser():
#   def __init__(self,name=None,homeDir=None):
#     self.lock = None
#     self.__user = User(name=name,homeDir=homeDir,_locked=True)
# 
#   def __enter__(self):
#     try:
#       self.__user.endUser.makedirs(self.__user.config["lock-dir"])
#     except OSError as exception:
#       if exception.errno != errno.EEXIST:
#         raise
#     try:
#       self.lock = subuserlib.lock.getLock(self.__user.endUser.get_file(os.path.join(self.__user.config["lock-dir"],"registry.lock"),'w'),timeout=1)
#       self.lock.__enter__()
#     except IOError as e:
#       if e.errno != errno.EINTR:
#         raise e
#       sys.exit("Another subuser process is currently running and has a lock on the registry. Please try again later.")
#     return self.__user
# 
#   def __exit__(self, type, value, traceback):
#     self.lock.__exit__()
# 
# Path: subuserlib/classes/permissionsAccepters/acceptPermissionsAtCLI.py
# class AcceptPermissionsAtCLI(PermissionsAccepter,UserOwnedObject):
#   def __init__(self,user,alwaysAccept = False):
#     self.alwaysAccept = alwaysAccept
#     UserOwnedObject.__init__(self,user)
# 
#   def accept(self,subuser,newDefaults,oldDefaults,userApproved):
#     if userApproved is None:
#       subuserlib.print.printWithoutCrashing(subuser.name+u": would like to have the following permissions:")
#       newDefaults.describe()
#       createNewPermissions = True
#     else:
#       createNewPermissions = False
#       (removedPermissions,additionsAndChanges) = subuserlib.permissions.compare(newDefaults = newDefaults, oldDefaults=oldDefaults, userApproved=userApproved)
#       if additionsAndChanges == {} and removedPermissions == [] and not subuser.wereEntryPointsExposedThisRun():
#         return
#       if not additionsAndChanges == {}:
#         subuserlib.print.printWithoutCrashing(subuser.name+" would like to add/change the following permissions:")
#         subuserlib.print.printWithoutCrashing(subuserlib.permissions.getDescription(additionsAndChanges))
#       if not removedPermissions == []:
#         subuserlib.print.printWithoutCrashing("")
#         subuserlib.print.printWithoutCrashing(subuser.name+" no longer needs the following permissions:")
#         removedPermisisonsDict = {}
#         for removedPermission in removedPermissions:
#           removedPermisisonsDict[removedPermission] = oldDefaults[removedPermission]
#         subuserlib.print.printWithoutCrashing(subuserlib.permissions.getDescription(removedPermisisonsDict))
#       if "entrypoints" in additionsAndChanges and subuser.entryPointsExposed:
#         subuserlib.print.printWithoutCrashing(subuser.name+" would like to expose the following entrypoints to the system PATH:")
#         for entrypoint in additionsAndChanges["entrypoints"].keys():
#           subuserlib.print.printWithoutCrashing(entrypoint)
#       if subuser.wereEntryPointsExposedThisRun():
#         if subuser.permissions["entrypoints"]:
#           subuserlib.print.printWithoutCrashing(subuser.name+" would like to expose the following entrypoints to the system PATH:")
#           for entrypoint in subuser.permissions["entrypoints"].keys():
#             subuserlib.print.printWithoutCrashing(entrypoint)
#         else:
#           subuserlib.print.printWithoutCrashing("Entrypoints marked to be exposed, but nothing to expose.")
#           if additionsAndChanges == {} and removedPermissions == []:
#             return
#     options = OrderedDict([("A","Accept and apply changes")
#                           ,("E","Apply changes and edit result")
#                           ,("e","Ignore request and edit permissions by hand")
#                           ,("r","Reject permissions.")])
#     if createNewPermissions:
#       del options["e"]
#     for option,description in options.items():
#       subuserlib.print.printWithoutCrashing(option+" - "+description)
#     if self.alwaysAccept:
#       subuserlib.print.printWithoutCrashing("A")
#       choice = "A"
#     else:
#       choice = None
#     while not choice in options:
#       try:
#         choice = input("Please select an option:")
#       except EOFError:
#         choice = "r"
#     if (choice == "A") or (choice == "E"):
#       if createNewPermissions:
#         subuser.createPermissions(newDefaults)
#       else:
#         subuser.permissions.applyChanges(removedPermissions,additionsAndChanges)
#       subuser.permissions.save()
#     if (choice == "E") or (choice == "e"):
#       subuser.editPermissionsCLI()
#     if choice == "r":
#       if createNewPermissions:
#         subuser.createPermissions(subuserlib.permissions.load(permissionsString="{}",logger=self.user.registry))
#         subuser.permissions.save()


import sys
import optparse
import subuserlib.verify
import subuserlib.commandLineArguments
import subuserlib.profile
from subuserlib.classes.user import LockedUser
from subuserlib.classes.permissionsAccepters.acceptPermissionsAtCLI import AcceptPermissionsAtCLI
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#external imports
#internal imports

####################################################
def parseCliArgs(realArgs):
  usage = "usage: subuser repair [options]"
  description = """
Repair your subuser installation.

This is usefull when migrating from one machine to another.  You can copy your ~/.subuser folder to the new machine and run repair, and things should just work.
"""
  parser = optparse.OptionParser(usage=usage,description=description,formatter=subuserlib.commandLineArguments.HelpFormatterThatDoesntReformatDescription())
  parser.add_option("--accept",dest="accept",action="store_true",default=False,help="Acceppt permissions without asking.")
  parser.add_option("--prompt",dest="prompt",action="store_true",default=False,help="Prompt before installing new images.")
  parser.add_option("--no-build",dest="build",action="store_false",default=True,help="Don't build missing images.")
  return parser.parse_args(args=realArgs)

@subuserlib.profile.do_cprofile
def runCommand(realArgs):
  options,arguments=parseCliArgs(realArgs)

# Path: subuserlib/classes/user.py
# class LockedUser():
#   def __init__(self,name=None,homeDir=None):
#     self.lock = None
#     self.__user = User(name=name,homeDir=homeDir,_locked=True)
# 
#   def __enter__(self):
#     try:
#       self.__user.endUser.makedirs(self.__user.config["lock-dir"])
#     except OSError as exception:
#       if exception.errno != errno.EEXIST:
#         raise
#     try:
#       self.lock = subuserlib.lock.getLock(self.__user.endUser.get_file(os.path.join(self.__user.config["lock-dir"],"registry.lock"),'w'),timeout=1)
#       self.lock.__enter__()
#     except IOError as e:
#       if e.errno != errno.EINTR:
#         raise e
#       sys.exit("Another subuser process is currently running and has a lock on the registry. Please try again later.")
#     return self.__user
# 
#   def __exit__(self, type, value, traceback):
#     self.lock.__exit__()
# 
# Path: subuserlib/classes/permissionsAccepters/acceptPermissionsAtCLI.py
# class AcceptPermissionsAtCLI(PermissionsAccepter,UserOwnedObject):
#   def __init__(self,user,alwaysAccept = False):
#     self.alwaysAccept = alwaysAccept
#     UserOwnedObject.__init__(self,user)
# 
#   def accept(self,subuser,newDefaults,oldDefaults,userApproved):
#     if userApproved is None:
#       subuserlib.print.printWithoutCrashing(subuser.name+u": would like to have the following permissions:")
#       newDefaults.describe()
#       createNewPermissions = True
#     else:
#       createNewPermissions = False
#       (removedPermissions,additionsAndChanges) = subuserlib.permissions.compare(newDefaults = newDefaults, oldDefaults=oldDefaults, userApproved=userApproved)
#       if additionsAndChanges == {} and removedPermissions == [] and not subuser.wereEntryPointsExposedThisRun():
#         return
#       if not additionsAndChanges == {}:
#         subuserlib.print.printWithoutCrashing(subuser.name+" would like to add/change the following permissions:")
#         subuserlib.print.printWithoutCrashing(subuserlib.permissions.getDescription(additionsAndChanges))
#       if not removedPermissions == []:
#         subuserlib.print.printWithoutCrashing("")
#         subuserlib.print.printWithoutCrashing(subuser.name+" no longer needs the following permissions:")
#         removedPermisisonsDict = {}
#         for removedPermission in removedPermissions:
#           removedPermisisonsDict[removedPermission] = oldDefaults[removedPermission]
#         subuserlib.print.printWithoutCrashing(subuserlib.permissions.getDescription(removedPermisisonsDict))
#       if "entrypoints" in additionsAndChanges and subuser.entryPointsExposed:
#         subuserlib.print.printWithoutCrashing(subuser.name+" would like to expose the following entrypoints to the system PATH:")
#         for entrypoint in additionsAndChanges["entrypoints"].keys():
#           subuserlib.print.printWithoutCrashing(entrypoint)
#       if subuser.wereEntryPointsExposedThisRun():
#         if subuser.permissions["entrypoints"]:
#           subuserlib.print.printWithoutCrashing(subuser.name+" would like to expose the following entrypoints to the system PATH:")
#           for entrypoint in subuser.permissions["entrypoints"].keys():
#             subuserlib.print.printWithoutCrashing(entrypoint)
#         else:
#           subuserlib.print.printWithoutCrashing("Entrypoints marked to be exposed, but nothing to expose.")
#           if additionsAndChanges == {} and removedPermissions == []:
#             return
#     options = OrderedDict([("A","Accept and apply changes")
#                           ,("E","Apply changes and edit result")
#                           ,("e","Ignore request and edit permissions by hand")
#                           ,("r","Reject permissions.")])
#     if createNewPermissions:
#       del options["e"]
#     for option,description in options.items():
#       subuserlib.print.printWithoutCrashing(option+" - "+description)
#     if self.alwaysAccept:
#       subuserlib.print.printWithoutCrashing("A")
#       choice = "A"
#     else:
#       choice = None
#     while not choice in options:
#       try:
#         choice = input("Please select an option:")
#       except EOFError:
#         choice = "r"
#     if (choice == "A") or (choice == "E"):
#       if createNewPermissions:
#         subuser.createPermissions(newDefaults)
#       else:
#         subuser.permissions.applyChanges(removedPermissions,additionsAndChanges)
#       subuser.permissions.save()
#     if (choice == "E") or (choice == "e"):
#       subuser.editPermissionsCLI()
#     if choice == "r":
#       if createNewPermissions:
#         subuser.createPermissions(subuserlib.permissions.load(permissionsString="{}",logger=self.user.registry))
#         subuser.permissions.save()


# Path: subuserlib/builtInCommands/repair.py
import sys
import optparse
import subuserlib.verify
import subuserlib.commandLineArguments
import subuserlib.profile
from subuserlib.classes.user import LockedUser
from subuserlib.classes.permissionsAccepters.acceptPermissionsAtCLI import AcceptPermissionsAtCLI

#!/usr/bin/python3
# -*- coding: utf-8 -*-
#external imports
#internal imports

####################################################
def parseCliArgs(realArgs):
  usage = "usage: subuser repair [options]"
  description = """
Repair your subuser installation.

This is usefull when migrating from one machine to another.  You can copy your ~/.subuser folder to the new machine and run repair, and things should just work.
"""
  parser = optparse.OptionParser(usage=usage,description=description,formatter=subuserlib.commandLineArguments.HelpFormatterThatDoesntReformatDescription())
  parser.add_option("--accept",dest="accept",action="store_true",default=False,help="Acceppt permissions without asking.")
  parser.add_option("--prompt",dest="prompt",action="store_true",default=False,help="Prompt before installing new images.")
  parser.add_option("--no-build",dest="build",action="store_false",default=True,help="Don't build missing images.")
  return parser.parse_args(args=realArgs)

@subuserlib.profile.do_cprofile
def runCommand(realArgs):
  options,arguments=parseCliArgs(realArgs)

  lockedUser = LockedUser()