[MediaWiki-commits] [Gerrit] pywikibot.tools: Raise error if bz2 not found - change (pywikibot/core)

2016-03-14 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: pywikibot.tools: Raise error if bz2 not found
..


pywikibot.tools: Raise error if bz2 not found

bz2 is required in pywikibot to open compressed files. But,
sometimes python is not compiled with bz2. This causes pywikibot
to fail while it is being imported.

This commit shows a warning in such cases and tries to use
bz2file as an alternative. If both are not found, it raises an
error lazily (i.e. only when bz2 is needed to open a compressed
file).

Bug: T123092
Change-Id: Iace28a5b356cff19118983fd089770c75a1b4dff
---
M pywikibot/tools/__init__.py
M requirements.txt
M setup.py
M tests/tools_tests.py
4 files changed, 49 insertions(+), 3 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index c2c5bba..0c5bcee 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -8,7 +8,6 @@
 from __future__ import absolute_import, print_function, unicode_literals
 __version__ = '$Id$'
 
-import bz2
 import collections
 import gzip
 import inspect
@@ -38,6 +37,15 @@
 
 from pywikibot.logging import debug
 
+try:
+import bz2
+except ImportError as bz2_import_error:
+try:
+import bz2file as bz2
+warn('package bz2 was not found; using bz2file', ImportWarning)
+except ImportError:
+warn('package bz2 and bz2file were not found', ImportWarning)
+bz2 = bz2_import_error
 
 if PYTHON_VERSION < (3, 5):
 # although deprecated in 3 completely no message was emitted until 3.5
@@ -988,6 +996,8 @@
 extension = ''
 
 if extension == 'bz2':
+if isinstance(bz2, ImportError):
+raise bz2
 return wrap(bz2.BZ2File(filename, mode), 1)
 elif extension == 'gz':
 return wrap(gzip.open(filename, mode), 0)
diff --git a/requirements.txt b/requirements.txt
index 1e6945e..3658030 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -91,3 +91,8 @@
 
 # scripts/weblinkchecker.py
 memento_client>=0.5.1
+
+# tools/__init__.py
+# pywikibot prefers using the inbuilt bz2 module if python was compiled with
+# bz2 support. But if it wasn't, bz2file is used instead.
+# bz2file
diff --git a/setup.py b/setup.py
index f56eab6..97f121a 100644
--- a/setup.py
+++ b/setup.py
@@ -41,7 +41,7 @@
 if not python_is_supported():
 raise RuntimeError(versions_required_message % sys.version)
 
-test_deps = []
+test_deps = ['bz2file']
 
 dependencies = ['requests']
 
@@ -141,6 +141,12 @@
 
 script_deps['data_ingestion.py'] = extra_deps['csv']
 
+try:
+import bz2  # noqa: unused import
+except ImportError:
+# Use bz2file if the python is not compiled with bz2 support.
+dependencies.append('bz2file')
+
 # Some of the ui_tests depend on accessing the console window's menu
 # to set the console font and copy and paste, achieved using pywinauto
 # which depends on pywin32.
diff --git a/tests/tools_tests.py b/tests/tools_tests.py
index 2d0935e..ed039b5 100644
--- a/tests/tools_tests.py
+++ b/tests/tools_tests.py
@@ -20,7 +20,9 @@
 from pywikibot import tools
 
 from tests import join_xml_data_path
-from tests.aspects import unittest, DeprecationTestCase, TestCase, 
MetaTestCaseClass
+from tests.aspects import (
+unittest, require_modules, DeprecationTestCase, TestCase, MetaTestCaseClass
+)
 from tests.utils import expected_failure_if, add_metaclass
 
 
@@ -106,6 +108,29 @@
 self.assertEqual(self._get_content(self.base_file + '.bz2', 
use_extension=False),
  self.original_content)
 
+@require_modules('bz2file')
+def test_open_archive_with_bz2file(self):
+"""Test open_archive when bz2file library."""
+old_bz2 = tools.bz2
+try:
+tools.bz2 = __import__('bz2file')
+self.assertEqual(self._get_content(self.base_file + '.bz2'),
+ self.original_content)
+self.assertEqual(self._get_content(self.base_file + '.bz2',
+   use_extension=False),
+ self.original_content)
+finally:
+tools.bz2 = old_bz2
+
+def test_open_archive_without_bz2(self):
+"""Test open_archive when bz2 and bz2file are not available."""
+old_bz2 = tools.bz2
+try:
+tools.bz2 = ImportError()
+self.assertRaises(ImportError, self._get_content, self.base_file + 
'.bz2')
+finally:
+tools.bz2 = old_bz2
+
 def test_open_archive_gz(self):
 """Test open_archive with gz compressor in the standard library."""
 self.assertEqual(self._get_content(self.base_file + '.gz'), 
self.original_content)

-- 
To view, visit https://gerrit.wikimedia.org/r/277207
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings


[MediaWiki-commits] [Gerrit] pywikibot.tools: Raise error if bz2 not found - change (pywikibot/core)

2016-03-14 Thread AbdealiJK (Code Review)
AbdealiJK has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/277207

Change subject: pywikibot.tools: Raise error if bz2 not found
..

pywikibot.tools: Raise error if bz2 not found

bz2 is required in pywikibot to open compressed files. But,
sometimes python is not compiled with bz2. This causes pywikibot
to fail while it is being imported.

This commit shows a warning in such cases and tries to use
bz2file as an alternative. If both are not found, it raises an
error lazily (i.e. only when bz2 is needed to open a compressed
file).

Bug T123092
Change-Id: Iace28a5b356cff19118983fd089770c75a1b4dff
---
M pywikibot/tools/__init__.py
1 file changed, 11 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/07/277207/1

diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index c2c5bba..5f62a92 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -8,7 +8,6 @@
 from __future__ import absolute_import, print_function, unicode_literals
 __version__ = '$Id$'
 
-import bz2
 import collections
 import gzip
 import inspect
@@ -38,6 +37,15 @@
 
 from pywikibot.logging import debug
 
+try:
+import bz2
+except ImportError as bz2_import_error:
+try:
+import bz2file as bz2
+warn('package bz2 was not found; using bz2file', ImportWarning)
+except ImportError:
+warn('package bz2 and bz2file were not found', ImportWarning)
+bz2 = False
 
 if PYTHON_VERSION < (3, 5):
 # although deprecated in 3 completely no message was emitted until 3.5
@@ -988,6 +996,8 @@
 extension = ''
 
 if extension == 'bz2':
+if not bz2:
+raise bz2_import_error
 return wrap(bz2.BZ2File(filename, mode), 1)
 elif extension == 'gz':
 return wrap(gzip.open(filename, mode), 0)

-- 
To view, visit https://gerrit.wikimedia.org/r/277207
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iace28a5b356cff19118983fd089770c75a1b4dff
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: AbdealiJK 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits