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
42
43 globals().update(core.namespace())
44
46 package = globals()[package_name]
47 for module_name in package.__all__:
48 exec('import %s.%s' % (package_name, module_name))
49 mod = getattr(package, module_name)
50 for element_name in dir(mod):
51 if not element_name.startswith('_'):
52 element = getattr(mod, element_name)
53 if type(element) is types.FunctionType:
54 globals()[element_name] = element
55
57 commands = {}
58 for command_name in command_names:
59 command_module = __import__('%s.%s' % (package_name, command_name), globals(), locals(), command_name)
60 commands[command_name] = getattr(command_module, command_name)
61 return commands
62
63
64
66 """Invoke osh interpreter. Each argument is a function invocation identifying a command.
67 The command sequence corresponds to the sequence of arguments.
68 """
69
70 pipeline = None
71 try:
72 pipeline = apiparser._sequence_op(ops)
73 except Exception, e:
74 print >>sys.stderr, e
75 sys.exit(1)
76
77 command = core.Command(pipeline)
78 command.execute()
79 last_op = ops[-1]
80 if type(last_op) is _ReturnList:
81 return last_op.list
82 else:
83 return None
84
86
87 _unwrap_singleton = None
88 _list = None
89
91 core.Op.__init__(self, '', (0, 0))
92 self._unwrap_singleton = unwrap_singleton
93 self._list = []
94
97
99 if self._unwrap_singleton and len(object) == 1:
100 self._list.append(object[0])
101 else:
102 self._list.append(object)
103
106
107 list = property(lambda self: self._list)
108
109
111 """Input tuples are accumulated in a list which is returned as the value of the
112 C{osh()} invocation. If unwrap_singleton is True, then items in the list that
113 are 1-object sequences are unwrapped, e.g. (419,) -> 419.
114 """
115 return _ReturnList(unwrap_singleton)
116
117
118
119
121 """Replaces the standard error handler (which prints to stderr). An error handler
122 takes these arguments:
123 - C{line}: A line printed to stderr by the failing operation.
124 - C{op}: The failing operation.
125 - C{input}: The input to the failing operation.
126 - C{host}: The host on which the error occurred (in case of remote execution).
127 """
128 error.set_error_handler(handler)
129
131 """Replaces the standard exception handler (which prints to stderr). An exception handler
132 takes these arguments:
133 - C{exception}: The exception being handled.
134 - C{op}: The operation that caused the exception.
135 - C{input}: The input to the operation that caused the exception.
136 - C{host}: The host on which the exception occurred (in case of remote execution).
137 """
138 error.set_exception_handler(handler)
139
140
141
142
144 """Control osh debugging: 0 = off, 1 = parse tree, 2 = command execution.
145 """
146 core.verbosity = verbosity
147
148
149
150
151 _import_package('command')
152