def run(self, parser): """ Process the remove operation. """ # Parse command-line args = parser.parse_args() # Set sam log level # sam-rm --dry-run if args.dryRun: self.setLogLevel(3) # info else: self.setLogLevel(args.verbose) # check command line if args.range and (args.firstImage is not None or args.lastImage is not None): self.logger.error( 'You cannot cumulate multiple options to specify the range of sequence.' ) exit(1) if '.' in args.inputs or '..' in args.inputs: self.logger.error('You cannot remove folders "." or "..".') exit(1) # sam-rm -a detectionMethod = sequenceParser.eDetectionDefault if args.all: detectionMethod = sequenceParser.eDetectionDefaultWithDotFile # sam-rm --detect-negative if args.detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # sam-rm -e filters = [] if args.expression: filters.append(args.expression) # get list of items for each inputs error = 0 for inputPath in args.inputs: items = [] # input is a directory if not os.path.basename(inputPath): items.append( sequenceParser.Item(sequenceParser.eTypeFolder, inputPath)) # else browse directory with a filter, to find the corresponding Item else: # get path to browse pathToBrowse = os.path.dirname(inputPath) if not pathToBrowse: pathToBrowse = '.' # get filter filterToBrowse = [] filterToBrowse.extend(filters) filterToBrowse.append(os.path.basename(inputPath)) # browse self.logger.debug('Browse in "' + pathToBrowse + '" with the following filters: ' + str(filterToBrowse)) items = sequenceParser.browse(pathToBrowse, detectionMethod, filterToBrowse) # print error if no items were found if len(items) == 0: self.logger.error( 'No files, sequences or folders correspond to "' + inputPath + '".') error = 1 continue err = self._removeItems(items, args, detectionMethod, filters) if err: error = err exit(error)
def _printItem(self, item, args, level): """ Print the item depending on the command line options. """ itemType = item.getType() filePath = '' detailed = '' detailedSequence = '' # sam-ls --explode-sequences sequenceExploded = False if args.explodeSequences and itemType == sequenceParser.eTypeSequence: sequence = item.getSequence() for frameRange in sequence.getFrameRanges(): # for each frame range, print a new item as sequence subSequence = sequenceParser.Sequence(sequence.getPrefix(), sequence.getPadding(), sequence.getSuffix(), frameRange.first, frameRange.last, frameRange.step) if subSequence.__str__() not in self._sequenceExploded: self._sequenceExploded.append(subSequence.__str__()) sequenceExploded = True self._printItem( sequenceParser.Item(subSequence, item.getFolder()), args, level) # to skip recursivity if sequenceExploded: return # sam-ls -l if args.longListing: # type - date - size characterFromType = 'a' if itemType == sequenceParser.eTypeUndefined: characterFromType = '?' elif itemType == sequenceParser.eTypeFolder: characterFromType = 'd' elif itemType == sequenceParser.eTypeFile: characterFromType = 'f' elif itemType == sequenceParser.eTypeSequence: characterFromType = 's' elif itemType == sequenceParser.eTypeLink: characterFromType = 'l' # type - permissions - user - group - lastUpdate - size itemStat = sequenceParser.ItemStat(item) permissions = '' permissions += 'r' if itemStat.ownerCanRead else '-' permissions += 'w' if itemStat.ownerCanWrite else '-' permissions += 'x' if itemStat.ownerCanExecute else '-' permissions += 'r' if itemStat.groupCanRead else '-' permissions += 'w' if itemStat.groupCanWrite else '-' permissions += 'x' if itemStat.groupCanExecute else '-' permissions += 'r' if itemStat.otherCanRead else '-' permissions += 'w' if itemStat.otherCanWrite else '-' permissions += 'x' if itemStat.otherCanExecute else '-' lastUpdate = date.fromtimestamp( itemStat.modificationTime).strftime('%d/%m/%y') minSize = samUtils.getReadableSize( itemStat.minSize) if itemStat.minSize != itemStat.size else '-' maxSize = samUtils.getReadableSize( itemStat.maxSize) if itemStat.maxSize != itemStat.size else '-' detailed = '{:1}{:9}'.format(characterFromType, permissions) detailed += ' {:8} {:8} {:8}'.format(itemStat.getUserName(), itemStat.getGroupName(), lastUpdate) detailed += ' {:6} {:6} {:6}'.format( minSize, maxSize, samUtils.getReadableSize(itemStat.size)) detailed += '\t' # only for sequences: [ begin : end ] nbFiles - nbMissingFiles if itemType == sequenceParser.eTypeSequence: sequence = item.getSequence() detailedSequence = '[{first}:{last}] {nbFiles} files'.format( first=sequence.getFirstTime(), last=sequence.getLastTime(), nbFiles=sequence.getNbFiles()) nbHoles = (sequence.getLastTime() - sequence.getFirstTime() + 1) - sequence.getNbFiles() if nbHoles: detailedSequence += ' - {nbHoles} missing files'.format( nbHoles=nbHoles) # sam-ls --absolute-path if args.absolutePath: filePath += os.path.abspath(item.getFolder()) # sam-ls --relative-path if args.relativePath: filePath += (item.getFolder() if item.getFolder()[0] != '/' else '.') filePath += ('/' if filePath[-1] != '/' else '') # filename filename = item.getFilename() # sam-ls --format if itemType == sequenceParser.eTypeSequence: filename = samUtils.getSequenceNameWithFormatting( item.getSequence(), args.format) # sam-ls --no-color if args.noColor: filePath = os.path.join(filePath, filename) else: if itemType == sequenceParser.eTypeFolder: # blue is not visible without bold filePath = colored.blue(os.path.join(filePath, filename), bold=True) elif itemType == sequenceParser.eTypeFile: filePath = colored.green(os.path.join(filePath, filename)) elif itemType == sequenceParser.eTypeSequence: # magenta is not visible without bold filePath = colored.magenta(os.path.join(filePath, filename), bold=True) elif itemType == sequenceParser.eTypeLink: filePath = colored.cyan(os.path.join(filePath, filename)) else: filePath = colored.red(os.path.join(filePath, filename)) filePath += ' \t' # sam-ls -R / sam-ls -L indentTree = '' if args.recursive and args.level != 0: indentTree += '| ' * (level - 1) indentTree += '|__ ' # display toPrint = detailed + filePath + detailedSequence # if first level or no tree formatting if level == 0 or args.level == 0: puts(toPrint.format()) else: with indent(level, quote=indentTree): puts(toPrint.format()) if itemType == sequenceParser.eTypeSequence: self._itemPrinted.append(item.getSequence().__str__()) else: self._itemPrinted.append(item.getFilename())
def run(self, parser): """ Process the list operation. """ # Parse command-line args = parser.parse_args() # sam-ls --absolute-path --relative-path if args.absolutePath and args.relativePath: self._displayCommandLineHelp(parser) exit(1) # Set sam log level self.setLogLevel(args.verbose) class InputToBrowse(object): """ Represents an input to browse: a path and a list of filters. """ def __init__(self, inputPath): self.inputPath = inputPath self.filters = [] def addFilter(self, filterToAdd): self.filters.append(filterToAdd) # translate user inputs to a list of InputToBrowse inputsToBrowse = [] for inputPath in args.inputs: # if the input is a directory, add it and continue if os.path.isdir(inputPath): inputsToBrowse.append(InputToBrowse(inputPath)) continue # else split the input to a path and a filename subPath = os.path.dirname(inputPath) if not subPath: subPath = '.' filename = os.path.basename(inputPath) # add the path and the filename as an expression inputToBrowse = InputToBrowse(subPath) inputToBrowse.addFilter(filename) inputsToBrowse.append(inputToBrowse) # if no user input, will browse in the current working directory if not inputsToBrowse: inputsToBrowse.append(InputToBrowse(os.getcwd())) # sam-ls -a detectionMethod = sequenceParser.eDetectionDefault if args.all: detectionMethod = sequenceParser.eDetectionDefaultWithDotFile # sam-ls --detect-negative if args.detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # sam-ls --detect-without-holes if args.detectWithoutHoles: detectionMethod = detectionMethod | sequenceParser.eDetectionSequenceWithoutHoles # sam-ls -e for expression in args.expression: for inputToBrowse in inputsToBrowse: inputToBrowse.addFilter(expression) error = 0 # for each input to browse, print the finding items for inputToBrowse in inputsToBrowse: items = [] inputPath = inputToBrowse.inputPath filters = inputToBrowse.filters try: self.logger.debug('Browse in "' + inputPath + '" with the following filters: ' + str(filters)) items = sequenceParser.browse(inputPath, detectionMethod, filters) except IOError as e: self.logger.debug('IOError raised: "' + str(e) + '".') # if the given input does not correspond to anything if 'No such file or directory' in str(e): # try to create a sequence from the given input sequence = sequenceParser.Sequence() self.logger.debug('BrowseSequence on "' + inputPath + '".') isSequence = sequenceParser.browseSequence( sequence, inputPath) if isSequence: item = sequenceParser.Item(sequence, os.getcwd()) # check if the sequence contains at least one element if len(item.explode()): items.append(item) # else error else: self.logger.warning(e) continue # else it's not a directory else: self.logger.debug( 'Try a new browse with the given input name as filter.' ) # new path to browse newBrowsePath = os.path.dirname(inputPath) if not newBrowsePath: newBrowsePath = '.' # new filter newFilter = [] newFilter.extend(filters) newFilter.append(os.path.basename(inputPath)) # new browse self.logger.debug('Browse in "' + newBrowsePath + '" with the following filters: ' + str(newFilter)) items += sequenceParser.browse(newBrowsePath, detectionMethod, newFilter) if not len(items): self.logger.warning('No items found for input "' + inputPath + '" with the following filters: ' + str(filters)) error = 1 else: self._printItems(items, args, detectionMethod, filters) exit(error)
def run(self, parser): """ Process the list operation. """ # Parse command-line args = parser.parse_args() # Set sam log level self.setLogLevel(args.verbose) # inputs to scan inputs = [] for input in args.inputs: # if exists add the path if os.path.exists(input): inputs.append(input) # else use it as a filter expression else: args.expression.append(input) if not inputs: inputs.append(os.getcwd()) # sam-ls -a detectionMethod = sequenceParser.eDetectionDefault if args.all: detectionMethod = sequenceParser.eDetectionDefaultWithDotFile # sam-ls --detect-negative if args.detectNegative: detectionMethod = detectionMethod | sequenceParser.eDetectionNegative # sam-ls --detect-without-holes if args.detectWithoutHoles: detectionMethod = detectionMethod | sequenceParser.eDetectionSequenceWithoutHoles # sam-ls -e filters = [] for expression in args.expression: filters.append(expression) # get list of items for each inputs for input in inputs: items = [] try: self.logger.info('Launch a browse on "' + input + '" with the following filters: ' + str(filters)) items = sequenceParser.browse(input, detectionMethod, filters) except IOError as e: # if the given input does not correspond to anything if 'No such file or directory' in str(e): # try to create a sequence from the given input sequence = sequenceParser.Sequence() self.logger.info('Launch a browseSequence on "' + input + '".') isSequence = sequenceParser.browseSequence(sequence, input) if isSequence: item = sequenceParser.Item(sequence, os.getcwd()) # check if the sequence contains at least one element if len(item.explode()): items.append(item) # else error else: self.logger.warning(e) continue # else it's not a directory: try a new browse with the given input name as filter else: # new path to browse newBrowsePath = os.path.dirname(input) if not newBrowsePath: newBrowsePath = '.' # new filter newFilter = [] newFilter.extend(filters) newFilter.append(os.path.basename(input)) # new browse self.logger.info('Launch a browse on "' + newBrowsePath + '" with the following filters: ' + str(newFilter)) items += sequenceParser.browse(newBrowsePath, detectionMethod, newFilter) if not len(items): self.logger.warning('No items found for input "' + input + '".') else: self.printItems(items, args, detectionMethod, filters)