Import from database

Steve Holden steve at holdenweb.com
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`)
) TYPE=MyISAM;

#### 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, 
modCompileTime)
                             values (%s, %s, %s, now())""", (modname, 
marshal.dumps(c), package))
     print "Added", modname
     conn.commit()

def importall(path, modlist):
     os.chdir(path)
     for f in glob.glob("*"):
         if os.path.isdir(f):
             fn = os.path.join(path, f, "__init__.py")
             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 != 
"__init__.py":
             importpy(os.path.join(path, f), ".".join(modlist+[f[:-3]]), 0)

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

regards
  Steve




More information about the Python-list mailing list