at_produce.py :  » Development » Leo » Leo-4.7.1-final » leo » plugins » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Leo 
Leo » Leo 4.7.1 final » leo » plugins » at_produce.py
#@+leo-ver=4-thin
#@+node:ekr.20040915085351:@thin at_produce.py
#@<< docstring >>
#@+node:ekr.20050311110307:<< docstring >>
'''
Executes commands in nodes whose body text starts with @produce.
To use, put in the body text of a node::

    @produce javac -verbose Test.java

To execute, you goto Outline and look at Produce.  Choose Execute All Produce
or Execute Tree Produce.  The Tree does the current Tree, All does the whole
Outline.  Executing will fire javac, or whatever your using.  @produce functions
as a directive.  After executing, a log file/node is created at the top of the
Outline.  Any output, even error messages, should be there.

It executes in a hierarchal manner.  Nodes that come before that contain @produce
go first.

Im hoping that this orthogonal to @run nodes and anything like that.  Its not
intended as a replacement for make or Ant, but as a simple substitute when that
machinery is overkill.

WARNING: trying to execute a non-existent command will hang Leo.
'''
#@nonl
#@-node:ekr.20050311110307:<< docstring >>
#@nl

#@@language python
#@@tabwidth -4

from __future__ import generators# To make this plugin work with Python 2.2.

#@<< imports >>
#@+node:ekr.20040915085715:<< imports >>
import leo.core.leoGlobals as g
import leo.core.leoPlugins as leoPlugins

from leoNodes import *

Tk = g.importExtension('Tkinter',pluginName=__name__,verbose=True)

import os
import threading
import time
# import weakref
#@-node:ekr.20040915085715:<< imports >>
#@nl
__version__ = '.3'
#@<< version history >>
#@+node:ekr.20050311110629:<< version history >>
#@@killcolor

#@+at
# 
# .2 EKR:
#     - Move all docs into docstring.
#     - Created init function.
#     - Use keywords dict to get c.  Removed haveseen dict.
# 
# .3 EKR:
#     - Added from __future__ import generators to suppress warning in Python 
# 2.2.
#@-at
#@nonl
#@-node:ekr.20050311110629:<< version history >>
#@nl

pr = '@' + 'produce'

#@+others
#@+node:ekr.20050311110629.1:init
def init ():

    ok = Tk is not None

    if ok: # Ok for unit testing: adds menu and new directive.

        leoPlugins.registerHandler(('new','open2'),addMenu)
        g.globalDirectiveList.append('produce')
        g.plugin_signon(__name__)

    return ok
#@nonl
#@-node:ekr.20050311110629.1:init
#@+node:ekr.20040915085351.2:makeProduceList & allies
def makeProduceList( c, root = True ):

    pl = []
    if root:
        rvnode = c.rootVnode()
        stopnode = rvnode
    else:
        rvnode = c.currentVnode()
        stopnode = rvnode.next()

    for z in travel(rvnode,stopnode):
        body = z.b
        body = body.split( '\n' )
        body = filter(teststart, body)
        if body:
            map( lambda i : pl.append( i ), body )

    return pl
#@nonl
#@+node:ekr.20040915085351.3:teststart
def teststart( a ):
    return a.startswith( pr )
#@nonl
#@-node:ekr.20040915085351.3:teststart
#@+node:ekr.20040915085351.4:travel
def travel(vn,stopnode):

    while vn:
        yield vn
        vn = vn.threadNext()
        if vn == stopnode:
            vn = None
#@nonl
#@-node:ekr.20040915085351.4:travel
#@-node:ekr.20040915085351.2:makeProduceList & allies
#@+node:ekr.20040915085351.5:exeProduce
def exeProduce(  c, root = True ):

    pl = makeProduceList(c, root)
    # Define the callback with argument bound to p1.
    #@    @+others
    #@+node:ekr.20040915085351.6:runPL
    def runPL(pl=pl):
        f = open( 'produce.log', 'w+' )
        for z in pl:
            if z.startswith(pr):
                z = z.lstrip( pr )
                z = z.lstrip()
                f.write( 'produce: %s\n' % z )
                fi, fo, fe  = os.popen3( z )
                while 1:
                    txt = fo.read()
                    f.write( txt )
                    if txt == '': break
                while 1:
                    txt = fe.read()
                    f.write( txt )
                    if txt == '': break
                fi.close()
                fo.close()
                fe.close() 
                f.write('===============\n' )    
        f.seek( 0 )
        rv = c.rootVnode()
        nv = rv.insertAfter()
        c.setBodyString(nv,f.read() )
        c.setHeadString(nv,'produce.log from %s' % time.asctime() )
        f.close()
        os.remove( 'produce.log' )
    #@nonl
    #@-node:ekr.20040915085351.6:runPL
    #@-others
    t = threading.Thread( target = runPL )
    t.setDaemon( True )
    t.start()
#@nonl
#@-node:ekr.20040915085351.5:exeProduce
#@+node:ekr.20040915085351.7:addMenu
def addMenu( tag, keywords ):

    c = keywords.get('c')
    if not c: return

    men = c.frame.menu.getMenu( 'Outline' )
    men2 = Tk.Menu( men , tearoff = 0 )
    men.add_cascade( menu = men2, label = 'Produce' )
    c.add_command(men2,
        label = "Execute All Produce",
        command = lambda  c = c: exeProduce( c ))
    c.add_command(men2,
        label = "Execute Tree Produce",
        command = lambda c = c: exeProduce( c, root = False ) )
#@nonl
#@-node:ekr.20040915085351.7:addMenu
#@-others
#@nonl
#@-node:ekr.20040915085351:@thin at_produce.py
#@-leo
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.