Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove more Python 2 artifacts #184

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions scripts/rosdistro_build_cache
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import argparse
import gzip
import logging
Expand Down Expand Up @@ -92,13 +90,7 @@ def main():
if args.dist_names and dist_name not in args.dist_names:
continue
# write the cache
data = yaml.dump(cache.get_data(), Dumper=CacheYamlDumper)

# On Python 3, we must encode the unicode yaml str prior to writing it,
# whereas for Python 2, the yaml output is a str which cannot be further
# encoded (compared with a unicode object).
if sys.version_info[0] >= 3:
data = data.encode('utf-8')
data = yaml.dump(cache.get_data(), Dumper=CacheYamlDumper).encode()

with open('%s-cache.yaml' % dist_name, 'wb') as f:
print('- write cache file "%s-cache.yaml"' % dist_name)
Expand Down
7 changes: 1 addition & 6 deletions scripts/rosdistro_convert
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,11 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import os
import subprocess
import sys
import tempfile
try:
from urllib.error import HTTPError
except ImportError:
from urllib2 import HTTPError
from urllib.error import HTTPError

from rosdistro.common import rmtree
from rosdistro.loader import load_url
Expand Down
2 changes: 0 additions & 2 deletions scripts/rosdistro_freeze_source
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import argparse
import os.path
import sys
Expand Down
2 changes: 0 additions & 2 deletions scripts/rosdistro_migrate_to_rep_141
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env python3

from __future__ import print_function

import argparse
import gzip
import os
Expand Down
2 changes: 0 additions & 2 deletions scripts/rosdistro_migrate_to_rep_143
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env python3

from __future__ import print_function

import argparse

from rosdistro.verify import _yaml_header_lines
Expand Down
2 changes: 0 additions & 2 deletions src/rosdistro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import gzip
import logging
import os
Expand Down
5 changes: 1 addition & 4 deletions src/rosdistro/aptdistro.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
try:
from urllib2 import urlopen
except ImportError:
from urllib.request import urlopen
from urllib.request import urlopen


class AptDistro:
Expand Down
2 changes: 0 additions & 2 deletions src/rosdistro/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

from errno import EACCES, EPERM
import os
import shutil
Expand Down
6 changes: 2 additions & 4 deletions src/rosdistro/develdistro.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
from urllib.request import urlopen

import yaml


Expand Down
7 changes: 0 additions & 7 deletions src/rosdistro/distribution_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import sys

from . import logger
Expand Down Expand Up @@ -71,11 +69,6 @@ def __init__(self, name, data=None, distribution_file_data=None):
self.source_repo_package_xmls[repo_name] = SourceRepositoryCache(repo_data)
self.distribution_file.source_packages = self.get_source_packages()

# if Python 2 has converted the xml to unicode, convert it back
for k, v in self.release_package_xmls.items():
if not isinstance(v, str) and not isinstance(v, bytes):
self.release_package_xmls[k] = v.encode('utf-8')

def get_data(self):
data = {}
data['type'] = 'cache'
Expand Down
9 changes: 1 addition & 8 deletions src/rosdistro/distribution_cache_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import gzip
import os
import re
Expand Down Expand Up @@ -155,12 +153,7 @@ def __init__(self, *args, **kwargs):

def ignore_aliases(self, content):
""" Allow strings that look like package XML to alias to each other in the YAML output. """
try:
basestring
except NameError:
# Python 3
basestring = str
return not (isinstance(content, basestring) and '<package' in content)
return not (isinstance(content, str) and '<package' in content)

def represent_mapping(self, tag, mapping, flow_style=False):
""" Gives compact representation for the distribution_file section, while allowing the package
Expand Down
7 changes: 0 additions & 7 deletions src/rosdistro/freeze_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import subprocess
import sys
import threading
Expand Down Expand Up @@ -109,11 +107,6 @@ def _worker(work_queue):
source_repo.version = hash
break

if not isinstance(source_repo.version, str):
# On Python 2, explicitly encode back to string. Unnecessary on Python 3, because
# we're already a unicode string since subprocess.check_output returned a bytes.
source_repo.version = source_repo.version.encode()

work_queue.task_done()

except subprocess.CalledProcessError as e:
Expand Down
2 changes: 0 additions & 2 deletions src/rosdistro/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import gzip
try:
from cStringIO import StringIO
Expand Down
14 changes: 4 additions & 10 deletions src/rosdistro/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,9 @@

import socket
import time
try:
from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.error import URLError
except ImportError:
from urllib2 import urlopen
from urllib2 import HTTPError
from urllib2 import URLError
from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.error import URLError


def load_url(url, retry=2, retry_period=1, timeout=10, skip_decode=False):
Expand All @@ -59,9 +54,8 @@ def load_url(url, retry=2, retry_period=1, timeout=10, skip_decode=False):
raise URLError(str(e) + ' (%s)' % url)
except socket.timeout as e:
raise socket.timeout(str(e) + ' (%s)' % url)
# Python 2/3 Compatibility
contents = fh.read()
if isinstance(contents, str) or skip_decode:
if skip_decode:
return contents
else:
return contents.decode('utf-8')
9 changes: 2 additions & 7 deletions src/rosdistro/manifest_provider/bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,10 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

try:
from urllib.request import urlopen, Request
from urllib.error import URLError
except ImportError:
from urllib2 import urlopen, Request
from urllib2 import URLError

import base64
import os
from urllib.request import urlopen, Request
from urllib.error import URLError

from rosdistro import logger

Expand Down
18 changes: 2 additions & 16 deletions src/rosdistro/manifest_provider/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@

def sanitize_xml(xml_string):
""" Returns a version of the supplied XML string with comments and all whitespace stripped,
including runs of spaces internal to text nodes. The returned string will be encoded,
so str (Python 2) or bytes (Python 3).
including runs of spaces internal to text nodes. The returned value will be bytes.
"""
def _squash(node):
# remove comment nodes
Expand All @@ -58,22 +57,9 @@ def _squash(node):
if x.nodeType == minidom.Node.ELEMENT_NODE:
_squash(x)
return node
try:
# Python 2. The minidom module parses as ascii, so we have to pre-encode.
if isinstance(xml_string, unicode):
xml_string = xml_string.encode('utf-8')
except NameError:
# Python 3. Strings are native unicode.
pass

xml_node = _squash(minidom.parseString(xml_string))
try:
# Python 2. Encode the resultant XML as a str.
unicode
return xml_node.toxml('utf-8')
except NameError:
# Python 3. Return native bytes.
return xml_node.toxml()
return xml_node.toxml()


class CachedManifestProvider(object):
Expand Down
9 changes: 2 additions & 7 deletions src/rosdistro/manifest_provider/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,11 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

try:
from urllib.request import urlopen, Request
from urllib.error import URLError
except ImportError:
from urllib2 import urlopen, Request
from urllib2 import URLError

import base64
import json
import os
from urllib.request import urlopen, Request
from urllib.error import URLError

from catkin_pkg.package import parse_package_string

Expand Down
13 changes: 2 additions & 11 deletions src/rosdistro/manifest_provider/tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@
import tarfile
import tempfile
import urllib

try:
from urllib.request import urlopen, Request
except ImportError:
from urllib2 import urlopen, Request
from urllib.request import urlopen, Request

from catkin_pkg.package import InvalidPackage, parse_package_string
from catkin_pkg.packages import find_package_paths
Expand Down Expand Up @@ -70,12 +66,7 @@ def tar_manifest_provider(_dist_name, repo, pkg_name):
response = urlopen(request)
with tarfile.open(fileobj=io.BytesIO(response.read())) as tar:
package_xml = tar.extractfile(subdir + '/package.xml').read()

# Python2 returns strings, Python3 returns bytes-- support both
try:
return package_xml.decode('utf-8')
except AttributeError:
return package_xml
return package_xml.decode('utf-8')


def tar_source_manifest_provider(repo):
Expand Down
2 changes: 0 additions & 2 deletions src/rosdistro/release_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

from .release_file import ReleaseFile


Expand Down
2 changes: 0 additions & 2 deletions src/rosdistro/release_cache_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import gzip
import os
import re
Expand Down
10 changes: 2 additions & 8 deletions src/rosdistro/rosdistro.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
import tarfile
import tempfile
import threading
try:
from urllib.request import urlopen
from urllib.error import HTTPError
except ImportError:
from urllib2 import urlopen
from urllib2 import HTTPError
from urllib.request import urlopen
from urllib.error import HTTPError

import yaml

Expand Down Expand Up @@ -382,8 +378,6 @@ def retrieve_dependencies(package_xml):


def get_package_dependencies(package_xml):
if not os.path.abspath("/usr/lib/pymodules/python2.7") in sys.path:
sys.path.append("/usr/lib/pymodules/python2.7")
from catkin_pkg import package as catkin_pkg

pkg = catkin_pkg.parse_package_string(package_xml)
Expand Down
2 changes: 0 additions & 2 deletions src/rosdistro/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import difflib
import sys

Expand Down
2 changes: 0 additions & 2 deletions src/rosdistro/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

from .verify import _to_yaml, _yaml_header_lines


Expand Down
14 changes: 4 additions & 10 deletions test/test_manifest_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,9 @@ def test_git_source():
def mock_get_url_contents(req):
import re

# For python3, look for the 'str' type; for Python 2, the 'unicode' type
try:
text_type = unicode
except NameError:
text_type = str

# The urlopen() function from urllib or urllib2 takes either a string or a
# urllib.Request object in; determine the URL in either case.
if isinstance(req, text_type):
# The urlopen() function from urllib takes either a string or a
# urllib.request.Request object in; determine the URL in either case.
if isinstance(req, str):
haystack = req
else:
haystack = req.get_full_url()
Expand Down Expand Up @@ -124,7 +118,7 @@ def test_sanitize():
assert '<a>abc</a>' in sanitize_xml('<a>ab<!-- comment -->c</a>')
assert '<a><b/><c>ab c</c></a>' in sanitize_xml('<a><b> </b> <c> ab c </c></a>')

# This unicode check should be valid on both Python 2 and 3.
# This unicode check should be valid.
assert '<a>français</a>' in sanitize_xml('<a> français </a>')

# subsequent parse calls will collapse empty tags, therefore sanitize should do the same
Expand Down
Loading