# 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__()


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

def parseCliArgs(realArgs):
  usage = "usage: subuser remove-old-images"
  description = """ Remove old, no longer used, installed images.  Note, once you do this, you will no longer be able to return to previous configuration states with subuser registry rollback or subuser update lock-subuser-to."""
  parser=optparse.OptionParser(usage=usage,description=description,formatter=subuserlib.commandLineArguments.HelpFormatterThatDoesntReformatDescription())
  parser.add_option("--dry-run", dest="dryrun",action="store_true",default=False,help="Don't actually delete the images. Print which images would be deleted.")
  parser.add_option("--repo", dest="repoId",default=None,help="Only remove images from the given repository.")
  parser.add_option("--image-source", dest="imageSourceName",default=None,help="Remove old images from a specific image source. (Must be used in conjunction with --repo)")
  parser.add_option("-y", dest="yes",default=False,action="store_true",help="Don't ask, just delete unneeded images.")
  return parser.parse_args(args=realArgs)

@subuserlib.profile.do_cprofile
def runCommand(realArgs):
  """
  Remove images that are installed, but are not associated with any subusers.
  """
  options,args = 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/builtInCommands/remove-old-images.py
import sys
import optparse
import subuserlib.commandLineArguments
import subuserlib.removeOldImages
import subuserlib.profile
from subuserlib.classes.user import LockedUser

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

def parseCliArgs(realArgs):
  usage = "usage: subuser remove-old-images"
  description = """ Remove old, no longer used, installed images.  Note, once you do this, you will no longer be able to return to previous configuration states with subuser registry rollback or subuser update lock-subuser-to."""
  parser=optparse.OptionParser(usage=usage,description=description,formatter=subuserlib.commandLineArguments.HelpFormatterThatDoesntReformatDescription())
  parser.add_option("--dry-run", dest="dryrun",action="store_true",default=False,help="Don't actually delete the images. Print which images would be deleted.")
  parser.add_option("--repo", dest="repoId",default=None,help="Only remove images from the given repository.")
  parser.add_option("--image-source", dest="imageSourceName",default=None,help="Remove old images from a specific image source. (Must be used in conjunction with --repo)")
  parser.add_option("-y", dest="yes",default=False,action="store_true",help="Don't ask, just delete unneeded images.")
  return parser.parse_args(args=realArgs)

@subuserlib.profile.do_cprofile
def runCommand(realArgs):
  """
  Remove images that are installed, but are not associated with any subusers.
  """
  options,args = parseCliArgs(realArgs)

  lockedUser = LockedUser()