Skip to content

Commit

Permalink
Update for python3
Browse files Browse the repository at this point in the history
Use six for compatibility between python2 and python3 and tox for
running tests. Biggest source of actual code change is around supporting
rich comparision methods in python3.
  • Loading branch information
wfscheper committed Jun 7, 2017
1 parent 6f97229 commit fb5d351
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 81 deletions.
1 change: 0 additions & 1 deletion pymaven/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
from .versioning import Version
from .versioning import VersionRange


__all__ = ["Artifact", "Version", "VersionRange"]
24 changes: 17 additions & 7 deletions pymaven/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@
in a maven repository
"""

import functools
import re
import sys

import six

from .errors import ArtifactParseError
from .versioning import VersionRange

if sys.version_info > (2,):
from .utils import cmp

MAVEN_COORDINATE_RE = re.compile(
r'(?P<group_id>[^:]+)'
Expand All @@ -34,6 +40,7 @@
)


@functools.total_ordering
class Artifact(object):
"""Represents an artifact within a maven repository."""

Expand Down Expand Up @@ -69,22 +76,18 @@ def __init__(self, coordinate):
def __cmp__(self, other):
if self is other:
return 0

if not isinstance(other, Artifact):
if isinstance(other, basestring):
if isinstance(other, six.string_types):
try:
return cmp(self, Artifact(other))
except ArtifactParseError:
pass
return 1

result = cmp(self.group_id, other.group_id)
if result == 0:
result = cmp(self.artifact_id, other.artifact_id)

if result == 0:
result = cmp(self.type, other.type)

if result == 0:
if self.classifier is None:
if other.classifier is not None:
Expand All @@ -94,13 +97,20 @@ def __cmp__(self, other):
result = -1
else:
result = cmp(self.classifier, other.classifier)

if result == 0:
result = cmp(self.version.version,
other.version.version)

return result

def __eq__(self, other):
return self.__cmp__(other) == 0

def __lt__(self, other):
return self.__cmp__(other) < 0

def __ne__(self, other):
return self.__cmp__(other) != 0

def __hash__(self):
return hash((self.group_id, self.artifact_id, self.version, self.type,
self.classifier))
Expand Down
16 changes: 8 additions & 8 deletions pymaven/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
#


try:
from xml.etree import cElementTree as ElementTree
except ImportError:
from xml.etree import ElementTree

from urlparse import urlparse
import getpass
import hashlib
import json
import logging
import os
import posixpath

from six.moves.urllib.parse import urlparse
import requests
import six

from . import utils
from .artifact import Artifact
Expand All @@ -37,6 +33,10 @@
from .pom import Pom
from .versioning import VersionRange

try:
from xml.etree import cElementTree as ElementTree
except ImportError:
from xml.etree import ElementTree

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(self, cacheDir=None):
if cacheDir is None:
cacheDir = "/tmp/%s-maven-cache" % getpass.getuser()
if not os.path.exists(cacheDir):
os.makedirs(cacheDir, mode=0700)
os.makedirs(cacheDir, mode=0o700)
self.cacheDir = cacheDir

def _gen_key(self, method, uri, query_params):
Expand Down Expand Up @@ -155,7 +155,7 @@ class MavenClient(object):
""" Client for talking to a maven repository
"""
def __init__(self, *urls):
if isinstance(urls, basestring):
if isinstance(urls, six.string_types):
urls = [urls]
self._repos = []
for url in urls:
Expand Down
12 changes: 6 additions & 6 deletions pymaven/pom.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import re

from lxml import etree
import six

from .artifact import Artifact
from .utils import memoize
from .versioning import VersionRange


POM_PARSER = etree.XMLParser(
recover=True,
remove_comments=True,
Expand Down Expand Up @@ -286,15 +286,15 @@ def dependencies(self):
((group, artifact, str(version)), True))

for key, value in itertools.chain(
self._find_import_deps().iteritems(),
self._find_deps().iteritems(),
self._find_relocations().iteritems()):
six.iteritems(self._find_import_deps()),
six.iteritems(self._find_deps()),
six.iteritems(self._find_relocations())):
dependencies.setdefault(key, set()).update(value)

for profile in self._find_profiles():
for key, value in itertools.chain(
self._find_deps(profile).iteritems(),
self._find_relocations(profile).iteritems()):
six.iteritems(self._find_deps(profile)),
six.iteritems(self._find_relocations(profile))):
dependencies.setdefault(key, set()).update(value)

return dependencies
Expand Down
21 changes: 20 additions & 1 deletion pymaven/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,28 @@


from functools import wraps
from urlparse import urlsplit, urlunsplit
import posixpath

from six.moves.urllib.parse import urlsplit
from six.moves.urllib.parse import urlunsplit


def cmp(x, y):
"""
Replacement for built-in funciton cmp that was removed in Python 3
Compare the two objects x and y and return an integer according to
the outcome. The return value is negative if x < y, zero if x == y
and strictly positive if x > y.
"""
if x is None and y is None:
return 0
elif x is None:
return -1
elif y is None:
return 1
return (x > y) - (x < y)


def memoize(name):
def wrap(func):
Expand Down
Loading

0 comments on commit fb5d351

Please sign in to comment.