All articles, tagged with “python”

Trac on Jython

Some days ago i start discussion about current status of Python frameworks on Jython (only Russian version available). In short summary — many people work on this problem and Django was first.

Now i want to talk about Trac on Jython status. Google don’t provide any useful results on query about Trac on Jython, therefore i start more detailed research.

If you try to run Trac 0.11.1 on Jython you need to run all Trac dependencies, which includes Setuptools (already worked fine on Jython) and Genshi. Main problem there is Genshi library which dependence on xml.parsers.expat which in python bindings to native expat library. This require writing JNI wrapper for expat library. Also Genshi use some low-level functions of CPyhton like: AST rewriting, and CPython bytecode generation. (Some discussions about Genshi on Jython).

Genshi also used in TurboGears 2 framework and some people work on Genshi porting to Jython. This work was started as GSoC 2008 project: Improving TurboGears2 support on Jython. You can read some status reports about progress: Weekly status report, TG2 (already partly working) on Jython — status update,

Use libapt for package quering

Second part of my work in SoC was implement support of DEB-based distribution in OVAL interpreter. After discussion with my mentor i choice libapt for interacting with Debian package system.

But i have a troubles with this library because it have poor documentation. I start use regression test, which i found in library source but some of them seems broken. After some attempt to find problem i move to apt-get source and use them to build my first package querying program. apt-get use dpkg cache file to obtain information about all available packages. Therefor source for querying packages from dpkg cache file seems like:


bool checkExist (string package) { bool exist = false;

    pkgCacheFile *Cache = new pkgCacheFile();

    OpTextProgress Prog(*_config);
    if (Cache->Open(Prog, true) == false) {
            cerr << "I need more priveleges." << endl;
    }

    pkgCache::PkgIterator Pkg = (*Cache)->FindPkg (package);

    if (strcmp (Pkg.Name(), package.c_str()) == 0 and (Pkg.CurrentVer()))
            exist = true;

    Cache->Close();

    return (exist);

}

I think this approach is good and compact but have one lack in size of querying cache. Because we query over all available packages, not only installed. And for my machine this number about 20000.

My mentor Javier propose another approach based on apt-sort source code. This approach use dpkg status file for querying information about installed packages. There another version of checkExist function:


bool DPKGCheckExist (string name) { FileFd Fd(StatusFile, StatusFile::ReadOnly); pkgTagFile Tags(&Fd);

    bool found = false;
    if (_error->PendingError() == true)
            return "false";

    // Parse.
    vector List;
    pkgTagSection Section;
    unsigned long Offset = Tags.Offset();

    while (Tags.Step(Section) == true && found == false)
    {
            PkgName Tmp;
            /* Fetch the name, auto-detecting if this is a source file or a package file */
            Tmp.Name = Section.FindS("Package");

            if (Tmp.Name.empty() == true)
                    //return _error->Error("Unknown package record!");
                    return "false";

            if ( stringcasecmp(Tmp.Name,name) == 0 )
            {
                    if (stringcasecmp(Tmp.Stat,"install ok installed") == 0 )
                    {
                            return true;
                    } else {
                            cout << "Package not fully installed" << endl;
                            return false;
                    }

                    found = true;
            }

            Tmp.Offset = Offset;
            Tmp.Length = Section.size();

            Offset = Tags.Offset();
    }

    if (found == false)
            return "ERROR";
    if (_error->PendingError() == true)
            return "ERROR";

}

This example contain more code, but also allow to extract more information about installed packages. And this fragments would move to other functions which used to extraction DPKG package information. Currently i use this example in my work.

If you would plan use of libapt in you project this two approach may help you get started in rigth way.

Python and XML

During last two weeks i work with Python and XML. This is my best time that i remember! I use my favorite tools, that i love for its universality, portability and expressiveness. But too long i have only theoretical experience. And now i forget many basic things. This is hard to recollect all this things and trick.

But everday i use it for practice things, and as my code is ugly yet, but i think after some time — i again start write good code.

And now — Let’s code continue.