4 Utility module for things not specific to DQ
7 from __future__
import with_statement
9 from contextlib
import contextmanager
10 from logging
import getLogger; log =
getLogger(
"DQUtils.general")
12 from time
import time, mktime, strptime, strftime, gmtime
13 from six.moves
import getoutput
17 Returns package version as determined from "cmt show versions"
19 if "/" not in package_name
and default_prefix:
20 package_name =
"/".
join([default_prefix, package_name])
21 output =
getoutput(
"cmt show versions %s" % package_name).
split(
"\n")
22 output =
list(map(str.strip, output))
28 Generate kcachegrind profile information for a call.
32 with kgrind_profile() as p:
35 from cProfile
import Profile
40 from .ext.lsprofcalltree
import KCacheGrind
41 with open(filename,
"w")
as fd:
44 def daemonize(function, delay, error_args=None, **kwargs):
46 Run a daemon which executes `function` every `delay` seconds.
48 from time
import sleep
51 log.info(
"Sleeping for %i seconds..", delay)
54 log.info(
"Current time: %s", strftime(
"%d/%m/%Y %H:%M:%S"))
58 if error_args
is not None:
65 Send an error email containing `exception_info`
66 TODO: Doesn't contain complete information
71 from email.mime.text
import MIMEText
72 from getpass
import getuser
73 from socket
import gethostname
75 exceptionType, exceptionValue, exceptionTraceback = exception_info
77 user, host = getuser(), gethostname()
79 extra_info = [
"Was running as user '%s' on machine '%s'" % (user, host),
83 msg = MIMEText(
"\n\n".
join(body + extra_info +
84 traceback.format_tb(exceptionTraceback)))
86 msg[
'Subject'] = subject
91 "Peter Onyisi <peter.onyisi@cern.ch>"
97 msg[
'To'] =
", ".
join(to)
102 s.sendmail(from_, to, msg.as_string())
105 log.error(
"Error email sent.")
109 Returns true if all input arguments are equal (must be hashable)
111 return len(
set(inputs)) == 1
115 Generate all permutations of `input_list`
117 if len(input_list) <= 1:
121 for i
in range(len(perm)+1):
122 yield perm[:i] + input_list[0:1] + perm[i:]
128 while type(result)
is list
and len(result)
and callable(result[0]):
131 result = result[0]([])
135 if type(l)
is not list:
144 Turn down the logging verbosity temporarily
146 if log_level
is None:
147 log_level = log.WARNING
150 logger = log.getLogger()
152 orig_level = logger.level
153 logger.setLevel(log_level)
158 logger.setLevel(orig_level)
162 "A context manager which spits out how long the block took to execute"
168 log.debug(
"Took %.2f to %s", end - start, name)
171 return [item
for items
in zip(*args)
for item
in items]
175 Returns number of nanoseconds between unix epoch and date in the form
178 return int(mktime(strptime(date_string,
"%d/%m/%Y")) * 1e9)
182 Returns a string representation of `nanounix` nanoseconds
185 sub_second =
"%03i" % ((nanounix %
int(1e9)) / 1e6)
187 return strftime(
"%Y%m%d:%H%M%S.", gmtime(nanounix / 1e9)) + sub_second
189 return "[BadTime: %s]" %
int(nanounix)