diff --git a/lib/Makefile.dep b/lib/Makefile.dep deleted file mode 100644 index 2e2da0d..0000000 --- a/lib/Makefile.dep +++ /dev/null @@ -1,10 +0,0 @@ -assert.o: assert.c -britney-py.o: britney-py.c dpkg.h templates.h memory.h -checklib.o: checklib.c dpkg.h templates.h memory.h -dpkg.o: dpkg.c dpkg.h templates.h memory.h -freelist.o: freelist.c templates.h memory.h -memory2.o: memory2.c -memory3.o: memory3.c memory.h templates.h freelist.h -memory.o: memory.c memory.h templates.h freelist.h -dpkg-lib.o: dpkg-lib.cpp dpkg.h templates.h memory.h -Makefile.dep : Makefile assert.c britney-py.c checklib.c dpkg.c freelist.c memory2.c memory3.c memory.c dpkg.h freelist.h memory.h templates.h diff --git a/lib/britney-py.c b/lib/britney-py.c index aa0690f..87675e8 100644 --- a/lib/britney-py.c +++ b/lib/britney-py.c @@ -852,6 +852,168 @@ static PyObject *apt_versioncmp(PyObject *self, PyObject *args) { return Py_BuildValue("i", res); } +/************************************************************************** + * britney.buildSystem() -- build a fake package system, with the only purpose of + * calling the is_installable method on the packages. + ******************************************************/ + +static PyObject *build_system(PyObject *self, PyObject *args) { + int pos = 0; + char *arch; + PyObject *pkgs, *key, *value, *pyString; + + (void)self; /* unused */ + + if (!PyArg_ParseTuple(args, "sO", &arch, &pkgs) || + !PyDict_Check(pkgs)) return NULL; + + /* Fields and positions for the binary package: + # VERSION = 0 + # SECTION = 1 + # SOURCE = 2 + # SOURCEVER = 3 + # ARCHITECTURE = 4 + # PREDEPENDS = 5 + # DEPENDS = 6 + # CONFLICTS = 7 + # PROVIDES = 8 + # RDEPENDS = 9 + # RCONFLICTS = 10 + */ + + dpkg_packages *dpkg_pkgs = new_packages(arch); + + /* loop on the dictionary keys to build the packages */ + while (PyDict_Next(pkgs, &pos, &key, &value)) { + + /* initialize the new package */ + dpkg_package *pkg; + pkg = block_malloc(sizeof(dpkg_package)); + pkg->package = PyString_AsString(key); + pkg->priority = 0; + pkg->details = NULL; + pkg->depends[2] = NULL; + pkg->depends[3] = NULL; + + pyString = PyList_GetItem(value, 0); + if (pyString == NULL) continue; + pkg->version = PyString_AsString(pyString); + + pyString = PyList_GetItem(value, 2); + if (pyString == NULL) continue; + pkg->source = PyString_AsString(pyString); + + pyString = PyList_GetItem(value, 3); + if (pyString == NULL) continue; + pkg->source_ver = PyString_AsString(pyString); + + pyString = PyList_GetItem(value, 4); + if (pyString == NULL) continue; + pkg->arch_all = (!strcmp(PyString_AsString(pyString), "all") ? 1 : 0); + + pyString = PyList_GetItem(value, 5); + if (pyString == NULL) continue; + pkg->depends[0] = read_dep_andor(PyString_AsString(pyString)); + + pyString = PyList_GetItem(value, 6); + if (pyString == NULL) continue; + pkg->depends[1] = read_dep_andor(PyString_AsString(pyString)); + + pyString = PyList_GetItem(value, 7); + if (pyString == NULL) continue; + pkg->conflicts = read_dep_and(PyString_AsString(pyString)); + + pyString = PyList_GetItem(value, 8); + if (pyString == NULL) continue; + pkg->provides = read_packagenames(PyString_AsString(pyString)); + + add_package(dpkg_pkgs, pkg); + } + + dpkgpackages *res; + res = PyObject_NEW(dpkgpackages, &Packages_Type); + if (res == NULL) return NULL; + + res->pkgs = dpkg_pkgs; + res->freeme = FREE; + res->ref = NULL; + + return (PyObject *)res; +} + +static PyObject *remove_binary(PyObject *self, PyObject *args) { + char *pkg_name; + dpkgpackages *pkgs; + + (void)self; /* unused */ + + if (!PyArg_ParseTuple(args, "Os", &pkgs, &pkg_name)) + return NULL; + + dpkg_collected_package *cpkg = lookup_packagetbl(pkgs->pkgs->packages, pkg_name); + if (cpkg == NULL) return Py_BuildValue("i", 0); + + remove_package(pkgs->pkgs, cpkg); + return Py_BuildValue("i", 1); +} + +static PyObject *add_binary(PyObject *self, PyObject *args) { + char *pkg_name; + dpkgpackages *pkgs; + PyObject *value, *pyString; + + (void)self; /* unused */ + + if (!PyArg_ParseTuple(args, "OsO", &pkgs, &pkg_name, &value) || + !PyList_Check(value)) return NULL; + + /* initialize the new package */ + dpkg_package *pkg; + pkg = block_malloc(sizeof(dpkg_package)); + pkg->package = pkg_name; + pkg->priority = 0; + pkg->details = NULL; + pkg->depends[2] = NULL; + pkg->depends[3] = NULL; + + pyString = PyList_GetItem(value, 0); + if (pyString == NULL) return NULL; + pkg->version = PyString_AsString(pyString); + + pyString = PyList_GetItem(value, 2); + if (pyString == NULL) return NULL; + pkg->source = PyString_AsString(pyString); + + pyString = PyList_GetItem(value, 3); + if (pyString == NULL) return NULL; + pkg->source_ver = PyString_AsString(pyString); + + pyString = PyList_GetItem(value, 4); + if (pyString == NULL) return NULL; + pkg->arch_all = (!strcmp(PyString_AsString(pyString), "all") ? 1 : 0); + + pyString = PyList_GetItem(value, 5); + if (pyString == NULL) return NULL; + pkg->depends[0] = read_dep_andor(PyString_AsString(pyString)); + + pyString = PyList_GetItem(value, 6); + if (pyString == NULL) return NULL; + pkg->depends[1] = read_dep_andor(PyString_AsString(pyString)); + + pyString = PyList_GetItem(value, 7); + if (pyString == NULL) return NULL; + pkg->conflicts = read_dep_and(PyString_AsString(pyString)); + + pyString = PyList_GetItem(value, 8); + if (pyString == NULL) return NULL; + pkg->provides = read_packagenames(PyString_AsString(pyString)); + + add_package(pkgs->pkgs, pkg); + + return Py_BuildValue("i", 1); +} + + /************************************************************************** * module initialisation ***********************/ @@ -862,6 +1024,10 @@ static PyMethodDef britneymethods[] = { { "versioncmp", apt_versioncmp, METH_VARARGS, NULL }, + { "buildSystem", build_system, METH_VARARGS, NULL }, + { "removeBinary", remove_binary, METH_VARARGS, NULL }, + { "addBinary", add_binary, METH_VARARGS, NULL }, + { NULL, NULL, 0, NULL } }; diff --git a/lib/dpkg.c b/lib/dpkg.c index 396e34d..0a2a0ac 100644 --- a/lib/dpkg.c +++ b/lib/dpkg.c @@ -19,9 +19,9 @@ static dpkg_package *read_package( FILE *f ); static collpackagelist **get_matching_low(collpackagelist **addto, dpkg_packages *pkgs, dependency *dep, int line); static collpackagelist *get_matching(dpkg_packages *pkgs, deplist *depopts, int line); -static deplist *read_dep_and(char *buf); -static deplistlist *read_dep_andor(char *buf); -static ownedpackagenamelist *read_packagenames(char *buf); +deplist *read_dep_and(char *buf); +deplistlist *read_dep_andor(char *buf); +ownedpackagenamelist *read_packagenames(char *buf); static dpkg_sources *read_sources_file(char *filename, int n_arches); static dpkg_source *new_source(dpkg_sources *owner); static dpkg_source *read_source(FILE *f, dpkg_sources *owner); @@ -33,8 +33,8 @@ static void remove_virtualpackage(virtualpkgtbl *vpkgs, char *pkgname, dpkg_collected_package *cpkg); static char *read_packagename(char **buf, char *end); static char *read_until_char(char **buf, char *end); -static void add_package(dpkg_packages *pkgs, dpkg_package *pkg); -static void remove_package(dpkg_packages *pkgs, dpkg_collected_package *pkg); +void add_package(dpkg_packages *pkgs, dpkg_package *pkg); +void remove_package(dpkg_packages *pkgs, dpkg_collected_package *pkg); static dpkg_source_note *copy_source_note(dpkg_source_note *srcn); #if 0 @@ -483,7 +483,7 @@ dpkg_packages *new_packages(char *arch) { return result; } -static void add_package(dpkg_packages *pkgs, dpkg_package *pkg) +void add_package(dpkg_packages *pkgs, dpkg_package *pkg) { ownedpackagenamelist *v; dpkg_collected_package *cpkg; @@ -502,7 +502,7 @@ static void add_package(dpkg_packages *pkgs, dpkg_package *pkg) } } -static void remove_package(dpkg_packages *pkgs, dpkg_collected_package *cpkg) { +void remove_package(dpkg_packages *pkgs, dpkg_collected_package *cpkg) { ownedpackagenamelist *v; packagenamelist *aff; dpkg_collected_package *p; @@ -626,7 +626,7 @@ static void add_virtualpackage(virtualpkgtbl *vpkgs, char *package, * Parsing Helper Functions */ -static ownedpackagenamelist *read_packagenames(char *buf) { +ownedpackagenamelist *read_packagenames(char *buf) { ownedpackagenamelist *result = NULL; ownedpackagenamelist **addto = &result; @@ -688,7 +688,7 @@ static char *read_packagename(char **buf, char *end) { return read_until_char(buf, end); } -static deplist *read_dep_and(char *buf) { +deplist *read_dep_and(char *buf) { return read_deplist(&buf, ',', '\0'); } @@ -723,7 +723,7 @@ static deplist *read_deplist(char **buf, char sep, char end) { return result; } -static deplistlist *read_dep_andor(char *buf) { +deplistlist *read_dep_andor(char *buf) { deplistlist *result = NULL; deplistlist **addto = &result; diff --git a/lib/dpkg.h b/lib/dpkg.h index b07d7bd..93a7d08 100644 --- a/lib/dpkg.h +++ b/lib/dpkg.h @@ -204,4 +204,11 @@ void commit_changes(dpkg_sources_note *srcsn); int versioncmp(char *left, char *right); int cmpversions(char *left, int op, char *right); +void add_package(dpkg_packages *pkgs, dpkg_package *pkg); +void remove_package(dpkg_packages *pkgs, dpkg_collected_package *pkg); +deplist *read_dep_and(char *buf); +deplistlist *read_dep_andor(char *buf); +ownedpackagenamelist *read_packagenames(char *buf); +dpkg_packages *new_packages(char *arch); + #endif diff --git a/lib/example.py b/lib/example.py new file mode 100755 index 0000000..48026a1 --- /dev/null +++ b/lib/example.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +import sys +import britney + +# VERSION = 0 +# SECTION = 1 +# SOURCE = 2 +# SOURCEVER = 3 +# ARCHITECTURE = 4 +# PREDEPENDS = 5 +# DEPENDS = 6 +# CONFLICTS = 7 +# PROVIDES = 8 +# RDEPENDS = 9 +# RCONFLICTS = 10 + +packages = {'phpldapadmin': ['1.0', 'web', 'phpldapadmin', '1.0', 'all', '', 'apache2 (>= 2.0)', '', '', [], []], + 'apache2': ['2.0', 'web', 'apache2', '2.0', 'i386', '', '', 'phpldapadmin (<= 1.0~)', '', [], []], + } + +system = britney.buildSystem('i386', packages) +print system.is_installable('phpldapadmin'), system.packages +britney.removeBinary(system, 'apache2') +print system.is_installable('phpldapadmin'), system.packages +britney.addBinary(system, 'apache2', ['2.0', 'web', 'apache2', '2.0', 'i386', '', '', 'phpldapadmin (<= 1.0~)', '', [], []]) +print system.is_installable('phpldapadmin'), system.packages