def write_default_config(): confdir = expanduser('~/.retorrent') skelfile = '/usr/share/retorrent/' + config_filename + '_skel' if pexists(skelfile): if not pexists(pjoin(confdir, config_filename + '_skel')): print('Creating a skeleton $HOME/.retorrent/retorrent.conf, ' + 'please configure it to your system') mkdir_p(confdir) shutil.copyfile('/usr/share/retorrent/retorrent.conf_skel', expanduser('~/.retorrent/retorrent.conf_skel')) else: print('Please configure the retorrent.conf_skel in ${HOME}/.retorrent ' + 'and rename it to ' + config_filename) else: print('Cannot find ' + config_filename + ' or a valid skeleton ' + config_filename + '.') print('Please create and configure %s or check your installation. ' % (pjoin(confdir, config_filename)))
def _main(smbsafe=False): _, categories_conf = confparse.parse_retorrentconf() for_postprocessing = {} for _, category in categories_conf.items(): # print(category) category_home = category['symlink_path'] if smbsafe: category_home = category['smbsafe_symlink_path'] mkdir_p(expanduser(category_home)) debugprint('Considering: %s' % (basename(category_home))) removed = [] # remove broken symlinks for elem in os.listdir(category_home): elem_path = pjoin(category_home, elem) if not islink(elem_path): if isfile(elem_path): print('!! file in symlink dir: %s' % (elem_path)) elif not len(os.listdir(elem_path)): os.rmdir(elem_path) else: for f in os.listdir(elem_path): fpath = pjoin(elem_path, f) if not islink(fpath): print('Non-symlinked file detected in folder %s in %s' % (basename(fpath), elem_path)) print('\t Non-symlinked folders should only contain symlinks') elif not isfile(fpath): # broken symlink os.remove(fpath) else: if lexists(elem_path) and not pexists(elem_path): print('Broken symlink! Removing.', elem) removed += [elem] os.remove(elem_path) if category_home in category['content_paths']: print('The content home is in the list of content paths. Cannot continue.') continue # /mnt/foo/video/tv, /mnt/bar/video/tv etc. for content_dir in category['content_paths']: debugprint('Looking at: %r' % (content_dir,)) if not pexists(content_dir): # dir doesn't exist, no need to create it & check it continue for content in os.listdir(content_dir): content = content.decode('utf-8') content_path = pjoin(content_dir, content) debugprint('Examining: %s' % (content_path,), 2) # empty dir in content dir if isdir(content_path) and not len(os.listdir(content_path)): os.rmdir(content_path) print('%s: removed empty content dir' % (content_path,)) continue # symlinks in content dir elif islink(content_path): print('%s: symlink in content dir') if not pexists(content_path): print('%s: symlink was broken, removing') os.remove(content_path) # content can either be movie.avi or series.name/ content_abspath = abspath(pjoin(content_dir, content)) symlink_path = abspath(pjoin(category_home, content)) if smbsafe: symlink_path = abspath(pjoin(category_home, smbify(content))) # missing or broken symlink if not pexists(symlink_path): if islink(symlink_path): os.remove(symlink_path) os.symlink(content_abspath, symlink_path) if content in removed: print('Replaced a broken symlink', content) # symlink with same name already exists # and doesn't point to same location elif islink(symlink_path) and \ not realpath(symlink_path) == \ realpath(content_abspath): debugprint('%s already exists, and there are two candidates, %s and %s' % (symlink_path, realpath(symlink_path), realpath(content_abspath))) oldlink_realpath = realpath(symlink_path) # they're both links to different directories # create dir, populate with symlinks to the content # in both if isdir(oldlink_realpath) and \ isdir(content_abspath): # remove oldlink os.remove(symlink_path) # mkdir foo mkdir_p(symlink_path) link_contents(oldlink_realpath, symlink_path) link_contents(content_abspath, symlink_path) else: print('Duplicate files, can\'t combine :(') print('\t', oldlink_realpath) print('\t', content_abspath) # dir found in category_home. # Broken symlinks and empty dirs should be gone already elif not islink(symlink_path) and \ isdir(symlink_path): debugprint('Will post-process: %s' % (symlink_path,)) for_postprocessing.setdefault(symlink_path, []).append(content_abspath) else: # the symlink exists + points at this content :D debugprint('%s - OK' % (symlink_path,), 2) for symlink_path, content_paths in for_postprocessing.iteritems(): debugprint('Postprocessing: %s' % (symlink_path,)) for c in content_paths: link_contents(c, symlink_path) # prune dirs full of symlinks to the same place for elem in os.listdir(category_home): if isdir(elem): #If dir is full of symlinks, and all symlinks are to the same dir, # remove all symlinks, then symlink the dir symlinkdir_contents_paths = [pjoin(symlink_path, l) for l in os.listdir(symlink_path)] if (all([islink(f) for f in symlinkdir_contents_paths]) and all_symlinks_to_same_dir(content_abspath, symlinkdir_contents_paths)): for f in symlinkdir_contents_paths: if islink(f): os.remove(f) else: print('ERROR! Tried to remove a file! %s' % (f)) os.rmdir(symlink_path) print('\t%s no longer sourced from multiple locations' % (symlink_path,)) os.symlink(content_abspath, symlink_path) else: link_contents(content_abspath, symlink_path) print('\t%s: sourced from multiple locations' % (symlink_path,))