1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 """
28 A collection of utilities to assist applications in manipulating the
29 process and process_params tables in LIGO Light-Weight XML documents.
30 """
31
32
33 import os
34 import socket
35 from six import StringIO
36 import time
37
38
39 from glue import git_version
40 from .. import ligolw
41 from .. import lsctables
42 from .. import types as ligolwtypes
43 import six
44
45
46 try:
47 from lal import UTCToGPS as _UTCToGPS
48 except ImportError:
49
50
51 from glue import gpstime
52 _UTCToGPS = lambda utc: int(gpstime.GpsSecondsFromPyUTC(time.mktime(utc)))
53
54
55 __author__ = "Kipp Cannon <kipp.cannon@ligo.org>, Larne Pekowsky <lppekows@physics.syr.edu>"
56 __version__ = "git id %s" % git_version.id
57 __date__ = git_version.date
58
59
60
61
62
63
64
65
66
67
68
70 """
71 Try to retrieve the username from a variety of sources. First the
72 environment variable LOGNAME is tried, if that is not set the
73 environment variable USERNAME is tried, if that is not set the
74 password database is consulted (only on Unix systems, if the import
75 of the pwd module succeeds), finally if that fails KeyError is
76 raised.
77 """
78 try:
79 return os.environ["LOGNAME"]
80 except KeyError:
81 pass
82 try:
83 return os.environ["USERNAME"]
84 except KeyError:
85 pass
86 try:
87 import pwd
88 return pwd.getpwuid(os.getuid())[0]
89 except (ImportError, KeyError):
90 raise KeyError
91
92
93 -def append_process(xmldoc, program = None, version = None, cvs_repository = None, cvs_entry_time = None, comment = None, is_online = False, jobid = 0, domain = None, ifos = None):
143
144
146 """
147 Set the end time in a row in a process table to the current time.
148 """
149 process.end_time = _UTCToGPS(time.gmtime())
150 return process
151
152
184
185
187 """
188 Return a list of the values stored in the process_params table for
189 params named param for the program(s) named program. The values
190 are returned as Python native types, not as the strings appearing
191 in the XML document. If require_unique_program is True (default),
192 then the document must contain exactly one program with the
193 requested name, otherwise ValueError is raised. If
194 require_unique_program is not True, then there must be at least one
195 program with the requested name otherwise ValueError is raised.
196 """
197 process_ids = lsctables.ProcessTable.get_table(xmldoc).get_ids_by_program(program)
198 if len(process_ids) < 1:
199 raise ValueError("process table must contain at least one program named '%s'" % program)
200 elif require_unique_program and len(process_ids) != 1:
201 raise ValueError("process table must contain exactly one program named '%s'" % program)
202 return [row.pyvalue for row in lsctables.ProcessParamsTable.get_table(xmldoc) if (row.process_id in process_ids) and (row.param == param)]
203
204
211
212
214 """
215 Generator function yields (name, type, value) tuples constructed
216 from a dictionary of name/value pairs. The tuples are suitable for
217 input to append_process_params(). This is intended as a
218 convenience for converting command-line options into process_params
219 rows. The name values in the output have "--" prepended to them
220 and all "_" characters replaced with "-". The type strings are
221 guessed from the Python types of the values. If a value is a
222 Python list (or instance of a subclass thereof), then one tuple is
223 produced for each of the items in the list.
224
225 Example:
226
227 >>> list(process_params_from_dict({"verbose": True, "window": 4.0, "include": ["/tmp", "/var/tmp"]}))
228 [(u'--window', u'real_8', 4.0), (u'--verbose', None, None), (u'--include', u'lstring', '/tmp'), (u'--include', u'lstring', '/var/tmp')]
229 """
230 for name, values in paramdict.items():
231
232 name = u"--%s" % name.replace("_", "-")
233
234 if values is True or values is False:
235 yield (name, None, None)
236 elif values is not None:
237 if not isinstance(values, list):
238 values = [values]
239 for value in values:
240 yield (name, ligolwtypes.FromPyType[type(value)], value)
241
242
244 """
245 Register the current process and params to an XML document.
246 program is the name of the program. paramdict is a dictionary of
247 name/value pairs that will be used to populate the process_params
248 table; see process_params_from_dict() for information on how these
249 name/value pairs are interpreted. Any additional keyword arguments
250 are passed to append_process(). Returns the new row from the
251 process table.
252 """
253 process = append_process(xmldoc, program = program, **kwargs)
254 append_process_params(xmldoc, process, process_params_from_dict(paramdict))
255 return process
256
257
258
259 -def register_to_ldbd(client, program, paramdict, version = u'0', cvs_repository = u'-', cvs_entry_time = 0, comment = u'-', is_online = False, jobid = 0, domain = None, ifos = u'-'):
260 """
261 Register the current process and params to a database via a
262 LDBDClient. The program and paramdict arguments and any additional
263 keyword arguments are the same as those for register_to_xmldoc().
264 Returns the new row from the process table.
265 """
266 xmldoc = ligolw.Document()
267 xmldoc.appendChild(ligolw.LIGO_LW())
268 process = register_to_xmldoc(xmldoc, program, paramdict, version = version, cvs_repository = cvs_repository, cvs_entry_time = cvs_entry_time, comment = comment, is_online = is_online, jobid = jobid, domain = domain, ifos = ifos)
269
270 fake_file = StringIO()
271 xmldoc.write(fake_file)
272 client.insert(fake_file.getvalue())
273
274 return process
275