Example #1
0
    def findManagedItemsInsideDirectory(myClass, targetDirectory):
        '''Return a list of managed items below this path in rough order that they should be cleaned'''

        targetDirectory = pathHelpers.normalizePath(targetDirectory)

        if not os.path.isdir(targetDirectory) or os.path.islink(
                targetDirectory):
            raise ValueError(
                'findManagedItemsInsideDirectory requires a directory as the input, and it can not be a symlink'
                + str(targetDirectory))

        itemsToClean = []
        if myClass.managedItems is not None:
            for thisItem in myClass.managedItems:
                if pathHelpers.pathInsideFolder(thisItem, targetDirectory):
                    itemsToClean.append(thisItem)
        # Note: this should mean that once sorted and reversed that mounted items get taken care of before their mount-points
        if myClass.managedMounts is not None:
            for thisMount in myClass.managedMounts:
                if pathHelpers.pathInsideFolder(thisMount, targetDirectory):
                    itemsToClean.append(thisMount)

        itemsToClean.sort(
        )  # items inside others should now be below their parents
        itemsToClean.reverse()  # the opposite should now be true

        return itemsToClean
Example #2
0
	def findManagedItemsInsideDirectory(myClass, targetDirectory):
		'''Return a list of managed items below this path in rough order that they should be cleaned'''
		
		targetDirectory = pathHelpers.normalizePath(targetDirectory)
		
		if not os.path.isdir(targetDirectory) or os.path.islink(targetDirectory):
			raise ValueError('findManagedItemsInsideDirectory requires a directory as the input, and it can not be a symlink' + str(targetDirectory))
		
		itemsToClean = []
		if myClass.managedItems is not None:
			for thisItem in myClass.managedItems:
				if pathHelpers.pathInsideFolder(thisItem, targetDirectory):
					itemsToClean.append(thisItem)
		# Note: this should mean that once sorted and reversed that mounted items get taken care of before their mount-points
		if myClass.managedMounts is not None:
			for thisMount in myClass.managedMounts:
				if pathHelpers.pathInsideFolder(thisMount, targetDirectory):
					itemsToClean.append(thisMount)
		
		itemsToClean.sort() # items inside others should now be below their parents
		itemsToClean.reverse() # the opposite should now be true
		
		return itemsToClean
Example #3
0
 def pathInsideFolderTestHelper(self, testPath, testFolder, expectedResult):
     result = pathHelpers.pathInsideFolder(testPath, testFolder)
     self.assertEqual(
         result, expectedResult,
         'When testing pathInsideFolder on whether "%s" was inside "%s" it incorrectly returned %s'
         % (testPath, testFolder, result))
Example #4
0
	def cleanupItem(myClass, targetPath):
		'''Dispose of an item'''
		
		if targetPath is None:
			raise ValueError('cleanupItem called with an empty targetPath')
		
		targetPath = pathHelpers.normalizePath(targetPath)
		
		# -- confirm that this item is a managed item, or in a manged space
		
		managedItem		= False
		managedMount	= False
		managedSpace	= False
		
		if targetPath in myClass.managedItems:
			managedItem = True
			managedSpace = True
		else:
			for thisManagedSpace in myClass.managedItems:
				if os.path.isdir(thisManagedSpace) and not os.path.islink(thisManagedSpace) and os.path.lexists(targetPath):
					if pathHelpers.pathInsideFolder(targetPath, thisManagedSpace) and os.lstat(targetPath)[stat.ST_DEV] == os.lstat(thisManagedSpace)[stat.ST_DEV]:
						managedSpace = True
						break
		
		if targetPath in myClass.managedMounts:
			managedMount = True
		
		if managedMount is False and managedSpace is False:
			raise ValueError('cleanupItem handed a path that was not in a managed space or a managed mount: ' + targetPath)
		
		if not os.path.lexists(targetPath):
			if True in [managedItem, managedSpace]:
				# the item no longer exists, we just have to clean it out of managedItems and/or managedMount
				if managedItem is True:
					myClass.managedItems.remove(targetPath)
				if managedMount is True:
					myClass.managedMounts.remove(targetPath)
				
				return
			else:
				raise ValueError('cleanupItem handed a path that does not exist: ' + targetPath)
		
		# -- find any managed items inside this one, and let them handle their business first
		if os.path.isdir(targetPath) and not os.path.islink(targetPath):
			for thisItem in myClass.findManagedItemsInsideDirectory(targetPath):
				myClass.cleanupItem(thisItem)
		
		# -- if this is a mount, unmount it
		if os.path.ismount(targetPath):
			volumeTools.unmountVolume(targetPath)
		
		# -- if this is in controlled space, wipe it
		if managedSpace is True:
			
			# handle the simple cases of a soft-link or a file
			if os.path.islink(targetPath) or os.path.isfile(targetPath):
				try:
					os.unlink(targetPath)
				except OSError:
					# assume that this was a permissions error, and try to chmod it into cooperating
					os.chmod(thisFile, stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
					os.unlink(thisFile)
			
			# handle folders
			else:
				# make sure that the permissions on the root folder are ok
				if not os.access(targetPath, os.R_OK | os.X_OK):
					os.chmod(targetPath, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
				
				# walk up the tree to remove any volumes mounted into the path
				for root, dirs, files in os.walk(targetPath, topdown=True):
					
					# unmount the directory if it is a volume
					if os.path.ismount(root):
						volumeTools.unmountVolume(root) # ToDo: log this
						dirs = [] # make sure we don't try to decend into folders that are no longer there
						continue
					
					# delete all files, continuing through failures
					for thisFile in [os.path.join(root, internalName) for internalName in files]:
						try:
							try:
								os.unlink(thisFile)
							except OSError:
								# assume that this was a permissions error, and try to chmod it into cooperating
								os.chmod(thisFile, stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
								os.unlink(thisFile)
						
						except: # ToDo: make this more specific
							pass # ToDo: log this				
				
					# catch any symlinks
					for thisFolder in [os.path.join(root, internalName) for internalName in dirs]:
						# make sure we can make it into all sub-folders and delete them:
						if not os.access(thisFolder, os.R_OK | os.X_OK):
							os.chmod(thisFolder, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
						
						if os.path.islink(thisFolder):
							try:
								os.unlink(thisFolder)
							except: # ToDo: make this more specific
								pass # ToDo: log this	
				
				# now that there are no mounted volumes, there should be no files, so delete the folders
				for root, dirs, files in os.walk(targetPath, topdown=False):
					try:
						os.rmdir(root)
					except Exception, error: # ToDo: make this more specific
						sys.stderr.write('Unable to delete folder: "%s" got error: %s' % (root, str(error))) # ToDo: logging
Example #5
0
    def cleanupItem(myClass, targetPath):
        '''Dispose of an item'''

        if targetPath is None:
            raise ValueError('cleanupItem called with an empty targetPath')

        targetPath = pathHelpers.normalizePath(targetPath)

        # -- confirm that this item is a managed item, or in a manged space

        managedItem = False
        managedMount = False
        managedSpace = False

        if targetPath in myClass.managedItems:
            managedItem = True
            managedSpace = True
        else:
            for thisManagedSpace in myClass.managedItems:
                if os.path.isdir(thisManagedSpace) and not os.path.islink(
                        thisManagedSpace) and os.path.lexists(targetPath):
                    if pathHelpers.pathInsideFolder(
                            targetPath, thisManagedSpace) and os.lstat(
                                targetPath)[stat.ST_DEV] == os.lstat(
                                    thisManagedSpace)[stat.ST_DEV]:
                        managedSpace = True
                        break

        if targetPath in myClass.managedMounts:
            managedMount = True

        if managedMount is False and managedSpace is False:
            raise ValueError(
                'cleanupItem handed a path that was not in a managed space or a managed mount: '
                + targetPath)

        if not os.path.lexists(targetPath):
            if True in [managedItem, managedSpace]:
                # the item no longer exists, we just have to clean it out of managedItems and/or managedMount
                if managedItem is True:
                    myClass.managedItems.remove(targetPath)
                if managedMount is True:
                    myClass.managedMounts.remove(targetPath)

                return
            else:
                raise ValueError(
                    'cleanupItem handed a path that does not exist: ' +
                    targetPath)

        # -- find any managed items inside this one, and let them handle their business first
        if os.path.isdir(targetPath) and not os.path.islink(targetPath):
            for thisItem in myClass.findManagedItemsInsideDirectory(
                    targetPath):
                myClass.cleanupItem(thisItem)

        # -- if this is a mount, unmount it
        if os.path.ismount(targetPath):
            volumeTools.unmountVolume(targetPath)

        # -- if this is in controlled space, wipe it
        if managedSpace is True:

            # handle the simple cases of a soft-link or a file
            if os.path.islink(targetPath) or os.path.isfile(targetPath):
                try:
                    os.unlink(targetPath)
                except OSError:
                    # assume that this was a permissions error, and try to chmod it into cooperating
                    os.chmod(thisFile,
                             stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
                    os.unlink(thisFile)

            # handle folders
            else:
                # make sure that the permissions on the root folder are ok
                if not os.access(targetPath, os.R_OK | os.X_OK):
                    os.chmod(
                        targetPath, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
                        | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)

                # walk up the tree to remove any volumes mounted into the path
                for root, dirs, files in os.walk(targetPath, topdown=True):

                    # unmount the directory if it is a volume
                    if os.path.ismount(root):
                        volumeTools.unmountVolume(root)  # ToDo: log this
                        dirs = [
                        ]  # make sure we don't try to decend into folders that are no longer there
                        continue

                    # delete all files, continuing through failures
                    for thisFile in [
                            os.path.join(root, internalName)
                            for internalName in files
                    ]:
                        try:
                            try:
                                os.unlink(thisFile)
                            except OSError:
                                # assume that this was a permissions error, and try to chmod it into cooperating
                                os.chmod(
                                    thisFile,
                                    stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
                                os.unlink(thisFile)

                        except:  # ToDo: make this more specific
                            pass  # ToDo: log this

                    # catch any symlinks
                    for thisFolder in [
                            os.path.join(root, internalName)
                            for internalName in dirs
                    ]:
                        # make sure we can make it into all sub-folders and delete them:
                        if not os.access(thisFolder, os.R_OK | os.X_OK):
                            os.chmod(
                                thisFolder,
                                stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
                                | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)

                        if os.path.islink(thisFolder):
                            try:
                                os.unlink(thisFolder)
                            except:  # ToDo: make this more specific
                                pass  # ToDo: log this

                # now that there are no mounted volumes, there should be no files, so delete the folders
                for root, dirs, files in os.walk(targetPath, topdown=False):
                    try:
                        os.rmdir(root)
                    except Exception, error:  # ToDo: make this more specific
                        sys.stderr.write(
                            'Unable to delete folder: "%s" got error: %s' %
                            (root, str(error)))  # ToDo: logging
Example #6
0
	def pathInsideFolderTestHelper(self, testPath, testFolder, expectedResult):
		result = pathHelpers.pathInsideFolder(testPath, testFolder)
		self.assertEqual(result, expectedResult, 'When testing pathInsideFolder on whether "%s" was inside "%s" it incorrectly returned %s' % (testPath, testFolder, result))