-
Notifications
You must be signed in to change notification settings - Fork 42
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
Check whether addons are Python 3 compatible #38
Comments
I love that idea, especially as it will help us migrate :) |
I would add a config setting where you can add a list of transformations. The following transformations should can be applied for all Kodi/XBMC versions: dict |
While thinking about this a bit more, I looked for any other alternatives to Sadly, there isn't anything already existing that meets our requirements. I did find this though, which is a flake8 plugin that has some 2to3 cases. Do you think creating something like this for our particular usecase would make sense? |
Hrm, maybe we can just talk to the cpython guys or do a PR? Just do an argument like |
What output would you expect from the tool? |
@Rechi Running The problem is, we want to report issues and not really fix them! Sadly, I haven't had much luck with 2to3 - it just isn't made for reporting and it can be done but will need significant changes to the codebase, which I'm not sure will be worth it. The best direction I currently have is to create something like this: https://github.com/openstack-dev/hacking/blob/master/hacking/checks/python23.py OpenStack Hacking is a tool very similar to what we're trying to build for addon-check - code quality tests and what not! It has been created as a flake8 plugin which contains a lot of custom checks. One of the custom checks is to test whether the code is Python 3 compatible. We can just copy this verbatim and then add more Py2/3 checks to it (there's a lot of those in the lib2to3 codebase.) What do you think about this approach? /cc @razzeee |
Can you put a number on how many things we would need to add? Really not looking forward to reimplementing a known factor. |
The suggestion with running 2to3 multiple times and using git to track changes was just something that came quickly into my mind. from lib2to3 import refactor
class KodiRefactoringTool(refactor.RefactoringTool):
def print_output(self, old, new, filename, equal):
if not equal:
print(filename)
for fix in ['dict', 'except', 'filter', 'has_key', 'import', 'itertools', 'map', 'ne', 'next', 'print', 'renames', 'types', 'xrange', 'zip']:
print(fix)
rt = KodiRefactoringTool(sorted(['lib2to3.fixes.fix_' + fix]), {}, sorted(['lib2to3.fixes.fix_' + fix]))
rt.refactor(['repo-scripts/script.module.requests'], None, None)
print("") |
@Rechi That is an amazing approach. I extended it to also print a diff of changes required, or we can extract just the line that the error is occurring on: import difflib
from lib2to3 import refactor
class KodiRefactoringTool(refactor.RefactoringTool):
def print_output(self, old, new, filename, equal):
# A fix has been made
if not equal:
print(filename)
# This diff can be commented to the Pull Request itself?
# So that users can see and fix?
diff = difflib.unified_diff(old.splitlines(), new.splitlines())
print("\n".join(diff))
# This will come from our config file
list_of_fixes = ['dict']
for fix in list_of_fixes:
print(fix)
fixer = ['lib2to3.fixes.fix_' + fix]
rt = KodiRefactoringTool(fixer, options=None, explicit=fixer)
rt.refactor(['/home/mzfr/dev/Demo/plugin.video.reddit_viewer/'])
print("\n\n") Except for the fact that this makes multiple passes over the addon code (which isn't really a problem since the code won't be big?) this approach is very good as it doesn't involve re-inventing the wheel! We now just need to decide how we want to report the issues to the user:
Here, by print I mean a comment on the user's pull request. |
Well code for a single addon might not be big. But code for a whole repo might be. So if we can optimize it, we should. Otherwise we will wait for the builds longer. Well what about an |
I'll do some runs on the big plugin branches and report times that this approach takes. |
Please also do something on repo-resources, which should be fine, if you detect if it really is a py file. But just in case ;P |
Hey @razzeee I'm trying to understand more about how Kodi's Python plugins run. Few questions:
|
|
This is fixed by #100 |
This is a generalization of @Rechi's comment here.
I was wondering if we should add a checker that detects whether the addon code is Python 3 compatible?
A very simple way is to just run
2to3
on the code and see if it detects any issues. We could try out other tools like modernize etc. as well. Even though these tools mostly detect if any cosmetic changes are required. They serve as a good first step towards python 3 compatibility.We could log their output as warning or even suggest changes like use
print() rather than print
inline as a comment on the pull request itself.An extension to this would be to just run
2to3
on the code that has been touched in the pull request, rather than running it on the entire file.Please let me know if this sounds reasonable and I can start working on a pull for this!
The text was updated successfully, but these errors were encountered: