| # Gaudi Framework, version v22r4 |

Home |  | Generated: Fri Sep 2 2011

# FdsRegistry.py

Go to the documentation of this file.

    00001 # @file GaudiMP.FdsRegistry.py
    00002 # @purpose file descriptor registration and handling
    00003 # @author Mous Tatarkhanov <tmmous@berkeley.edu>
    00004
    00005 _O_ACCMODE = 3  #access-mode check for file flags.
    00006
    00007 import logging
    00008 msg = logging.getLogger( 'FdsRegistry' )
    00009
    00010 class FdsDict(dict):
    00011     name = "fds_dict"
    00012     curdir = None
    00013
    00014     def __missing__(self, key):
    00015         self[key] = ""
    00016         return ""
    00017
    00018     def fname(self,i):
    00019         if i in self:
    00020             return self[i][0]
    00021         else:
    00022             msg.warning ("fds_dict:fname: No Key %s" % i)
    00023             return ""
    00024
    00025     def fds(self, fname):
    00026         return [i for i, v in self.iteritems() if v[0]==fname]
    00027
    00028     def has_name(self, fname):
    00029         for v in self.values():
    00030             if (v[0] == fname):
    00031                 return True
    00032         return False
    00033
    00034     # i - is the fd index (int)
    00035     def add(self, i, fname, iomode, flags):
    00036         self[i] = (fname, iomode, flags)
    00037         return
    00038
    00039     def iomode(self,i):
    00040         if i in self:
    00041             return self[i][1]
    00042         else:
    00043             msg.warning ("fds_dict:iomode: No Key %s" % i)
    00044             return ""
    00045
    00046     def get_output_fds(self):
    00047         return [i for i in self.keys() if self[i][1]=='<OUTPUT>']
    00048
    00049     def get_input_fds(self):
    00050         return [i for i in self.keys() if self[i][1]=='<INPUT>']
    00051
    00052     def get_fds_in_dir(self, dir=""):
    00053         import os
    00054         if dir == "" and self.curdir is not None:
    00055             dir = self.curdir
    00056         msg.debug("get_fds_in_dir(%s)" % dir)
    00057         return [i for i in self.keys()
    00058                 if os.path.samefile(os.path.dirname(self[i][0]), dir) ]
    00059
    00060     def create_symlinks(self, wkdir=""):
    00061         """
    00062         create necessary symlinks in worker's dir if the fd is <INPUT>
    00063         otherwise copy <OUTPUT> file
    00064         """
    00065         import os,shutil
    00066         msg.info("create_symlinks: %s" % self.get_fds_in_dir())
    00067         #some files expected to be in curdir
    00068         for fd in self.get_fds_in_dir():
    00069             src = self[fd][0]
    00070             iomode = self[fd][1]
    00071             dst = os.path.join(wkdir, os.path.basename(src))
    00072             if iomode == "<INPUT>":
    00073                 if os.path.exists(dst):
    00074                     # update_io_registry took care of this
    00075                     msg.debug("fds_dict.create_symlink:update_io_registry took care of src=%s" % src)
    00076                     pass
    00077                 else:
    00078                     msg.debug("fds_dict.create_symlink:(symlink) src=%s, iomode=%s" % (src,iomode))
    00079                     os.symlink(src, dst)
    00080             else:
    00081                 msg.debug("fds_dict.create_symlink: (copy) src=%s, dst=%s" % (src, dst))
    00082                 shutil.copy(src, dst)
    00083                 pass
    00084         return
    00085
    00086     def extract_fds(self, dir=""):
    00087         """parse the fds of the processs -> build fds_dict
    00088         """
    00089         import os, fcntl
    00090         msg.info("extract_fds: making snapshot of parent process file descriptors")
    00091         self.curdir = os.path.abspath(os.curdir)
    00092         iomode = '<INPUT>'
    00093
    00094         procfd = '/proc/self/fd'
    00095         fds = os.listdir(procfd)
    00096         for i in fds:
    00097             fd = int(i)
    00098             if (fd==1 or fd==2):
    00099                 #leave stdout and stderr to redirect_log
    00100                 continue
    00101             realname = os.path.realpath(os.path.join(procfd,i))
    00102             if os.path.exists(realname):
    00103                 flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    00104                 if (flags & _O_ACCMODE) == 0: #read-only --> <INPUT>
    00105                     iomode = "<INPUT>"
    00106                 else:
    00107                     iomode = "<OUTPUT>"
    00108
    00109                 self.add(fd, realname, iomode, flags)
    00110
    00111         msg.debug( "extract_fds.fds_dict=%s" % self)
    00112         return
    00113
    00114     pass #FdsDict
    00115

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines