jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/612271 )

Change subject: [bugfix] import svn_utils later in version.py
......................................................................

[bugfix] import svn_utils later in version.py

- with setuptools 49.2.0 it is discouraged to import distutils before
  Setuptools. Therefore import svn_utils when needed
- also remove Python 2 related code in version.py

Bug: T257772
Change-Id: I5b4bd3e45cbd6055b808fd279e3c2c671dc28b5a
---
M pywikibot/version.py
1 file changed, 19 insertions(+), 37 deletions(-)

Approvals:
  Zhuyifei1999: Looks good to me, but someone else must approve
  Xqt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/version.py b/pywikibot/version.py
index 0c6423e..db22ac2 100644
--- a/pywikibot/version.py
+++ b/pywikibot/version.py
@@ -5,43 +5,28 @@
 #
 # Distributed under the terms of the MIT license.
 #
-from __future__ import absolute_import, division, unicode_literals
-
 import codecs
 import datetime
-from importlib import import_module
 import json
 import os
+import pathlib
 import socket
 import subprocess
 import sys
 import time
 import xml.dom.minidom

-from contextlib import closing
+from contextlib import closing, suppress
 from distutils import log
 from distutils.sysconfig import get_python_lib
+from importlib import import_module
 from io import BytesIO
 from warnings import warn

-try:
-    from setuptools import svn_utils
-except ImportError:
-    try:
-        from setuptools_svn import svn_utils
-    except ImportError as e:
-        svn_utils = e
-
-try:
-    import pathlib
-except ImportError:
-    import pathlib2 as pathlib  # Python 2
-
-
 import pywikibot

 from pywikibot import config2 as config
-from pywikibot.tools import deprecated, PY2, UnicodeType
+from pywikibot.tools import deprecated

 cache = None
 _logger = 'version'
@@ -79,17 +64,15 @@
     data['cmp_ver'] = 'n/a'

     if online:
-        try:
+        with suppress(Exception):
             hsh3 = getversion_onlinerepo('tags/stable')
             hsh2 = getversion_onlinerepo()
             hsh1 = data['hsh']
             data['cmp_ver'] = 'UNKNOWN' if not hsh1 else (
                 'OUTDATED' if hsh1 not in (hsh2, hsh3) else 'ok')
-        except Exception:
-            pass

     data['hsh'] = data['hsh'][:7]  # make short hash from full hash
-    return '%(tag)s (%(hsh)s, %(rev)s, %(date)s, %(cmp_ver)s)' % data
+    return '{tag} ({hsh}, {rev}, {date}, {cmp_ver})'.format(**data)


 def getversiondict():
@@ -135,7 +118,7 @@
         pywikibot.debug('version algorithm exceptions:\n%r'
                         % exceptions, _logger)

-    if isinstance(date, UnicodeType):
+    if isinstance(date, str):
         datestring = date
     elif isinstance(date, time.struct_time):
         datestring = time.strftime('%Y/%m/%d, %H:%M:%S', date)
@@ -166,14 +149,14 @@
         with open(filename) as entries:
             version = entries.readline().strip()
             if version != '12':
-                for i in range(3):
+                for _ in range(3):
                     entries.readline()
                 tag = entries.readline().strip()
                 t = tag.split('://', 1)
                 t[1] = t[1].replace('svn.wikimedia.org/svnroot/pywikipedia/',
                                     '')
                 tag = '[{0}] {1}'.format(*t)
-                for i in range(4):
+                for _ in range(4):
                     entries.readline()
                 date = time.strptime(entries.readline()[:19],
                                      '%Y-%m-%dT%H:%M:%S')
@@ -233,8 +216,11 @@
         - hash (git hash for the Subversion revision)
     @rtype: C{tuple} of three C{str} and a C{time.struct_time}
     """
-    if isinstance(svn_utils, Exception):
-        raise svn_utils
+    try:
+        from setuptools import svn_utils
+    except ImportError:
+        from setuptools_svn import svn_utils
+
     tag = 'pywikibot-core'
     _program_dir = path or _get_program_dir()
     svninfo = svn_utils.SvnInfo(_program_dir)
@@ -417,10 +403,8 @@
         with codecs.open(fn, 'r', 'utf-8') as f:
             for line in f.readlines():
                 if line.find('__version__') == 0:
-                    try:
+                    with suppress(Exception):
                         exec(line)
-                    except Exception:
-                        pass
                     break
         stat = os.stat(fn)
         mtime = datetime.datetime.fromtimestamp(stat.st_mtime).isoformat(' ')
@@ -442,6 +426,7 @@
     """
     if hasattr(module, '__version__'):
         return module.__version__[5:-1]
+    return None


 def get_module_filename(module):
@@ -459,13 +444,12 @@
     """
     if hasattr(module, '__file__') and os.path.exists(module.__file__):
         filename = module.__file__
-        if PY2:
-            filename = os.path.abspath(filename)
         if filename[-4:-1] == '.py' and os.path.exists(filename[:-1]):
             filename = filename[:-1]
         program_dir = _get_program_dir()
         if filename[:len(program_dir)] == program_dir:
             return filename
+    return None


 def get_module_mtime(module):
@@ -480,6 +464,7 @@
     filename = get_module_filename(module)
     if filename:
         return datetime.datetime.fromtimestamp(os.stat(filename).st_mtime)
+    return None


 def package_versions(modules=None, builtins=False, standard_lib=None):
@@ -508,7 +493,7 @@

     # Improve performance by removing builtins from the list if possible.
     if builtins is False:
-        root_packages = list(root_packages - builtin_packages)
+        root_packages = root_packages - builtin_packages

     std_lib_packages = []

@@ -541,9 +526,6 @@
             if '__init__.py' in path:
                 path = path[0:path.index('__init__.py')]

-            if PY2:
-                path = path.decode(sys.getfilesystemencoding())
-
             info['path'] = path
             assert path not in paths, 'Path of the package is in defined paths'
             paths[path] = name

-- 
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/612271
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I5b4bd3e45cbd6055b808fd279e3c2c671dc28b5a
Gerrit-Change-Number: 612271
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Dvorapa <[email protected]>
Gerrit-Reviewer: Matěj Suchánek <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: Zhuyifei1999 <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to