'Convert all pickle download checkpoints to .state files.'

def convert_download_checkpoints(self):

    checkpoint_dir = self.session.get_downloads_pstate_dir()
    filelist = os.listdir(checkpoint_dir)
    if (not any([filename.endswith('.pickle') for filename in filelist])):
        return
    if os.path.exists(checkpoint_dir):
        for old_filename in glob.glob(os.path.join(checkpoint_dir, '*.pickle')):
            try:
                with open(old_filename, 'rb') as old_file:
                    old_checkpoint = pickle.load(old_file)
            except (EOFError, KeyError):
                os.remove(old_filename)
                continue
            new_checkpoint = RawConfigParser()
            new_checkpoint.add_section('downloadconfig')
            new_checkpoint.add_section('state')
            for (key, value) in old_checkpoint['dlconfig'].iteritems():
                if (key in ['saveas', 'max_upload_rate', 'max_download_rate', 'super_seeder', 'mode', 'selected_files', 'correctedfilename']):
                    new_checkpoint.set('downloadconfig', key, value)
            new_checkpoint.set('state', 'version', PERSISTENTSTATE_CURRENTVERSION)
            new_checkpoint.set('state', 'engineresumedata', old_checkpoint['engineresumedata'])
            new_checkpoint.set('state', 'dlstate', old_checkpoint['dlstate'])
            new_checkpoint.set('state', 'metainfo', old_checkpoint['metainfo'])
            with open(old_filename.replace('.pickle', '.state'), 'wb') as new_file:
                new_checkpoint.write(new_file)
            os.remove(old_filename)
