def test_getNewMountPoint(self): '''Test the creation and deleteion of a mount point''' folderPath = tempFolderManager.getNewMountPoint() self.assertTrue(folderPath is not None, 'Called with no options getNewMountPoint gave back None') self.assertTrue(os.path.isdir(folderPath), 'Called with no options getNewMountPoint returned a string that was not a path to an existing folder: ' + str(folderPath)) self.assertTrue(tempFolderManager.getManagedPathForPath(folderPath) is not None, 'Called with no options getNewMountPoint returned a path that was not in any managed path (according to getManagedPathForPath): ' + folderPath)
def test_getNewMountPoint(self): '''Test the creation and deleteion of a mount point''' folderPath = tempFolderManager.getNewMountPoint() self.assertTrue( folderPath is not None, 'Called with no options getNewMountPoint gave back None') self.assertTrue( os.path.isdir(folderPath), 'Called with no options getNewMountPoint returned a string that was not a path to an existing folder: ' + str(folderPath)) self.assertTrue( tempFolderManager.getManagedPathForPath(folderPath) is not None, 'Called with no options getNewMountPoint returned a path that was not in any managed path (according to getManagedPathForPath): ' + folderPath)
'The path given to the -m/--mount-point option must not be a mount point, got: ' + options.mountPoint) options.mountPoint = pathHelpers.normalizePath(options.mountPoint) else: # parent folder if not os.path.isdir(options.parentFolder): optionsParser.error( 'The path given to the -p/--parent-folder option must be a folder, got: ' + options.parentFolder) elif os.path.ismount(options.parentFolder): optionsParser.error( 'The path given to the -p/--parent-folder option must not be a mount point, got: ' + options.parentFolder) options.mountPoint = tempFolderManager.getNewMountPoint( parentFolder=options.parentFolder) tempFolderManager.removeManagedItem( options.mountPoint ) # so it does not get cleaned back up immediately # ---- process currentMountPoints = dmgManager.getDMGMountPoints(imageToMount) if options.allowOtherMount is True and currentMountPoints is not None: sys.stdout.write(currentMountPoints[0] + options.lineEnding) sys.exit(0) elif options.allowOtherMount is False and currentMountPoints is not None: sys.stderr.write('The image (%s) was already mounted at: %s\n' % (imageToMount, ", ".join(currentMountPoints))) sys.exit(1)
def mountImage(myClass, dmgFile, mountPoint=None, mountInFolder=None, mountReadWrite=None, shadowFile=None, paranoidMode=False): '''Mount an image''' # -- validate input # dmgFile if dmgFile is None or not os.path.exists(dmgFile) or os.path.ismount(dmgFile): raise ValueError('mountImage requires dmgFile be a path to a file, got: ' + dmgFile) dmgFile = pathHelpers.normalizePath(dmgFile, followSymlink=True) # mountPoint/mountInFolder if mountPoint is not None and mountInFolder is not None: raise ValueError('mountImage can only be called with mountPoint or mountInFolder, not both') elif mountPoint is not None and os.path.ismount(mountPoint): raise ValueError('mountImage called with a mountPoint that is already a mount point: ' + mountPoint) elif mountPoint is not None and os.path.isdir(mountPoint): if len(os.listdir(mountPoint)) != 0: raise ValueError('mountImage called with a mountPoint that already has contents: %s (%s)' % (mountPoint, str(os.listdir(mountPoint)))) elif mountPoint is not None and os.path.exists(mountPoint): raise ValueError('mountImage called with a mountPoint that already exists and is not a folder: ' + mountPoint) elif mountPoint is not None: tempFolderManager.add elif mountInFolder is not None and not os.path.isdir(mountInFolder): raise ValueError('mountImage called with a mountInFolder path that is not a folder: ' + mountInFolder) elif mountInFolder is not None: mountPoint = tempFolderManager.getNewMountPoint() else: # create a default mount point mountPoint = tempFolderManager.getNewMountPoint() mountPoint = pathHelpers.normalizePath(mountPoint, followSymlink=True) # mountReadWrite if mountReadWrite not in [None, True, False]: raise ValueError('The only valid options for mountReadWrite are True or False') # shadowFile if shadowFile is True: # generate a temporary one shadowFile = tempFolderManager.getNewTempFile(suffix='.shadow') elif shadowFile is not None: shadowFile = pathHelpers.normalizePath(shadowFile, followSymlink=True) if os.path.isfile(shadowFile): # work here pass # just use the file elif os.path.isdir(shadowFile): # a directory to put the shadow file in shadowFile = tempFolderManager.getNewTempFile(parentFolder=shadowFile, suffix='.shadow') elif os.path.isdir(os.path.dirname(shadowFile)): # the path does not exist, but the directory it is in looks good pass else: # not valid raise ValueError('The path given for the shadow file does not look valid: ' + str(shadowFile)) # paranoidMode if paranoidMode not in [True, False]: raise ValueError('checksumImage must be either True, or False. Got: ' + str(paranoidMode)) # -- check to see if it is already mounted existingMountPoints = myClass.getDMGMountPoints(dmgFile) if existingMountPoints is not None: raise RuntimeError('The image (%s) was already mounted: %s' % (dmgFile, ", ".join(existingMountPoints))) # -- construct the command command = ['/usr/bin/hdiutil', 'attach', dmgFile, '-plist', '-mountpoint', mountPoint, '-nobrowse', '-owners', 'on'] if mountReadWrite is True and shadowFile is None and self.writeable is False: shadowFile = tempFolderManager.getNewTempFile(suffix='.shadow') elif mountReadWrite is False and shadowFile is None: command += ['-readonly'] if paranoidMode is False: command += ['-noverify', '-noautofsck'] else: command += ['-verify', '-autofsck'] if shadowFile is not None: command += ['-shadow', shadowFile] # -- run the command process = managedSubprocess(command, processAsPlist=True) mountInfo = process.getPlistObject() actualMountedPath = None for thisEntry in mountInfo['system-entities']: if 'mount-point' in thisEntry: if actualMountedPath != None and os.path.ismount(actualMountedPath): # ToDo: think all of this through, prefereabley before it is needed raise Exception('This item (%s) seems to be mounted at two places , this is possible, but now that it is happening larkost needs to work on this' % self.filePath) actualMountedPath = thisEntry['mount-point'] # ToDo: make sure that this is the mount point we requested break # assume that there is only one mountable item... otherwise we are hosed already if actualMountedPath is None: raise RuntimeError('Error: image could not be mounted') return actualMountedPath
elif options.mountPoint is not None: # mountPoint if not os.path.isdir(options.mountPoint): optionsParser.error('The path given to the -m/--mount-point option must be a folder, got: ' + options.mountPoint) elif os.path.ismount(options.mountPoint): optionsParser.error('The path given to the -m/--mount-point option must not be a mount point, got: ' + options.mountPoint) options.mountPoint = pathHelpers.normalizePath(options.mountPoint) else: # parent folder if not os.path.isdir(options.parentFolder): optionsParser.error('The path given to the -p/--parent-folder option must be a folder, got: ' + options.parentFolder) elif os.path.ismount(options.parentFolder): optionsParser.error('The path given to the -p/--parent-folder option must not be a mount point, got: ' + options.parentFolder) options.mountPoint = tempFolderManager.getNewMountPoint(parentFolder=options.parentFolder) tempFolderManager.removeManagedItem(options.mountPoint) # so it does not get cleaned back up immediately # ---- process currentMountPoints = dmgManager.getDMGMountPoints(imageToMount) if options.allowOtherMount is True and currentMountPoints is not None: sys.stdout.write(currentMountPoints[0] + options.lineEnding) sys.exit(0) elif options.allowOtherMount is False and currentMountPoints is not None: sys.stderr.write('The image (%s) was already mounted at: %s\n' % (imageToMount, ", ".join(currentMountPoints))) sys.exit(1) actualMountPoint = dmgManager.mountImage(imageToMount, mountPoint=options.mountPoint, mountReadWrite=options.readWrite, shadowFile=None, paranoidMode=options.paranoidMode) if actualMountPoint != options.mountPoint:
def mountImage(myClass, dmgFile, mountPoint=None, mountInFolder=None, mountReadWrite=None, shadowFile=None, paranoidMode=False): '''Mount an image''' # -- validate input # dmgFile if dmgFile is None or not os.path.exists(dmgFile) or os.path.ismount( dmgFile): raise ValueError( 'mountImage requires dmgFile be a path to a file, got: ' + dmgFile) dmgFile = pathHelpers.normalizePath(dmgFile, followSymlink=True) # mountPoint/mountInFolder if mountPoint is not None and mountInFolder is not None: raise ValueError( 'mountImage can only be called with mountPoint or mountInFolder, not both' ) elif mountPoint is not None and os.path.ismount(mountPoint): raise ValueError( 'mountImage called with a mountPoint that is already a mount point: ' + mountPoint) elif mountPoint is not None and os.path.isdir(mountPoint): if len(os.listdir(mountPoint)) != 0: raise ValueError( 'mountImage called with a mountPoint that already has contents: %s (%s)' % (mountPoint, str(os.listdir(mountPoint)))) elif mountPoint is not None and os.path.exists(mountPoint): raise ValueError( 'mountImage called with a mountPoint that already exists and is not a folder: ' + mountPoint) elif mountPoint is not None: tempFolderManager.add elif mountInFolder is not None and not os.path.isdir(mountInFolder): raise ValueError( 'mountImage called with a mountInFolder path that is not a folder: ' + mountInFolder) elif mountInFolder is not None: mountPoint = tempFolderManager.getNewMountPoint() else: # create a default mount point mountPoint = tempFolderManager.getNewMountPoint() mountPoint = pathHelpers.normalizePath(mountPoint, followSymlink=True) # mountReadWrite if mountReadWrite not in [None, True, False]: raise ValueError( 'The only valid options for mountReadWrite are True or False') # shadowFile if shadowFile is True: # generate a temporary one shadowFile = tempFolderManager.getNewTempFile(suffix='.shadow') elif shadowFile is not None: shadowFile = pathHelpers.normalizePath(shadowFile, followSymlink=True) if os.path.isfile(shadowFile): # work here pass # just use the file elif os.path.isdir(shadowFile): # a directory to put the shadow file in shadowFile = tempFolderManager.getNewTempFile( parentFolder=shadowFile, suffix='.shadow') elif os.path.isdir(os.path.dirname(shadowFile)): # the path does not exist, but the directory it is in looks good pass else: # not valid raise ValueError( 'The path given for the shadow file does not look valid: ' + str(shadowFile)) # paranoidMode if paranoidMode not in [True, False]: raise ValueError( 'checksumImage must be either True, or False. Got: ' + str(paranoidMode)) # -- check to see if it is already mounted existingMountPoints = myClass.getDMGMountPoints(dmgFile) if existingMountPoints is not None: raise RuntimeError('The image (%s) was already mounted: %s' % (dmgFile, ", ".join(existingMountPoints))) # -- construct the command command = [ '/usr/bin/hdiutil', 'attach', dmgFile, '-plist', '-mountpoint', mountPoint, '-nobrowse', '-owners', 'on' ] if mountReadWrite is True and shadowFile is None and self.writeable is False: shadowFile = tempFolderManager.getNewTempFile(suffix='.shadow') elif mountReadWrite is False and shadowFile is None: command += ['-readonly'] if paranoidMode is False: command += ['-noverify', '-noautofsck'] else: command += ['-verify', '-autofsck'] if shadowFile is not None: command += ['-shadow', shadowFile] # -- run the command process = managedSubprocess(command, processAsPlist=True) mountInfo = process.getPlistObject() actualMountedPath = None for thisEntry in mountInfo['system-entities']: if 'mount-point' in thisEntry: if actualMountedPath != None and os.path.ismount( actualMountedPath): # ToDo: think all of this through, prefereabley before it is needed raise Exception( 'This item (%s) seems to be mounted at two places , this is possible, but now that it is happening larkost needs to work on this' % self.filePath) actualMountedPath = thisEntry[ 'mount-point'] # ToDo: make sure that this is the mount point we requested break # assume that there is only one mountable item... otherwise we are hosed already if actualMountedPath is None: raise RuntimeError('Error: image could not be mounted') return actualMountedPath