Package Ganga :: Package Lib :: Package MonitoringServices :: Package Dashboard :: Module CommonUtil
[hide private]
[frames] | no frames]

Source Code for Module Ganga.Lib.MonitoringServices.Dashboard.CommonUtil

 1  """Common utilities. 
 2   
 3  N.B. This code is under development and should not generally be used or relied upon. 
 4   
 5  """ 
 6   
 7  #----- utility methods ----- 
 8   
9 -def env(key):
10 """Return the environment variable value corresponding to key. 11 12 If the variable is undefined or empty then None is returned. 13 """ 14 import os 15 return strip_to_none(os.environ.get(key))
16 17 # utility method copied from Ganga.Utility.util
18 -def hostname():
19 """ Try to get the hostname in the most possible reliable way as described in the Python LibRef.""" 20 import socket 21 try: 22 return socket.gethostbyaddr(socket.gethostname())[0] 23 # [bugfix #20333]: 24 # while working offline and with an improper /etc/hosts configuration 25 # the localhost cannot be resolved 26 except: 27 return 'localhost'
28
29 -def stdout(command):
30 """Execute the command in a subprocess and return stdout. 31 32 If the exit code is non-zero then None is returned. 33 """ 34 import popen2 35 p = popen2.Popen3(command) 36 rc = p.wait() 37 if rc == 0: 38 return p.fromchild.read() 39 else: 40 return None
41
42 -def strip_to_none(value):
43 """Returns the stripped string representation of value, or None if this is 44 None or an empty string.""" 45 if value is None: 46 return None 47 text = str(value).strip() 48 if len(text) == 0: 49 return None 50 return text
51
52 -def utcnow():
53 """Return a UTC datetime with no timezone specified.""" 54 import datetime 55 return datetime.datetime.utcnow()
56