diff options
author | Detlev Casanova <detlev.casanova@gmail.com> | 2010-08-01 16:47:01 +0200 |
---|---|---|
committer | Detlev Casanova <detlev.casanova@gmail.com> | 2010-08-01 16:47:01 +0200 |
commit | 0e76a31c866aef80cf7c5f04e54bef3e3150a761 (patch) | |
tree | 61b6b134175dbd162e2d6a6d7c6bc80f3279322c | |
parent | Fix get_all_cpv_use, implement get_properties (diff) | |
download | c-portage-0e76a31c866aef80cf7c5f04e54bef3e3150a761.tar.gz c-portage-0e76a31c866aef80cf7c5f04e54bef3e3150a761.tar.bz2 c-portage-0e76a31c866aef80cf7c5f04e54bef3e3150a761.zip |
Implement get_hard_masked and get_path,
Fix right method calls,
Add a contains function to StringList,
Make the test application look like emerge :
./run-test porthole
Gives the best version to install with it's use flags.
-rwxr-xr-x | run-test.sh | 2 | ||||
-rw-r--r-- | src/dataconnect.c | 95 | ||||
-rw-r--r-- | src/dataconnect.h | 4 | ||||
-rw-r--r-- | src/flag.c | 2 | ||||
-rw-r--r-- | src/stringlist.c | 15 | ||||
-rw-r--r-- | src/stringlist.h | 1 | ||||
-rw-r--r-- | src/tester.c | 178 |
7 files changed, 166 insertions, 131 deletions
diff --git a/run-test.sh b/run-test.sh index fc98e60..5b23a71 100755 --- a/run-test.sh +++ b/run-test.sh @@ -6,4 +6,4 @@ echo Compile tester: gcc src/tester.c -lportage -g -W -Wall -Isrc/ -L.libs/ --std=c99 -o tester echo Run : -LD_LIBRARY_PATH=.libs/ ./tester +LD_LIBRARY_PATH=.libs/ ./tester $1 diff --git a/src/dataconnect.c b/src/dataconnect.c index dc9d2ee..6c2764d 100644 --- a/src/dataconnect.c +++ b/src/dataconnect.c @@ -53,9 +53,32 @@ StringList* portageGetVersions(const char *pkg, int include_masked) return ret; } -//int portageGetHardMasked(const char*) -//{ -//} +int portageGetHardMasked(const char* pkg, StringList** no_check, StringList** check) +{ + PyObject *obj = executeFunction("portage.api.data_connect", "get_hard_masked", "(z)", pkg); + if (!obj || !PySequence_Check(obj)) + { + if (obj) + { + Py_DECREF(obj); + } + return 0; + } + + PyObject *pynocheck = PySequence_GetItem(obj, 0); + assert(pynocheck); + PyObject *pycheck = PySequence_GetItem(obj, 1); + assert(pycheck); + + *no_check = listToCList(pynocheck); + *check = listToCList(pycheck); + + Py_DECREF(pynocheck); + Py_DECREF(pycheck); + Py_DECREF(obj); + + return 1; +} StringList* portageGetInstalledFiles(const char *pkg) { @@ -81,16 +104,10 @@ char* portageBestVersion(StringList *pkgs) { assert(pkgs); PyObject *pylist = cListToPyList(pkgs); - PyObject *obj = executeFunction("portage.api.data_connect", "best_version", "(O)", pylist); + PyObject *obj = executeFunction("portage.api.data_connect", "best", "(O)", pylist); Py_DECREF(pylist); - if (!obj || !PyString_Check(obj)) - { - if (obj) - { - Py_DECREF(obj); - } + if (!obj) return NULL; - } char *ret = pyStringToString(obj); @@ -124,14 +141,8 @@ char* portageGetDepEbuild(const char *pkg) { assert(pkg); PyObject *obj = executeFunction("portage.api.data_connect", "get_dep_ebuild", "(z)", pkg); - if (!obj || !PyUnicode_Check(obj)) - { - if (obj) - { - Py_DECREF(obj); - } + if (!obj) return NULL; - } char *ret = pyStringToString(obj); @@ -166,14 +177,8 @@ char* portageGetMaskingReason(const char *pkg) { assert(pkg); PyObject *obj = executeFunction("portage.api.data_connect", "get_masking_reason", "(z)", pkg); - if (!obj || !PyUnicode_Check(obj)) - { - if (obj) - { - Py_DECREF(obj); - } + if (!obj) return NULL; - } char *ret = pyStringToString(obj); @@ -207,14 +212,8 @@ char* portageGetPackageSizeString(const char *pkg) { assert(pkg); PyObject *obj = executeFunction("portage.api.data_connect", "get_size", "(z)", pkg); - if (!obj || !PyString_Check(obj)) - { - if (obj) - { - Py_DECREF(obj); - } + if (!obj) return NULL; - } char *ret = pyStringToString(obj); @@ -262,14 +261,8 @@ char* portageGetOverlay(const char *pkg) { assert(pkg); PyObject *obj = executeFunction("portage.api.data_connect", "get_overlay", "(z)", pkg); - if (!obj || !PyUnicode_Check(obj)) - { - if (obj) - { - Py_DECREF(obj); - } + if (!obj) return NULL; - } char *ret = pyStringToString(obj); @@ -305,14 +298,8 @@ char* portageGetOverlayNameFromPkg(const char *pkg) Py_INCREF(none); PyObject *obj = executeFunction("portage.api.data_connect", "get_overlay_name", "(Oz)", none, pkg); Py_DECREF(none); - if (!obj || !PyUnicode_Check(obj)) - { - if (obj) - { - Py_DECREF(obj); - } + if (!obj) return NULL; - } char *ret = pyStringToString(obj); @@ -321,9 +308,19 @@ char* portageGetOverlayNameFromPkg(const char *pkg) return ret; } -//char* portageGetPath(const char*, int) -//{ -//} +char* portageGetPath(const char* pkg, int vardb) +{ + assert(pkg); + PyObject *obj = executeFunction("portage.api.data_connect", "get_path", "(zI)", pkg, vardb); + if (!obj) + return NULL; + + char *ret = pyStringToString(obj); + + Py_DECREF(obj); + + return ret; +} StringList* portageGetResolvedPkgs() diff --git a/src/dataconnect.h b/src/dataconnect.h index 05d006a..167da12 100644 --- a/src/dataconnect.h +++ b/src/dataconnect.h @@ -5,7 +5,7 @@ #include "packageproperties.h" StringList* portageGetVersions(const char*, int); -//int portageGetHardMasked(const char*); +int portageGetHardMasked(const char*, StringList**, StringList**); StringList* portageGetInstalledFiles(const char*); char* portageBestVersion(StringList*); @@ -22,7 +22,7 @@ int portageIsOverlay(const char*); char* portageGetOverlay(const char*); char* portageGetOverlayNameFromPath(const char*); char* portageGetOverlayNameFromPkg(const char*); -//char* portageGetPath(const char*, int); +char* portageGetPath(const char*, int); StringList* portageGetResolvedPkgs(); StringList* portageGetUnresolvedPkgs(); @@ -161,7 +161,7 @@ int portageGetAllCpvUse(const char* pkg, StringList** use, StringList** use_expa StringList* portageGetFlags(const char* pkg) { assert(pkg); - PyObject *obj = executeFunction("portage.api.flag", "get_installed_use", "(z)", pkg); + PyObject *obj = executeFunction("portage.api.flag", "get_flags", "(z)", pkg); if (!obj) return NULL; diff --git a/src/stringlist.c b/src/stringlist.c index ad1acf3..8f37dac 100644 --- a/src/stringlist.c +++ b/src/stringlist.c @@ -28,7 +28,7 @@ int stringListInsertAt(StringList *l, unsigned int pos, char *str) if(!l || !l->list || l->count < pos) return 0; - l->list[pos] = str; + l->list[pos] = strdup(str); return 1; } @@ -77,7 +77,7 @@ StringList* listToCList(PyObject* list) if (PyUnicode_Check(elem)) { PyObject *tmp = PyUnicode_AsUTF8String(elem); - val = strdup(PyString_AsString(tmp)); + val = PyString_AsString(tmp); Py_DECREF(tmp); } else if (PyString_Check(elem)) @@ -125,6 +125,17 @@ void stringListPrint(StringList* list) } } +int stringListContains(StringList* list, char* str) +{ + for(unsigned int i = 0; i < list->count; i++) + { + if (0 == strcmp(list->list[i], str)) + return 1; + } + + return 0; +} + /* * Frees a string list and it's data */ diff --git a/src/stringlist.h b/src/stringlist.h index 60800f0..82373b6 100644 --- a/src/stringlist.h +++ b/src/stringlist.h @@ -10,6 +10,7 @@ unsigned int stringListCount(StringList*); int stringListInsertAt(StringList*, unsigned int, char*); char* stringListGetAt(StringList*, unsigned int); void stringListPrint(StringList*); +int stringListContains(StringList*, char*); void stringListFree(StringList*); #endif diff --git a/src/tester.c b/src/tester.c index 45ce798..d58adec 100644 --- a/src/tester.c +++ b/src/tester.c @@ -1,90 +1,116 @@ #include <stdlib.h> #include <stdio.h> #include <assert.h> +#include <string.h> #include "portage.h" -int main(int argc, char *argv[]) +void printUseList(char* pkg) { - argc = argc; - argv = argv; - int ret = 0; - portageInit(); - - PortageSettings *ps = portageSettingsCreate(); - - StringList *world = portageSettingsGetWorld(ps); - stringListPrint(world); - printf("\n"); - stringListFree(world); + StringList *uses = portageGetFlags(pkg); + StringList *iUses = portageGetInstalledUse(pkg); + + int count = 0; + for (unsigned i = 0; i < stringListCount(uses); i++) + { + if (!stringListContains(iUses, stringListGetAt(uses, i))) + count++; + } - char *ak = portageSettingsAcceptKeywords(ps); - printf("ACCEPT_KEYWORDS=%s\n", ak); - free(ak); - - char *pd = portageSettingsPortdir(ps); - printf("PORTDIR=%s\n", pd); - free(pd); - - char *pdo = portageSettingsPortdirOverlay(ps); - printf("PORTDIR_OVERLAY=%s\n", pdo); - free(pdo); - - StringList *useflags = portageSettingsSystemUseFlags(ps); - stringListPrint(useflags); - printf("\n"); - stringListFree(useflags); - - portageSettingsFree(ps); - - char *bp = portageGetBestEbuild("kde-base/kdelibs"); - printf("kdelibs best package = %s\n", bp); + StringList *tmp_list = stringListCreate(count + stringListCount(iUses)); + + for (unsigned i = 0; i < stringListCount(iUses); i++) + { + stringListInsertAt(tmp_list, i, stringListGetAt(iUses, i)); + } + + int k = stringListCount(iUses); + for (unsigned i = 0; i < stringListCount(uses); i++) + { + if (!stringListContains(iUses, stringListGetAt(uses, i))) + stringListInsertAt(tmp_list, k++, stringListGetAt(uses, i)); + } + + for (unsigned i = 0; i < stringListCount(tmp_list); i++) + { + printf("%c%s%s", i < stringListCount(iUses) ? '+' : '-', stringListGetAt(tmp_list, i), i == stringListCount(tmp_list) - 1 ? "" : " "); + } + + stringListFree(uses); + stringListFree(iUses); + stringListFree(tmp_list); +} - char *oname = portageGetOverlay(bp); - printf("kdelibs overlay name = %s\n", oname); - free(oname); - free(bp); +int main(int argc, char *argv[]) +{ + int ret = 0; + if (argc < 2) + { + printf("Please provide 1 package name.\n"); + return -1; + } - /*StringList *resolved = portageGetResolvedPkgs(); - stringListPrint(resolved); - printf("\n"); - stringListFree(resolved); - - StringList *unresolved = portageGetUnresolvedPkgs(); - stringListPrint(unresolved); - printf("\n"); - stringListFree(unresolved); - - StringList *installed = portageGetInstalledList(); - printf("Installed (%d) :\n", stringListCount(installed)); - stringListPrint(installed); - printf("\n"); - stringListFree(installed); - - StringList *all = portageGetAllNodes(); - printf("All Nodes (%d) :\n", stringListCount(all)); - stringListPrint(all); - printf("\n"); - stringListFree(all);*/ - - StringList *use = 1, *use_expand_hidden = 0, *usemasked = 0, *useforced = 0; - ret = portageGetAllCpvUse("kde-base/kdelibs-4.4.5", &use, &use_expand_hidden, &usemasked, &useforced); - if (!ret) - printf("ret was false, troubles ahead.\n"); - stringListPrint(use); - printf("\n"); - stringListFree(use); - stringListPrint(use_expand_hidden); - printf("\n"); - stringListFree(use_expand_hidden); - stringListPrint(usemasked); - printf("\n"); - stringListFree(usemasked); - stringListPrint(useforced); - printf("\n"); - stringListFree(useforced); + portageInit(); - + char *pkg = argv[1]; + StringList *versions = portageGetVersions(pkg, 0); + if (!versions) + { + printf("All masked?\n"); + return -1; + } + + if (0 == stringListCount(versions)) + { + stringListFree(versions); + versions = portageGetVersions(pkg, 1); + if (!versions) + { + printf("Bad ebuild ?\n"); + return -1; + } + + if (0 == stringListCount(versions)) + { + printf("No such ebuild '%s'\n", pkg); + stringListFree(versions); + return -1; + } + + char *best_pkg = portageBestVersion(versions); + assert(best_pkg); + + stringListFree(versions); + + StringList *m_status = portageGetMaskingStatus(best_pkg); + + for(unsigned int i = 0; i < stringListCount(m_status); i++) + { + if (strcmp(stringListGetAt(m_status, i), "") != 0) + { + printf("Package %s is masked :\n", best_pkg); + char *m_reason = portageGetMaskingReason(best_pkg); + printf("%s\n", m_reason); + free(m_reason); + } + } + + stringListFree(m_status); + free(best_pkg); + return -1; + } + + char *best_pkg = portageBestVersion(versions); + assert(best_pkg); + + char *size = portageGetPackageSizeString(best_pkg); + + printf("%s USE=\"", best_pkg); + printUseList(best_pkg); + printf("\" %s\n", size); + + free(size); + free(best_pkg); portageFinalize(); |