]> git.ipfire.org Git - pakfire.git/commitdiff
Unify util.py.
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Aug 2011 11:39:57 +0000 (13:39 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Aug 2011 11:39:57 +0000 (13:39 +0200)
pakfire/packages/base.py
pakfire/packages/file.py
pakfire/packages/packager.py
pakfire/packages/util.py [deleted file]
pakfire/util.py
po/POTFILES.in
po/pakfire.pot
src/_pakfiremodule.c
src/util.c [new file with mode: 0644]
src/util.h [new file with mode: 0644]

index 9394f2a935687f90ff464ae3dc4589a864be91ce..1767ad3293bc0d13d03bd1d11b84d0253bfc2051 100644 (file)
@@ -5,12 +5,9 @@ import logging
 import os
 import xml.sax.saxutils
 
-import util
-
+import pakfire.util as util
 from pakfire.i18n import _
 
-from pakfire.util import make_progress
-
 class Package(object):
        def __init__(self, pakfire, repo=None):
                self.pakfire = pakfire
@@ -27,7 +24,8 @@ class Package(object):
                if not self.name == other.name:
                        return cmp(self.name, other.name)
 
-               ret = util.version_compare(self.version_tuple, other.version_tuple)
+               ret = util.version_compare(self.pakfire.pool,
+                       self.friendly_version, other.friendly_version)
 
                # XXX this is to move packages that have been built a while ago and
                # do not have all the meta information that they won't be evaluated
@@ -216,14 +214,6 @@ class Package(object):
 
                return int(epoch)
 
-       @property
-       def version_tuple(self):
-               """
-                       Returns a tuple like (epoch, version, release) that can
-                       be used to compare versions of packages.
-               """
-               return (self.epoch, self.version, self.release)
-
        @property
        def arch(self):
                raise NotImplementedError
index a07cab99d1f95b28bb82c7ae015f91be9e857187..81f072221536d8dbefc16786c38b1c76891ed70c 100644 (file)
@@ -7,8 +7,6 @@ import tarfile
 import tempfile
 import xattr
 
-import util
-
 import pakfire.util as util
 import pakfire.compress as compress
 from pakfire.errors import FileError
index f268e87ff40d46dc19bb5a8b83a985d3177df140..eecd17c95b75ed8741b11d8a0c3c40af84ae4078 100644 (file)
@@ -15,8 +15,8 @@ import xattr
 import zlib
 
 import pakfire.compress
+import pakfire.util as util
 from pakfire.util import rm
-import util
 
 from pakfire.constants import *
 from pakfire.i18n import _
diff --git a/pakfire/packages/util.py b/pakfire/packages/util.py
deleted file mode 100644 (file)
index 310bb1a..0000000
+++ /dev/null
@@ -1,181 +0,0 @@
-#!/usr/bin/python
-
-from __future__ import division
-
-import hashlib
-import re
-
-from pakfire.constants import *
-
-def version_compare_epoch(e1, e2):
-       # If either e1 or e2 is None, we cannot say anything
-       if None in (e1, e2):
-               return 0
-
-       return cmp(e1, e2)
-
-def version_compare_version(v1, v2):
-       return cmp(v1, v2)
-
-def version_compare_release(r1, r2):
-       # If either e1 or e2 is None, we cannot say anything
-       if None in (r1, r2):
-               return 0
-
-       d1, d2 = None, None
-
-       if "." in r1:
-               r1, d1 = r1.split(".", 1)
-
-       if "." in r2:
-               r2, d2 = r2.split(".", 1)
-
-       # Compare the distribution tag at first.
-       if d1 and d2:
-               ret = cmp(d1, d2)
-
-               if not ret == 0:
-                       return ret
-
-       r1 = int(r1)
-       r2 = int(r2)
-
-       return cmp(r1, r2)
-
-def version_compare((e1, v1, r1), (e2, v2, r2)):
-       ret = version_compare_epoch(e1, e2)
-       if not ret == 0:
-               return ret
-
-       ret = version_compare_version(v1, v2)
-       if not ret == 0:
-               return ret
-
-       return version_compare_release(r1, r2)
-
-def text_wrap(s, length=65):
-       t = []
-       s = s.split()
-
-       l = []
-       for word in s:
-               l.append(word)
-
-               if len(" ".join(l)) >= length:
-                       t.append(l)
-                       l = []
-
-       if l:
-               t.append(l)
-
-       return [" ".join(l) for l in t]
-
-def format_size(s):
-       sign = 1
-
-       # If s is negative, we save the sign and run the calculation with the
-       # absolute value of s.
-       if s < 0:
-               sign = -1
-               s = -1 * s
-
-       units = (" ", "k", "M", "G", "T")
-       unit = 0
-
-       while s >= 1024 and unit < len(units):
-               s /= 1024
-               unit += 1
-
-       return "%d %s" % (int(s) * sign, units[unit])
-
-def format_time(s):
-       return "%02d:%02d" % (s // 60, s % 60)
-
-def format_speed(s):
-       return "%sB/s" % format_size(s)
-
-def calc_hash1(filename=None, data=None):
-       h = hashlib.sha1()
-
-       if filename:
-               f = open(filename)
-               buf = f.read(BUFFER_SIZE)
-               while buf:
-                       h.update(buf)
-                       buf = f.read(BUFFER_SIZE)
-
-               f.close()
-
-       elif data:
-               h.update(data)
-
-       return h.hexdigest()
-
-def parse_pkg_expr(s):
-       # Possible formats:
-       #   gcc=4.0.0
-       #   gcc=4.0.0-1
-       #   gcc=4.0.0-1.ip3
-       #   gcc=0:4.0.0-1
-       #   gcc=0:4.0.0-1.ip3
-       #   gcc>=...
-       #   gcc>...
-       #   gcc<...
-       #   gcc<=...
-
-       (name, exp, epoch, version, release) = (None, None, None, None, None)
-
-       m = re.match(r"([A-Za-z0-9\-\+]+)(=|\<|\>|\>=|\<=)([0-9]+\:)?([0-9A-Za-z\.]+)-?([0-9]+\.?[a-z0-9]+|[0-9]+)?", s)
-
-       if m:
-               (name, exp, epoch, version, release) = m.groups()
-
-               # Remove : from epoch and convert to int
-               if epoch:
-                       epoch = epoch.replace(":", "")
-                       epoch = int(epoch)
-
-       return (exp, name, epoch, version, release)
-
-def test_parse_pkg_expr():
-       strings = (
-               "gcc=4.0.0",
-               "gcc=4.0.0-1",
-               "gcc=4.0.0-1.ip3",
-               "gcc=0:4.0.0-1",
-               "gcc=0:4.0.0-1.ip3",
-               "gcc>=4.0.0-1",
-               "gcc>4.0.0-1",
-               "gcc<4.0.0-1",
-               "gcc<=4.0.0-1",
-               "openssl-devel>=1.0.0d-2",
-       )
-
-       for s in strings:
-               print s, parse_pkg_expr(s)
-
-def parse_virtual_expr(s):
-       # pkgconfig(bla)=1.2.3
-
-       (type, name, exp, version) = (None, None, None, None)
-
-       m = re.match(r"^([A-Za-z0-9]+)\(([A-Za-z0-9\.\-\+:]+)\)?(=|\<|\>|\>=|\<=)?([A-Za-z0-9\.\-]+)?", s)
-
-       if m:
-               (type, name, exp, version) = m.groups()
-
-       return (type, exp, name, version)
-
-def test_parse_virtual_expr():
-       strings = (
-               "pkgconfig(libxml-2.0)",
-               "pkgconfig(libxml-2.0)=1.2.3",
-               "pkgconfig(libxml-2.0)>=1.2.3",
-       )
-
-       for s in strings:
-               print s, parse_virtual_expr(s)
-
-if __name__ == "__main__":
-       test_parse_pkg_expr()
-       test_parse_virtual_expr()
index 8dcdacd14274f72f72b35becc5335dbd891c828d..774b495210d8e28f3ba6f0db42adc4533a67ccb3 100644 (file)
@@ -1,6 +1,9 @@
 #!/usr/bin/python
 
+from __future__ import division
+
 import fcntl
+import hashlib
 import os
 import progressbar
 import random
@@ -11,10 +14,12 @@ import sys
 import termios
 import time
 
-from errors import Error
-from packages.util import calc_hash1, format_size, format_speed, format_time
+from constants import *
 from i18n import _
 
+# Import binary version of version_compare
+from _pakfire import version_compare
+
 def cli_is_interactive():
        """
                Say weather a shell is interactive or not.
@@ -136,3 +141,61 @@ def terminal_size():
                        cr = (25, 80)
 
        return int(cr[1]), int(cr[0])
+
+def format_size(s):
+       sign = 1
+
+       # If s is negative, we save the sign and run the calculation with the
+       # absolute value of s.
+       if s < 0:
+               sign = -1
+               s = -1 * s
+
+       units = (" ", "k", "M", "G", "T")
+       unit = 0
+
+       while s >= 1024 and unit < len(units):
+               s /= 1024
+               unit += 1
+
+       return "%d %s" % (int(s) * sign, units[unit])
+
+def format_time(s):
+       return "%02d:%02d" % (s // 60, s % 60)
+
+def format_speed(s):
+       return "%sB/s" % format_size(s)
+
+def calc_hash1(filename=None, data=None):
+       h = hashlib.sha1()
+
+       if filename:
+               f = open(filename)
+               buf = f.read(BUFFER_SIZE)
+               while buf:
+                       h.update(buf)
+                       buf = f.read(BUFFER_SIZE)
+
+               f.close()
+
+       elif data:
+               h.update(data)
+
+       return h.hexdigest()
+
+def text_wrap(s, length=65):
+       t = []
+       s = s.split()
+
+       l = []
+       for word in s:
+               l.append(word)
+
+               if len(" ".join(l)) >= length:
+                       t.append(l)
+                       l = []
+
+       if l:
+               t.append(l)
+
+       return [" ".join(l) for l in t]
index 06d0db1767e127188d5451d120847f99da01d375..f4f16067190c3f9517a90419f57c1243899d3cb4 100644 (file)
@@ -22,7 +22,6 @@ pakfire/packages/make.py
 pakfire/packages/packager.py
 pakfire/packages/solv.py
 pakfire/packages/source.py
-pakfire/packages/util.py
 pakfire/packages/virtual.py
 pakfire/repository/base.py
 pakfire/repository/cache.py
@@ -49,3 +48,4 @@ src/solver.c
 src/step.c
 src/test.py
 src/transaction.c
+src/util.c
index 19b3fcf111b6686261827049a239784c4896c045..536ed04d5459d5cc8111c0c488816bf7abd9de42 100644 (file)
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2011-07-31 19:32+0000\n"
+"POT-Creation-Date: 2011-08-03 13:36+0200\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -288,71 +288,71 @@ msgstr ""
 msgid "One or more dependencies could not been resolved."
 msgstr ""
 
-#: ../pakfire/packages/base.py:72
+#: ../pakfire/packages/base.py:70
 msgid "Name"
 msgstr ""
 
-#: ../pakfire/packages/base.py:73 ../pakfire/transaction.py:164
+#: ../pakfire/packages/base.py:71 ../pakfire/transaction.py:164
 msgid "Arch"
 msgstr ""
 
-#: ../pakfire/packages/base.py:74 ../pakfire/transaction.py:164
+#: ../pakfire/packages/base.py:72 ../pakfire/transaction.py:164
 msgid "Version"
 msgstr ""
 
-#: ../pakfire/packages/base.py:75
+#: ../pakfire/packages/base.py:73
 msgid "Release"
 msgstr ""
 
-#: ../pakfire/packages/base.py:76 ../pakfire/transaction.py:165
+#: ../pakfire/packages/base.py:74 ../pakfire/transaction.py:165
 msgid "Size"
 msgstr ""
 
-#: ../pakfire/packages/base.py:77
+#: ../pakfire/packages/base.py:75
 msgid "Repo"
 msgstr ""
 
-#: ../pakfire/packages/base.py:78
+#: ../pakfire/packages/base.py:76
 msgid "Summary"
 msgstr ""
 
-#: ../pakfire/packages/base.py:79
+#: ../pakfire/packages/base.py:77
 msgid "Groups"
 msgstr ""
 
-#: ../pakfire/packages/base.py:80
+#: ../pakfire/packages/base.py:78
 msgid "URL"
 msgstr ""
 
-#: ../pakfire/packages/base.py:81
+#: ../pakfire/packages/base.py:79
 msgid "License"
 msgstr ""
 
-#: ../pakfire/packages/base.py:84
+#: ../pakfire/packages/base.py:82
 msgid "Description"
 msgstr ""
 
-#: ../pakfire/packages/base.py:90
+#: ../pakfire/packages/base.py:88
 msgid "UUID"
 msgstr ""
 
-#: ../pakfire/packages/base.py:91
+#: ../pakfire/packages/base.py:89
 msgid "Build ID"
 msgstr ""
 
-#: ../pakfire/packages/base.py:92
+#: ../pakfire/packages/base.py:90
 msgid "Build date"
 msgstr ""
 
-#: ../pakfire/packages/base.py:93
+#: ../pakfire/packages/base.py:91
 msgid "Build host"
 msgstr ""
 
-#: ../pakfire/packages/base.py:95
+#: ../pakfire/packages/base.py:93
 msgid "Provides"
 msgstr ""
 
-#: ../pakfire/packages/base.py:100
+#: ../pakfire/packages/base.py:98
 msgid "Requires"
 msgstr ""
 
@@ -461,7 +461,7 @@ msgstr ""
 msgid "Running transaction"
 msgstr ""
 
-#: ../pakfire/util.py:39
+#: ../pakfire/util.py:44
 #, python-format
 msgid "%s [y/N]"
 msgstr ""
index 610096ba0974734e7b9fcf50b137104654111775..151ef77dc3616c31f8dd4c8b06db90298aff3718 100644 (file)
 #include "solver.h"
 #include "step.h"
 #include "transaction.h"
+#include "util.h"
 
 static PyMethodDef pakfireModuleMethods[] = {
+       {"version_compare", (PyCFunction)version_compare, METH_VARARGS, NULL},
        { NULL, NULL, 0, NULL }
 };
 
diff --git a/src/util.c b/src/util.c
new file mode 100644 (file)
index 0000000..6e82093
--- /dev/null
@@ -0,0 +1,18 @@
+
+#include <Python.h>
+
+#include "util.h"
+
+PyObject *version_compare(PyObject *self, PyObject *args) {
+       Pool *pool;
+       const char *evr1, *evr2;
+
+       if (!PyArg_ParseTuple(args, "Oss", &pool, &evr1, &evr2)) {
+               /* XXX raise exception */
+               return NULL;
+       }
+
+       int ret = pool_evrcmp_str(pool, evr1, evr2, EVRCMP_COMPARE);
+
+       return Py_BuildValue("i", ret);
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644 (file)
index 0000000..e43bc48
--- /dev/null
@@ -0,0 +1,11 @@
+
+#ifndef PAKFIRE_UTIL_H
+#define PAKFIRE_UTIL_H
+
+#include <Python.h>
+
+#include <satsolver/evr.h>
+
+extern PyObject *version_compare(PyObject *self, PyObject *args);
+
+#endif