From cf9fbf7b3b67ca3e94359bc0af709ba142d216d6 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Wed, 4 Nov 2015 21:48:05 -0500 Subject: [PATCH 1/2] FIX: python 3 compatibility --- src/gitsweep/cli.py | 7 ++++++- src/gitsweep/tests/test_cli.py | 9 ++++++--- src/gitsweep/tests/testcases.py | 34 ++++++++++++++++++++++----------- tox.ini | 6 +++--- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/gitsweep/cli.py b/src/gitsweep/cli.py index 6b995b5..142596d 100644 --- a/src/gitsweep/cli.py +++ b/src/gitsweep/cli.py @@ -8,6 +8,11 @@ from gitsweep.inspector import Inspector from gitsweep.deleter import Deleter +try: + input = raw_input +except NameError: + pass + class CommandLine(object): @@ -142,7 +147,7 @@ def _sweep(self): if not args.force: sys.stdout.write('\nDelete these branches? (y/n) ') - answer = raw_input() + answer = input() if args.force or answer.lower().startswith('y'): sys.stdout.write('\n') for ref in ok_to_delete: diff --git a/src/gitsweep/tests/test_cli.py b/src/gitsweep/tests/test_cli.py index 9ddd6ce..e066875 100644 --- a/src/gitsweep/tests/test_cli.py +++ b/src/gitsweep/tests/test_cli.py @@ -1,4 +1,7 @@ -from mock import patch +try: + from unittest.mock import patch +except ImportError: + from mock import patch from gitsweep.tests.testcases import CommandTestCase @@ -135,7 +138,7 @@ def test_will_cleanup(self): self.make_commit() self.command('git merge branch{0}'.format(i)) - with patch('gitsweep.cli.raw_input', create=True) as ri: + with patch('gitsweep.cli.input', create=True) as ri: ri.return_value = 'y' (retcode, stdout, stderr) = self.gscommand('git-sweep cleanup') @@ -173,7 +176,7 @@ def test_will_abort_cleanup(self): self.make_commit() self.command('git merge branch{0}'.format(i)) - with patch('gitsweep.cli.raw_input', create=True) as ri: + with patch('gitsweep.cli.input', create=True) as ri: ri.return_value = 'n' (retcode, stdout, stderr) = self.gscommand('git-sweep cleanup') diff --git a/src/gitsweep/tests/testcases.py b/src/gitsweep/tests/testcases.py index fd9299c..aae78f1 100644 --- a/src/gitsweep/tests/testcases.py +++ b/src/gitsweep/tests/testcases.py @@ -1,12 +1,14 @@ import sys from os import chdir, getcwd from os.path import join, basename +import os from tempfile import mkdtemp from unittest import TestCase from uuid import uuid4 as uuid from shutil import rmtree from shlex import split -from contextlib import contextmanager, nested +from contextlib import contextmanager +import contextlib from textwrap import dedent from mock import patch @@ -60,7 +62,7 @@ def setUp(self): This will create the root commit in the test repository automaticall. """ super(GitSweepTestCase, self).setUp() - + os.environ['GIT_AUTHOR_NAME'] = 'test' repodir = mkdtemp() self.repodir = repodir @@ -248,16 +250,26 @@ def gscommand(self, command): patches = ( patch.object(sys, 'stdout'), patch.object(sys, 'stderr')) - - with nested(*patches): - stdout = sys.stdout - stderr = sys.stderr - try: - self.cli.run() - except SystemExit as se: - pass + try: + with contextlib.ExitStack() as stack: + [stack.enter_context(p) for p in patches] + stdout = sys.stdout + stderr = sys.stderr + try: + self.cli.run() + except SystemExit as se: + se_code = se.code + + except AttributeError: + with contextlib.nested(*patches): + stdout = sys.stdout + stderr = sys.stderr + try: + self.cli.run() + except SystemExit as se: + se_code = se.code stdout = ''.join([i[0][0] for i in stdout.write.call_args_list]) stderr = ''.join([i[0][0] for i in stderr.write.call_args_list]) - return (se.code, stdout, stderr) + return (se_code, stdout, stderr) diff --git a/tox.ini b/tox.ini index e46ef65..f62524a 100644 --- a/tox.ini +++ b/tox.ini @@ -4,8 +4,8 @@ deps = mock commands = python -m gitsweep.scripts.test -[testenv:2.6] -basepython = python2.6 - [testenv:2.7] basepython = python2.7 + +[testenv:3.5] +basepython = python3.5 From 68f6775896844a7237c9b063eca1b4ea2cce615d Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 5 Nov 2015 10:17:10 -0500 Subject: [PATCH 2/2] MNT: cleaned up last bits of python2/3 docs --- README.rst | 4 ++-- setup.py | 7 ++----- tox.ini | 4 ++++ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 5414eb6..88867b3 100644 --- a/README.rst +++ b/README.rst @@ -167,7 +167,7 @@ To run the tests, bootstrap Buildout and run this command: ... $ ./bin/test -We also use Tox_. It will run the tests for Python 2.6 and 2.7. +We also use Tox_. It will run the tests for Python 2.7, 3.4, and 3.5. :: @@ -177,7 +177,7 @@ Requirements ------------ * Git >= 1.7 -* Python >= 2.6 +* Python 2.7, 3.4 3.5 License ------- diff --git a/setup.py b/setup.py index e5483d8..7439f4e 100644 --- a/setup.py +++ b/setup.py @@ -11,10 +11,6 @@ install_requires = [ 'GitPython>=0.3.2RC1'] -# Add argparse if less than Python 2.7 -if sys.version_info[0] <= 2 and sys.version_info[1] < 7: - install_requires.append('argparse>=1.2.1') - setup(name='git-sweep', version=version, description="Clean up branches from your Git remotes", @@ -26,8 +22,9 @@ 'Intended Audience :: Developers', 'Natural Language :: English', 'Operating System :: POSIX', - 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', 'Topic :: Software Development :: Quality Assurance', 'Topic :: Software Development :: Version Control', 'Topic :: Text Processing' diff --git a/tox.ini b/tox.ini index f62524a..cec56b3 100644 --- a/tox.ini +++ b/tox.ini @@ -7,5 +7,9 @@ commands = python -m gitsweep.scripts.test [testenv:2.7] basepython = python2.7 + +[testenv:3.4] +basepython = python3.4 + [testenv:3.5] basepython = python3.5