Combining python and sqlite DB into a single, "executeable".
rogerb at rogerbinns.com
Thu Oct 8 00:00:58 CEST 2009
[Please do not email me *and* the list - it is highly annoying]
Tom Cumming wrote:
> Thanks!, but I already thought of your suggestion. I've already gotten
> the clear impression that the amount of work to implement this is more than the ROI.
It isn't anywhere near as hard or as much work as you think!
> Having said that, it might work if on exiting the app could re-zip itself.
See the atexit module.
> One _big_ problem with zip files or compiled python executables is
> moving the file
> from machine to machine. It wouldn't be platform independent anymore.
You are the one who mentioned executables which generally means platform
specific binaries (and in the vast majority of cases that platform is
Windows) :-) Being an executable means you don't have to care if Python is
If you want it platform independent then you are stuck with Python text
scripts. You can put one in a zip file and use PYTHONPATH or you can use
the approach I showed at the bottom of my original message.
> What I like best so far, is to have something like:
You pretty much outlined what I wrote at the bottom of my message.
> Convince the python parser not to go here...
You can't do that. The entire source file has to be valid Python. You can
get Python to not take action on parts by making it a multi-line string you
do nothing with (ie surround with """) or by prefixing each line with # to
make it a comment. I'd recommend a string at the end with base64 encoded
contents. This would be very few lines of code to maintain along with
tempfile.NamedTemporaryFile and atexit. My estimate is a total of 10-20
lines of code.
> Now the trick is to get the sqlite library to start looking for the
> sqlite database at, "offset".
You cannot use the standard SQLite API to do that. SQLite does have a way
of writing custom file access (known as VFS) that could do it and the APSW
SQLite Python wrapper provides that functionality. See
(Disclaimer: I am the author of APSW). However writing a VFS is *way* more
work than something simple to extract data from the end of a script to a
temporary file and put the data back at exit. And you'd have to ensure all
machines have both Python and APSW installed.
More information about the Python-list