~divmod-dev/divmod.org/trunk

« back to all changes in this revision

Viewing changes to Epsilon/epsilon/process.py

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-29 20:33:04 UTC
  • mfrom: (2749.1.1 remove-epsilon-1325289)
  • Revision ID: exarkun@twistedmatrix.com-20140629203304-gdkmbwl1suei4m97
mergeĀ lp:~exarkun/divmod.org/remove-epsilon-1325289

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- test-case-name: epsilon.test.test_process -*-
2
 
# Copyright (c) 2008 Divmod.  See LICENSE for details.
3
 
 
4
 
"""
5
 
Process and stdio related functionality.
6
 
"""
7
 
 
8
 
import os, sys, imp
9
 
 
10
 
from zope.interface import implements
11
 
 
12
 
from twisted.internet import reactor
13
 
from twisted.application.service import IService, Service
14
 
from twisted.internet.stdio import StandardIO
15
 
 
16
 
from epsilon.structlike import record
17
 
 
18
 
 
19
 
def spawnProcess(processProtocol, executable, args=(), env={},
20
 
                 path=None, uid=None, gid=None, usePTY=0,
21
 
                 packages=()):
22
 
    """Launch a process with a particular Python environment.
23
 
 
24
 
    All arguments as to reactor.spawnProcess(), except for the
25
 
    addition of an optional packages iterable.  This should be
26
 
    of strings naming packages the subprocess is to be able to
27
 
    import.
28
 
    """
29
 
 
30
 
    env = env.copy()
31
 
 
32
 
    pythonpath = []
33
 
    for pkg in packages:
34
 
        p = os.path.split(imp.find_module(pkg)[1])[0]
35
 
        if p.startswith(os.path.join(sys.prefix, 'lib')):
36
 
            continue
37
 
        pythonpath.append(p)
38
 
    pythonpath = list(set(pythonpath))
39
 
    pythonpath.extend(env.get('PYTHONPATH', '').split(os.pathsep))
40
 
    env['PYTHONPATH'] = os.pathsep.join(pythonpath)
41
 
 
42
 
    return reactor.spawnProcess(processProtocol, executable, args,
43
 
                                env, path, uid, gid, usePTY)
44
 
 
45
 
def spawnPythonProcess(processProtocol, args=(), env={},
46
 
                       path=None, uid=None, gid=None, usePTY=0,
47
 
                       packages=()):
48
 
    """Launch a Python process
49
 
 
50
 
    All arguments as to spawnProcess(), except the executable
51
 
    argument is omitted.
52
 
    """
53
 
    return spawnProcess(processProtocol, sys.executable,
54
 
                        args, env, path, uid, gid, usePTY,
55
 
                        packages)
56
 
 
57
 
 
58
 
 
59
 
class StandardIOService(record('protocol'), Service):
60
 
    """
61
 
    Service for connecting a protocol to stdio.
62
 
    """
63
 
    def startService(self):
64
 
        """
65
 
        Connect C{self.protocol} to standard io.
66
 
        """
67
 
        StandardIO(self.protocol)