Import from database

Steve Holden steve at
Tue Jan 25 16:08:33 EST 2005

Kartic wrote:

> Steve,
> Hmmm... Yes, I see what you are saying. Could you post your create
> table statement so that I can create a db and play around with
> dbimport?
> Thanks,
> --Kartic
Here it is, plus also the loader program I used to suck in the standard 
library (in case it's that that's faulty):

CREATE TABLE `module` (
   `modName` varchar(25) NOT NULL default '',
   `modPackage` tinyint(1) NOT NULL default '0',
   `modCompileTime` timestamp(14) NOT NULL,
   `modCode` longtext,
   PRIMARY KEY  (`modName`)

#### WARNING: email client may wrap some lines ...
# Establish standard library in database
import db
import os
import glob
import sys
import marshal

conn = db.conn()
curs = conn.cursor()

if sys.argv[1] == "-d":
     curs.execute("delete from module")
     print "All existing modules cleared"
     del sys.argv[1]

def importpy(path, modname, package):
     print "Importing", path, modname
     c = compile(file(path).read(), "db:%s" % modname, "exec")
     curs.execute("""delete from module where modName = %s""", (modname,))
     curs.execute("""insert into module (modName, modCode, modPackage, 
                             values (%s, %s, %s, now())""", (modname, 
marshal.dumps(c), package))
     print "Added", modname

def importall(path, modlist):
     for f in glob.glob("*"):
         if os.path.isdir(f):
             fn = os.path.join(path, f, "")
             if os.path.exists(fn):
                 ml = modlist + [f]
                 importpy(fn, ".".join(ml), 1)
                 importall(os.path.join(path, f), ml)
         elif f.endswith('.py') and '.' not in f[:-3] and f != 
             importpy(os.path.join(path, f), ".".join(modlist+[f[:-3]]), 0)

if __name__ == "__main__":
     for path in sys.argv[1:]:
         importall(path, [])


More information about the Python-list mailing list