Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands
Previously Ridzwan Aminuddin wrote: Hi! Thanks for your reply. I heeded your advice and tried fooling around with the stderr. Apparently even if i set the classpath through my execution statement i.e : command = java -jar -classpath /var/lib/zope2.8/instance/plone-site/Extensions/ test.jar os.system(command) it doesn't seem to work. Because even though i have denoted the path to the jar file, this python external method requires me to be in the atual working directory in order o sucessfully run this command. What i tried was to write a shell script instead #!/bin/bash # My shell script path='/var/lib/zope2.8/instance/plone-site/Extensions' cd $path java -jar test.jar this shell script is executed by my external method : command = home/ewan/myScript os.system(command) This works perfectly. So now i know it has to do with the physical working directory i am in. Simply setting the classpath doesn't work or giving the absolute path to the jar file doesn't work either. i.e : command = java -jar /var/lib/zope2.8/instance/plone-site/Extensions/test.jar os.system(command) Anyone knows why this is so? and is there any way around this? I find it inefficient that i have my DTML calling My Python calling a Shell Script that runs an executable. Such a long chain! That is due to something in your java setup or java application, not something in Zope. So we can't help you with that. Wichert. -- Wichert Akkerman [EMAIL PROTECTED]It is simple to make things. http://www.wiggy.net/ It is hard to make things simple. ___ Zope-Dev maillist - Zope-Dev@zope.org http://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands
Ridzwan Aminuddin wrote at 2007-2-9 17:53 -0800: Oh yes, i forgot to mention that in my code i did use the absolute paths to my java .jar file as you can see i my code below, the absolute path to the Extensions directory is in the variable homedir. I also set my classpath to point to the folder just in case.. Relative paths do work -- but, they are (as always) relative to the current working directory. And unless you (or some buggy extension) have changed it, the current working directory will be Zope's instance home. ... homedir = os.getcwd() homedir = homedir + /Extensions command = cd +homedir os.popen(command) The popen above still has no effect. You should remove code without effect as it clutters the source. command = java -jar + '' + homedir + / + ActiveLearningTools.jar + ' ' + prepArticles test.txt os.popen(command) This will look for ActiveLearningTools.jar in instancehome/Extensions. But the output file test.txt will be put into instancehome (as this name is still relative). Have you already told us in what way your ExternalMethod fails? Do you get an exception? Or is simply the output file not produced? Or does the output file remain empty? -- Dieter ___ Zope-Dev maillist - Zope-Dev@zope.org http://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands
Hi! Thanks for your reply. Honestly, I'm not sure why the external method is skipped. I've tried several methods to debug and changed my python code to see the effects. I wrote another Java Jar. called test.jar that when executed prints out a line to stdout and creates a text file to the same directory that the jar is placed in. I placed this jar file as well as the external method python file in the Extensions directory /var/lib/zope2.8/instance/plone-site/Extensions This is how the external method looks like: def runYahooSearch2(self): import os homedir = os.getcwd() # on my machine homedir is /var/lib/zope2.8/instance/plone-site command = java -jar + homedir + /Extensions/test.jar whatisread = os.popen(command).read() print whatisread This external method is triggered in my DTML code. I've created other external methods before so i know i've done it right.. but the os.system() command for java -jar just doesn't seem to work. I've tried to use other commands such as ls which works perfectly: command = ls whatisread = os.popen(command).read() print whatisread This will sucessfully list out all the files/folders in the working directory. Its just this java -jar which does not execute at all... I know it doesn't execute at all cos it doesn't output a file or print the string. If i manually type out into the terminal : java -jar /var/lib/zope2.8/instance/plone-site/Extensions/test.jar it works perfectly, it prints out the string as well as outputs a text file into the directory the jar file is in. But using the external method given above just gives me a blank output. I've also tried popen2: from popen2 import popen2 outputTGT, inputTGT = popen2(command) print outputTGT.readline() and still the jar is simply skipped and not executed. Is there any other method i can use instead of os.popen os.popen2 and os.system to run this command line statement?... or any way i can catch the exception which is being thrown when the java -jar is attempting to run ( if there is any in the firt place) ? Maybe this would help in the debugging process. Thanks in advance! Cheers Wan -Original Message- From: [EMAIL PROTECTED] Sent: Mon, 12 Feb 2007 21:24:24 +0100 To: [EMAIL PROTECTED] Subject: Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands Ridzwan Aminuddin wrote at 2007-2-9 17:53 -0800: Oh yes, i forgot to mention that in my code i did use the absolute paths to my java .jar file as you can see i my code below, the absolute path to the Extensions directory is in the variable homedir. I also set my classpath to point to the folder just in case.. Relative paths do work -- but, they are (as always) relative to the current working directory. And unless you (or some buggy extension) have changed it, the current working directory will be Zope's instance home. ... homedir = os.getcwd() homedir = homedir + /Extensions command = cd +homedir os.popen(command) The popen above still has no effect. You should remove code without effect as it clutters the source. command = java -jar + '' + homedir + / + ActiveLearningTools.jar + ' ' + prepArticles test.txt os.popen(command) This will look for ActiveLearningTools.jar in instancehome/Extensions. But the output file test.txt will be put into instancehome (as this name is still relative). Have you already told us in what way your ExternalMethod fails? Do you get an exception? Or is simply the output file not produced? Or does the output file remain empty? -- Dieter KEEP SPYWARE OFF YOUR COMPUTER - Protect your computer with Spyware Terminator! Visit http://www.spywareterminator.com/install and find out more! ___ Zope-Dev maillist - Zope-Dev@zope.org http://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands
Ridzwan Aminuddin wrote at 2007-2-12 19:41 -0800: ... This external method is triggered in my DTML code. I've created other external methods before so i know i've done it right.. but the os.system() command for java -jar just doesn't seem to work. I've tried to use other commands such as ls which works perfectly: command = ls whatisread = os.popen(command).read() print whatisread Thus, you have found out that the problem is not with ExternalMethod or popen in principle but with java calls done this way. I would expect that java cannot be executed (maybe, because the PATH variable is different when run from Zope and java is not on this PATH) or that java is executed but dislikes something in the environment. In both cases, I would expect some problem message on stderr. Thus, I (in your place) would check how I could capture the stderr of the popen process and carefully look at it. One possibility would be to redirect stderr in the popen command itself (... /tmp/java.stderr). In principle, it is possible to start java from an ExternalMethod with os.system and friends. We do this all the time. -- Dieter ___ Zope-Dev maillist - Zope-Dev@zope.org http://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope )
[Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands
Hi guys... I wrote an external python script which would grab some HTML and so some text and file preprocessing. The script then is supposed to run come command line methods... using os.system() .. however when i run the script triggered by my DTML code it manages to do the grabbing of the html and saving it into a file ans stuff... but once it hits the portion where its supposed to run the command lines.. it just skips everything and goes right to the end after which this error message is shown : Error Type IOError Error Value [Errno 2] No such file or directory: '/var/lib/zope2.8/instance/plone-site/Extensions/output.txt' Request made at If the command line executables had run this error would not occur as one of the jar files was supposed to output a text file called output.txt. I tried running this script, modified on its own off the terminal using python test.py and it works perfectly... but once DTML calls this script it is unable to run the cammand line portion... can anyone please help me out or point me in the right direction... Thanks and regards wan Here is my code : def removeIllegalChars( tgtString ): import os import sys from yahoo.search.news import NewsSearch import re tgtString = re.sub('([^!]([^]|\n)*)', '', tgtString) tgtString = re.sub('([^]([^]|\n)*)', '', tgtString) tgtString = re.sub('\n', '', tgtString) tgtString = re.sub('\t', '', tgtString) tgtString = re.sub('\r', '', tgtString) tgtString = re.sub(' ', '', tgtString) if tgtString: try: tgtString = str(tgtString) except UnicodeEncodeError: tgtString = str(tgtString.encode('U8')) return tgtString def getYahooAPI(): import os import sys from yahoo.search.news import NewsSearch import re queryWord = Singapore print (Fetching data from YahooAPI with query : + queryWord ) srch = NewsSearch('YahooDemo',query = Singapore asean cup, language = 'en', results = 50) info = srch.parse_results() info.total_results_available homedir = os.getcwd() tgtFile = homedir + /Extensions/InputString.txt writeFile = open(tgtFile, 'w') for result in info.results: writeFile.write(Article\n) writeFile.write(Title\n) writeFile.write(removeIllegalChars(result['Title'])+ \n) writeFile.write(URL\n) writeFile.write(removeIllegalChars(result['ClickUrl']) + \n) writeFile.write(Summary\n) writeFile.write(removeIllegalChars(result['Summary']) + \n) writeFile.write(Date\n) writeFile.write(removeIllegalChars(result['PublishDate']) + \n) writeFile.write(Source\n) writeFile.write(removeIllegalChars(result['NewsSource']) + \n) writeFile.write(Body\n) writeFile.write(removeIllegalChars(result['Summary']) + \n) writeFile.write(EndOfFile) writeFile.close def runYahooSearch2(self): import os import sys from yahoo.search.news import NewsSearch import re #--- # still trying to figure out how to get the absolute path to this script # commands below work for windows... but not for linux. Need to find a platform # independant one # print 'sys.argv[0] =', sys.argv[0] # homedir = os.path.dirname(sys.argv[0]) # for windows # print 'path =', pathname getYahooAPI() homedir = os.getcwd() homedir = homedir + /Extensions command = cd +homedir os.popen(command) command = java -jar + '' + homedir + / + ActiveLearningTools.jar + ' ' + prepArticles test.txt os.popen(command) print Completed step 1 : preparing the articles command = homedir + / + vcluster + -zscores + ClutoMatrix + 2 sysCode = os.system(command) print Completed step 2 : Clustering the articles using Cluto command = java -jar + '' + homedir + / + ActiveLearningTools.jar + ' ' + processClusters output.txt sysCode = os.system(command) filename = homedir +/output.txt openfile = open(filename, 'r') whatisread = openfile.read() openfile.close() #os.remove(output.txt) return whatisread ___ Zope-Dev maillist - Zope-Dev@zope.org http://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists -
Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands
--On 9. Februar 2007 00:47:09 -0800 Ridzwan Aminuddin [EMAIL PROTECTED] wrote: Hi guys... I wrote an external python script which would grab some HTML and so some text and file preprocessing. The script then is supposed to run come command line methods... using os.system() .. however when i run the script triggered by my DTML code it manages to do the grabbing of the html and saving it into a file ans stuff... but once it hits the portion where its supposed to run the command lines.. it just skips everything and goes right to the end after which this error message is shown : Error Type IOError Error Value [Errno 2] No such file or directory: '/var/lib/zope2.8/instance/plone-site/Extensions/output.txt' Request made at You can resolve this issue on your own by checking why the file is generated in the wrong place or by it is looked up at the wrong place..adding some debug code or using pdb should be sufficient to track this down. -aj pgpentm0fFNoK.pgp Description: PGP signature ___ Zope-Dev maillist - Zope-Dev@zope.org http://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands
Hi, Since you say changing the working environment in zope is dangerous, is there any way around for what i need to do? Is there a way i can find out the exact directory zope is on at that moment? and if i can find it is it a good idea for me to place the additional files i need to run my .jar in that directory? Or should i just do everything in an absolute path method? i.e when i wanna run my jar instead of using this as my command java -jar ActivelearningTools.jar output.txt I will do: java -jar /var/lib/zope2.8/instance/plone-site/Extensions/ActiveLearningTools.jar /var/lib/zope2.8/instance/plone-site/Extensions/output.txt Thanks in advance! wan -Original Message- From: [EMAIL PROTECTED] Sent: Fri, 9 Feb 2007 19:58:06 +0100 To: [EMAIL PROTECTED] Subject: Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands Ridzwan Aminuddin wrote at 2007-2-9 00:47 -0800: ... command = cd +homedir os.popen(command) This popen is useless. It will change the working directory in the new (!) process that executes command. But the effect is lost as soon as the process ends (after the cd has been executed). os.popen('cd ...') will not affect the working directory of the Zope process (or any later 'os.popen'). Be warned: you should *not* change Zope's working directory. Changing global resources (such as the working directory) is dangerous in a multi thread environment (such as Zope). -- Dieter ___ Zope-Dev maillist - Zope-Dev@zope.org http://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands
Oh yes, i forgot to mention that in my code i did use the absolute paths to my java .jar file as you can see i my code below, the absolute path to the Extensions directory is in the variable homedir. I also set my classpath to point to the folder just in case.. Any ideas why it won't work? and any suggestions for workarounds? Thanks! homedir = os.getcwd() homedir = homedir + /Extensions command = cd +homedir os.popen(command) command = java -jar + '' + homedir + / + ActiveLearningTools.jar + ' ' + prepArticles test.txt os.popen(command) -Original Message- From: [EMAIL PROTECTED] Sent: Fri, 9 Feb 2007 17:39:58 -0800 To: [EMAIL PROTECTED] Subject: Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands Hi, Since you say changing the working environment in zope is dangerous, is there any way around for what i need to do? Is there a way i can find out the exact directory zope is on at that moment? and if i can find it is it a good idea for me to place the additional files i need to run my .jar in that directory? Or should i just do everything in an absolute path method? i.e when i wanna run my jar instead of using this as my command java -jar ActivelearningTools.jar output.txt I will do: java -jar /var/lib/zope2.8/instance/plone-site/Extensions/ActiveLearningTools.jar /var/lib/zope2.8/instance/plone-site/Extensions/output.txt Thanks in advance! wan -Original Message- From: [EMAIL PROTECTED] Sent: Fri, 9 Feb 2007 19:58:06 +0100 To: [EMAIL PROTECTED] Subject: Re: [Zope-dev] Extrenal method unable to run the os.popen() or os.system() commands Ridzwan Aminuddin wrote at 2007-2-9 00:47 -0800: ... command = cd +homedir os.popen(command) This popen is useless. It will change the working directory in the new (!) process that executes command. But the effect is lost as soon as the process ends (after the cd has been executed). os.popen('cd ...') will not affect the working directory of the Zope process (or any later 'os.popen'). Be warned: you should *not* change Zope's working directory. Changing global resources (such as the working directory) is dangerous in a multi thread environment (such as Zope). -- Dieter ___ Zope-Dev maillist - Zope-Dev@zope.org http://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope ) ___ Zope-Dev maillist - Zope-Dev@zope.org http://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope )