This repository has been archived by the owner on Apr 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
printenv.py
executable file
·124 lines (98 loc) · 3.79 KB
/
printenv.py
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
"""Prints the environment variables and some Modules API output."""
import cgi
import os
import sys
from google.appengine.api import modules
from google.appengine.api import users
import webapp2
PAGE_TEMPLATE = """\
<html>
<head>
<title>printenv</title>
</head>
<body>
<a href="{}">login</a><br><a href="{}">logout</a>
<h1>The environment variables are:</h1>
{}
<h1>The CGI arguments are:</h1>
{}
<h1>The Modules API results are:</h1>
{}
</body>
</html>
"""
KEY_VALUE_TEMPLATE = '<b><tt>{}</tt></b> = <tt>{}</tt>'
STDIN_TEMPLATE = ('No CGI arguments given; here are the contents '
'of stdin (length={}):')
def html_for_env_var(key):
"""Returns an HTML snippet for an environment variable.
Args:
key: A string representing an environment variable name.
Returns:
String HTML representing the value and variable.
"""
value = os.getenv(key)
return KEY_VALUE_TEMPLATE.format(key, value)
def html_for_cgi_argument(argument, form):
"""Returns an HTML snippet for a CGI argument.
Args:
argument: A string representing an CGI argument name in a form.
form: A CGI FieldStorage object.
Returns:
String HTML representing the CGI value and variable.
"""
value = form[argument].value if argument in form else None
return KEY_VALUE_TEMPLATE.format(argument, value)
def html_for_modules_method(method_name, *args, **kwargs):
"""Returns an HTML snippet for a Modules API method.
Args:
method_name: A string containing a Modules API method.
args: Positional arguments to be passed to the method.
kwargs: Keyword arguments to be passed to the method.
Returns:
String HTML representing the Modules API method and value.
"""
method = getattr(modules, method_name)
value = method(*args, **kwargs)
return KEY_VALUE_TEMPLATE.format(method_name, value)
class MainHandler(webapp2.RequestHandler):
def get(self):
"""GET handler that serves environment data."""
environment_variables_output = [html_for_env_var(key)
for key in sorted(os.environ)]
cgi_arguments_output = []
if os.getenv('CONTENT_TYPE') == 'application/x-www-form-urlencoded':
# Note: a blank Content-type header will still sometimes
# (in dev_appserver) show up as 'application/x-www-form-urlencoded'
form = cgi.FieldStorage()
if not form:
cgi_arguments_output.append('No CGI arguments given...')
else:
for cgi_argument in form:
cgi_arguments_output.append(
html_for_cgi_argument(cgi_argument, form))
else:
data = sys.stdin.read()
cgi_arguments_output.append(STDIN_TEMPLATE.format(len(data)))
cgi_arguments_output.append(cgi.escape(data))
modules_api_output = [
html_for_modules_method('get_current_module_name'),
html_for_modules_method('get_current_version_name'),
html_for_modules_method('get_current_instance_id'),
html_for_modules_method('get_modules'),
html_for_modules_method('get_versions'),
html_for_modules_method('get_default_version'),
html_for_modules_method('get_num_instances'),
html_for_modules_method('get_hostname'),
]
result = PAGE_TEMPLATE.format(
users.CreateLoginURL(self.request.url),
users.CreateLogoutURL(self.request.url),
'<br>\n'.join(environment_variables_output),
'<br>\n'.join(cgi_arguments_output),
'<br>\n'.join(modules_api_output),
)
self.response.write(result)
APP = webapp2.WSGIApplication([
('/.*', MainHandler)
], debug=True)