Re: portage from C (was Re: [gentoo-portage-dev] Hello! && portage UI)
sorry lines char tt; for (tt=0; tt<40; tt++) fputc(tt, inputf); /.../ fputc(EOF, inputf); are not needed at all and do nothing -- i was just testing a thought before i catched how those pipes and forks work in linux :) i have mostly done things in windows and c++ and WinApi so i dont catch everything immedietly yet :) 2006/3/16, tvali <[EMAIL PROTECTED]>: > So I was the last one :) > > My example is using python interpreter -- it's maybe simpler to use in > many cases, but theoretically slower. > > It is, anyway, not so advanced to import portage, but you can add that line ;) > > 2006/3/15, solar <[EMAIL PROTECTED]>: > > On Wed, 2006-03-15 at 21:42 +0100, Kevin F. Quinn (Gentoo) wrote: > > > On Wed, 15 Mar 2006 09:52:13 -0500 > > > solar <[EMAIL PROTECTED]> wrote: > > > > > > > On Wed, 2006-03-15 at 10:51 +0100, Paul de Vrieze wrote: > > > > > > > > [snip] > > > > > > > > > Actually, C++ being strongly related to C, can just use the C > > > > > python api's. As such it could directly interface with python, and > > > > > use the python portage api. > > > > > > > > If you could demonstrate an 'import portage' and a simple > > > > printf("PORTDIR=%s\n", PORTDIR); in C I'd be highly interested. > > > > > > Try attached :) Had a go 'coz I was curious. Compile with: > > > > > > gcc -o getportdir getportdir.c -lpython2.4 > > > > > > Does the equivalent of: > > > > > > #!/usr/bin/portage > > > import portage; > > > print portage.settings["PORTDIR"]; > > > > > > (more or less). Docs on the API itself (which comes with Python) are at > > > http://docs.python.org/api/api.html > > > > thanks Kevin, > > marienz also put 3 examples together to demonstrate it. > > > > /me feels like a kid in a candy store. > > > > thanks guys. > > > > > > -- > > solar <[EMAIL PROTECTED]> > > Gentoo Linux > > > > -- > > gentoo-portage-dev@gentoo.org mailing list > > > > > > > -- > tvali > (e-mail: "[EMAIL PROTECTED]"; msn: "[EMAIL PROTECTED]"; > icq: "317-492-912") > > Ühe eesti internetifirma lehel kohtasin tsitaati: > If you don't do it excellently, dont do it at all. Because if it's not > excellent, it won't be profitable or fun, and if you're not in > business for fun or profit, what the hell are you doing here? > Robert Townsend > > > -- tvali (e-mail: "[EMAIL PROTECTED]"; msn: "[EMAIL PROTECTED]"; icq: "317-492-912") Ühe eesti internetifirma lehel kohtasin tsitaati: If you don't do it excellently, dont do it at all. Because if it's not excellent, it won't be profitable or fun, and if you're not in business for fun or profit, what the hell are you doing here? Robert Townsend -- gentoo-portage-dev@gentoo.org mailing list
Re: portage from C (was Re: [gentoo-portage-dev] Hello! && portage UI)
So I was the last one :) My example is using python interpreter -- it's maybe simpler to use in many cases, but theoretically slower. It is, anyway, not so advanced to import portage, but you can add that line ;) 2006/3/15, solar <[EMAIL PROTECTED]>: > On Wed, 2006-03-15 at 21:42 +0100, Kevin F. Quinn (Gentoo) wrote: > > On Wed, 15 Mar 2006 09:52:13 -0500 > > solar <[EMAIL PROTECTED]> wrote: > > > > > On Wed, 2006-03-15 at 10:51 +0100, Paul de Vrieze wrote: > > > > > > [snip] > > > > > > > Actually, C++ being strongly related to C, can just use the C > > > > python api's. As such it could directly interface with python, and > > > > use the python portage api. > > > > > > If you could demonstrate an 'import portage' and a simple > > > printf("PORTDIR=%s\n", PORTDIR); in C I'd be highly interested. > > > > Try attached :) Had a go 'coz I was curious. Compile with: > > > > gcc -o getportdir getportdir.c -lpython2.4 > > > > Does the equivalent of: > > > > #!/usr/bin/portage > > import portage; > > print portage.settings["PORTDIR"]; > > > > (more or less). Docs on the API itself (which comes with Python) are at > > http://docs.python.org/api/api.html > > thanks Kevin, > marienz also put 3 examples together to demonstrate it. > > /me feels like a kid in a candy store. > > thanks guys. > > > -- > solar <[EMAIL PROTECTED]> > Gentoo Linux > > -- > gentoo-portage-dev@gentoo.org mailing list > > -- tvali (e-mail: "[EMAIL PROTECTED]"; msn: "[EMAIL PROTECTED]"; icq: "317-492-912") Ühe eesti internetifirma lehel kohtasin tsitaati: If you don't do it excellently, dont do it at all. Because if it's not excellent, it won't be profitable or fun, and if you're not in business for fun or profit, what the hell are you doing here? Robert Townsend #include #include #include #include #include void python(int input[2], int output[2]) { dup2(output[1], STDOUT_FILENO); dup2(input[0], STDIN_FILENO); close(output[0]); close(output[1]); close(input[0]); close(input[1]); char *const args[] = { "/usr/bin/python", NULL }; execv(args[0], args); _exit(EXIT_FAILURE); } int main(int argc, char *argv[]) { int input[2]; pipe(input); int output[2]; pipe(output); if (fork() == 0) python(input, output); FILE *inputf, *outputf; close(output[1]); close(input[0]); outputf = fdopen(output[0], "r"); inputf = fdopen(input[1], "w"); fputs("print 123\n", inputf); fclose(inputf); char tt; for (tt=0; tt<40; tt++) fputc(tt, inputf); int c; while ((c = fgetc(outputf)) != EOF) fputc(c, stdout); fputc(EOF, inputf); wait(NULL); return 0; }
Re: portage from C (was Re: [gentoo-portage-dev] Hello! && portage UI)
On Wed, 2006-03-15 at 21:42 +0100, Kevin F. Quinn (Gentoo) wrote: > On Wed, 15 Mar 2006 09:52:13 -0500 > solar <[EMAIL PROTECTED]> wrote: > > > On Wed, 2006-03-15 at 10:51 +0100, Paul de Vrieze wrote: > > > > [snip] > > > > > Actually, C++ being strongly related to C, can just use the C > > > python api's. As such it could directly interface with python, and > > > use the python portage api. > > > > If you could demonstrate an 'import portage' and a simple > > printf("PORTDIR=%s\n", PORTDIR); in C I'd be highly interested. > > Try attached :) Had a go 'coz I was curious. Compile with: > > gcc -o getportdir getportdir.c -lpython2.4 > > Does the equivalent of: > > #!/usr/bin/portage > import portage; > print portage.settings["PORTDIR"]; > > (more or less). Docs on the API itself (which comes with Python) are at > http://docs.python.org/api/api.html thanks Kevin, marienz also put 3 examples together to demonstrate it. /me feels like a kid in a candy store. thanks guys. -- solar <[EMAIL PROTECTED]> Gentoo Linux -- gentoo-portage-dev@gentoo.org mailing list
portage from C (was Re: [gentoo-portage-dev] Hello! && portage UI)
On Wed, 15 Mar 2006 09:52:13 -0500 solar <[EMAIL PROTECTED]> wrote: > On Wed, 2006-03-15 at 10:51 +0100, Paul de Vrieze wrote: > > [snip] > > > Actually, C++ being strongly related to C, can just use the C > > python api's. As such it could directly interface with python, and > > use the python portage api. > > If you could demonstrate an 'import portage' and a simple > printf("PORTDIR=%s\n", PORTDIR); in C I'd be highly interested. Try attached :) Had a go 'coz I was curious. Compile with: gcc -o getportdir getportdir.c -lpython2.4 Does the equivalent of: #!/usr/bin/portage import portage; print portage.settings["PORTDIR"]; (more or less). Docs on the API itself (which comes with Python) are at http://docs.python.org/api/api.html -- Kevin F. Quinn #include #include int main(int argc, char **argv) { PyObject *portage, *portage_const, *portdir, *portage_dict, *portage_const_dict, portage_db; PyObject *portage_settings, *portage_settings_dict; PyObject *key, *value; int pos; Py_Initialize(); portage = PyImport_ImportModule ("portage"); portage_dict = PyModule_GetDict (portage); portage_settings = PyDict_GetItemString (portage_dict, "settings"); // it's an instance of a class portdir = PyObject_GetItem (portage_settings, PyString_FromString("PORTDIR")); if (portdir == NULL) { fprintf(stderr, "Failed to find portdir\n"); } else { fprintf(stdout, "PORTDIR=\"%s\"\n", PyString_AsString(portdir)); fflush(stdout); } Py_Finalize(); } signature.asc Description: PGP signature