3
0
mirror of https://git.launchpad.net/ubuntu-dev-tools synced 2025-03-13 16:11:15 +00:00

Pull out Python 2 support hacks

This commit is contained in:
Stefano Rivera 2019-09-04 19:17:00 -03:00
parent bb765237db
commit 301569e809
26 changed files with 40 additions and 251 deletions

@ -22,6 +22,7 @@ import glob
import optparse
import os
import shutil
import subprocess
import sys
import tempfile
@ -39,7 +40,6 @@ from ubuntutools.logger import Logger
from ubuntutools.misc import (system_distribution, vendor_to_distroinfo,
codename_to_distribution)
from ubuntutools.question import YesNoQuestion
from ubuntutools import subprocess
def error(msg):

@ -30,6 +30,7 @@
# that the target distribution is always meant to be Ubuntu Hardy.
import os
import subprocess
import sys
import debian.deb822
@ -40,7 +41,6 @@ import ubuntutools.version
from ubuntutools.config import UDTConfig
from ubuntutools.logger import Logger
from ubuntutools.question import YesNoQuestion
from ubuntutools import subprocess
class PbuilderDist(object):

@ -4,12 +4,11 @@ from setuptools import setup
import glob
import os
import re
import codecs
# look/set what version we have
changelog = "debian/changelog"
if os.path.exists(changelog):
head = codecs.open(changelog, 'r', 'utf-8', 'replace').readline()
head = open(changelog, 'r', encoding='utf-8').readline()
match = re.compile(r".*\((.*)\).*").match(head)
if match:
version = match.group(1)

@ -27,13 +27,13 @@ import os
import re
import shutil
import sys
from subprocess import call, check_call, Popen, PIPE
from tempfile import mkdtemp
from distro_info import UbuntuDistroInfo, DistroDataOutdated
from ubuntutools.config import ubu_email
from ubuntutools.question import YesNoQuestion, EditFile
from ubuntutools.subprocess import call, check_call, Popen, PIPE
from ubuntutools.update_maintainer import update_maintainer, restore_maintainer
try:

@ -20,11 +20,11 @@
#
# ##################################################################
import codecs
import fnmatch
import optparse
import os
import shutil
import subprocess
import sys
import textwrap
import urllib.request
@ -44,13 +44,12 @@ from ubuntutools.requestsync.mail import (
get_debian_srcpkg as requestsync_mail_get_debian_srcpkg)
from ubuntutools.requestsync.lp import get_debian_srcpkg, get_ubuntu_srcpkg
from ubuntutools.version import Version
from ubuntutools import subprocess
def remove_signature(dscname):
'''Removes the signature from a .dsc file if the .dsc file is signed.'''
dsc_file = codecs.open(dscname, encoding='utf-8')
dsc_file = open(dscname, encoding='utf-8')
if dsc_file.readline().strip() == "-----BEGIN PGP SIGNED MESSAGE-----":
unsigned_file = []
# search until begin of body found
@ -65,7 +64,7 @@ def remove_signature(dscname):
unsigned_file.append(line)
dsc_file.close()
dsc_file = codecs.open(dscname, "w", encoding='utf-8')
dsc_file = open(dscname, "w", encoding='utf-8')
dsc_file.writelines(unsigned_file)
dsc_file.close()
@ -169,7 +168,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror,
# read Debian distribution from debian/changelog if not specified
if debian_dist is None:
line = codecs.open("debian/changelog", encoding='utf-8').readline()
line = open("debian/changelog", encoding='utf-8').readline()
debian_dist = line.split(" ")[2].strip(";")
if not fakesync:
@ -199,7 +198,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror,
shutil.rmtree(directory, True)
# write changes file
changes_file = codecs.open(changes_filename, "w", encoding='utf-8')
changes_file = open(changes_filename, "w", encoding='utf-8')
changes_file.writelines(changes)
changes_file.close()

@ -21,10 +21,9 @@
# ##################################################################
import optparse
import subprocess
import sys
from ubuntutools import subprocess
def extract(iso, path):
command = ['isoinfo', '-R', '-i', iso, '-x', path]

@ -35,6 +35,7 @@ import hashlib
import json
import os.path
import re
import subprocess
import sys
from debian.changelog import Changelog, Version
@ -46,11 +47,6 @@ from ubuntutools.config import UDTConfig
from ubuntutools.lp.lpapicache import (Launchpad, Distribution,
SourcePackagePublishingHistory)
from ubuntutools.logger import Logger
from ubuntutools import subprocess
if sys.version_info[0] >= 3:
basestring = str
unicode = str
class DownloadError(Exception):
@ -587,14 +583,14 @@ class FakeSPPH(object):
if since_version is None:
return self._changelog
if isinstance(since_version, basestring):
if isinstance(since_version, str):
since_version = Version(since_version)
new_entries = []
for block in Changelog(self._changelog):
if block.version <= since_version:
break
new_entries.append(unicode(block))
new_entries.append(str(block))
return u''.join(new_entries)

@ -19,9 +19,9 @@
#
import os
import subprocess
from ubuntutools.logger import Logger
from ubuntutools import subprocess
def _build_preparation(result_directory):

@ -21,8 +21,6 @@
#
# Based on code written by Jonathan Davies <jpds@ubuntu.com>
from __future__ import print_function
# Uncomment for tracing LP API calls
# import httplib2
# httplib2.debuglevel = 1
@ -44,27 +42,6 @@ from ubuntutools.lp.udtexceptions import (AlreadyLoggedInError,
PocketDoesNotExistError,
SeriesNotFoundException)
if sys.version_info[0] >= 3:
basestring = str
unicode = str
# Shameless steal from python-six
def add_metaclass(metaclass):
"""Class decorator for creating a class with a metaclass."""
def wrapper(cls):
orig_vars = cls.__dict__.copy()
slots = orig_vars.get('__slots__')
if slots is not None:
if isinstance(slots, str):
slots = [slots]
for slots_var in slots:
orig_vars.pop(slots_var)
orig_vars.pop('__dict__', None)
orig_vars.pop('__weakref__', None)
return metaclass(cls.__name__, cls.__bases__, orig_vars)
return wrapper
__all__ = [
'Archive',
@ -140,15 +117,14 @@ class MetaWrapper(type):
cls._cache = dict()
@add_metaclass(MetaWrapper)
class BaseWrapper(object):
class BaseWrapper(object, metaclass=MetaWrapper):
'''
A base class from which other wrapper classes are derived.
'''
resource_type = None # it's a base class after all
def __new__(cls, data):
if isinstance(data, basestring) and data.startswith(str(Launchpad._root_uri)):
if isinstance(data, str) and data.startswith(str(Launchpad._root_uri)):
# looks like a LP API URL
# check if it's already cached
cached = cls._cache.get(data)
@ -225,7 +201,7 @@ class Distribution(BaseWrapper):
'''
Fetch the distribution object identified by 'dist' from LP.
'''
if not isinstance(dist, basestring):
if not isinstance(dist, str):
raise TypeError("Don't know what do with '%r'" % dist)
cached = cls._cache.get(dist)
if not cached:
@ -385,7 +361,7 @@ class Archive(BaseWrapper):
'''
if pocket is None:
pockets = frozenset(('Proposed', 'Updates', 'Security', 'Release'))
elif isinstance(pocket, basestring):
elif isinstance(pocket, str):
pockets = frozenset((pocket,))
else:
pockets = frozenset(pocket)
@ -593,14 +569,14 @@ class SourcePackagePublishingHistory(BaseWrapper):
if since_version is None:
return self._changelog
if isinstance(since_version, basestring):
if isinstance(since_version, str):
since_version = Version(since_version)
new_entries = []
for block in Changelog(self._changelog):
if block.version <= since_version:
break
new_entries.append(unicode(block))
new_entries.append(str(block))
return u''.join(new_entries)
def getBinaries(self):
@ -719,8 +695,7 @@ class MetaPersonTeam(MetaWrapper):
return cls._me
@add_metaclass(MetaPersonTeam)
class PersonTeam(BaseWrapper):
class PersonTeam(BaseWrapper, metaclass=MetaPersonTeam):
'''
Wrapper class around a LP person or team object.
'''
@ -743,7 +718,7 @@ class PersonTeam(BaseWrapper):
'''
Fetch the person or team object identified by 'url' from LP.
'''
if not isinstance(person_or_team, basestring):
if not isinstance(person_or_team, str):
raise TypeError("Don't know what do with '%r'" % person_or_team)
cached = cls._cache.get(person_or_team)
if not cached:
@ -771,9 +746,9 @@ class PersonTeam(BaseWrapper):
raise TypeError("'%r' is not an Archive object." % archive)
if not isinstance(distroseries, DistroSeries):
raise TypeError("'%r' is not a DistroSeries object." % distroseries)
if package is not None and not isinstance(package, basestring):
if package is not None and not isinstance(package, str):
raise TypeError('A source package name expected.')
if component is not None and not isinstance(component, basestring):
if component is not None and not isinstance(component, str):
raise TypeError('A component name expected.')
if package is None and component is None:
raise ValueError('Either a source package name or a component has '

@ -22,9 +22,8 @@
#
# ##################################################################
from __future__ import print_function
# Modules.
from subprocess import Popen, PIPE
import locale
import os
import sys
@ -32,7 +31,6 @@ import sys
import distro_info
from ubuntutools.lp.udtexceptions import PocketDoesNotExistError
from ubuntutools.subprocess import Popen, PIPE
_system_distribution_chain = []

@ -16,20 +16,12 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from __future__ import print_function
import codecs
import tempfile
import os
import re
import subprocess
import sys
import ubuntutools.subprocess
if sys.version_info[0] < 3:
input = raw_input # noqa, pylint: disable=undefined-variable
open = codecs.open
class Question(object):
def __init__(self, options, show_help=True):
@ -143,8 +135,7 @@ class EditFile(object):
done = False
while not done:
old_mtime = os.stat(self.filename).st_mtime
ubuntutools.subprocess.check_call(['sensible-editor',
self.filename])
subprocess.check_call(['sensible-editor', self.filename])
modified = old_mtime != os.stat(self.filename).st_mtime
placeholders_present = False
if self.placeholders:

@ -20,8 +20,6 @@
# Please see the /usr/share/common-licenses/GPL-2 file for the full text
# of the GNU General Public License license.
from __future__ import print_function
import re
from debian.deb822 import Changes

@ -20,13 +20,12 @@
# Please see the /usr/share/common-licenses/GPL-2 file for the full text
# of the GNU General Public License license.
from __future__ import print_function
import os
import re
import sys
import smtplib
import socket
import subprocess
import tempfile
from debian.changelog import Changelog, Version
@ -36,11 +35,6 @@ from ubuntutools.archive import rmadison, FakeSPPH
from ubuntutools.lp.udtexceptions import PackageNotFoundException
from ubuntutools.logger import Logger
from ubuntutools.question import confirmation_prompt, YesNoQuestion
from ubuntutools import subprocess
if sys.version_info[0] >= 3:
basestring = str
unicode = str
__all__ = [
@ -117,7 +111,7 @@ def get_ubuntu_delta_changelog(srcpkg):
distribution = block.distributions.split()[0].split('-')[0]
if debian_info.valid(distribution):
break
delta += [unicode(change) for change in block.changes()
delta += [str(change) for change in block.changes()
if change.strip()]
return u'\n'.join(delta)

@ -17,8 +17,8 @@
import os
import re
import subprocess
from ubuntutools import subprocess
from ubuntutools.logger import Logger
from ubuntutools.sponsor_patch.question import ask_for_manual_fixing
from functools import reduce

@ -15,8 +15,6 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from __future__ import print_function
import sys
from ubuntutools.question import Question, YesNoQuestion

@ -15,16 +15,14 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from __future__ import print_function
import os
import re
import subprocess
import sys
import debian.changelog
import debian.deb822
from ubuntutools import subprocess
from ubuntutools.logger import Logger
from ubuntutools.question import Question, YesNoQuestion

@ -15,18 +15,16 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from __future__ import print_function
import os
import pwd
import shutil
import subprocess
import sys
from distro_info import UbuntuDistroInfo
from launchpadlib.launchpad import Launchpad
from ubuntutools import subprocess
from ubuntutools.logger import Logger
from ubuntutools.update_maintainer import (update_maintainer,
MaintainerUpdateException)
@ -37,9 +35,6 @@ from ubuntutools.sponsor_patch.patch import Patch
from ubuntutools.sponsor_patch.question import ask_for_manual_fixing
from ubuntutools.sponsor_patch.source_package import SourcePackage
if sys.version_info[0] < 3:
range = xrange # noqa, pylint: disable=redefined-builtin,undefined-variable
def is_command_available(command, check_sbin=False):
"Is command in $PATH?"

@ -1,126 +0,0 @@
"""Drop-in replacement for subprocess with better defaults
This is an API-compatible replacement for the built-in subprocess
module whose defaults better line up with our tastes.
In particular, it:
- Adds support for the restore_signals flag if subprocess itself
doesn't support it
- Adds support for the encoding flag if subprocess itself doesn't
support it
- Defaults close_fds to True
"""
from __future__ import absolute_import
import inspect
import codecs
import signal
import subprocess
import sys
from subprocess import PIPE, STDOUT, CalledProcessError
__all__ = ['Popen', 'call', 'check_call', 'check_output', 'CalledProcessError',
'PIPE', 'STDOUT']
class Popen(subprocess.Popen):
def __init__(self, *args, **kwargs):
kwargs.setdefault('close_fds', True)
if sys.version_info[0] >= 3:
getargs = inspect.getfullargspec
encoding = None
else:
getargs = inspect.getargspec
encoding = kwargs.pop('encoding', None)
if 'restore_signals' not in getargs(subprocess.Popen.__init__)[0]:
given_preexec_fn = kwargs.pop('preexec_fn', None)
restore_signals = kwargs.pop('restore_signals', True)
def preexec_fn():
if restore_signals:
for sig in ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ'):
if hasattr(signal, sig):
signal.signal(getattr(signal, sig),
signal.SIG_DFL)
if given_preexec_fn:
given_preexec_fn()
kwargs['preexec_fn'] = preexec_fn
subprocess.Popen.__init__(self, *args, **kwargs)
if encoding is not None:
for channel in ('stdin', 'stdout', 'stderr'):
fd = getattr(self, channel)
if fd is not None:
setattr(self, channel, codecs.EncodedFile(fd, encoding))
# call, check_call, and check_output are copied directly from the
# subprocess module shipped with Python 2.7.1-5ubuntu2
def call(*popenargs, **kwargs):
"""Run command with arguments. Wait for command to complete, then
return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
"""
return Popen(*popenargs, **kwargs).wait()
def check_call(*popenargs, **kwargs):
"""Run command with arguments. Wait for command to complete. If
the exit code was zero then return, otherwise raise
CalledProcessError. The CalledProcessError object will have the
return code in the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
check_call(["ls", "-l"])
"""
retcode = call(*popenargs, **kwargs)
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise CalledProcessError(retcode, cmd)
return 0
def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string.
If the exit code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and output in the output attribute.
The arguments are the same as for the Popen constructor. Example:
>>> check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
The stdout argument is not allowed as it is used internally.
To capture standard error in the result, use stderr=STDOUT.
>>> check_output(["/bin/sh", "-c",
... "ls -l non_existent_file ; exit 0"],
... stderr=STDOUT)
'ls: non_existent_file: No such file or directory\n'
"""
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = Popen(stdout=PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise CalledProcessError(retcode, cmd, output=output)
return output

@ -21,9 +21,6 @@ import sys
import setup
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest

@ -17,7 +17,6 @@
import os.path
import shutil
import sys
import tempfile
from io import BytesIO
try:
@ -64,18 +63,11 @@ class DscVerificationTestCase(unittest.TestCase):
fn = 'test-data/example_1.0.orig.tar.gz'
with open(fn, 'rb') as f:
data = f.read()
if sys.version_info[0] >= 3:
last_byte = chr(data[-1] ^ 8).encode()
else:
last_byte = chr(ord(data[-1]) ^ 8)
data = data[:-1] + last_byte
m = mock.MagicMock(name='open', spec=open)
m.return_value = BytesIO(data)
if sys.version_info[0] >= 3:
target = 'builtins.open'
else:
target = '__builtin__.open'
with mock.patch(target, m):
with mock.patch('builtins.open', m):
self.assertFalse(self.dsc.verify_file(fn))
def test_sha1(self):

@ -49,15 +49,9 @@ class ConfigTestCase(unittest.TestCase):
def setUp(self):
super(ConfigTestCase, self).setUp()
if sys.version_info[0] < 3:
self.assertRegex = self.assertRegexpMatches
m = mock.mock_open()
m.side_effect = self._fake_open
if sys.version_info[0] >= 3:
target = 'builtins.open'
else:
target = '__builtin__.open'
patcher = mock.patch(target, m)
patcher = mock.patch('builtins.open', m)
self.addCleanup(patcher.stop)
patcher.start()

@ -14,9 +14,9 @@
"""test_flake8.py - Run flake8 check"""
import subprocess
import sys
from ubuntutools import subprocess
from ubuntutools.test import get_source_files, unittest, unittest_verbosity

@ -18,10 +18,10 @@ import fcntl
import os
import select
import signal
import subprocess
import time
import setup
from ubuntutools import subprocess
from ubuntutools.test import unittest
TIMEOUT = 10

@ -17,10 +17,10 @@
import os
import re
import subprocess
import sys
from ubuntutools.test import get_source_files, unittest, unittest_verbosity
from ubuntutools import subprocess
CONFIG = os.path.join(os.path.dirname(__file__), "pylint.conf")

@ -231,15 +231,9 @@ class UpdateMaintainerTestCase(unittest.TestCase):
# pylint: disable=C0103
def setUp(self):
if sys.version_info[0] < 3:
self.assertRegex = self.assertRegexpMatches
m = mock.mock_open()
m.side_effect = self._fake_open
if sys.version_info[0] >= 3:
target = 'builtins.open'
else:
target = '__builtin__.open'
patcher = mock.patch(target, m)
patcher = mock.patch('builtins.open', m)
self.addCleanup(patcher.stop)
patcher.start()
m = mock.MagicMock(side_effect=self._fake_isfile)

@ -14,8 +14,6 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from __future__ import print_function
"""This module is for updating the Maintainer field of an Ubuntu package."""
import os