From 102c5ee7def3e4267302ad2d060288e9a17cd0b8 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 3 Aug 2011 13:39:57 +0200 Subject: [PATCH] Unify util.py. --- pakfire/packages/base.py | 16 +--- pakfire/packages/file.py | 2 - pakfire/packages/packager.py | 2 +- pakfire/packages/util.py | 181 ----------------------------------- pakfire/util.py | 67 ++++++++++++- po/POTFILES.in | 2 +- po/pakfire.pot | 38 ++++---- src/_pakfiremodule.c | 2 + src/util.c | 18 ++++ src/util.h | 11 +++ 10 files changed, 120 insertions(+), 219 deletions(-) delete mode 100644 pakfire/packages/util.py create mode 100644 src/util.c create mode 100644 src/util.h diff --git a/pakfire/packages/base.py b/pakfire/packages/base.py index 9394f2a93..1767ad329 100644 --- a/pakfire/packages/base.py +++ b/pakfire/packages/base.py @@ -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 diff --git a/pakfire/packages/file.py b/pakfire/packages/file.py index a07cab99d..81f072221 100644 --- a/pakfire/packages/file.py +++ b/pakfire/packages/file.py @@ -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 diff --git a/pakfire/packages/packager.py b/pakfire/packages/packager.py index f268e87ff..eecd17c95 100644 --- a/pakfire/packages/packager.py +++ b/pakfire/packages/packager.py @@ -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 index 310bb1a03..000000000 --- a/pakfire/packages/util.py +++ /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() diff --git a/pakfire/util.py b/pakfire/util.py index 8dcdacd14..774b49521 100644 --- a/pakfire/util.py +++ b/pakfire/util.py @@ -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] diff --git a/po/POTFILES.in b/po/POTFILES.in index 06d0db176..f4f160671 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -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 diff --git a/po/pakfire.pot b/po/pakfire.pot index 19b3fcf11..536ed04d5 100644 --- a/po/pakfire.pot +++ b/po/pakfire.pot @@ -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 \n" "Language-Team: LANGUAGE \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 "" diff --git a/src/_pakfiremodule.c b/src/_pakfiremodule.c index 610096ba0..151ef77dc 100644 --- a/src/_pakfiremodule.c +++ b/src/_pakfiremodule.c @@ -12,8 +12,10 @@ #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 index 000000000..6e8209336 --- /dev/null +++ b/src/util.c @@ -0,0 +1,18 @@ + +#include + +#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 index 000000000..e43bc484a --- /dev/null +++ b/src/util.h @@ -0,0 +1,11 @@ + +#ifndef PAKFIRE_UTIL_H +#define PAKFIRE_UTIL_H + +#include + +#include + +extern PyObject *version_compare(PyObject *self, PyObject *args); + +#endif -- 2.39.5