From: Robert W. B. <rb...@di...> - 2001-01-23 00:00:54
|
Hello, (tnx for the speedy reply Finn!) Unfortunately, I'm still stuck. Maybe this is different enough that I should include some code examples and the full story: In using Tomcat-3.2.1, Jython-2.0, sun jdk-1.2.2, win98. I've placed jython.jar in %TOMCAT_HOME%\webapps\jython\WEB-INF\lib and would also like to place the below servlet (and many frozen modules) in the same lib directory as jyDispatch.jar. Ahhh, the dream of an entirely frozen webapp that could be delivered to clients in a *.war file just like all the java servlet/JSP webapps out there, but secretly using Jython :) Here's the code: -------------------------------------------------------------- #filename: jyDispatch.py import sys sys.prefix="." from javax import servlet from javax.servlet import http import os class jyDispatch(http.HttpServlet): def service(self,req, res): 'Jython servlet dispatcher' mtimes = {} ctx = self.getServletContext() #test junk out = res.getWriter() out.println("<br>sys.path=" + str(sys.path)) out.println("<br>sys.prefix=" + str(sys.prefix)) out.println("<br>cwd=" + str(sys.getcwd())) # Get the path of the *.py file to be dispatched path = ctx.getRealPath( req.getServletPath() ) mtime = os.stat(path)[7] #Other stuff... ---------------------------------------------------------------- The "sys.prefix='.'" was added as suggested and it was compiled with the implied: jythonc -j jyDispatch.jar -a jyDispatch.py However, none of the javaos code is accessible. getcwd, and stat in the example above are hidden. It seems very much like the example you (Finn) used, but I'm having no luck muxing with the sys.prefix or sys.path in the servlet to get around this. All hints and comments are welcome as always :) Thanks in advance. -Robert [Robert W. Bill] >>Freezing servlet applications have resulted in difficulty with >>the javaos module. The simple scenario is I need to use >>os.stat(file), but all frozen applications return the error: >> >> AttributeError: class 'org.python.modules.os' has no >> attribute 'stat' >> >>When using jythonc, i've tried: >> >> jythonc -a -j app.jar jyDispatch.py >> jythonc -a -j app.jar jyDispatch.py javaos os re string >> jythonc -c -j app.jar jyDispatch.py javaos etc.... >> >>All result in the above AttributeError. >> >>I've assumed stat requires os + javaos- does it? >Yes. >>How do I freeze an application so that I can use it? >When I just tried to freeze this little module I got an NPE: > > import os > print os.stat > >This is an unintended sideeffect on a hack I added to satisfy >site.py's use of the os module. It occur when sys.prefix is None >which typically happens for frozen applications. > >Changing the module to: > > import sys > sys.prefix = "." > > import os > print os.stat > >and it runs as expected. I compiled the module with the >simple: > > jythonc.bat -j app.jar -a app.py >I'm not 100% sure this is the situation you are seeing, but >please give it a try. >regards, >finn |
From: <bc...@wo...> - 2001-02-07 14:05:51
|
[Robert W. Bill] >In using Tomcat-3.2.1, Jython-2.0, sun jdk-1.2.2, win98. My tests have been with tomcat-4.0m5, jdk1.3 & WinNT. >I've >placed jython.jar in %TOMCAT_HOME%\webapps\jython\WEB-INF\lib and >would also like to place the below servlet (and many frozen >modules) in the same lib directory as jyDispatch.jar. I copied the jyDispatch.jar to ...\WEB-INF\lib. The web.xml contained: <web-app> <servlet> <servlet-name>jyDispatch</servlet-name> <servlet-class>jyDispatch</servlet-class> </servlet> <servlet-mapping> <servlet-name>jyDispatch</servlet-name> <url-pattern>*.py</url-pattern> </servlet-mapping> </web-app> >Ahhh, the >dream of an entirely frozen webapp that could be delivered to >clients in a *.war file just like all the java servlet/JSP >webapps out there, but secretly using Jython :) Difficult as long as the dispatcher is written in jython. In the recent PyServlet.java, the dispatcher can initialize the PythonInterpreter with a usefull python.home because it is written in java. So the dream is possible with the newly added PyServlet.java code. The application is not frozen and the use of jython is quite visible if the user looks in the WEB-INF directory, but with a web.xml file likes this: <web-app> <!-- The JSP page compiler and execution servlet --> <servlet> <servlet-name>PyServlet</servlet-name> <servlet-class>org.python.util.PyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>PyServlet</servlet-name> <url-pattern>*.py</url-pattern> </servlet-mapping> </web-app> inside a .war file like this: test.py WEB-INF/web.xml WEB-INF/lib/jython.jar WEB-INF/lib/Lib/javaos.py the application is deployable without any outside code or configuration (only when using the CVS version of Jython). >Here's the code: >-------------------------------------------------------------- >#filename: jyDispatch.py >import sys >sys.prefix="." >from javax import servlet >from javax.servlet import http >import os > >class jyDispatch(http.HttpServlet): > def service(self,req, res): > 'Jython servlet dispatcher' > mtimes = {} > ctx = self.getServletContext() > > #test junk > out = res.getWriter() > out.println("<br>sys.path=" + str(sys.path)) > out.println("<br>sys.prefix=" + str(sys.prefix)) > out.println("<br>cwd=" + str(sys.getcwd())) I think you meant os.getcwd(). > # Get the path of the *.py file to be dispatched > path = ctx.getRealPath( req.getServletPath() ) > mtime = os.stat(path)[7] This works for my using the CVS and the setup I described at the very top. > #Other stuff... >---------------------------------------------------------------- > >The "sys.prefix='.'" was added as suggested and This hack is no longer needed with the CVS version. regards, finn |