469,578 Members | 1,914 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

Hooking file open

Hi,

My goal is to detect all (or most) file dependencies of a script (i.e.
modules, dlls, data files). Currently, after a script is finished
executing I use sys.modules to determine all module dependencies, and I
use win32process.EnumProcessModules to determine DLL dependencies. This
works reasonably well, however I would still like to detect dependencies
from regular file open calls.

I did a little test by modifying __builtins__.open to point to a custom
function that forwards the call to the original open function and saves
the filename to a list. It seemed to work for simple test cases. Here is
the code:

def open_hook(*args,**kwargs):
r = open_real(*args,**kwargs)
saveFileName(args[0])
return r

open_real = __builtins__.open
__builtins__.open = open_hook

Is this a safe approach? Is there a more efficient way of doing this? I
realize that some dependencies will still fall through my checks,
especially file opens from C extensions, which is fine. I just want to
be able to detect the most common use cases. Any other suggestions are
appreciated.

-Farshid
Nov 1 '06 #1
3 2720
You might consider trapping calls to file() too, which is an alias for
open().

Also, I think I'd do my logging before calling the real function. It depends
how you want to deal with exceptions.

Farshid Lashkari wrote:
Hi,

My goal is to detect all (or most) file dependencies of a script (i.e.
modules, dlls, data files). Currently, after a script is finished
executing I use sys.modules to determine all module dependencies, and I
use win32process.EnumProcessModules to determine DLL dependencies. This
works reasonably well, however I would still like to detect dependencies
from regular file open calls.

I did a little test by modifying __builtins__.open to point to a custom
function that forwards the call to the original open function and saves
the filename to a list. It seemed to work for simple test cases. Here is
the code:

def open_hook(*args,**kwargs):
r = open_real(*args,**kwargs)
saveFileName(args[0])
return r

open_real = __builtins__.open
__builtins__.open = open_hook

Is this a safe approach? Is there a more efficient way of doing this? I
realize that some dependencies will still fall through my checks,
especially file opens from C extensions, which is fine. I just want to
be able to detect the most common use cases. Any other suggestions are
appreciated.

-Farshid
--
Dale Strickland-Clark
Riverhall Systems - www.riverhall.co.uk

Nov 1 '06 #2
Dale Strickland-Clark wrote:
You might consider trapping calls to file() too, which is an alias for
open().
Thanks, I didn't know about that.
Also, I think I'd do my logging before calling the real function. It depends
how you want to deal with exceptions.
I placed the logging after the call to the real function so that files
that don't exist will not be logged. However, I'm already checking for
file existence during the post process of the script running, so I guess
it doesn't really make too much of a difference.

Thanks for the help.

-Farshid
Nov 1 '06 #3
Dale Strickland-Clark wrote:
You might consider trapping calls to file() too, which is an alias for
open().

Also, I think I'd do my logging before calling the real function. It depends
how you want to deal with exceptions.
OP should hook into os.open as well. Plus, I don't think the database
modules (dbm, gdbm, bsddb) use Python open, so might want to hook into
those as well. You have be careful with extension modules; they often
open files their own way.
Carl Banks

Nov 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Matthew Kelly | last post: by
79 posts views Thread by pinkfloydhomer | last post: by
3 posts views Thread by Rick Strahl [MVP] | last post: by
reply views Thread by webstuff | last post: by
5 posts views Thread by Ryan Ginstrom | last post: by
8 posts views Thread by pigeonrandle | last post: by
reply views Thread by joemango | last post: by
1 post views Thread by Sean | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.