Commits

Ronny Pfannschmidt committed 3962cbc

decuple subprocess steps creation from execution

Comments (0)

Files changed (7)

glas_process/baseproc.py

 from .model import Step, Event
 
 
+def makestep(procdir, _id, steper, **kw):
+    return Step(
+        _id=procdir.get_id(steper, _id),
+        status='prepared',
+        steper=steper,
+        **kw)
+
 class Proc(object):
+
+
     def save_with_batch(self, doc):
         self.procdir.save_with_batch(doc)
 
-    def makestep(self, _id, steper, **kw):
-        self.step = Step(
-            _id=self.procdir.get_id(steper, _id),
-            steper=steper,
-            status='prepared',
-            **kw)
-        self.save_with_batch(self.step)
 
-    def __init__(self, procdir):
+    def __init__(self, procdir, step):
         self.procdir = procdir
         self.queue = Queue()
         self.greenlets = []
         self._control = None
-        self.step = None
+        self.step = step
+        self.save_with_batch(step)
 
     def spawn(self, func, *k, **kw):
         res = gevent.spawn(func, *k,  **kw)
             if isinstance(doc, dict):
                 doc = Event(**doc)
             if not doc._id:
-                doc._id = '%s:%s' %(self.step._id, i)
+                doc._id = '%s:%s' % (self.step._id, i)
             if not doc.step:
                 doc.step = self.step._id
             doc.index = i

glas_process/procdir.py

             endkey=[step, stream, {}],
         )
 
+    def run(self, doc):
+        if doc.steper == 'popen':
+            from .subprocess import SubProcessProc
+
+            proc = SubProcessProc(self, doc)
+
+        return proc.wait()
 
 
 def ProcBatch(object):

glas_process/scm.py

-from .baseproc import Proc
+from .baseproc import Proc, makestep
 import anyvc
 
 
 class ScmProc(Proc):
     def __init__(self, procdir, scminfo, _id=None):
-        Proc.__init__(self, procdir)
-        self.scminfo = scminfo
-        self.makestep(_id, steper='scm', **scminfo)
+        Proc.__init__(self,
+                      procdir,
+                      makestep(
+                          procdir, _id,
+                          steper='scm',
+                          **scminfo)
+                     )
 
     def create_or_pull(self):
         "clone or pull (as in hg pull)"

glas_process/subprocess.py

 import subprocess
 import gevent
 from gevent.socket import wait_read
-from glas_process.baseproc import Proc
+from glas_process.baseproc import Proc, makestep
 
 
-def make_python(proc, script, _id=None):
-    return SubProcessProc(proc, 'python', ['python', '-'], script, _id)
+def prepare_python(proc, script, _id=None):
+    return makestep(
+        proc, _id,
+        type='python',
+        steper='popen',
+        args=['python', '-'],
+        stdin=script,
+    )
 
 
-def make_subprocess(proc, cmd, _id=None):
-    return SubProcessProc(proc, 'popen', cmd, None, _id)
+def prepare_subprocess(proc, cmd, _id=None):
+    return makestep(
+        proc, _id,
+        type='subprocess',
+        steper='popen',
+        args=[str(x) for x in cmd],
+        stdin=None,
+    )
 
 
 class SubProcessProc(Proc):
 
-    def __init__(self, proc, steper, args, stdin, _id=None):
-        Proc.__init__(self, proc)
-        self.makestep(_id,
-            steper=steper,
-            args=[str(x) for x in args],
-            stdin=stdin,
-        )
-
     def create(self):
         self.step.status = 'running'
         self.save_with_batch(self.step)

testing/test_events.py

 import copy
-from glas_process.subprocess import make_python, make_subprocess
+from glas_process.subprocess import prepare_python, prepare_subprocess
 
 def pytest_funcarg__procdir(request):
     procdir = request.getfuncargvalue('procdir')
 
 def check_events(procdir, checks):
     print procdir._events
+    assert len(procdir._events) >= len(checks)
     for event, check in zip(procdir._events, checks):
         print event, check
         for attr, expected in zip(check[::2], check[1::2]):
 
 def test_run_simple_process(procdir):
     procdir.path.ensure('somefile')
-    proc = make_subprocess(procdir, ['ls'], _id='the_ls')
-    proc.wait()
+    step = prepare_subprocess(procdir, ['ls'], _id='the_ls')
+    procdir.run(step)
     checks = [
         ('doc_type', "Step",
          'status', 'prepared'),
     check_events(procdir, checks)
 
 def test_simple_process_failure(procdir):
-    make_subprocess(procdir, ['false']).wait()
+    step = prepare_subprocess(procdir, ['false'])
+    procdir.run(step)
     checks = [
         ('doc_type', "Step",
          'status', 'prepared'),
     check_events(procdir, checks)
 
 def test_cat_process(procdir):
-    p = make_subprocess(procdir,
+    doc = prepare_subprocess(procdir,
                         ['head', '-n', '30', '/etc/services'],
                         _id='cat')
-    p.wait()
+    procdir.run(doc)
     for event in procdir._events:
         if hasattr(event, 'lineno'):
             print event, event.line.rstrip()
             print event
 
 def test_python(procdir):
-    p = make_python(procdir, 'print 1\n')
-    p.run()
+    doc = prepare_python(procdir, 'print 1\n')
+    procdir.run(doc)
     checks = [
         ('doc_type', "Step",
          'status', 'prepared'),

testing/test_scmprocdir.py

     assert procdir.path.check()
     p.create_or_pull()
 
-
     p.update_wd()
 

testing/test_views.py

 from __future__ import print_function
-from glas_process.subprocess import make_subprocess
+from glas_process.subprocess import prepare_subprocess
 
 def test_lines_show(procdir):
     procdir.path.ensure('test')
-    ls = make_subprocess(procdir,['ls'], _id='ls:short')
-    ls_long = make_subprocess(procdir, ['ls', '-l'], _id='ls:long')
-    ls.wait()
-    ls_long.wait()
+    ls = prepare_subprocess(procdir,['ls'], _id='ls:short')
+    ls_long = prepare_subprocess(procdir, ['ls', '-l'], _id='ls:long')
+    procdir.run(ls)
+    procdir.run(ls_long)
+
     for task in ('ls:short', 'ls:long'):
         print('task', task)
         streams = procdir.find_streams(task)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.