External Method Working Directory?
Folks, I have an external method that at this point is made up of nothing but os.getcwd() and lives in my Extensions directory. When it is run, it reports the working directory as being one of my Products directories. Can anyone tell me what the default working directory for an external method is? Further, I need this to create a file temporarily in Extensions. I would prefer not to have to hardwire the location of Extension into the code, since that would make the script non-portable to other instances easily. Any advice on this would be appreciated. J. P. Withers
Products?? That is odd. I thought it was the var/ directory or at least the zope instance home directory. From the External method you should have access to INSTANCE_HOME and SOFTWARE_HOME. Try if these are what you want. I doubt that you'll find your Extensions/ dir in any of them so you might have do something like: import os return os.path.join(INSTANCE_HOME, 'Extensions') 2005/9/28, jwithers <withers@rampeffect.com>:
Folks,
I have an external method that at this point is made up of nothing but os.getcwd() and lives in my Extensions directory. When it is run, it reports the working directory as being one of my Products directories.
Can anyone tell me what the default working directory for an external method is?
Further, I need this to create a file temporarily in Extensions. I would prefer not to have to hardwire the location of Extension into the code, since that would make the script non-portable to other instances easily.
Any advice on this would be appreciated.
J. P. Withers
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
-- Peter Bengtsson, work www.fry-it.com home www.peterbe.com hobby www.issuetrackerproduct.com
jwithers, a) Is your os.getcwd() (in /Extensions) called from a Product (in /Products?) Otherwise I have not idea b) Regarding temporary files .. I've used stuff like ... import tempfile tmpFile2 = tempfile.mktemp() theFile=open( tmpFile2,'rb') result = theFile.read() theFile.close() os.remove(tmpFile2) I think this may depreciated but do a google on "python tempfile" and you can use the updated modules if they apply to your issue. David jwithers wrote:
Folks,
I have an external method that at this point is made up of nothing but os.getcwd() and lives in my Extensions directory. When it is run, it reports the working directory as being one of my Products directories.
Can anyone tell me what the default working directory for an external method is?
Further, I need this to create a file temporarily in Extensions. I would prefer not to have to hardwire the location of Extension into the code, since that would make the script non-portable to other instances easily.
Any advice on this would be appreciated.
J. P. Withers
____
jwithers wrote: eep, another one, hope we're not related ;-)
Can anyone tell me what the default working directory for an external method is?
You can't rely on it being anything. If you're working with files, you need to be very careful...
Further, I need this to create a file temporarily in Extensions.
Why?
I would prefer not to have to hardwire the location of Extension into the code, since that would make the script non-portable to other instances easily.
Any advice on this would be appreciated.
I'd suggest looking at python's tempfile module... Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
On Thu, 2005-09-29 at 00:20, Chris Withers wrote:
eep, another one, hope we're not related ;-)
When I saw your posts on newsgroups years ago, I almost gave up python programming altogether Chris, thinking that any given field of endeavor needs only one Withers, but decided to go ahead and chance it.
Can anyone tell me what the default working directory for an external method is?
You can't rely on it being anything. If you're working with files, you need to be very careful...
Further, I need this to create a file temporarily in Extensions.
Why?
Because I need to feed the file to Mailman for evil purposes of my own (well, fairly mundane purposes of allowing my zope app to manipulate mailman list memberships, really) that the prototype mailman adapter I found doesn't meet. And yeah, tempfile would work, although, I have to admit I really don't see the point of it when you are only storing one file. I am probably missing some usecase or good programming practice here, but it seems like it saves you a single line for the delete in return for an extra module import. Peter Bengtsson gave me the solution in just using the INSTANCE_HOME variable directly, which is bound in external scripts. That beat the daylights out of the solution that had come up wjth the help of some folks on ICQ of using self.Control_Panel.getINSTANCE_HOME(). And Deiter is right and it is dim to put the temporary file in Extensions and it will be put in var instead. Not sure what I was thinking there. John Withers
I would prefer not to have to hardwire the location of Extension into the code, since that would make the script non-portable to other instances easily.
Any advice on this would be appreciated.
I'd suggest looking at python's tempfile module...
Chris
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 jwithers wrote:
Because I need to feed the file to Mailman for evil purposes of my own (well, fairly mundane purposes of allowing my zope app to manipulate mailman list memberships, really) that the prototype mailman adapter I found doesn't meet.
BTW, I have experimented with the XML-RPC patch for Mailman 2.1.6: http://sourceforge.net/tracker/index.php?func=detail&aid=1244799&group_id=10... It seems to work nicely from "command-line" Python, although I haven't yet got around to driving it from within Zope.
And yeah, tempfile would work, although, I have to admit I really don't see the point of it when you are only storing one file. I am probably missing some usecase or good programming practice here, but it seems like it saves you a single line for the delete in return for an extra module import.
What you really buy with that import is that other people have thought about, and fixed bugs for, the race conditions involved in creating tempfiles.
Peter Bengtsson gave me the solution in just using the INSTANCE_HOME variable directly, which is bound in external scripts. That beat the daylights out of the solution that had come up wjth the help of some folks on ICQ of using self.Control_Panel.getINSTANCE_HOME(). And Deiter is right and it is dim to put the temporary file in Extensions and it will be put in var instead. Not sure what I was thinking there.
In a "locked down" configuration, the user as whom Zope runs won't be able to write into any directory on the system *except* the 'var' directory (and a 'logs' directory if you feel obsessive about keeping the logs elsewhere), so that works out well. Tres. - -- =================================================================== Tres Seaver +1 202-558-7113 tseaver@palladion.com Palladion Software "Excellence by Design" http://palladion.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFDPEhq+gerLs4ltQ4RAibEAKCCqeR2hpZ0bCkH1aAueTfT6k358wCgp5nE Xve1bwxcwQ+rKehuxtNpaKw= =YUXq -----END PGP SIGNATURE-----
jwithers wrote:
Because I need to feed the file to Mailman for evil purposes of my own (well, fairly mundane purposes of allowing my zope app to manipulate mailman list memberships, really) that the prototype mailman adapter I found doesn't meet.
When I've needed to do simple stuff with this is the past, I just manipulate mailmain by send email from Zope to mailman's admin interface...
And yeah, tempfile would work, although, I have to admit I really don't see the point of it when you are only storing one file. I am probably missing some usecase or good programming practice here, but it seems like it saves you a single line for the delete in return for an extra module import.
What happens when two users do something that creates one of these temp files at exactly the same time? ;-) cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
When I've needed to do simple stuff with this is the past, I just manipulate mailmain by send email from Zope to mailman's admin interface...
Pipes seem easier to me. Responses from the command line scripts seem much easier to use for confirmation. And there are things you can do easily from the cli interface that you either can't do through email, are difficult to do through email, or possibly I just don't know how to do through email.
And yeah, tempfile would work, although, I have to admit I really don't see the point of it when you are only storing one file. I am probably missing some usecase or good programming practice here, but it seems like it saves you a single line for the delete in return for an extra module import.
What happens when two users do something that creates one of these temp files at exactly the same time? ;-)
Yah, you and Tres Seaver (OL) both made this point, and thanks for saving me from having much wailing and gnashing of teeth over my dimness once this actually went live. Thanks for the help, J.P. Withers
jwithers wrote at 2005-9-28 15:43 -0700:
... I have an external method that at this point is made up of nothing but os.getcwd() and lives in my Extensions directory. When it is run, it reports the working directory as being one of my Products directories.
Some products are broken (they change the current working directory). Apparently, you have one of them installed. In general, it is a bad idea to change the current working directory in a multi-threaded application (such as Zope). An External Method does not have a specific working directory "when it is run". Because Zope must not modify the working directory, it cannot let it follow the "run of an External Method". The current working directory remains what it was. On a system with well behaving products, the current working directory remains the "INSTANCE_HOME".
... Further, I need this to create a file temporarily in Extensions. I would prefer not to have to hardwire the location of Extension into the code, since that would make the script non-portable to other instances easily.
The "Extensions" directory is a bad place for temporary files... -- Dieter
participants (6)
-
Chris Withers -
David Hassalevris -
Dieter Maurer -
jwithers -
Peter Bengtsson -
Tres Seaver