Skip to content

Commit

Permalink
Remove the use of py3compat and six modules
Browse files Browse the repository at this point in the history
- remove use of six
- remove py3compat (some functions moved to misc)
- minor cleanup
  • Loading branch information
itziakos committed May 2, 2024
1 parent 3ad7274 commit 74bfb50
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 277 deletions.
54 changes: 22 additions & 32 deletions okonomiyaki/file_formats/_egg_info.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import io
import posixpath
import re

import six
import zipfile2

from attr import attr, attributes
Expand All @@ -14,8 +14,7 @@
from okonomiyaki.platforms import (
EPDPlatform, PlatformABI, PythonABI, PythonImplementation)
from okonomiyaki.utils import (
compute_sha256, decode_if_needed, encode_if_needed, parse_assignments)
from okonomiyaki.utils.py3compat import StringIO, string_types
compute_sha256, decode_if_needed, parse_assignments)
from okonomiyaki.versions import EnpkgVersion, MetadataVersion
from .legacy import (
_guess_abi_tag, _guess_platform_abi, _guess_platform_tag, _guess_python_tag)
Expand Down Expand Up @@ -119,7 +118,7 @@ def split_egg_name(s):


def parse_rawspec(spec_string):
spec = parse_assignments(StringIO(spec_string.replace('\r', '')))
spec = parse_assignments(io.StringIO(spec_string.replace('\r', '')))

metadata_version_string = spec.get(_TAG_METADATA_VERSION)
if metadata_version_string is not None:
Expand All @@ -144,7 +143,7 @@ def parse_rawspec(spec_string):
for k, v in res.items():
# Some values are not string-like, so filter on the type that needs
# conversion
if isinstance(v, six.binary_type):
if isinstance(v, bytes):
res[k] = decode_if_needed(v)

res[_TAG_PACKAGES] = [decode_if_needed(v) for v in res[_TAG_PACKAGES]]
Expand Down Expand Up @@ -182,7 +181,7 @@ def text_attr(**kw):
for k in ("validator", ):
if k in kw:
raise ValueError("Cannot pass '{0}' argument".format(k))
return attr(validator=instance_of(six.text_type), **kw)
return attr(validator=instance_of(str), **kw)


def text_or_none_attr(**kw):
Expand All @@ -192,10 +191,9 @@ def text_or_none_attr(**kw):
for k in ("validator", ):
if k in kw:
raise ValueError("Cannot pass '{0}' argument".format(k))
return attr(validator=optional(instance_of(six.text_type)), **kw)
return attr(validator=optional(instance_of(str)), **kw)


@six.python_2_unicode_compatible
@attributes(frozen=True)
class Requirement(object):
"""
Expand Down Expand Up @@ -449,7 +447,7 @@ def _from_data(cls, data, epd_platform):
@classmethod
def from_egg(cls, path_or_file):
sha256 = None
if isinstance(path_or_file, string_types):
if isinstance(path_or_file, str):
if (
may_be_in_platform_blacklist(path_or_file)
or may_be_in_python_tag_blacklist(path_or_file)
Expand Down Expand Up @@ -481,7 +479,7 @@ def _create_spec_depend(zp):
)
return cls._from_data(data, epd_platform)

if isinstance(path_or_file, string_types):
if isinstance(path_or_file, str):
with zipfile2.ZipFile(path_or_file) as zp:
return _create_spec_depend(zp)
else:
Expand Down Expand Up @@ -576,20 +574,12 @@ def to_string(self):
template = _METADATA_TEMPLATES.get(self.metadata_version, None)
data = self._to_dict()

if six.PY2:
# Hack to avoid the 'u' prefix to appear in the spec/depend entries
for k, v in data.items():
data[k] = encode_if_needed(v)

# This is just to ensure the exact same string as the produced by the
# legacy buildsystem
if len(self.packages) == 0:
data[_TAG_PACKAGES] = "[]"
else:
if six.PY2:
packages = [decode_if_needed(p) for p in self.packages]
else:
packages = self.packages
packages = self.packages
data[_TAG_PACKAGES] = (
u"[\n{0}\n]".format(
"\n".join(" '{0}',".format(p) for p in packages)
Expand Down Expand Up @@ -712,7 +702,7 @@ def from_egg(cls, path_or_file, strict=True):
errors, at the risk of data loss.
"""
sha256 = None
if isinstance(path_or_file, string_types):
if isinstance(path_or_file, str):
if cls._may_be_in_blacklist(path_or_file):
sha256 = compute_sha256(path_or_file)
else:
Expand Down Expand Up @@ -772,7 +762,7 @@ def _compute_all_metadata(fp):

return summary, pkg_info_string, spec_depend

if isinstance(path_or_file, string_types):
if isinstance(path_or_file, str):
with zipfile2.ZipFile(path_or_file) as zp:
summary, pkg_info_string, spec_depend = _compute_all_metadata(zp)
else:
Expand Down Expand Up @@ -866,12 +856,12 @@ def __init__(self, raw_name, version, platform, python, abi_tag,
self.platform = platform
""" The platform, as a Platform instance."""

if isinstance(python, string_types):
if isinstance(python, str):
python = PythonImplementation.from_string(python)
self.python = python
""" The python implementation."""

if abi_tag is not None and isinstance(abi_tag, six.string_types):
if abi_tag is not None and isinstance(abi_tag, str):
abi_tag = PythonABI(abi_tag)

self.abi = abi_tag
Expand All @@ -880,7 +870,7 @@ def __init__(self, raw_name, version, platform, python, abi_tag,

if (
platform_abi is not None
and isinstance(platform_abi, six.string_types)):
and isinstance(platform_abi, str)):
platform_abi = PlatformABI(platform_abi)
self.platform_abi = platform_abi

Expand Down Expand Up @@ -950,7 +940,7 @@ def name(self):

@property
def pkg_info(self):
if isinstance(self._pkg_info, six.string_types):
if isinstance(self._pkg_info, str):
self._pkg_info = PackageInfo.from_string(self._pkg_info)
return self._pkg_info

Expand Down Expand Up @@ -995,7 +985,7 @@ def spec_depend_string(self):

@property
def upstream_version(self):
return six.text_type(self.version.upstream)
return str(self.version.upstream)

@property
def _python(self):
Expand All @@ -1020,8 +1010,8 @@ def _spec_depend(self):
"abi_tag": self.abi_tag,
"platform_tag": self.platform_tag,
"platform_abi": self.platform_abi_tag,
"packages": [six.text_type(p) for p in self.runtime_dependencies],
"metadata_version": six.text_type(self.metadata_version),
"packages": [str(p) for p in self.runtime_dependencies],
"metadata_version": str(self.metadata_version),
}
return LegacySpecDepend._from_data(args, self.platform)

Expand Down Expand Up @@ -1049,19 +1039,19 @@ def to_json_dict(self):
if self.platform is None:
epd_platform = None
else:
epd_platform = six.text_type(self.platform)
epd_platform = str(self.platform)

return {
_JSON_METADATA_VERSION: six.text_type(self.metadata_version),
_JSON_METADATA_VERSION: str(self.metadata_version),
_JSON__RAW_NAME: self._raw_name,
_JSON_VERSION: six.text_type(self.version),
_JSON_VERSION: str(self.version),
_JSON_EPD_PLATFORM: epd_platform,
_JSON_PYTHON_TAG: self.python_tag,
_JSON_ABI_TAG: self.abi_tag,
_JSON_PLATFORM_TAG: self.platform_tag,
_JSON_PLATFORM_ABI_TAG: self.platform_abi_tag,
_JSON_RUNTIME_DEPENDENCIES: [
six.text_type(p) for p in self.runtime_dependencies],
str(p) for p in self.runtime_dependencies],
_JSON_SUMMARY: self.summary}

# Protocol implementations
Expand Down
17 changes: 9 additions & 8 deletions okonomiyaki/file_formats/_package_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
We support 1.0, 1.1, 1.2 and 2.0.
"""
import contextlib
import io
import os
import os.path
import warnings

import zipfile2

from okonomiyaki.utils import py3compat, compute_sha256
from okonomiyaki.utils import compute_sha256
from okonomiyaki.errors import OkonomiyakiError

from ._blacklist import EGG_PKG_INFO_BLACK_LIST, may_be_in_pkg_info_blacklist
from ._wheel_info import WheelInfo

Expand Down Expand Up @@ -97,7 +98,7 @@ def from_path(cls, path, strict=True):

@classmethod
def from_wheel(cls, path_or_file, strict=True):
if isinstance(path_or_file, py3compat.string_types):
if isinstance(path_or_file, str):
wheel_info = WheelInfo.from_path(path_or_file)

with zipfile2.ZipFile(path_or_file) as fp:
Expand All @@ -124,7 +125,7 @@ def from_egg(cls, path_or_file, strict=True):
understood as a zipfile-like object.
"""
sha256 = None
if isinstance(path_or_file, py3compat.string_types):
if isinstance(path_or_file, str):
if may_be_in_pkg_info_blacklist(path_or_file):
sha256 = compute_sha256(path_or_file)
else:
Expand All @@ -134,7 +135,7 @@ def from_egg(cls, path_or_file, strict=True):

@classmethod
def _from_egg(cls, path_or_file, sha256, strict=True):
if isinstance(path_or_file, py3compat.string_types):
if isinstance(path_or_file, str):
with zipfile2.ZipFile(path_or_file) as fp:
data = _read_pkg_info(fp)
else:
Expand All @@ -149,9 +150,9 @@ def _from_egg(cls, path_or_file, sha256, strict=True):

@classmethod
def from_string(cls, s):
if not isinstance(s, py3compat.text_type):
if not isinstance(s, str):
raise ValueError("Expected text value, got {0!r}".format(type(s)))
fp = py3compat.StringIO(s)
fp = io.StringIO(s)
msg = _parse(fp)

kw = {}
Expand Down Expand Up @@ -248,7 +249,7 @@ def __init__(self, metadata_version, name, version, platforms=None,
self.provides_extra = provides_extra or ()

def to_string(self, metadata_version_info=MAX_SUPPORTED_VERSION):
s = py3compat.StringIO()
s = io.StringIO()
self._write_field(s, 'Metadata-Version', self.metadata_version)
self._write_field(s, 'Name', self.name)
self._write_field(s, 'Version', self.version)
Expand Down
19 changes: 6 additions & 13 deletions okonomiyaki/file_formats/setuptools_egg.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import os.path
import re
import sys
try:
import sysconfig
except ImportError: # Python 2.6 support
sysconfig = None
import sysconfig
import warnings

from okonomiyaki.errors import OkonomiyakiError
from okonomiyaki.platforms import PythonImplementation
from okonomiyaki.utils import py3compat
from ._egg_info import _guess_python_tag
from ._package_info import PackageInfo

Expand Down Expand Up @@ -90,14 +86,11 @@ def _guess_abi_from_python(python):


def _guess_abi_from_running_python():
if sysconfig is None:
try:
soabi = sysconfig.get_config_var('SOABI')
except IOError as e: # pip issue #1074
warnings.warn("{0}".format(e), RuntimeWarning)
soabi = None
else:
try:
soabi = sysconfig.get_config_var('SOABI')
except IOError as e: # pip issue #1074
warnings.warn("{0}".format(e), RuntimeWarning)
soabi = None

if soabi and soabi.startswith('cpython-'):
return 'cp' + soabi.split('-', 2)[1]
Expand Down Expand Up @@ -169,7 +162,7 @@ def __init__(self, name, version, platform, python, abi_tag, pkg_info):
self.version = version

self.platform = platform
if isinstance(python, py3compat.string_types):
if isinstance(python, str):
python = PythonImplementation.from_string(python)
self.python = python
self.abi_tag = abi_tag
Expand Down
17 changes: 1 addition & 16 deletions okonomiyaki/file_formats/tests/test_egg_file_format.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
# coding=utf-8
import os
import os.path
import shutil
import stat
import sys
import tempfile
import textwrap
import unittest
import zipfile2

if sys.version_info < (2, 7): # noqa
import unittest2 as unittest
else:
import unittest

import os.path as op

from okonomiyaki.platforms import EPDPlatform
from okonomiyaki.utils import compute_md5
from okonomiyaki.utils import py3compat
from okonomiyaki.versions import EnpkgVersion

from ..egg import EggBuilder, EggRewriter
from .._egg_info import Dependencies, EggMetadata, LegacySpecDepend
from .._package_info import PackageInfo

from .common import PIP_PKG_INFO, TRAITS_SETUPTOOLS_EGG


Expand Down Expand Up @@ -336,12 +327,6 @@ def setUp(self):
def tearDown(self):
shutil.rmtree(self.prefix)

def assertCountEqual(self, first, second, msg=None):
if py3compat.PY2:
return self.assertItemsEqual(first, second, msg)
else:
return unittest.TestCase.assertCountEqual(self, first, second, msg)

def assertSameArchive(self, first, second, arcname):
with zipfile2.ZipFile(first) as first_fp:
with zipfile2.ZipFile(second) as second_fp:
Expand Down
7 changes: 2 additions & 5 deletions okonomiyaki/platforms/_platform.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from __future__ import absolute_import

import enum
import platform
import sys
import subprocess

import enum
import six
from attr import attr, attributes
from attr.validators import instance_of

from okonomiyaki.errors import OkonomiyakiError
from okonomiyaki.versions import SemanticVersion

from ._arch import Arch, X86


Expand Down Expand Up @@ -57,7 +55,6 @@ class NameKind(enum.Enum):
}


@six.python_2_unicode_compatible
@attributes(repr=False, frozen=True)
class Platform(object):
"""
Expand All @@ -80,7 +77,7 @@ class Platform(object):
share the same kind, 'debian'.
"""

release = attr(validator=instance_of(six.string_types))
release = attr(validator=instance_of(str))
"""
The release string. May be an empty string
"""
Expand Down
4 changes: 1 addition & 3 deletions okonomiyaki/platforms/abi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import six

from attr import attributes, attr
from attr.validators import instance_of

Expand All @@ -15,7 +13,7 @@

@attributes
class PlatformABI(object):
pep425_tag = attr(validator=instance_of(six.text_type))
pep425_tag = attr(validator=instance_of(str))

@staticmethod
def pep425_tag_string(abi):
Expand Down
Loading

0 comments on commit 74bfb50

Please sign in to comment.