Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def is_location_valid(self, location=None):
"""Whether the location is a valid file.
:param location:
:type location: str
:return:
:rtype: bool
"""
return ek(os.path.isfile, location or self._location)
logger.log(str(self.indexerid) + u': Loading all episodes from the show directory ' + self.location,
logger.DEBUG)
# get file list
media_files = helpers.listMediaFiles(self.location)
logger.log(u'%s: Found files: %s' %
(self.indexerid, media_files), logger.DEBUG)
# create TVEpisodes from each media file (if possible)
sql_l = []
for media_file in media_files:
cur_episode = None
logger.log(str(self.indexerid) + u': Creating episode from ' + media_file, logger.DEBUG)
try:
cur_episode = self.make_ep_from_file(ek(os.path.join, self.location, media_file))
except (ShowNotFoundException, EpisodeNotFoundException) as e:
logger.log(u'Episode ' + media_file + u' returned an exception: ' + ex(e), logger.ERROR)
continue
except EpisodeDeletedException:
logger.log(u'The episode deleted itself when I tried making an object for it', logger.DEBUG)
if cur_episode is None:
continue
# see if we should save the release name in the db
ep_file_name = ek(os.path.basename, cur_episode.location)
ep_file_name = ek(os.path.splitext, ep_file_name)[0]
try:
parse_result = NameParser(False, showObj=self, tryIndexers=True).parse(ep_file_name)
except (InvalidNameException, InvalidShowException):
same_file = False
cur_ep = self.get_episode(season, current_ep)
if not cur_ep:
try:
cur_ep = self.get_episode(season, current_ep, filepath)
if not cur_ep:
raise EpisodeNotFoundException
except EpisodeNotFoundException:
logger.log(u'{0}: Unable to figure out what this file is, skipping {1}'.format
(self.indexerid, filepath), logger.ERROR)
continue
else:
# if there is a new file associated with this ep then re-check the quality
if not cur_ep.location or ek(os.path.normpath, cur_ep.location) != ek(os.path.normpath, filepath):
logger.log(
u'{0}: The old episode had a different file associated with it, '
u're-checking the quality using the new filename {1}'.format(self.indexerid, filepath),
logger.DEBUG)
check_quality_again = True
with cur_ep.lock:
old_size = cur_ep.file_size
cur_ep.location = filepath
# if the sizes are the same then it's probably the same file
same_file = old_size and cur_ep.file_size == old_size
cur_ep.check_for_meta_files()
if root_ep is None:
root_ep = cur_ep
else:
def cache_image(self, image_url):
"""
Store cache of image in cache dir
:param image_url: Source URL
"""
path = ek(os.path.abspath, ek(os.path.join, sickbeard.CACHE_DIR, 'images', 'anidb'))
if not ek(os.path.exists, path):
ek(os.makedirs, path)
full_path = ek(os.path.join, path, ek(os.path.basename, image_url))
if not ek(os.path.isfile, full_path):
helpers.download_file(image_url, full_path, session=self.session)
cur_loc, base_name_only=False, subfolders=True)
if related_files:
logger.log(u'{id}: Found hanging associated files for {ep}, deleting: {files}'.format
(id=self.indexerid, ep=episode_num(season, episode), files=related_files),
logger.WARNING)
for related_file in related_files:
try:
ek(os.remove, related_file)
except Exception as e:
logger.log(u'Could not delete associated file: {0}. Error: {1}'.format
(related_file, e), logger.WARNING)
# clean up any empty season folders after deletion of associated files
for sub_dir in ek(os.listdir, self.location):
helpers.delete_empty_folders(ek(os.path.join, self.location, sub_dir), self.location)
if sql_l:
main_db_con = db.DBConnection()
main_db_con.mass_action(sql_l)
self.delete_episode()
return False
# don't update show status if show dir is missing, unless it's missing on purpose
if not self.show.is_location_valid() and \
not sickbeard.CREATE_MISSING_SHOW_DIRS and not sickbeard.ADD_SHOWS_WO_DIR:
logger.log(u'The show dir %s is missing, not bothering to change the episode statuses '
u"since it'd probably be invalid" % self.show.raw_location)
return
if self.location:
logger.log(u'{id}: Setting status for {ep} based on status {status} and location {location}'.format
(id=self.show.indexerid, ep=episode_num(season, episode),
status=statusStrings[self.status], location=self.location), logger.DEBUG)
if not ek(os.path.isfile, self.location):
if (self.airdate >= datetime.date.today() or self.airdate == datetime.date.fromordinal(1)) and \
self.status in (UNAIRED, UNKNOWN, WANTED):
# Need to check if is UNAIRED otherwise code will step into second 'IF'
# and make episode as default_ep_status
# If is a leaked episode and user manually snatched, it will respect status
# If is a fake (manually snatched), when user set as FAILED, status will be WANTED
# and code below will make it UNAIRED again
logger.log(u'%s: Episode airs in the future or has no airdate, marking it %s' %
(self.show.indexerid, statusStrings[UNAIRED]), logger.DEBUG)
self.status = UNAIRED
elif self.status in (UNAIRED, UNKNOWN):
# Only do UNAIRED/UNKNOWN, it could already be snatched/ignored/skipped,
# or downloaded/archived to disconnected media
logger.log(u'Episode has already aired, marking it %s' %
statusStrings[self.show.default_ep_status], logger.DEBUG)
self.status = self.show.default_ep_status if self.season > 0 else SKIPPED # auto-skip specials
logger.log(u'Attempt to %s cache file %s' % (action, cache_file))
try:
if sickbeard.TRASH_REMOVE_SHOW:
send2trash(cache_file)
else:
ek(os.remove, cache_file)
except OSError as e:
logger.log(u'Unable to %s %s: %s / %s' % (action, cache_file, repr(e), str(e)), logger.WARNING)
# remove entire show folder
if full:
try:
logger.log(u'Attempt to %s show folder %s' % (action, self.location))
# check first the read-only attribute
file_attribute = ek(os.stat, self.location)[0]
if not file_attribute & stat.S_IWRITE:
# File is read-only, so make it writeable
logger.log(u'Attempting to make writeable the read only folder %s' % self.location, logger.DEBUG)
try:
ek(os.chmod, self.location, stat.S_IWRITE)
except Exception:
logger.log(u'Unable to change permissions of %s' % self.location, logger.WARNING)
if sickbeard.TRASH_REMOVE_SHOW:
send2trash(self.location)
else:
ek(shutil.rmtree, self.location)
logger.log(u'%s show folder %s' % (('Deleted', 'Trashed')[sickbeard.TRASH_REMOVE_SHOW],
self.raw_location))