def distributeDirectory(self, sourceDirectory): """ Move or copy a folder. """ log.trace('distributeDirectory: %s %s' % (self.contentType, sourceDirectory)) for _root, _dir_names, _file_names in os.walk(sourceDirectory): # don't scan ignored subdirs for _dir_name in _dir_names: if ignored(_dir_name) and DadVision.args.ignore: log.verbose( "Ignoring %r" % os.path.join(_root, _dir_name)[len(sourceDirectory):]) _dir_names.remove(_dir_name) # sort file types contained in directory _requiresUnpack = [] _simpleCopyOK = [] for _file in sorted(_file_names): if ignored(_file) and DadVision.args.ignore: log.verbose("Ignoring %r" % os.path.join(_root, _file)) continue if self.RAR_RE.search(_file): _requiresUnpack.append(os.path.join(_root, _file)) else: _simpleCopyOK.append(os.path.join(_root, _file)) # handle normal files for _file in _simpleCopyOK: try: self.distributeFile(_file) except: an_error = traceback.format_exc() log.verbose( traceback.format_exception_only( type(an_error), an_error)[-1]) log.warning('Distribute skipped for: {}'.format( os.path.basename(_file))) # handle unpacking files if len(_requiresUnpack) > 0: self._unpackDirectory(_requiresUnpack) return
def distributeDirectory(self, sourceDirectory): """ Move or copy a folder. """ log.trace('distributeDirectory: %s %s' % (self.contentType, sourceDirectory)) for _root, _dir_names, _file_names in os.walk(sourceDirectory): # don't scan ignored subdirs for _dir_name in _dir_names: if ignored(_dir_name) and DadVision.args.ignore: log.verbose("Ignoring %r" % os.path.join(_root, _dir_name)[len(sourceDirectory):]) _dir_names.remove(_dir_name) # sort file types contained in directory _requiresUnpack = [] _simpleCopyOK = [] for _file in sorted(_file_names): if ignored(_file) and DadVision.args.ignore: log.verbose("Ignoring %r" % os.path.join(_root, _file)) continue if self.RAR_RE.search(_file): _requiresUnpack.append(os.path.join(_root, _file)) else: _simpleCopyOK.append(os.path.join(_root, _file)) # handle normal files for _file in _simpleCopyOK: try: self.distributeFile(_file) except: an_error = traceback.format_exc() log.verbose(traceback.format_exception_only(type(an_error), an_error)[-1]) log.warning('Distribute skipped for: {}'.format(os.path.basename(_file))) # handle unpacking files if len(_requiresUnpack) > 0: self._unpackDirectory(_requiresUnpack) return
def distributeFile(self, pathname): log.trace('distributeFile: %s %s' % (self.contentType, pathname)) """ Move or copy a single file. """ # ignored file? if DadVision.args.ignore and ignored(os.path.basename(pathname)): log.verbose("Ignoring %r!" % (pathname)) return if os.path.splitext(pathname)[1][1:].lower() not in DadVision.settings.MediaExt: log.verbose("Ignoring %r!" % (pathname)) return self._setContentType(pathname) if self.contentType == 'Movies' and DadVision.args.suppress_movies: log.verbose('Skipping Movie: %s' % pathname) return if self.contentType == "Series": _destinationDir = DadVision.settings.NewSeriesDir elif self.contentType == "Movies": _destinationDir = DadVision.settings.NewMoviesDir else: _destinationDir = DadVision.settings.UnpackDir # Adds the last directory to target name _destinationDir = os.path.join(_destinationDir, os.path.basename(os.path.dirname(pathname))) _distributedFile = os.path.join(_destinationDir, os.path.basename(pathname)) if os.path.exists(_distributedFile) and os.path.getsize(_distributedFile) == os.path.getsize(pathname): log.verbose("Skipped Copy: Already at Destination:") log.verbose(" {}".format(os.path.basename(pathname))) log.verbose(" {}".format(os.path.basename(_distributedFile))) else: # make sure target folder exists if not os.path.exists(_destinationDir): log.debug("Creating %r" % _destinationDir.rstrip(os.sep) + os.sep,) os.makedirs(_destinationDir) os.chmod(_destinationDir, 0775) # copy file, possibly across devices. try: cmd = ['rsync', '-rptvhogLR', '--progress', '--partial-dir=.rsync-partial', os.path.basename(pathname), _destinationDir] log.verbose(' '.join(cmd)) check_call(cmd, shell=False, stdin=None, stdout=None, stderr=None, cwd=os.path.dirname(pathname)) except CalledProcessError, exc: log.error("Incremental rsync Command returned with RC=%d, Ending" % (exc.returncode)) raise UnexpectedErrorOccured(exc)
class RenameSeries(Series): def __init__(self): log.trace('__init__ method: Started') super(RenameSeries, self).__init__() series_group = DadVision.cmdoptions.parser.add_argument_group( "Series Overrides", description=None) series_group.add_argument( "--sn", "--name", dest='series_name', type=str, default=None, nargs='?', help="Override Parsed Series Name with this value") self.parser = FileParser() self.topShows = getList(list='topshows', rtn=dict) self.regex_repack = re.compile('^.*(repack|proper).*$', re.IGNORECASE) self.check_suffix = re.compile( '^(?P<seriesname>.+?)[ \._\-](?P<year>[0-9][0-9][0-9][0-9]|US|us|Us)$', re.VERBOSE) self.regex_SeriesDir = re.compile( '^{}.*$'.format(DadVision.settings.SeriesDir), re.IGNORECASE) # self.check_year = re.compile('^(?P<seriesname>.+?)[ \._\-](?P<year>[0-9][0-9][0-9][0-9])$', re.VERBOSE) # self.check_US = re.compile('^(?P<seriesname>.+?)[ \._\-](?P<country>US)$', re.VERBOSE) self.hostname = socket.gethostname() self.xbmc_update_required = False self._last_series = None self.existing_series = os.listdir(DadVision.settings.SeriesDir) return @useLibraryLogging def renameSeries(self, pathname): log.trace("rename method: pathname:{}".format(pathname)) if os.path.isfile(pathname): log.debug("-----------------------------------------------") log.debug("Directory: %s" % os.path.split(pathname)[0]) log.debug("Filename: %s" % os.path.split(pathname)[1]) try: self.renameFile(os.path.realpath(pathname)) except (SeriesNotFound, SeasonNotFound, EpisodeNotFound), msg: an_error = traceback.format_exc() log.verbose( traceback.format_exception_only(type(an_error), an_error)[-1]) log.error('Unable to Rename File: {}'.format(msg)) elif os.path.isdir(pathname): for _root, _dirs, _files in os.walk(os.path.abspath(pathname), followlinks=False): for _dir in _dirs[:]: if ignored(_dir): log.debug("Ignoring %r" % os.path.join(_root, _dir)) self._del_dir(os.path.join(_root, _dir), Tree=True) continue if _dirs == [] and _files == []: self._del_dir(_root) continue elif _files == []: continue _files.sort() for _file_name in _files: _path_name = os.path.join(_root, _file_name) log.debug( "-----------------------------------------------") log.debug("Filename: %s" % _path_name) ext = os.path.splitext(_path_name)[1][1:] if ignored(_file_name) or os.path.splitext(_file_name)[1][ 1:] not in DadVision.settings.MediaExt: self._del_file(_path_name) self._del_dir(_root) continue try: self.renameFile(_path_name) except (SeriesNotFound, SeasonNotFound, EpisodeNotFound), msg: an_error = traceback.format_exc() log.verbose( traceback.format_exception_only( type(an_error), an_error)[-1]) log.error('Unable to Rename File: {}'.format(msg)) continue
def distributeFile(self, pathname): log.trace('distributeFile: %s %s' % (self.contentType, pathname)) """ Move or copy a single file. """ # ignored file? if DadVision.args.ignore and ignored(os.path.basename(pathname)): log.verbose("Ignoring %r!" % (pathname)) return if os.path.splitext( pathname)[1][1:].lower() not in DadVision.settings.MediaExt: log.verbose("Ignoring %r!" % (pathname)) return self._setContentType(pathname) if self.contentType == 'Movies' and DadVision.args.suppress_movies: log.verbose('Skipping Movie: %s' % pathname) return if self.contentType == "Series": _destinationDir = DadVision.settings.NewSeriesDir elif self.contentType == "Movies": _destinationDir = DadVision.settings.NewMoviesDir else: _destinationDir = DadVision.settings.UnpackDir # Adds the last directory to target name _destinationDir = os.path.join( _destinationDir, os.path.basename(os.path.dirname(pathname))) _distributedFile = os.path.join(_destinationDir, os.path.basename(pathname)) if os.path.exists(_distributedFile) and os.path.getsize( _distributedFile) == os.path.getsize(pathname): log.verbose("Skipped Copy: Already at Destination:") log.verbose(" {}".format(os.path.basename(pathname))) log.verbose(" {}".format( os.path.basename(_distributedFile))) else: # make sure target folder exists if not os.path.exists(_destinationDir): log.debug( "Creating %r" % _destinationDir.rstrip(os.sep) + os.sep, ) os.makedirs(_destinationDir) os.chmod(_destinationDir, 0775) # copy file, possibly across devices. try: cmd = [ 'rsync', '-rptvhogLR', '--progress', '--partial-dir=.rsync-partial', os.path.basename(pathname), _destinationDir ] log.verbose(' '.join(cmd)) check_call(cmd, shell=False, stdin=None, stdout=None, stderr=None, cwd=os.path.dirname(pathname)) except CalledProcessError, exc: log.error( "Incremental rsync Command returned with RC=%d, Ending" % (exc.returncode)) raise UnexpectedErrorOccured(exc)