Use full package paths in imports.
[python/dscho.git] / Mac / Contrib / mpwsystem / mpwsystem.py
blob34646c4ea5273eb568714e1d230450fd1ccff411
2 """mpwsystem -
3 A simple example of how to use Apple Events to implement a "system()"
4 call that invokes ToolServer on the command.
6 Contributed by Daniel Brotsky <dev@brotsky.com>.
8 (renamed from aesystem to mpwsystem by Jack)
10 system(cmd, infile = None, outfile = None, errfile = None)
12 1. Every call to system sets "lastStatus" and "lastOutput" to the
13 status and output
14 produced by the command when executed in ToolServer. (lastParameters
15 and lastAttributes
16 are set to the values of the AppleEvent result.)
18 2. system returns lastStatus unless the command result indicates a MacOS error,
19 in which case os.Error is raised with the errnum as associated value.
21 3. You can specify ToolServer-understandable pathnames for
22 redirection of input,
23 output, and error streams. By default, input is Dev:Null, output is captured
24 and returned to the caller, diagnostics are captured and returned to
25 the caller.
26 (There's a 64K limit to how much can be captured and returned this way.)"""
28 import os
29 import aetools
31 try: server
32 except NameError: server = aetools.TalkTo("MPSX", 1)
34 lastStatus = None
35 lastOutput = None
36 lastErrorOutput = None
37 lastScript = None
38 lastEvent = None
39 lastReply = None
40 lastParameters = None
41 lastAttributes = None
43 def system(cmd, infile = None, outfile = None, errfile = None):
44 global lastStatus, lastOutput, lastErrorOutput
45 global lastScript, lastEvent, lastReply, lastParameters, lastAttributes
46 cmdline = cmd
47 if infile: cmdline += " <" + infile
48 if outfile: cmdline += " >" + outfile
49 if errfile: cmdline += " " + str(chr(179)) + errfile
50 lastScript = "set Exit 0\r" + cmdline + "\rexit {Status}"
51 lastEvent = server.newevent("misc", "dosc", {"----" : lastScript})
52 (lastReply, lastParameters, lastAttributes) = server.sendevent(lastEvent)
53 if lastParameters.has_key('stat'): lastStatus = lastParameters['stat']
54 else: lastStatus = None
55 if lastParameters.has_key('----'): lastOutput = lastParameters['----']
56 else: lastOutput = None
57 if lastParameters.has_key('diag'): lastErrorOutput = lastParameters['diag']
58 else: lastErrorOutput = None
59 if lastParameters['errn'] != 0:
60 raise os.Error, lastParameters['errn']
61 return lastStatus
63 if __name__ == '__main__':
64 sts = system('alert "Hello World"')
65 print 'system returned', sts