Heya

I found myself in need of being able to run scripts that access the db
(using my apps model.py and .cfg's) from outside turbogears (i.e. from
the command line). I found I could do

tg-admin shell < script.py

to get the job done, but this starts up an interactive interpreter,
which is a bit messy. I just want to execute the file in a tg-provided
environment.

So I wrote a new command, 'exec', that takes a file as an argument and
executes it in the DB-aware context. Code is bellow, it is based on
the Shell command.

It works the same as shell except for one thing. When I run the shell
command, and type 'import model', it knows where model is and imports
it fine. But when I use execfile to run a supplied file, I have to
import my app first - i.e. it cannot find my model.py

e.g.

with 'tg-amin shell'
>>> import model
>>>

with ' tg-admin exec test.py'
test.py:
import app
from app import model

Any ideas why?

code:

class Execute(CommandWithDB):
    """Convenient execution of offline DB scripts.
    This command attempts to locate your configuration file and model module
    so that it can import everything from your model and make it available
    to execute a supplied python script in"""

    def run(self):
        "Execute the file"
        self.find_config()

        # this doesn't seem to work for Execute the same as for Shell
        mod = get_model()
        if mod:
            locals = mod.__dict__
        else:
            locals = dict(__name__="tg-admin")

        try:
            execfile(sys.argv[1], globals(), locals)
        except IndexError:
            print "No file to execute"


--
wavy davy

"True religion confronts earth with heaven
and brings eternity to bear on time"
 - A. W. Tozer

Reply via email to