File: virt_handler.py

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (216 lines) | stat: -rw-r--r-- 6,009 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#
#  virt_handler.py
#
#  $Id: virt_handler.py,v 1.1.1.1.2.1 2010/04/14 13:55:08 source Exp $
#
#  python proxy for OpenLink python plugin
#  
#  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
#  project.
#  
#  Copyright (C) 1998-2006 OpenLink Software
#  
#  This project is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; only version 2 of the License, dated June 1991.
#  
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#  
#  

import os;
import sys;
import traceback;

class VirtNullIO:
    def tell(self): return 0
    def read(self, n = -1): return ""
    def readline(self, length = None): return ""
    def readlines(self): return []
    def write(self, s): pass
    def writelines(self, list):
        self.write("".join(list))
    def isatty(self): return 0
    def flush(self): pass
    def close(self): pass
    def seek(self, pos, mode = 0): pass


class VirtCGIStdin(VirtNullIO):
    def __init__(self, init_val):
        self.pos = 0
        # note that self.buf sometimes contains leftovers
        # that were read, but not used when readline was used
        self.buf = init_val

    def read(self, n = -1):
        if n <= 0:
            return ""

        n2 = n + self.pos;
        if self.buf:
            s = self.buf[self.pos:n2]
            n = n - len(s)
        else:
            s = ""
        self.pos = self.pos + len(s)
        return s

    def readlines(self):
        s = (self.buf).split('\n')
        return map(lambda s: s + '\n', s)

    def readline(self, n = -1):

        if n == 0:
            return ""

        # look for \n in the buffer
        i = self.buf.find('\n')
        if i == -1:
            i = len (self.buf) - 1
        # carve out the piece, then shorten the buffer
        result = self.buf[:i+1]
        self.buf = self.buf[i+1:]
        self.pos = self.pos + len(result)
        return result
        

class VirtCGIStdout(VirtNullIO):

    def __init__(self):
        self.pos = 0
        self.headers_sent = 0
        self.headers = ""
        self.req_text = ""
	self.html_mode=0
        
    def write(self, s):

        if not s: return

        if self.html_mode and not self.headers_sent:
            self.headers = self.headers + s

            headers_over = 0

            ss = self.headers.split('\r\n\r\n', 1)
            if len(ss) < 2:
                ss = self.headers.split('\n\n', 1)
                if len(ss) >= 2:
                    headers_over = 1
            else:
                headers_over = 1
                    
            if headers_over:
                self.headers_sent = 1

        else:
            self.req_text = self.req_text + s
        
        self.pos = self.pos + len(s)

    def tell(self): return self.pos

    def get_headers(self) : return self.headers;
    def get_body(self) : return self.req_text;
    def set_html_mode(self) : self.html_mode=1;


def setup_cgi(env, stdin_txt, new_stdout, new_stderr):

    save_env = os.environ.copy()
    if env.has_key ("__VIRT_CGI") and env["__VIRT_CGI"] == '1':
        new_stdout.set_html_mode ();
    
    si = sys.stdin
    so = sys.stdout
    sr = sys.stderr

    os.environ.update(env)
 
    sys.stdout = new_stdout
    sys.stdin = VirtCGIStdin(stdin_txt)
    sys.stderr = new_stderr

    return save_env, si, so, sr
     

def restore_nocgi(sav_env, si, so, sr):

    osenv = os.environ

    for k in osenv.keys():
        del osenv[k]
    for k in sav_env:
        osenv[k] = sav_env[k]

    sys.stdout = si
    sys.stdin = so
    sys.stderr = sr


def call_string(base_uri,content,opts,params,lines):
	new_stdout = VirtCGIStdout();
	new_stderr = VirtCGIStdout();

	sys.argv = [ base_uri ];

	sav_env, si, so, sr = setup_cgi (opts, params, new_stdout, '')
	try:
	    eval (compile (content, base_uri, 'exec'), {})
	    restore_nocgi (sav_env, si, so, sr)

            body=new_stdout.get_body ();
            hdr=new_stdout.get_headers ();
            err=new_stderr.get_headers () + new_stderr.get_body ();
	    return body, hdr, err
	except:
	    restore_nocgi (sav_env, si, so, sr)
            a,b,c = sys.exc_info ();
	    ex_text = "".join (traceback.format_exception (a,b,c));
            body=new_stdout.get_body ();
            hdr=new_stdout.get_headers ();
            err=new_stderr.get_headers () + new_stderr.get_body ();
            return body, hdr, err, ex_text


def call_file(base_uri,opts,params,lines):
	new_stdout = VirtCGIStdout();
	new_stderr = VirtCGIStdout();

	sys.argv = [ base_uri ];

	sav_env, si, so, sr = setup_cgi (opts, params, new_stdout, '')
	try:
	    execfile (base_uri, {});
	    restore_nocgi (sav_env, si, so, sr)

            body=new_stdout.get_body ();
            hdr=new_stdout.get_headers ();
            err=new_stderr.get_headers () + new_stderr.get_body ();
	    return body, hdr, err
	except:
	    restore_nocgi (sav_env, si, so, sr)
            a,b,c = sys.exc_info ();
	    ex_text = "".join (traceback.format_exception (a,b,c));
            body=new_stdout.get_body ();
            hdr=new_stdout.get_headers ();
            err=new_stderr.get_headers () + new_stderr.get_body ();
            return body, hdr, err, ex_text

# testting code
if __name__ == '__main__':
    if os.environ.has_key ("__VIRT_CGI") and os.environ["__VIRT_CGI"] != '1' and not 12 == 11:
       print ("xx");
       
    a,b,c = call_file ('../lib/suite/admin/cgitest.py', { '__VIRT_CGI': '1' }, '', '');
    d = ''
    sys.stderr.write ('\n['+a+']['+b+']['+c+']['+d+']\n');