Exemplo n.º 1
0
def gen_map(tfilesdir):
    tfiles = [pjoin(tfilesdir, f) for f in listdir(tfilesdir) if f.endswith('torrent')]

    files_tfiles = {}
    for tfile in tfiles:
        files_tfiles.update(tfile_details(tfile))
    return files_tfiles
Exemplo n.º 2
0
def pick_file(dirpath):
    dircontent = listdir(dirpath)

    for f in dircontent:
        path = pjoin(dirpath, f)
        if isfile(path):
            return f
    raise Exception('Passed a dir with no content!')
Exemplo n.º 3
0
    def build_command_bundle(self, content_details, rename_map):
        #pylint: disable=too-many-locals
        # To avoid completely rewriting this ...
        content_abspath = content_details['content_abspath']
        num_discarded_files = content_details['num_discarded_files']
        orig_foldername = content_details['orig_foldername']
        orig_intermeds = content_details['orig_intermeds']
        orig_filenames = content_details['orig_filenames']
        orig_paths = content_details['orig_paths']

        ##### LET'S BUILD SOME COMMANDS!
        commands = []

        self.expected_dirs.append(self.dest_dirpath)

        commands.extend(['mkdir -p "%s"' % (self.dest_dirpath,)])

        rename_map_keys = sorted(rename_map.keys())
        commands.extend(['mv -nv "%s" "%s"' % (k, rename_map[k]) for k in rename_map_keys])

        torrentfile = tfile_from_filename(content_abspath, get_torrentfilesdir(self.global_conf))

        question = 'Should these be seeded?'
        if torrentfile:
            question = 'Seed using {f}?'.format(f=torrentfile)
        do_seed = optionator(question, ['yes', 'no', CANCEL])

        seeddir_paths = {}
        if do_seed == "":  # this means CANCEL
            return
        elif do_seed == "yes":
            if not torrentfile:
                torrentfile = self.old_find_torrentfile(content_abspath)

            torrentfile_commands = self.build_torrentfile_commands(
                torrentfile, content_abspath, orig_foldername, orig_paths, rename_map,
                orig_intermeds, orig_filenames)
            commands.extend(torrentfile_commands)
        else:
            # delete remainder of files in torrentdir
            # Don't delete the arg dir if it's the same as the target dir
            # (renaming files in-place)
            dud_files_remaining = num_discarded_files > 0
            only_ever_one_file_in_dir = (os.path.isdir(content_abspath) and len(orig_paths) == 1)

            if isdir(content_abspath):
                num_remaining_files = (len(listdir(content_abspath)) - len(orig_paths))
                dir_is_now_empty = (num_remaining_files == 0)
            else:
                dir_is_now_empty = False

            # delete the source if there are remaining dud files / the dir is empty,
            # provided that it's not also the dest
            if ((dud_files_remaining or only_ever_one_file_in_dir or dir_is_now_empty) and
                (realpath(content_abspath) != realpath(self.dest_dirpath))):

                if dir_is_now_empty:
                    commands.append('rmdir "%s"' % (content_abspath,))
                else:
                    # have taken wanted files, delete remaining dir
                    commands.append('echo "Files still in the folder"')
                    commands.append('ls -aR "%s/"' % (content_abspath,))
                    commands.append('rm -Irv "%s"' % (content_abspath,))

        # a command bundle
        return {
            'commands': commands,
            'symlinks': [seeddir_paths_ for _orig_path, seeddir_paths_ in seeddir_paths.items()],
            'torrentfile': torrentfile,
            'commands_run': []
        }