author | Rich Burridge <rich.burridge@sun.com> |
Mon, 30 Nov 2009 13:01:40 -0800 | |
changeset 1516 | 8c950a3b4171 |
parent 873 | b95d76c53b5e |
child 3171 | 525f5bdb3f62 |
permissions | -rw-r--r-- |
1516
8c950a3b4171
10485 move pkg(5) to Python 2.6
Rich Burridge <rich.burridge@sun.com>
parents:
873
diff
changeset
|
1 |
#!/usr/bin/python |
873
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
2 |
# |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
3 |
# copyright (c) 2004-2007, cherrypy team ([email protected]) |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
4 |
# all rights reserved. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
5 |
# |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
6 |
# redistribution and use in source and binary forms, with or without modification, |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
7 |
# are permitted provided that the following conditions are met: |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
8 |
# |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
9 |
# * redistributions of source code must retain the above copyright notice, |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
10 |
# this list of conditions and the following disclaimer. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
11 |
# * redistributions in binary form must reproduce the above copyright notice, |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
12 |
# this list of conditions and the following disclaimer in the documentation |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
13 |
# and/or other materials provided with the distribution. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
14 |
# * neither the name of the cherrypy team nor the names of its contributors |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
15 |
# may be used to endorse or promote products derived from this software |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
16 |
# without specific prior written permission. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
17 |
# |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
18 |
# this software is provided by the copyright holders and contributors "as is" and |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
19 |
# any express or implied warranties, including, but not limited to, the implied |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
20 |
# warranties of merchantability and fitness for a particular purpose are |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
21 |
# disclaimed. in no event shall the copyright owner or contributors be liable |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
22 |
# for any direct, indirect, incidental, special, exemplary, or consequential |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
23 |
# damages (including, but not limited to, procurement of substitute goods or |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
24 |
# services; loss of use, data, or profits; or business interruption) however |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
25 |
# caused and on any theory of liability, whether in contract, strict liability, |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
26 |
# or tort (including negligence or otherwise) arising in any way out of the use |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
27 |
# of this software, even if advised of the possibility of such damage. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
28 |
# |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
29 |
|
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
30 |
# |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
31 |
# Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
32 |
# Use is subject to license terms. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
33 |
# |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
34 |
|
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
35 |
import sys as _sys |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
36 |
import cherrypy as _cherrypy |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
37 |
from cherrypy import _cperror |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
38 |
from cherrypy import _cpwsgi |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
39 |
|
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
40 |
class DepotResponse(_cpwsgi.AppResponse): |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
41 |
""" This class is a partial combination of a cherrypy's original |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
42 |
AppResponse class with a change to "Stage 2" of setapp to provide |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
43 |
access to the write() callable specified by PEP 333. Access to this |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
44 |
callable is necessary to maintain a minimal memory and disk |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
45 |
footprint for streaming operations performed by the depot server, |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
46 |
such as filelist. """ |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
47 |
|
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
48 |
def __add_write_hook(self, s, h, exc): |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
49 |
# The WSGI specification includes a special write() |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
50 |
# callable returned by the start_response callable. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
51 |
# cherrypy traditionally hides this from applications |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
52 |
# as new WSGI applications and frameworks are not |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
53 |
# supposed to use it if at all possible. The write() |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
54 |
# callable is considered a hack to support imperative |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
55 |
# streaming APIs. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
56 |
# |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
57 |
# As a result, we have to provide access to the write() |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
58 |
# callable ourselves by replacing the default |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
59 |
# response_class with our own. This callable is |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
60 |
# provided so that streaming APIs can be treated as if |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
61 |
# their output had been yielded by an iterable. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
62 |
# |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
63 |
# The cherrypy singleton below is thread-local, and |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
64 |
# guaranteed to only be set for a specific request. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
65 |
# This means any callables that use the singleton |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
66 |
# to access this method are guaranteed to write output |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
67 |
# back to the same request. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
68 |
# |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
69 |
# See: http://www.python.org/dev/peps/pep-0333/ |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
70 |
# |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
71 |
_cherrypy.response.write = self.start_response(s, h, exc) |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
72 |
|
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
73 |
def setapp(self): |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
74 |
try: |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
75 |
self.request = self.get_request() |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
76 |
s, h, b = self.get_response() |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
77 |
self.iter_response = iter(b) |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
78 |
self.__add_write_hook(s, h, None) |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
79 |
except self.throws: |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
80 |
self.close() |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
81 |
raise |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
82 |
except _cherrypy.InternalRedirect, ir: |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
83 |
self.environ['cherrypy.previous_request'] = _cherrypy.serving.request |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
84 |
self.close() |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
85 |
self.iredirect(ir.path, ir.query_string) |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
86 |
return |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
87 |
except: |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
88 |
if getattr(self.request, "throw_errors", False): |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
89 |
self.close() |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
90 |
raise |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
91 |
|
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
92 |
tb = _cperror.format_exc() |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
93 |
_cherrypy.log(tb, severity=40) |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
94 |
if not getattr(self.request, "show_tracebacks", True): |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
95 |
tb = "" |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
96 |
s, h, b = _cperror.bare_error(tb) |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
97 |
self.iter_response = iter(b) |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
98 |
|
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
99 |
try: |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
100 |
self.__add_write_hook(s, h, _sys.exc_info()) |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
101 |
except: |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
102 |
# "The application must not trap any exceptions raised by |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
103 |
# start_response, if it called start_response with exc_info. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
104 |
# Instead, it should allow such exceptions to propagate |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
105 |
# back to the server or gateway." |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
106 |
# But we still log and call close() to clean up ourselves. |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
107 |
_cherrypy.log(traceback=True, severity=40) |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
108 |
self.close() |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
109 |
raise |
b95d76c53b5e
2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents:
diff
changeset
|
110 |