Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

A command does not auto-start itself

  • Loading branch information...
commit c86fcee4db253c1355833dc90974e8bf2d9c0d77 1 parent 95b2910
Kumar McMillan authored August 15, 2012
1  .gitignore
@@ -6,3 +6,4 @@
6 6
 *.mo
7 7
 *.db
8 8
 dist
  9
+docs/_build
48  desub/desub.py
@@ -20,19 +20,58 @@ def join(cmd_args, **kw):
20 20
 
21 21
     The return value is a :class:`desub.Desub` object.
22 22
 
23  
-    Example::
  23
+    Start by joining a command that may or may not
  24
+    already be running.
  25
+
  26
+    .. doctest::
  27
+        :hide:
24 28
 
25 29
         >>> import desub
  30
+
  31
+    .. doctest::
  32
+
26 33
         >>> proc = desub.join(['python', 'tests/cmd/loop.py'])
27  
-        >>> proc.pid
28  
-        1987
  34
+
  35
+    There will only ever be one instance of that command
  36
+    running unless you specify a custom *root*.
  37
+    Check to see if the command is running:
  38
+
  39
+        >>> proc.is_running()
  40
+        False
  41
+
  42
+    OK. It's not running, so start it up:
  43
+
  44
+        >>> proc.start()
  45
+        >>> proc.is_running()
  46
+        True
  47
+        >>> type(proc.pid)
  48
+        <type 'int'>
  49
+
  50
+    .. doctest::
  51
+        :hide:
  52
+
  53
+        >>> import time
  54
+        >>> time.sleep(1)
  55
+
  56
+    Fetch the output:
  57
+
29 58
         >>> proc.stdout.read()
30 59
         'stdout'
31 60
         >>> proc.stderr.read()
32 61
         'stderr'
  62
+
  63
+    Stop the command, if you want to:
  64
+
33 65
         >>> proc.stop()
34 66
         >>> proc.is_running()
35 67
         False
  68
+
  69
+    When you read the output of a stopped command,
  70
+    that's output from the subsequent run.
  71
+
  72
+        >>> proc.stdout.read()
  73
+        'stdout'
  74
+
36 75
     """
37 76
     return Desub(cmd_args, **kw)
38 77
 
@@ -75,8 +114,6 @@ def __init__(self, cmd_args, **kw):
75 114
         self.data['cmd_args'] = cmd_args
76 115
         self.data['keyword_args'] = repr(kw)
77 116
         self.save_data()
78  
-        if not self.is_running():
79  
-            self.start()
80 117
 
81 118
     def is_running(self):
82 119
         """True if the subprocess is running."""
@@ -129,7 +166,6 @@ def stop(self):
129 166
         if pp:
130 167
             try:
131 168
                 kill_process_nicely(pp)
132  
-                shutil.rmtree(self.root)
133 169
             except psutil.NoSuchProcess:
134 170
                 pass
135 171
 
6  docs/index.rst
Source Rendered
... ...
@@ -1,6 +1,6 @@
1  
-====
  1
+=====
2 2
 desub
3  
-====
  3
+=====
4 4
 
5 5
 .. highlight:: python
6 6
 
@@ -33,7 +33,7 @@ API
33 33
 
34 34
 .. autofunction:: desub.join
35 35
 
36  
-.. autoclass:: desub.Subd
  36
+.. autoclass:: desub.Desub
37 37
     :members: pid, stdout, stderr, stop, is_running
38 38
 
39 39
 Developers
2  setup.py
@@ -8,7 +8,7 @@ def path(name):
8 8
 
9 9
 
10 10
 setup(name='desub',
11  
-      version='1.0.1',
  11
+      version='1.0.2',
12 12
       description='Work with a detached subprocess.',
13 13
       long_description=path('README.rst').read(),
14 14
       author='Kumar McMillan',
23  tests/test.py
@@ -29,30 +29,43 @@ def cmd(self, name):
29 29
         return os.path.join(os.path.dirname(__file__), 'cmd', name)
30 30
 
31 31
     def test_start(self):
  32
+        self.proc.start()
32 33
         pp = psutil.Process(self.proc.pid)
33 34
         eq_(pp.status, 'running')
  35
+        self.proc.stop()
  36
+        self.assertRaises(psutil.NoSuchProcess, lambda: pp.status)
34 37
 
35 38
     @raises(psutil.NoSuchProcess)
36 39
     def test_stop(self):
  40
+        self.proc.start()
37 41
         pid = self.proc.pid
38 42
         self.proc.stop()
39 43
         psutil.Process(pid)
40 44
 
41  
-    def test_stop_clears_root(self):
  45
+    def test_stop_preserves_root(self):
  46
+        self.proc.start()
  47
+        assert os.path.exists(self.proc.root)
42 48
         self.proc.stop()
43  
-        assert not os.path.exists(self.proc.root)
  49
+        assert os.path.exists(self.proc.root)
44 50
 
45 51
     def test_output(self):
  52
+        self.proc.start()
46 53
         time.sleep(1)
47 54
         eq_(self.proc.stdout.read(), 'stdout')
48 55
         eq_(self.proc.stderr.read(), 'stderr')
49 56
 
50 57
     def test_join(self):
  58
+        self.proc.start()
51 59
         proc2 = self.join()
  60
+        proc2.start()
52 61
         eq_(self.proc.pid, proc2.pid)
53 62
 
54 63
     def test_default_root(self):
55  
-        pr = desub.join([sys.executable, self.cmd('loop.py')])
56  
-        self.addCleanup(lambda: shutil.rmtree(pr.root))
57  
-        assert pr.pid != self.proc.pid, (
  64
+        self.proc.start()
  65
+        tmp = tempfile.mkdtemp()
  66
+        pr = desub.join([sys.executable, self.cmd('loop.py')], root=tmp)
  67
+        try:
  68
+            assert pr.pid != self.proc.pid, (
58 69
                             'new root should make separate procs')
  70
+        finally:
  71
+            shutil.rmtree(pr.root)
12  tox.ini
... ...
@@ -1,9 +1,17 @@
1 1
 [tox]
2  
-envlist=py27
  2
+envlist=py26,py27,docs
3 3
 
4 4
 [testenv]
5 5
 deps=
6 6
     nose
7 7
     git+git://github.com/kumar303/nose-nicedots.git
8 8
 commands=
9  
-    nosetests --with-nicedots --stop []
  9
+    nosetests --with-nicedots --with-doctest --stop []
  10
+
  11
+[testenv:docs]
  12
+changedir=docs
  13
+deps=
  14
+    sphinx
  15
+    nose
  16
+commands=
  17
+    sphinx-build -W -b html -d {envtmpdir}/doctrees {toxinidir}/docs  {envtmpdir}/html

0 notes on commit c86fcee

Please sign in to comment.
Something went wrong with that request. Please try again.