Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Feb 7, 2018
0 parents commit d910648
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
libversion.egg-info
libversion/_libversion.so
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## 0.1.0

* Initial release, contains `version_compare`, flags and more
pythonic `Version` class with comparison operators
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2017-2018 Dmitry Marakasov <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Python bindings for libversion

[![Build Status](https://travis-ci.org/repology/py-libversion.svg?branch=master)](https://travis-ci.org/repology/py-libversion)

## Purpose

Provides a fast implementation of advanced generic version string
comparison algorithm.

See [libversion](https://github.com/repology/libversion) repository
for more details on the algorithm.

## Features

* Provides API similar to C library, `version_compare(a, b)` function
* Provides more pythonic `Version` class with overloaded comparison operators

## Requirements

* Python 3.4+
* [libversion](https://github.com/repology/libversion) 2.5.0+

## Example

```python
from libversion import Version

assert(Version("1.0") == Version("001.0.0"))

assert(Version("0.999") < Version("1.0alpha1"))
assert(Version("1.0alpha1") < Version("1.0alpha2"))
assert(Version("1.0alpha2") < Version("1.0beta1"))
assert(Version("1.0beta1") < Version("1.0pre1"))
assert(Version("1.0pre1") < Version("1.0rc1"))
assert(Version("1.0rc1") < Version("1.0"))
assert(Version("1.0") < Version("1.0patch1"))
```

## License

MIT license, copyright (c) 2018 Dmitry Marakasov <[email protected]>.
71 changes: 71 additions & 0 deletions _libversion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2017-2018 Dmitry Marakasov <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <Python.h>
#include <libversion/version.h>

static PyObject* version_compare(PyObject *self, PyObject *args) {
(void)self; // (unused)

const char *v1;
const char *v2;
int flags = 0;

if (!PyArg_ParseTuple(args, "ss|i", &v1, &v2, &flags))
return NULL;

return PyLong_FromLong(version_compare_flags(v1, v2, flags));
}

static PyMethodDef module_methods[] = {
{"version_compare", (PyCFunction)version_compare, METH_VARARGS, ""},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef module_definition = {
PyModuleDef_HEAD_INIT,
"_libversion",
NULL,
-1,
module_methods,
NULL,
NULL,
NULL,
NULL
};

PyMODINIT_FUNC PyInit__libversion(void) {
PyObject* m = PyModule_Create(&module_definition);

if (m == NULL)
return NULL;

PyModule_AddIntConstant(m, "P_IS_PATCH", VERSIONFLAG_P_IS_PATCH);
PyModule_AddIntConstant(m, "P_IS_PATCH_LEFT", VERSIONFLAG_P_IS_PATCH_LEFT);
PyModule_AddIntConstant(m, "P_IS_PATCH_RIGHT", VERSIONFLAG_P_IS_PATCH_RIGHT);

PyModule_AddIntConstant(m, "ANY_IS_PATCH", VERSIONFLAG_ANY_IS_PATCH);
PyModule_AddIntConstant(m, "ANY_IS_PATCH_LEFT", VERSIONFLAG_ANY_IS_PATCH_LEFT);
PyModule_AddIntConstant(m, "ANY_IS_PATCH_RIGHT", VERSIONFLAG_ANY_IS_PATCH_RIGHT);

return m;
}
102 changes: 102 additions & 0 deletions libversion/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright (c) 2017-2018 Dmitry Marakasov <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from libversion._libversion import version_compare, ANY_IS_PATCH, ANY_IS_PATCH_LEFT, ANY_IS_PATCH_RIGHT, P_IS_PATCH, P_IS_PATCH_LEFT, P_IS_PATCH_RIGHT


__version__ = '0.1.0'

__all__ = [
'version_compare',

'ANY_IS_PATCH',
'ANY_IS_PATCH_LEFT',
'ANY_IS_PATCH_RIGHT',
'P_IS_PATCH',
'P_IS_PATCH_LEFT',
'P_IS_PATCH_RIGHT',

'Version'
]


class Version:
P_IS_PATCH = 0x1
ANY_IS_PATCH = 0x2

def __init__(self, value, flags=0):
self.value = value
self.flags = flags

def _leftflags(self):
flags = 0
if self.flags & Version.P_IS_PATCH:
flags |= P_IS_PATCH_LEFT
if self.flags & Version.ANY_IS_PATCH:
flags |= ANY_IS_PATCH_LEFT
return flags

def _rightflags(self):
flags = 0
if self.flags & Version.P_IS_PATCH:
flags |= P_IS_PATCH_RIGHT
if self.flags & Version.ANY_IS_PATCH:
flags |= ANY_IS_PATCH_RIGHT
return flags

def _compare(self, other):
return version_compare(
self.value,
other.value,
self._leftflags() | other._rightflags()
)

def __str__(self):
return self.value

def __eq__(self, other):
if isinstance(other, Version):
return self._compare(other) == 0
return NotImplemented

def __ne__(self, other):
if isinstance(other, Version):
return self._compare(other) != 0
return NotImplemented

def __lt__(self, other):
if isinstance(other, Version):
return self._compare(other) < 0
return NotImplemented

def __le__(self, other):
if isinstance(other, Version):
return self._compare(other) <= 0
return NotImplemented

def __gt__(self, other):
if isinstance(other, Version):
return self._compare(other) > 0
return NotImplemented

def __ge__(self, other):
if isinstance(other, Version):
return self._compare(other) >= 0
return NotImplemented
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
50 changes: 50 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3

import subprocess
from setuptools import Extension, setup


def pkgconfig(package):
result = {}
for token in subprocess.check_output(['pkg-config', '--libs', '--cflags', package]).decode('utf-8').split():
if token.startswith('-I'):
result.setdefault('include_dirs', []).append(token[2:])
elif token.startswith('-L'):
result.setdefault('library_dirs', []).append(token[2:])
elif token.startswith('-l'):
result.setdefault('libraries', []).append(token[2:])
return result


setup(
name='libversion',
version='0.1.0',
description='Python bindings for libversion',
author='Dmitry Marakasov',
author_email='[email protected]',
url='https://github.com/repology/pylibversion',
license='MIT',
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Console',
'Environment :: Web Environment',
'License :: OSI Approved :: MIT License',
'Programming Language :: C',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Topic :: Software Development :: Version Control',
'Topic :: System :: Archiving :: Packaging',
'Topic :: System :: Software Distribution',
],
packages=['libversion'],
ext_modules=[
Extension(
'libversion._libversion',
sources=['_libversion.c'],
**pkgconfig('libversion')
)
],
test_suite='tests'
)
Empty file added tests/__init__.py
Empty file.
41 changes: 41 additions & 0 deletions tests/test_cversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2018 Dmitry Marakasov <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import unittest
from libversion import *


class TestLibVersion(unittest.TestCase):
def test_cversion_compare_no_flags(self):
self.assertEqual(version_compare("001.001", "1.1"), 0)
self.assertEqual(version_compare("1.0010", "1.01"), 1)
self.assertEqual(version_compare("1.0", "1.0a"), -1)

def test_cversion_compare_flag_p_is_patch(self):
self.assertEqual(version_compare("1.0p1", "1.0p1", P_IS_PATCH_RIGHT), -1)
self.assertEqual(version_compare("1.0p1", "1.0p1", P_IS_PATCH_LEFT), 1)

def test_cversion_compare_flag_any_is_patch(self):
self.assertEqual(version_compare("1.0a1", "1.0a1", ANY_IS_PATCH_RIGHT), -1)
self.assertEqual(version_compare("1.0a1", "1.0a1", ANY_IS_PATCH_LEFT), 1)


if __name__ == '__main__':
unittest.main()
49 changes: 49 additions & 0 deletions tests/test_pyversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright (c) 2018 Dmitry Marakasov <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import unittest
from libversion import Version


class TestLibVersion(unittest.TestCase):
def test_pyversion_compare_eq(self):
self.assertTrue(Version("") == Version(""))
self.assertTrue(Version("1") == Version("1"))
self.assertTrue(Version("001.001") == Version("1.1"))

def test_pyversion_compare_ne(self):
self.assertTrue(Version("1.1") != Version("1.2"))
self.assertTrue(Version("1.10") != Version("1.01"))

def test_pyversion_compare_lg(self):
self.assertTrue(Version("1.0") < Version("1.0a"))
self.assertTrue(Version("1.0010") > Version("1.01"))

def test_pyversion_compare_flag_p_is_patch(self):
self.assertTrue(Version("1.0p1") < Version("1.0p1", Version.P_IS_PATCH))
self.assertTrue(Version("1.0p1", Version.P_IS_PATCH) > Version("1.0p1"))

def test_pyversion_compare_flag_any_is_patch(self):
self.assertTrue(Version("1.0a1") < Version("1.0a1", Version.ANY_IS_PATCH))
self.assertTrue(Version("1.0a1", Version.ANY_IS_PATCH) > Version("1.0a1"))


if __name__ == '__main__':
unittest.main()

0 comments on commit d910648

Please sign in to comment.