/Tribler/Main/Utility/compat.py

https://gitlab.com/billyprice1/tribler
Python | 146 lines | 101 code | 27 blank | 18 comment | 40 complexity | 96cfc9ec820d0de1283952d09e7f7670 MD5 | raw file
  1. # Written by Egbert Bouman
  2. #
  3. # Things to handle backward compatability for old-style config files
  4. #
  5. import io
  6. import os
  7. import glob
  8. import json
  9. import pickle
  10. import cPickle
  11. import StringIO
  12. from ConfigParser import RawConfigParser
  13. from Tribler.Core.SessionConfig import SessionStartupConfig
  14. from Tribler.Main.globals import DefaultDownloadStartupConfig
  15. from Tribler.Core.simpledefs import PERSISTENTSTATE_CURRENTVERSION
  16. def convertSessionConfig(oldfilename, newfilename):
  17. # Convert tribler <= 6.2 session config file to tribler 6.3
  18. # We assume oldfilename exists
  19. with open(oldfilename, "rb") as f:
  20. sessconfig = pickle.load(f)
  21. # Upgrade to new config
  22. sconfig = SessionStartupConfig()
  23. for key, value in sessconfig.iteritems():
  24. if key in ['state_dir', 'install_dir', 'ip', 'minport', 'maxport', 'bind', 'ipv6_enabled',
  25. 'ipv6_binds_v4', 'timeout', 'timeout_check_interval', 'eckeypairfilename', 'megacache',
  26. 'nickname', 'mugshot', 'videoanalyserpath', 'peer_icon_path', 'live_aux_seeders']:
  27. sconfig.sessconfig.set('general', key, value)
  28. if key in ['mainline_dht', 'mainline_dht_port']:
  29. sconfig.sessconfig.set('mainline_dht', 'enabled' if key == 'mainline_dht' else key, value)
  30. if key == 'torrent_checking':
  31. sconfig.sessconfig.set('torrent_checking', 'enabled', value)
  32. if key in ['torrent_collecting', 'dht_torrent_collecting', 'torrent_collecting_max_torrents',
  33. 'torrent_collecting_dir', 'stop_collecting_threshold']:
  34. sconfig.sessconfig.set('torrent_collecting', 'enabled' if key == 'torrent_collecting' else key, value)
  35. if key in ['libtorrent', 'lt_proxytype', 'lt_proxyserver', 'lt_proxyauth']:
  36. sconfig.sessconfig.set('libtorrent', 'enabled' if key == 'libtorrent' else key, value)
  37. if key in ['dispersy_port', 'dispersy']:
  38. sconfig.sessconfig.set('dispersy', 'enabled' if key == 'dispersy' else key, value)
  39. # Save the new file, remove the old one
  40. sconfig.save(newfilename)
  41. os.remove(oldfilename)
  42. return sconfig
  43. def convertMainConfig(state_dir, oldfilename, newfilename):
  44. # Convert tribler <= 6.2 config files to tribler 6.3
  45. # We assume oldfilename exists
  46. with io.open(oldfilename, 'r', encoding='utf_8_sig') as f:
  47. corrected_config = StringIO.StringIO(f.read().replace('[ABC]', '[Tribler]'))
  48. config = RawConfigParser()
  49. config.readfp(corrected_config)
  50. # Convert user_download_choice.pickle
  51. udcfilename = os.path.join(state_dir, 'user_download_choice.pickle')
  52. if os.path.exists(udcfilename):
  53. with open(udcfilename, "r") as f:
  54. choices = cPickle.Unpickler(f).load()
  55. choices = dict([(k.encode('hex'), v) for k, v in choices["download_state"].iteritems()])
  56. config.set('Tribler', 'user_download_choice', json.dumps(choices))
  57. os.remove(udcfilename)
  58. # Convert gui_settings
  59. guifilename = os.path.join(state_dir, 'gui_settings')
  60. if os.path.exists(guifilename):
  61. with open(guifilename, "r") as f:
  62. for line in f.readlines():
  63. key, value = line.split('=')
  64. config.set('Tribler', key, value.strip())
  65. os.remove(guifilename)
  66. # Convert recent_download_history
  67. histfilename = os.path.join(state_dir, 'recent_download_history')
  68. if os.path.exists(histfilename):
  69. with open(histfilename, "r") as f:
  70. history = []
  71. for line in f.readlines():
  72. key, value = line.split('=')
  73. if value != '' and value != '\n':
  74. history.append(value.replace('\\\\', '\\').strip())
  75. config.set('Tribler', 'recent_download_history', json.dumps(history))
  76. os.remove(histfilename)
  77. with open(newfilename, "wb") as f:
  78. config.write(f)
  79. os.remove(oldfilename)
  80. def convertDefaultDownloadConfig(oldfilename, newfilename):
  81. # Convert tribler <= 6.2 default download config file to tribler 6.3
  82. # We assume oldfilename exists
  83. with open(oldfilename, "rb") as f:
  84. dlconfig = pickle.load(f)
  85. # Upgrade to new config
  86. ddsconfig = DefaultDownloadStartupConfig()
  87. for key, value in dlconfig.iteritems():
  88. if key in ['saveas', 'max_upload_rate', 'max_download_rate', 'super_seeder', 'mode', 'selected_files',
  89. 'correctedfilename']:
  90. ddsconfig.dlconfig.set('downloadconfig', key, value)
  91. # Save the new file, remove the old one
  92. ddsconfig.save(newfilename)
  93. os.remove(oldfilename)
  94. return ddsconfig
  95. def convertDownloadCheckpoints(checkpoint_dir):
  96. # Convert tribler <= 6.2 download checkpoints to tribler 6.3
  97. if os.path.exists(checkpoint_dir):
  98. for old_filename in glob.glob(os.path.join(checkpoint_dir, '*.pickle')):
  99. old_checkpoint = None
  100. try:
  101. with open(old_filename, "rb") as old_file:
  102. old_checkpoint = pickle.load(old_file)
  103. except:
  104. pass
  105. if old_checkpoint:
  106. new_checkpoint = RawConfigParser()
  107. new_checkpoint.add_section('downloadconfig')
  108. new_checkpoint.add_section('state')
  109. for key, value in old_checkpoint['dlconfig'].iteritems():
  110. if key in ['saveas', 'max_upload_rate', 'max_download_rate', 'super_seeder', 'mode',
  111. 'selected_files', 'correctedfilename']:
  112. new_checkpoint.set('downloadconfig', key, value)
  113. new_checkpoint.set('state', 'version', PERSISTENTSTATE_CURRENTVERSION)
  114. new_checkpoint.set('state', 'engineresumedata', old_checkpoint['engineresumedata'])
  115. new_checkpoint.set('state', 'dlstate', old_checkpoint['dlstate'])
  116. new_checkpoint.set('state', 'metainfo', old_checkpoint['metainfo'])
  117. with open(old_filename.replace('.pickle', '.state'), "wb") as new_file:
  118. new_checkpoint.write(new_file)
  119. os.remove(old_filename)