Trimming more stuff out.
[certmaster.git] / certmaster / utils.py
1 """
2 Copyright 2007-2008, Red Hat, Inc
3 see AUTHORS
4
5 This software may be freely redistributed under the terms of the GNU
6 general public license.
7
8 You should have received a copy of the GNU General Public License
9 along with this program; if not, write to the Free Software
10 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
11 """
12
13 import os
14 import string
15 import sys
16 import traceback
17 import xmlrpclib
18 import socket
19
20 REMOTE_ERROR = "REMOTE_ERROR"
21
22 def trace_me():
23 x = traceback.extract_stack()
24 bar = string.join(traceback.format_list(x))
25 return bar
26
27 def daemonize(pidfile=None):
28 """
29 Daemonize this process with the UNIX double-fork trick.
30 Writes the new PID to the provided file name if not None.
31 """
32
33 print pidfile
34 pid = os.fork()
35 if pid > 0:
36 sys.exit(0)
37 os.setsid()
38 os.umask(0)
39 pid = os.fork()
40
41 if pid > 0:
42 if pidfile is not None:
43 open(pidfile, "w").write(str(pid))
44 sys.exit(0)
45
46 def nice_exception(etype, evalue, etb):
47 etype = str(etype)
48 lefti = etype.index("'") + 1
49 righti = etype.rindex("'")
50 nicetype = etype[lefti:righti]
51 nicestack = string.join(traceback.format_list(traceback.extract_tb(etb)))
52 return [ REMOTE_ERROR, nicetype, str(evalue), nicestack ]
53
54 def get_hostname():
55 fqdn = socket.getfqdn()
56 host = socket.gethostname()
57 if fqdn.find(host) != -1:
58 return fqdn
59 else:
60 return host
61
62
63 def is_error(result):
64 if type(result) != list:
65 return False
66 if len(result) == 0:
67 return False
68 if result[0] == REMOTE_ERROR:
69 return True
70 return False
71
72
73