1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """The osh API (Application Programming Interface) supports the use of osh from Python code.
19 The recommended form of import is C{from osh.api import *}. This imports
20 the functions documented here as well as
21 the symbols from the module C{osh.builtins},
22 the C{Process} class, and C{processes}
23 function.
24
25 In general, the function C{F()} can be found in the module
26 C{osh.command.F}. See documentation for the package osh.command for
27 information on each function.
28 """
29
30 import sys
31 import types
32
33 from args import Option
34 import apiparser
35 import core
36 import config
37 import error
38 import command
39 import command.f
40 from builtins import *
41
43 package = globals()[package_name]
44 for module_name in package.__all__:
45 exec('import %s.%s' % (package_name, module_name))
46 mod = getattr(package, module_name)
47 for element_name in dir(mod):
48 if not element_name.startswith('_'):
49 element = getattr(mod, element_name)
50 if type(element) is types.FunctionType:
51 globals()[element_name] = element
52
53
54
55
57 """Invoke osh interpreter. Each argument is a function invocation identifying a command.
58 The command sequence corresponds to the sequence of arguments.
59 """
60
61 output_accumulator = None
62 if (ops[-1].__class__ is _ReturnList):
63 output_accumulator = []
64 ops = ops[:-1]
65
66 pipeline = None
67 try:
68 pipeline = apiparser._sequence_op(ops)
69 except Exception, e:
70 print >>sys.stderr, e
71 sys.exit(1)
72
73 if output_accumulator is not None:
74 pipeline.append_op(f(lambda *x: output_accumulator.append(x)))
75
76 pipeline.setup()
77 if core.verbosity >= 1:
78 print pipeline.dump()
79 try:
80 pipeline.execute()
81 except error.ExceptionHandlerException, e:
82 raise e.cause
83 return output_accumulator
84
85
86
87
88
91
93 """Input tuples are accumulated in a list which is returned as the value of the
94 C{osh()} invocation.
95 """
96 return _ReturnList()
97
98
99
100
102 """Replaces the standard error handler (which prints to stderr). An error handler
103 takes these arguments:
104 - C{line}: A line printed to stderr by the failing operation.
105 - C{op}: The failing operation.
106 - C{input}: The input to the failing operation.
107 - C{host}: The host on which the error occurred (in case of remote execution).
108 """
109 error.set_error_handler(handler)
110
112 """Replaces the standard exception handler (which prints to stderr). An exception handler
113 takes these arguments:
114 - C{exception}: The exception being handled.
115 - C{op}: The operation that caused the exception.
116 - C{input}: The input to the operation that caused the exception.
117 - C{host}: The host on which the exception occurred (in case of remote execution).
118 """
119 error.set_exception_handler(handler)
120
121
122
123
125 """Control osh debugging: 0 = off, 1 = parse tree, 2 = command execution.
126 """
127 core.verbosity = verbosity
128
129
130
131
132 _import_package('command')
133