PluginDownloader: replace automatic 2to3 step with a simple warning

The previous heuristic runs into false positives when imports are merged in __init__.py

More broadly though, it's unlikely automatic 2to3 is particularly useful in 2022 - plugins that were written ~10 years ago are unlikely to work even if syntax errors are fixed.
This commit is contained in:
James Lu 2022-06-23 13:12:13 -07:00 committed by Val Lorentz
parent 8ccf2c7175
commit d00113e92d
2 changed files with 12 additions and 40 deletions

View File

@ -121,11 +121,11 @@ class GithubRepository(GitRepository):
assert directory is not None, \
'No valid directory in supybot.directories.plugins.'
possibly_incompatible = False
try:
assert archive.getmember(prefix + dirname).isdir(), \
'This is not a valid plugin (it is a file, not a directory).'
run_2to3 = minisix.PY3
for file in archive.getmembers():
if file.name.startswith(prefix + dirname):
extractedFile = archive.extractfile(file)
@ -140,41 +140,17 @@ class GithubRepository(GitRepository):
os.mkdir(newFileName)
else:
with open(newFileName, 'ab') as fd:
reload_imported = False
for line in extractedFile.readlines():
if minisix.PY3:
if b'import reload' in line:
reload_imported = True
elif not reload_imported and \
b'reload(' in line:
fd.write(b'from importlib import reload\n')
reload_imported = True
if file.name.endswith('__init__.py') and \
line.startswith((b'import config', b'import plugin')):
possibly_incompatible = True
fd.write(line)
if newFileName.endswith('__init__.py'):
with open(newFileName) as fd:
lines = list(filter(lambda x:'import plugin' in x,
fd.readlines()))
if lines and lines[0].startswith('from . import'):
# This should be already Python 3-compatible
run_2to3 = False
finally:
archive.close()
del archive
if run_2to3:
try:
import lib2to3
except ImportError:
return _('Plugin is probably not compatible with your '
'Python version (3.x) and could not be converted '
'because 2to3 is not installed.')
import subprocess
fixers = []
subprocess.Popen(['2to3', '-wn', os.path.join(directory, plugin)]) \
.wait()
return _('Plugin was designed for Python 2, but an attempt to '
'convert it to Python 3 has been made. There is no '
'guarantee it will work, though.')
else:
if possibly_incompatible:
return _('Plugin installed. However, it may be incompatible with '
'Python 3 and require manual code changes to work correctly.')
return _('Plugin successfully installed.')
def getInfo(self, plugin):

View File

@ -29,11 +29,9 @@
###
import os
import sys
import shutil
from supybot.test import *
import supybot.utils.minisix as minisix
pluginsPath = '%s/test-plugins' % os.getcwd()
@ -80,17 +78,15 @@ class PluginDownloaderTestCase(PluginTestCase):
self.assertNotError('plugindownloader install Hoaas DuckDuckGo')
self._testPluginInstalled('DuckDuckGo')
def testInstallLegacyWarning(self):
self.assertRegexp('plugindownloader install frumious Codepoints',
'may be incompatible')
def testInfo(self):
self.assertResponse('plugindownloader info progval Twitter',
'Advanced Twitter plugin for Supybot, with capabilities '
'handling, and per-channel user account.')
if minisix.PY3:
def test_2to3(self):
self.assertRegexp('plugindownloader install SpiderDave Pastebin',
'convert')
self.assertNotError('load Pastebin')
if not network:
class PluginDownloaderTestCase(PluginTestCase):
pass