Go to the documentation of this file.00001
00002
00003
00004
00005 _O_ACCMODE = 3
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
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
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
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
00100 continue
00101 elif (fd==0):
00102
00103 continue
00104
00105 try:
00106 realname = os.path.realpath(os.path.join(procfd,i))
00107 except (OSError, IOError, TypeError):
00108
00109
00110 msg.debug( "failed to resolve: %s ... skipping", os.path.join(procfd,i) )
00111 continue
00112
00113 if os.path.exists(realname):
00114 try:
00115 flags = fcntl.fcntl(fd, fcntl.F_GETFL)
00116 if (flags & _O_ACCMODE) == 0:
00117 iomode = "<INPUT>"
00118 else:
00119 iomode = "<OUTPUT>"
00120
00121 self.add(fd, realname, iomode, flags)
00122 except (OSError, IOError):
00123
00124
00125 msg.debug( "failed access to: %s ... skipping", realname )
00126 continue
00127
00128
00129
00130
00131
00132
00133 msg.debug( "extract_fds.fds_dict=%s" % self)
00134 return
00135
00136 pass
00137