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
8
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
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
24
25
26 except:
27 return 'localhost'
28
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
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
53 """Return a UTC datetime with no timezone specified."""
54 import datetime
55 return datetime.datetime.utcnow()
56