diff --git a/.gitignore b/.gitignore index 71b79d3..b11bdd6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,9 @@ +*.egg-info *.pyc -.coverage -.venv -build -dist -extras -iptools.egg-info +/.coverage +/.tox/ +/.venv/ +/build/ +/dist/ +/extras/ setuptools-*.egg diff --git a/.travis.yml b/.travis.yml index eada3f2..70c1d3c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,22 @@ language: python python: - - "2.6" - "2.7" - - "3.2" - - "3.3" + - "3.4" + - "3.5" - "pypy" + - "pypy3" +sudo: false +matrix: + fast_finish: true + install: - - pip install . - - pip install -r tests/requirements.txt + - pip install wheel tox-travis + - python setup.py install bdist_wheel + - pip install ./dist/iptools-*.whl script: - - flake8 - - nosetests + - tox + - tox --installpkg ./dist/iptools-*.whl + notifications: email: - travis-ci+python-iptools@bd808.com diff --git a/iptools/__init__.py b/iptools/__init__.py index 608ea4b..181f845 100644 --- a/iptools/__init__.py +++ b/iptools/__init__.py @@ -22,13 +22,6 @@ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. -__version__ = '0.7.0-dev' - -__all__ = ( - 'IpRange', - 'IpRangeList', -) - # sniff for python2.x / python3k compatibility "fixes' try: @@ -51,10 +44,16 @@ def next(iterable): Sequence = object # end compatibility "fixes' - from . import ipv4 from . import ipv6 +__version__ = '0.7.0-dev' + +__all__ = ( + 'IpRange', + 'IpRangeList', +) + def _address2long(address): """ diff --git a/iptools/ipv4.py b/iptools/ipv4.py index c3d0127..3243482 100644 --- a/iptools/ipv4.py +++ b/iptools/ipv4.py @@ -23,6 +23,40 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. +import re + +# sniff for python2.x / python3k compatibility "fixes' +try: + basestring = basestring +except NameError: + # 'basestring' is undefined, must be python3k + basestring = str + +try: + bin = bin +except NameError: + # builtin bin function doesn't exist + def bin(x): + """ + From http://code.activestate.com/recipes/219300/#c7 + """ + if x < 0: + return '-' + bin(-x) + out = [] + if x == 0: + out.append('0') + while x > 0: + out.append('01'[x & 1]) + x >>= 1 + pass + try: + return '0b' + ''.join(reversed(out)) + except NameError: + out.reverse() + return '0b' + ''.join(out) + # end bin +# end compatibility "fixes' + __all__ = ( 'cidr2block', 'hex2ip', @@ -60,43 +94,6 @@ 'TEST_NET_3', ) - -import re - - -# sniff for python2.x / python3k compatibility "fixes' -try: - basestring = basestring -except NameError: - # 'basestring' is undefined, must be python3k - basestring = str - -try: - bin = bin -except NameError: - # builtin bin function doesn't exist - def bin(x): - """ - From http://code.activestate.com/recipes/219300/#c7 - """ - if x < 0: - return '-' + bin(-x) - out = [] - if x == 0: - out.append('0') - while x > 0: - out.append('01'[x & 1]) - x >>= 1 - pass - try: - return '0b' + ''.join(reversed(out)) - except NameError: - out.reverse() - return '0b' + ''.join(out) - # end bin -# end compatibility "fixes' - - #: Regex for validating an IPv4 address _DOTTED_QUAD_RE = re.compile(r'^(\d{1,3}\.){0,3}\d{1,3}$') @@ -285,6 +282,10 @@ def validate_netmask(s): True >>> validate_netmask('128.0.0.1') False + >>> validate_netmask('1.255.255.0') + False + >>> validate_netmask('0.255.255.0') + False :param s: String to validate as a dotted-quad notation netmask. @@ -293,7 +294,8 @@ def validate_netmask(s): :raises: TypeError """ if validate_ip(s): - mask = bin(ip2network(s))[2:] + # Convert to binary string, strip '0b' prefix, 0 pad to 32 bits + mask = bin(ip2network(s))[2:].zfill(32) # all left most bits must be 1, all right most must be 0 seen0 = False for c in mask: diff --git a/iptools/ipv6.py b/iptools/ipv6.py index 9fab832..97d00ee 100644 --- a/iptools/ipv6.py +++ b/iptools/ipv6.py @@ -23,6 +23,9 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. +import re +from . import ipv4 + __all__ = ( 'cidr2block', 'ip2long', @@ -52,11 +55,6 @@ 'UNSPECIFIED_ADDRESS', ) - -import re -from . import ipv4 - - #: Regex for validating an IPv6 in hex notation _HEX_RE = re.compile(r'^([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}$') diff --git a/setup.cfg b/setup.cfg index ec2d4e2..85d3e9c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,18 +1,19 @@ [nosetests] +verbosity=2 detailed-errors=1 with-coverage=1 with-doctest=1 cover-package=iptools cover-html=1 cover-html-dir=docs/_build/cover +cover-branches=1 [flake8] -ignore=F821,E402 count=1 show-pep8=1 show-source=1 statistics=1 -exclude=build,dist,docs,*.egg,*.egg-info +exclude=.tox,.venv,build,dist,docs,*.egg,*.egg-info [wheel] universal = 1 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..a99d698 --- /dev/null +++ b/tox.ini @@ -0,0 +1,8 @@ +[tox] +envlist = py27, py34, py35, pypy, pypy3 + +[testenv] +deps = -r{toxinidir}/tests/requirements.txt +commands = + flake8 + nosetests