Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/remove obsolete upgrades #6558

Merged
merged 2 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/tribler-common/tribler_common/version_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
In some sense, the system works exactly as GIT does: it "branches" from the last-non conflicting version dir
and "stashes" the state dirs with conflicting names by renaming them.

Note that due to failures in design pre-7.4 series and 7.4.x series get special treatment.
Versions prior to 7.5 are not supported.
"""


Expand Down Expand Up @@ -100,12 +100,6 @@ def __repr__(self):
return f'<{self.__class__.__name__}{{{self.version_str}}}>'

def get_directory(self):
if self.major_minor < (7, 4):
# This should only happen for old "7.0.0-GIT" case
return self.root_state_dir
if self.major_minor == (7, 4):
# 7.4.x are treated specially
return self.root_state_dir / (".".join(str(part) for part in LooseVersion(self.version_str).version[:3]))
return self.root_state_dir / ('%d.%d' % self.major_minor)

def state_exists(self):
Expand Down Expand Up @@ -207,11 +201,6 @@ def __init__(self, root_state_dir: Path, code_version_id: Optional[str] = None):
self.versions = versions = OrderedDict()
if self.file_path.exists():
self.load(self.file_path)
elif (root_state_dir / "triblerd.conf").exists():
# Pre-7.4 versions of Tribler don't have history file
# and can by detected by presence of the triblerd.conf file in the root directory
version = TriblerVersion(root_state_dir, "7.3", 0.0)
self.add_version(version)

versions_by_time = []
last_run_version = None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,127 +1,10 @@
import ast
import base64
import logging
import os
from configparser import MissingSectionHeaderError
from lib2to3.pgen2.parse import ParseError

from configobj import ConfigObj, ParseError as ConfigObjParseError

from tribler_common.simpledefs import STATEDIR_CHECKPOINT_DIR

from tribler_core.components.libtorrent.download_manager.download_config import DownloadConfig
from tribler_core.components.libtorrent.torrentdef import TorrentDef
from tribler_core.utilities.configparser import CallbackConfigParser
from tribler_core.components.libtorrent.utils.libtorrent_helper import libtorrent as lt
from tribler_core.utilities.unicode import recursive_ungarble_metainfo
from configobj import ConfigObj

logger = logging.getLogger(__name__)


def load_config(filename: str):
return ConfigObj(infile=filename, encoding='utf8')


def convert_config_to_tribler74(state_dir):
"""
Convert the download config files to Tribler 7.4 format. The extensions will also be renamed from .state to .conf
"""
from lib2to3.refactor import RefactoringTool, get_fixers_from_package
refactoring_tool = RefactoringTool(fixer_names=get_fixers_from_package('lib2to3.fixes'))

for filename in (state_dir / STATEDIR_CHECKPOINT_DIR).glob('*.state'):
convert_state_file_to_conf_74(filename, refactoring_tool=refactoring_tool)


def convert_state_file_to_conf_74(filename, refactoring_tool=None):
"""
Converts .pstate file (pre-7.4.0) to .conf file.
:param filename: .pstate file
:param refactoring_tool: RefactoringTool instance if using Python3
:return: fixed config
"""
def _fix_state_config(config):
for section, option in [('state', 'metainfo'), ('state', 'engineresumedata')]:
value = config.get(section, option, literal_eval=False)
if not value or not refactoring_tool:
continue

try:
value = str(refactoring_tool.refactor_string(value + '\n', option + '_2to3'))
ungarbled_dict = recursive_ungarble_metainfo(ast.literal_eval(value))
value = ungarbled_dict or ast.literal_eval(value)
config.set(section, option, base64.b64encode(lt.bencode(value)).decode('utf-8'))
except (ValueError, SyntaxError, ParseError) as ex:
logger.error("Config could not be fixed, probably corrupted. Exception: %s %s", type(ex), str(ex))
return None
return config

old_config = CallbackConfigParser()
try:
old_config.read_file(str(filename))
except MissingSectionHeaderError:
logger.error("Removing download state file %s since it appears to be corrupt", filename)
os.remove(filename)

# We first need to fix the .state file such that it has the correct metainfo/resumedata.
# If the config cannot be fixed, it is likely corrupted in which case we simply remove the file.
fixed_config = _fix_state_config(old_config)
if not fixed_config:
logger.error("Removing download state file %s since it could not be fixed", filename)
os.remove(filename)
return

# Remove dlstate since the same information is already stored in the resumedata
if old_config.has_option('state', 'dlstate'):
old_config.remove_option('state', 'dlstate')

try:
conf_filename = str(filename)[:-6] + '.conf'
new_config = load_config(conf_filename)
for section in old_config.sections():
for key, _ in old_config.items(section):
val = old_config.get(section, key)
if section not in new_config:
new_config[section] = {}
new_config[section][key] = val
new_config.write()
os.remove(filename)
except ConfigObjParseError:
logger.error("Could not parse %s file on upgrade so removing it", filename)
os.remove(filename)

return fixed_config


def convert_config_to_tribler75(state_dir):
"""
Convert the download config files from Tribler 7.4 to 7.5 format.
"""
for filename in (state_dir / STATEDIR_CHECKPOINT_DIR).glob('*.conf'):
try:
config = DownloadConfig.load(filename)

# Convert resume data
resumedata = config.get_engineresumedata()
if b'mapped_files' in resumedata:
resumedata.pop(b'mapped_files')
config.set_engineresumedata(resumedata)
config.write(str(filename))

# Convert metainfo
metainfo = config.get_metainfo()
if not config.config['download_defaults'].get('selected_files') or not metainfo:
# no conversion needed/possible, selected files will be reset to their default (i.e., all files)
continue
tdef = TorrentDef.load_from_dict(metainfo)
config.set_selected_files([tdef.get_index_of_file_in_files(fn)
for fn in config.config['download_defaults'].pop('selected_files')])
config.write(str(filename))
except (ConfigObjParseError, UnicodeDecodeError):
logger.error("Could not parse %s file so removing it", filename)
os.remove(filename)


def convert_config_to_tribler76(state_dir):
"""
Convert the download config files from Tribler 7.5 to 7.6 format.
Expand Down
Loading