The Gaudi Framework  master (123e188a)
Loading...
Searching...
No Matches
FdsRegistry.py
Go to the documentation of this file.
14
15_O_ACCMODE = 3 # access-mode check for file flags.
16
17import logging
18
19msg = logging.getLogger("FdsRegistry")
20
21
22class FdsDict(dict):
23 name = "fds_dict"
24 curdir = None
25
26 def __missing__(self, key):
27 self[key] = ""
28 return ""
29
30 def fname(self, i):
31 if i in self:
32 return self[i][0]
33 else:
34 msg.warning(f"fds_dict:fname: No Key {i}")
35 return ""
36
37 def fds(self, fname):
38 return [i for i, v in self.iteritems() if v[0] == fname]
39
40 def has_name(self, fname):
41 for v in self.values():
42 if v[0] == fname:
43 return True
44 return False
45
46 # i - is the fd index (int)
47 def add(self, i, fname, iomode, flags):
48 self[i] = (fname, iomode, flags)
49 return
50
51 def iomode(self, i):
52 if i in self:
53 return self[i][1]
54 else:
55 msg.warning(f"fds_dict:iomode: No Key {i}")
56 return ""
57
58 def get_output_fds(self):
59 return [i for i in self.keys() if self[i][1] == "<OUTPUT>"]
60
61 def get_input_fds(self):
62 return [i for i in self.keys() if self[i][1] == "<INPUT>"]
63
64 def get_fds_in_dir(self, dir=""):
65 import os
66
67 if dir == "" and self.curdir is not None:
68 dir = self.curdir
69 msg.debug(f"get_fds_in_dir({dir})")
70 return [
71 i for i in self.keys() if os.path.samefile(os.path.dirname(self[i][0]), dir)
72 ]
73
74 def create_symlinks(self, wkdir=""):
75 """
76 create necessary symlinks in worker's dir if the fd is <INPUT>
77 otherwise copy <OUTPUT> file
78 """
79 import os
80 import shutil
81
82 msg.info(f"create_symlinks: {self.get_fds_in_dir()}")
83 # some files expected to be in curdir
84 for fd in self.get_fds_in_dir():
85 src = self[fd][0]
86 iomode = self[fd][1]
87 dst = os.path.join(wkdir, os.path.basename(src))
88 if iomode == "<INPUT>":
89 if os.path.exists(dst):
90 # update_io_registry took care of this
91 msg.debug(
92 f"fds_dict.create_symlink:update_io_registry took care of src={src}"
93 )
94 pass
95 else:
96 msg.debug(
97 f"fds_dict.create_symlink:(symlink) src={src}, iomode={iomode}"
98 )
99 os.symlink(src, dst)
100 else:
101 msg.debug(f"fds_dict.create_symlink: (copy) src={src}, dst={dst}")
102 shutil.copy(src, dst)
103 pass
104 return
105
106 def extract_fds(self, dir=""):
107 """parse the fds of the processs -> build fds_dict"""
108 import fcntl
109 import os
110
111 msg.info("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, 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", os.path.join(procfd, i))
132 continue
133
134 if os.path.exists(realname):
135 try:
136 flags = fcntl.fcntl(fd, fcntl.F_GETFL)
137 if (flags & _O_ACCMODE) == 0: # read-only --> <INPUT>
138 iomode = "<INPUT>"
139 else:
140 iomode = "<OUTPUT>"
141
142 self.add(fd, realname, iomode, flags)
143 except OSError:
144 # likely saw a temoorary file; for now log a debug
145 # message, but this is fine if silently ignored
146 msg.debug("failed access to: %s ... skipping", realname)
147 continue
148
149 # at this point the list of fds may still include temp files
150 # TODO: figure out if they can be identified (seeing /tmp is
151 # not enough as folks like to run with data from /tmp b/c of
152 # space constraints on /afs
153
154 msg.debug(f"extract_fds.fds_dict={self}")
155 return
156
157 pass # FdsDict
add(self, i, fname, iomode, flags)
create_symlinks(self, wkdir="")