The Gaudi Framework  v36r1 (3e2fb5a8)
FdsRegistry.py
Go to the documentation of this file.
1 
14 
15 _O_ACCMODE = 3 # access-mode check for file flags.
16 
17 import logging
18 msg = logging.getLogger('FdsRegistry')
19 
20 
21 class FdsDict(dict):
22  name = "fds_dict"
23  curdir = None
24 
25  def __missing__(self, key):
26  self[key] = ""
27  return ""
28 
29  def fname(self, i):
30  if i in self:
31  return self[i][0]
32  else:
33  msg.warning("fds_dict:fname: No Key %s" % i)
34  return ""
35 
36  def fds(self, fname):
37  return [i for i, v in self.iteritems() if v[0] == fname]
38 
39  def has_name(self, fname):
40  for v in self.values():
41  if (v[0] == fname):
42  return True
43  return False
44 
45  # i - is the fd index (int)
46  def add(self, i, fname, iomode, flags):
47  self[i] = (fname, iomode, flags)
48  return
49 
50  def iomode(self, i):
51  if i in self:
52  return self[i][1]
53  else:
54  msg.warning("fds_dict:iomode: No Key %s" % i)
55  return ""
56 
57  def get_output_fds(self):
58  return [i for i in self.keys() if self[i][1] == '<OUTPUT>']
59 
60  def get_input_fds(self):
61  return [i for i in self.keys() if self[i][1] == '<INPUT>']
62 
63  def get_fds_in_dir(self, dir=""):
64  import os
65  if dir == "" and self.curdir is not None:
66  dir = self.curdir
67  msg.debug("get_fds_in_dir(%s)" % dir)
68  return [
69  i for i in self.keys()
70  if os.path.samefile(os.path.dirname(self[i][0]), dir)
71  ]
72 
73  def create_symlinks(self, wkdir=""):
74  """
75  create necessary symlinks in worker's dir if the fd is <INPUT>
76  otherwise copy <OUTPUT> file
77  """
78  import os
79  import shutil
80  msg.info("create_symlinks: %s" % self.get_fds_in_dir())
81  # some files expected to be in curdir
82  for fd in self.get_fds_in_dir():
83  src = self[fd][0]
84  iomode = self[fd][1]
85  dst = os.path.join(wkdir, os.path.basename(src))
86  if iomode == "<INPUT>":
87  if os.path.exists(dst):
88  # update_io_registry took care of this
89  msg.debug(
90  "fds_dict.create_symlink:update_io_registry took care of src=%s"
91  % src)
92  pass
93  else:
94  msg.debug(
95  "fds_dict.create_symlink:(symlink) src=%s, iomode=%s" %
96  (src, iomode))
97  os.symlink(src, dst)
98  else:
99  msg.debug("fds_dict.create_symlink: (copy) src=%s, dst=%s" %
100  (src, dst))
101  shutil.copy(src, dst)
102  pass
103  return
104 
105  def extract_fds(self, dir=""):
106  """parse the fds of the processs -> build fds_dict
107  """
108  import os
109  import fcntl
110  msg.info(
111  "extract_fds: making snapshot of parent process file descriptors")
112  self.curdir = os.path.abspath(os.curdir)
113  iomode = '<INPUT>'
114 
115  procfd = '/proc/self/fd'
116  fds = os.listdir(procfd)
117  for i in fds:
118  fd = int(i) # spurious entries should raise at this point
119  if (fd == 1 or fd == 2):
120  # leave stdout and stderr to redirect_log
121  continue
122  elif (fd == 0):
123  # with only a single controlling terminal, leave stdin alone
124  continue
125 
126  try:
127  realname = os.path.realpath(os.path.join(procfd, i))
128  except (OSError, IOError, TypeError):
129  # can fail because the symlink resolution (why is that needed
130  # anyway?) may follow while a temp file disappears
131  msg.debug("failed to resolve: %s ... skipping",
132  os.path.join(procfd, i))
133  continue
134 
135  if os.path.exists(realname):
136  try:
137  flags = fcntl.fcntl(fd, fcntl.F_GETFL)
138  if (flags & _O_ACCMODE) == 0: # read-only --> <INPUT>
139  iomode = "<INPUT>"
140  else:
141  iomode = "<OUTPUT>"
142 
143  self.add(fd, realname, iomode, flags)
144  except (OSError, IOError):
145  # likely saw a temoorary file; for now log a debug
146  # message, but this is fine if silently ignored
147  msg.debug("failed access to: %s ... skipping", realname)
148  continue
149 
150  # at this point the list of fds may still include temp files
151  # TODO: figure out if they can be identified (seeing /tmp is
152  # not enough as folks like to run with data from /tmp b/c of
153  # space constraints on /afs
154 
155  msg.debug("extract_fds.fds_dict=%s" % self)
156  return
157 
158  pass # FdsDict
GaudiMP.FdsRegistry.FdsDict.get_output_fds
def get_output_fds(self)
Definition: FdsRegistry.py:57
GaudiMP.FdsRegistry.FdsDict.create_symlinks
def create_symlinks(self, wkdir="")
Definition: FdsRegistry.py:73
GaudiMP.FdsRegistry.FdsDict.add
def add(self, i, fname, iomode, flags)
Definition: FdsRegistry.py:46
GaudiMP.FdsRegistry.FdsDict.get_input_fds
def get_input_fds(self)
Definition: FdsRegistry.py:60
GaudiMP.FdsRegistry.FdsDict
Definition: FdsRegistry.py:21
GaudiMP.FdsRegistry.FdsDict.has_name
def has_name(self, fname)
Definition: FdsRegistry.py:39
GaudiMP.FdsRegistry.FdsDict.curdir
curdir
Definition: FdsRegistry.py:23
GaudiMP.FdsRegistry.FdsDict.fname
def fname(self, i)
Definition: FdsRegistry.py:29
GaudiMP.FdsRegistry.FdsDict.get_fds_in_dir
def get_fds_in_dir(self, dir="")
Definition: FdsRegistry.py:63
GaudiMP.FdsRegistry.FdsDict.__missing__
def __missing__(self, key)
Definition: FdsRegistry.py:25
GaudiMP.FdsRegistry.FdsDict.iomode
def iomode(self, i)
Definition: FdsRegistry.py:50
GaudiMP.FdsRegistry.FdsDict.fds
def fds(self, fname)
Definition: FdsRegistry.py:36
GaudiMP.FdsRegistry.FdsDict.extract_fds
def extract_fds(self, dir="")
Definition: FdsRegistry.py:105