Hooking file open

Farshid Lashkari no at spam.com
Wed Nov 1 20:40:30 CET 2006


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



More information about the Python-list mailing list