Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Mac / Tools / CGI / BuildCGIApplet.py
blob0ee2ce0008050e8d8fbe3c61a5a40450d51a5c30
1 """BuildCGIApplet.py -- Create a CGI applet from a Python script.
3 Specilized version of BuildApplet, enabling Python CGI scripts to be
4 used under Mac web servers like WebStar. The __main__ program is
5 PythonCGISlave.py, which provides a compatibility layer, emulating
6 Unix-style CGI scripts. See CGI_README.txt for details.
7 """
9 import sys
10 import os
11 import macfs
12 import MacOS
13 from Carbon import Res
14 import EasyDialogs
15 import buildtools
16 import py_resource
19 def main():
20 try:
21 buildcgiapplet()
22 except buildtools.BuildError, detail:
23 EasyDialogs.Message(detail)
26 def buildcgiapplet():
27 buildtools.DEBUG=1
29 # Find the template
30 # (there's no point in proceeding if we can't find it)
32 template = buildtools.findtemplate()
33 wrapper = "PythonCGISlave.py"
34 if not os.path.exists("PythonCGISlave.py"):
35 wrapper = os.path.join(sys.exec_prefix, ":Mac:Tools:CGI", wrapper)
37 # Ask for source text if not specified in sys.argv[1:]
38 if not sys.argv[1:]:
39 srcfss, ok = macfs.PromptGetFile('Select a CGI script:', 'TEXT', 'APPL')
40 if not ok:
41 return
42 filename = srcfss.as_pathname()
43 dstfilename = mkcgifilename(filename)
44 dstfss, ok = macfs.StandardPutFile('Save application as:',
45 os.path.basename(dstfilename))
46 if not ok:
47 return
48 dstfilename = dstfss.as_pathname()
49 buildone(template, wrapper, filename, dstfilename)
50 else:
51 # Loop over all files to be processed
52 for filename in sys.argv[1:]:
53 dstfilename = mkcgifilename(filename)
54 buildone(template, wrapper, filename, dstfilename)
57 def mkcgifilename(filename):
58 if filename[-3:] == '.py':
59 filename = filename[:-3]
60 filename = filename + ".cgi"
61 return filename
64 def buildone(template, wrapper, src, dst):
65 buildtools.process(template, wrapper, dst, 1)
66 # write source as a PYC resource into dst
67 ref = Res.FSpOpenResFile(dst, 2)
68 try:
69 Res.UseResFile(ref)
70 py_resource.frompyfile(src, "CGI_MAIN", preload=1)
71 finally:
72 Res.CloseResFile(ref)
75 if __name__ == '__main__':
76 main()