'Renames an episode file and all related files to the location and filename as specified
in the naming settings.'

def rename(self):

    if (not ek(os.path.isfile, self.location)):
        logger.log(((u"Can't    perform    rename    on    " + self.location) + u"    when    it    doesn't    exist,    skipping"), logger.WARNING)
        return
    proper_path = self.proper_path()
    absolute_proper_path = ek(os.path.join, self.show.location, proper_path)
    (absolute_current_path_no_ext, file_ext) = ek(os.path.splitext, self.location)
    absolute_current_path_no_ext_length = len(absolute_current_path_no_ext)
    related_subs = []
    current_path = absolute_current_path_no_ext
    if absolute_current_path_no_ext.startswith(self.show.location):
        current_path = absolute_current_path_no_ext[len(self.show.location):]
    logger.log((((u'Renaming/moving    episode    from    the    base    path    ' + self.location) + u'    to    ') + absolute_proper_path), logger.DEBUG)
    if (proper_path == current_path):
        logger.log((((str(self.indexerid) + u':    File    ') + self.location) + u'    is    already    named    correctly,    skipping'), logger.DEBUG)
        return
    related_files = postProcessor.PostProcessor(self.location).list_associated_files(self.location, subfolders=True, rename=True)
    if (self.show.subtitles and sickbeard.SUBTITLES_DIR):
        subs_path = os.path.join(sickbeard.SUBTITLES_DIR, ek(os.path.basename, self.location))
        related_subs = postProcessor.PostProcessor(self.location).list_associated_files(subs_path, subtitles_only=True, subfolders=True, rename=True)
    logger.log((((u'Files    associated    to    ' + self.location) + u':    ') + str(related_files)), logger.DEBUG)
    result = helpers.rename_ep_file(self.location, absolute_proper_path, absolute_current_path_no_ext_length)
    for cur_related_file in related_files:
        cur_related_dir = ek(os.path.dirname, ek(os.path.abspath, cur_related_file))
        subfolder = cur_related_dir.replace(ek(os.path.dirname, ek(os.path.abspath, self.location)), u'')
        proper_related_dir = ek(os.path.dirname, ek(os.path.abspath, (absolute_proper_path + file_ext)))
        proper_related_path = absolute_proper_path.replace(proper_related_dir, (proper_related_dir + subfolder))
        cur_result = helpers.rename_ep_file(cur_related_file, proper_related_path, (absolute_current_path_no_ext_length + len(subfolder)))
        if (not cur_result):
            logger.log(((str(self.indexerid) + u':    Unable    to    rename    file    ') + cur_related_file), logger.ERROR)
    for cur_related_sub in related_subs:
        absolute_proper_subs_path = ek(os.path.join, sickbeard.SUBTITLES_DIR, self.formatted_filename())
        cur_result = helpers.rename_ep_file(cur_related_sub, absolute_proper_subs_path, absolute_current_path_no_ext_length)
        if (not cur_result):
            logger.log(((str(self.indexerid) + u':    Unable    to    rename    file    ') + cur_related_sub), logger.ERROR)
    with self.lock:
        if result:
            self.location = (absolute_proper_path + file_ext)
            for relEp in self.relatedEps:
                relEp.location = (absolute_proper_path + file_ext)
    for curEp in ([self] + self.relatedEps):
        curEp.checkForMetaFiles()
    sql_l = []
    with self.lock:
        for relEp in ([self] + self.relatedEps):
            sql_l.append(relEp.get_sql())
    if sql_l:
        main_db_con = db.DBConnection()
        main_db_con.mass_action(sql_l)
