uyuni-project / virtual-host-gatherer

Script to gather information about virtual system running on different kind of hypervisors.
6 stars 13 forks source link

.pylintrc init-hook script is not compatible with latest versions of pylint #21

Closed rtamalin closed 2 years ago

rtamalin commented 2 years ago

The init-hook script currently specified in the .pylintrc corresponds is as follows:

import os, sys

if 'VIRTUAL_ENV' in os.environ:

    ve_dir = os.environ['VIRTUAL_ENV']
    ve_dir in sys.path or sys.path.insert(0, ve_dir)
    activate_this = os.path.join(os.path.join(ve_dir, 'bin'), 'activate_this.py')

    # Fix for windows
    if not os.path.exists(activate_this):
        activate_this = os.path.join(os.path.join(ve_dir, 'Scripts'), 'activate_this.py')

    execfile(activate_this, dict(__file__=activate_this))

The execfile() on the last line is a Python 2 mechanism that is no longer available in Python 3, and backwards compatibility support for it has been dropped in more recent versions of pylint:

(pylint) ~/.../virtual-host-gatherer> pylint --rcfile=.pylintrc lib/gatherer/modules
Traceback (most recent call last):
  File "/home/vagrant/.venvs/pylint/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "/home/vagrant/.venvs/pylint/lib/python3.6/site-packages/pylint/__init__.py", line 24, in run_pylint
    PylintRun(sys.argv[1:])
  File "/home/vagrant/.venvs/pylint/lib/python3.6/site-packages/pylint/lint/run.py", line 340, in __init__
    "init-hook", utils._unquote(config_parser.get("MASTER", "init-hook"))
  File "/home/vagrant/.venvs/pylint/lib/python3.6/site-packages/pylint/lint/run.py", line 50, in cb_init_hook
    exec(value)  # pylint: disable=exec-used
  File "<string>", line 11, in <module>
  File "<string>", line 13, in <module>
NameError: name 'execfile' is not defined

Some research identified that the following is a safe equivalent for the execfile() call that works with older and newer Python and pylint versions:

exec(compile(open(activate_this, 'rb').read(), analyse_this, 'exec'), dict(__file__=analyse_this))

I'll proposed a PR with the necessary change soon.