jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/610062 )
Change subject: [4.0] Remove code duplication of pwb.py and setup.py ......................................................................
[4.0] Remove code duplication of pwb.py and setup.py
- Remove duplicate code or Python version check: pwb.py imports mandatory or script dependecies from setup.py and setup.py check the Python version supported. Re-use this check in pwb.py - Remove PY2-related code - Additional doc for check_modules - import Path after version checking
Bug: T239542 Bug: T213287 Change-Id: I476a42efc439f4fed5d10888ece76d73b294d0c8 --- M pwb.py M setup.py 2 files changed, 25 insertions(+), 44 deletions(-)
Approvals: Zhuyifei1999: Looks good to me, approved jenkins-bot: Verified
diff --git a/pwb.py b/pwb.py index e3711e0..3880526 100755 --- a/pwb.py +++ b/pwb.py @@ -18,8 +18,7 @@ # # Distributed under the terms of the MIT license. # -from __future__ import (absolute_import, division, - print_function, unicode_literals) +from __future__ import absolute_import, print_function, unicode_literals
from difflib import get_close_matches from importlib import import_module @@ -30,36 +29,9 @@
from warnings import warn
-PYTHON_VERSION = sys.version_info[:3] -PY2 = (PYTHON_VERSION[0] == 2) - -if not PY2: - from pathlib import Path -else: - from pathlib2 import Path - - -VERSIONS_REQUIRED_MESSAGE = """ -Pywikibot is not available on: -{version} - -This version of Pywikibot only supports Python 2.7.4+ or 3.4+. -""" - - -def python_is_supported(): - """Check that Python is supported.""" - # Any change to this must be copied to setup.py - return PYTHON_VERSION >= (3, 4, 0) or PY2 and PYTHON_VERSION >= (2, 7, 4) - - -if not python_is_supported(): - print(VERSIONS_REQUIRED_MESSAGE.format(version=sys.version)) - sys.exit(1)
pwb = None
- # The following snippet was developed by Ned Batchelder (and others) # for coverage [1], with python 3 support [2] added later, # and is available under the BSD license (see [3]) @@ -70,6 +42,7 @@ # [3] # https://bitbucket.org/ned/coveragepy/src/2c5fb3a8b81c/setup.py?at=default#cl...
+ def run_python_file(filename, argv, argvu, package=None): """Run a python file as if it were the main program on the command line.
@@ -79,17 +52,12 @@ """ # Create a module to serve as __main__ old_main_mod = sys.modules['__main__'] - # it's explicitly using str() to bypass unicode_literals in Python 2 - main_mod = types.ModuleType(str('__main__')) + main_mod = types.ModuleType('__main__') sys.modules['__main__'] = main_mod main_mod.__file__ = filename - if not PY2: - main_mod.__builtins__ = sys.modules['builtins'] - else: - main_mod.__builtins__ = sys.modules['__builtin__'] + main_mod.__builtins__ = sys.modules['builtins'] if package: - # it's explicitly using str() to bypass unicode_literals in Python 2 - main_mod.__package__ = str(package) + main_mod.__package__ = package
# Set sys.argv and the first path element properly. old_argv = sys.argv @@ -163,7 +131,16 @@
def check_modules(script=None): - """Check whether mandatory modules are present.""" + """Check whether mandatory modules are present. + + This also checks Python version when importing deptendencies from setup.py + + @param script: The script name to be checked for dependencies + @type script: str or None + @return: True if all dependencies are installed + @rtype: bool + @raise RuntimeError: wrong Python version found in setup.py + """ import pkg_resources if script: from setup import script_deps @@ -201,9 +178,15 @@ return not missing_requirements
-if not check_modules(): +try: + if not check_modules(): + raise RuntimeError('') # no further output needed +except RuntimeError as e: + print(e) sys.exit()
+from pathlib import Path # noqa: E402 + filename, script_args, global_args = handle_args(*sys.argv)
# Search for user-config.py before creating one. @@ -211,8 +194,6 @@ # directories. See config2.py for details on search order. # Use env var to communicate to config2.py pwb.py location (bug T74918). _pwb_dir = os.path.split(__file__)[0] -if sys.platform == 'win32' and PY2: - _pwb_dir = str(_pwb_dir) os.environ['PYWIKIBOT_DIR_PWB'] = _pwb_dir try: import pywikibot as pwb diff --git a/setup.py b/setup.py index fe41208..9ecd187 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@
PYTHON_VERSION = sys.version_info[:3]
-versions_required_message = """ +VERSIONS_REQUIRED_MESSAGE = """ Pywikibot is not available on: {version}
@@ -46,12 +46,12 @@
def python_is_supported(): """Check that Python is supported.""" - # Any change to this must be copied to pwb.py return PYTHON_VERSION >= (3, 5, 0)
if not python_is_supported(): - raise RuntimeError(versions_required_message.format(version=sys.version)) + # pwb.py checks this exception + raise RuntimeError(VERSIONS_REQUIRED_MESSAGE.format(version=sys.version))
# ------- setup extra_requires ------- # extra_deps = {
pywikibot-commits@lists.wikimedia.org